> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# MCP and OpenAPI

> How to expose external tools to AI agents using OpenAPI specs, MCP protocol, or built-in ADK tools, with security and least privilege advice

Back to your support agent.

You write a Python function that looks up a customer's order, and the agent can call it. That works great — until the agent needs to search the web, rate a Jira ticket, query a payment system, or work with a service that already exposes hundreds of API operations.

Do you really need to write a Python wrapper for every one of those operations? No — there are easier, standardized ways to expose existing tools to your agent.

![A dark-themed diagram titled "Tools Your Agent Needs" with a central "Agent" box connected by Python "wrapper" blocks to icons for Web search, Jira, Payments and a Service (312 operations). A user avatar labeled "You" is shown linked to the agent.](https://mintcdn.com/kodekloud-c4ac6d9a/heceQKBf52UJgxP-/images/Google-ADK-20/Advanced-Tools/MCP-and-OpenAPI/agent-python-wrappers-web-jira-payments.jpg?fit=max\&auto=format\&n=heceQKBf52UJgxP-\&q=85\&s=c2b7d9c8f98fe9b21463c46929e3eb2d)

Most tools your agent needs already exist; you only need a way to connect to them. One common approach is an OpenAPI specification.

What is OpenAPI?

* OpenAPI is a single machine-readable file (JSON or YAML) that describes every operation a REST API supports.
* A REST API is a web service your code talks to over HTTP — the same protocol your browser uses.

Example OpenAPI (YAML):

```yaml theme={null}
openapi: 3.0.3
info: { title: Payments API, version: "1.0" }
paths:
  /payments:
    get:
      operationId: listPayments
    post:
      operationId: createPayment
  /refunds/{id}:
    get:
      operationId: getRefund
```

Why OpenAPI helps agents

* A complete OpenAPI spec turns every documented endpoint into a callable tool without writing custom wrappers.
* Tools like ADK's OpenAPIToolset can ingest the spec and expose each operation directly to the model as an actionable tool.

Example: turn an OpenAPI spec into agent tools

```python theme={null}
# Create an OpenAPIToolset from a YAML spec string
toolset = OpenAPIToolset(spec_str=spec, spec_str_type="yaml")
agent = LlmAgent(model="gemini-2.5-flash", tools=[toolset])
```

If your organization already publishes an OpenAPI spec, prefer generating tools from the spec rather than hand-coding clients. This speeds development and reduces surface-area errors. For official doc reference, see the OpenAPI Specification: [https://swagger.io/specification/](https://swagger.io/specification/)

What if the provider isn't a REST API?
That's where MCP (Model Context Protocol) fits.

MCP explained

* MCP is a protocol and common language between an agent and an external tool provider — similar to how device drivers standardize access to hardware.
* Any tool provider that implements MCP can plug into any agent that speaks MCP, with no extra glue code.
* The MCP server typically runs a small program (often on the same machine or a reachable host). The agent and server exchange structured messages (JSON, etc.) over a transport such as HTTP or WebSocket.

Example conceptual flow for MCP (illustrative):

```python theme={null}
# Connect an MCP toolset and expose only read/list capabilities
mcp_toolset = MCPToolset(endpoint="http://localhost:12345")
agent = LlmAgent(name="support", tools=[mcp_toolset.filter(allow=["list_directory", "read_file"])])
```

Note how the filter limits the agent to `list_directory` and `read_file` only — a simple but important security habit. Don't connect a server and blindly hand over every capability to the model.

![A UI mockup titled "Filter Tools as a Security Habit" showing a permissions panel for "company-files" where list\_directory and read\_file are allowed while write\_file, delete\_file, and execute are disabled. An arrow points to an Agent icon and an "Agent tools" box listing the two permitted tools.](https://mintcdn.com/kodekloud-c4ac6d9a/heceQKBf52UJgxP-/images/Google-ADK-20/Advanced-Tools/MCP-and-OpenAPI/company-files-list-read-agent-tools.jpg?fit=max\&auto=format\&n=heceQKBf52UJgxP-\&q=85\&s=65a658e8342a0090e713af9d77ac6e5c)

Google's pre-built tools

* In addition to OpenAPI and MCP, Google ships pre-built ADK tools (for example, Google Search) that you can attach to an agent without implementing HTTP calls or parsing results yourself.

Example:

```python theme={null}
from google.adk.tools import google_search
agent = Agent(name="support", tools=[google_search])
```

Summary — three ways to give your agent external capabilities

| Approach | When to use | Example |
| - | - | - |
| OpenAPI spec | You already have a REST API with a published OpenAPI file | `OpenAPIToolset(spec_str=spec, spec_str_type="yaml")` |
| MCP server | Provider can implement MCP or for non-REST systems (filesystems, RPC services, legacy systems) | `MCPToolset(endpoint="http://host:port")` |
| Built-in ADK tools | Use Google-provided integrations for common needs (search, execution, etc.) | `from google.adk.tools import google_search` |

![A dark-themed diagram titled "Three Doors Out" showing three stylized doors labeled "OpenAPI spec," "MCP server," and a plain door icon. Below them is an "Agent" icon and an "Agent tools" bar with a highlighted "create\_payment" button.](https://mintcdn.com/kodekloud-c4ac6d9a/heceQKBf52UJgxP-/images/Google-ADK-20/Advanced-Tools/MCP-and-OpenAPI/three-doors-openapi-mcp-agent.jpg?fit=max\&auto=format\&n=heceQKBf52UJgxP-\&q=85\&s=06fb2905aef8ce36b5b9239940e7db36)

Security and safety considerations

* Powerful tools can be dangerous. Reading a file may be harmless; cancelling an order or issuing refunds is not.
* Apply the principle of least privilege: only expose the operations an agent truly needs.
* Add logging, approval workflows, and human-in-the-loop checks for risky actions.
* Use agent-side filters to restrict available operations and consider rate limits, audit trails, and explicit confirmation steps for high-risk endpoints.

Apply least privilege and safe approval flows when exposing powerful operations to models. Limit tools to the minimum needed and require human authorization for risky actions.

Further reading and references

* OpenAPI Specification — [https://swagger.io/specification/](https://swagger.io/specification/)
* Principle of Least Privilege (overview) — [https://owasp.org/www-community/Least\_Privilege](https://owasp.org/www-community/Least_Privilege)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/google-adk-2-0/module/73e63dcd-2829-46d7-bbaa-5ac2336079d7/lesson/ecaa745b-82c7-401a-ae76-9752c62198e6)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.