Below is a simple example that connects an Agent to the Agno MCP server:
The Basic Flow
1
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.
2
Initialize the MCP integration
Initialize the
MCPTools class and connect to the MCP server.
The recommended way to define the MCP server is to use the command or url parameters.
With command, you can pass the command used to run the MCP server you want. With url, you can pass the URL of the running MCP server you want to use.For example, to connect to the Agno documentation MCP server, you can do the following:3
Provide the MCPTools to the Agent
When initializing the Agent, pass the
MCPTools instance in the tools parameter. Remember to close the connection when you’re done.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:filesystem_agent.py
Connecting your MCP server
Using connect() and close()
It is recommended to use the connect() and close() methods to manage the connection lifecycle of the MCP server.
This is the recommended way to manage the connection lifecycle of the MCP server when using
Agent or Team instances.Automatic Connection Management
If you pass theMCPTools instance to the Agent or Team instances without first calling connect(), the connection will be managed automatically.
For example:
Here the connection to the MCP server (in the case of hosted MCP servers) is established and closed on each run.
Additionally the list of available tools is refreshed on each run.This has an impact on performance and is not recommended for production use.
Using Async Context Manager
UseMCPTools as an async context manager for automatic resource cleanup:
.connect() and .close() methods provide more control over connection lifecycle.
Automatic Connection Management in AgentOS
When using MCPTools within AgentOS, the lifecycle is automatically managed. No need to manually connect or disconnect theMCPTools instance. This does not automatically refresh connections. You can use refresh_connection to do so.
See the AgentOS + MCPTools page for more details.
This is the recommended way to manage the connection lifecycle of the MCP server when using
AgentOS.Connection Refresh
Setrefresh_connection on an MCPTools instance to check its MCP session before each run. An unhealthy session is reconnected, then the available tools are refreshed.
How it works
- When you call the
connect()method, a new session is established with the MCP server. If that server becomes unavailable, that connection is closed and a new one has to be established. - If you set
refresh_connectiontoTrue, each time the agent is run the connection to the MCP server is checked and re-established if needed, and the list of available tools is then refreshed. - This is particularly useful for hosted MCP servers that are prone to restarts or that often change their schema or list of tools.
- It is recommended to only use this when you manually manage the connection lifecycle of the MCP server, or when using agents/teams with
MCPToolsinAgentOS.
Transports
Transports define how MCP messages are sent and received. Agno supports the two standard MCP transports plus deprecated standalone SSE compatibility:- stdio for local subprocess servers
- Streamable HTTP for remote servers
- Standalone SSE for legacy servers. This transport is deprecated in Agno v3.0.4 and will be removed in a future release.
The stdio transport is the default in
MCPTools. Use Streamable HTTP for new remote servers.Best Practices
- Resource Cleanup: Always close MCP connections when done to prevent resource leaks:
- Error Handling: Always include proper error handling for MCP server connections and operations.
- Clear Instructions: Provide clear and specific instructions to your agent: