Model Context Protocol
The Model Context Protocol (MCP) enables Agents to interact with external systems through a standardized interface. You can connect your Agents to any MCP server, using Agno’s MCP integration.
Usage
Find the MCP server you want to use
You can use any working MCP server. To see some examples, you can check this GitHub repository, by the maintainers of the MCP themselves.
Initialize the MCP integration
Intialize the MCPTools
class as a context manager. The recommended way to define the MCP server, is to use the command
parameter and pass the command you would use to start the MCP server.
For example, to use the “mcp-server-git” server, you can do the following:
Provide the MCPTools to the Agent
When initializing the Agent, pass the MCPTools
class in the tools
parameter.
The agent will now be ready to use the MCP server:
Example: Filesystem Agent
Here’s a filesystem agent that uses the Filesystem MCP server to explore and analyze files:
StdIO vs SSE Transport
By default Agno’s MCP integration uses the stdio transport.
But you can also connect your Agents to MCP servers using the SSE transport:
For example, here we set up a simple local server and connect to it using the SSE transport:
Setup the server
Setup the client
Run the server
Run the client
Understanding Server Parameters
The recommended way to configure MCPTools
or MultiMCPTools
is to use the command
or url
parameters.
Alternatively, you can use the server_params
parameter with MCPTools
to configure the connection to the MCP server.
In the case of the StdIO transport, the server_params
parameter should be an instance of StdioServerParameters
. It contains the following keys:
command
: The command to run the MCP server.- Use
npx
for mcp servers that can be installed via npm (ornode
if running on Windows). - Use
uvx
for mcp servers that can be installed via uvx.
- Use
args
: The arguments to pass to the MCP server.env
: Optional environment variables to pass to the MCP server. Remember to include all current environment variables in theenv
dictionary. Ifenv
is not provided, the current environment variables will be used. e.g.
In the case of the SSE transport, the server_params
parameter should be an instance of SSEClientParams
. It contains the following fields:
url
: The URL of the MCP server.headers
: Headers to pass to the MCP server (optional).timeout
: Timeout for the connection to the MCP server (optional).sse_read_timeout
: Timeout for the SSE connection itself (optional).
Multiple MCP Servers
You can use multiple MCP servers in a single agent by using the MultiMCPTools
class.
More Flexibility
You can also create the MCP server yourself and pass it to the MCPTools
constructor.
Running MCP in Playground
You can also run MCP servers in the Agno Playground, which provides a web interface for interacting with your agents. Here’s an example of a GitHub agent running in the Playground:
Best Practices
-
Error Handling: Always include proper error handling for MCP server connections and operations.
-
Resource Cleanup: Use
MCPTools
orMultiMCPTools
as an async context manager to ensure proper cleanup of resources:
- Clear Instructions: Provide clear and specific instructions to your agent:
More Information
- Find a collection of MCP servers here.
- Read the MCP documentation to learn more about the Model Context Protocol.
- Checkout the Agno Cookbook for more examples of Agents that use MCP.