MCP Integration
Connect your agents to external tools and data using the Model Context Protocol (MCP).
Agency Swarm agents can interact with a wider range of tools and data sources beyond their built-in capabilities by using the Model Context Protocol (MCP). MCP is an open standard (view specification) that allows agents to communicate with external services like local file systems, databases, or custom APIs, as long as those services implement the protocol.
Think of MCP as a universal translator that lets your agent talk to specialized external tools.
Why use MCP?
- Access Local Resources: Let agents read/write local files or run local commands.
- Connect to Custom Services: Integrate with proprietary APIs or internal tools without writing specific Agency Swarm tool wrappers for each one, provided an MCP server exists.
- Leverage Existing MCP Tools: Utilize third-party tools that already support MCP.
Supported MCP Server Types
Agency Swarm provides helpers to connect to the three common MCP transport protocols. Choose the server type based on how your tool provider operates:
MCPServerStdio: For Command-Line Tools
MCPServerStdio: For Command-Line Tools
Use this if your tool server is a command-line program or script. Agency Swarm will start this program for you and communicate with it directly using its standard input/output.
- When to use: Your tool is a local script, an executable, or requires running a specific command to be activated (like the standard MCP filesystem server).
MCPServerSse: For Web Service Tools
MCPServerSse: For Web Service Tools
Use this if your tool server is already running as a web service at a specific HTTP URL. Agency Swarm connects to this URL to access tools exposed via Server-Sent Events (SSE).
- When to use: Your tool is provided by a web API, a microservice, or any server accessible via an HTTP endpoint that speaks MCP+SSE (like the Python server in the demo).
MCPServerStreamableHttp: For HTTP Streaming Tools
MCPServerStreamableHttp: For HTTP Streaming Tools
Use this if your tool server is a web service that implements the Streamable HTTP transport protocol. This replaces the deprecated HTTP+SSE transport and uses HTTP POST requests with optional Server-Sent Events (SSE) streaming for responses.
- When to use: Your tool server operates as an independent web service, supports multiple client connections, needs stateful or stateless operation, or requires server-to-client streaming capabilities.
Connecting Agents to MCP Servers
To give an agent access to MCP tools, you define the server connections and pass them to the agent’s mcp_servers
list during initialization. Agency Swarm then automatically discovers the tools offered by the server and makes them available to the agent under the name
you specified (e.g., Filesystem_Server.list_files
).
Follow these steps:
Step 1: Define Stdio Server Connection (e.g., Filesystem)
Step 1: Define Stdio Server Connection (e.g., Filesystem)
This example shows how to configure MCPServerStdio
to run the standard MCP filesystem tool using npx
.
Step 2: Define SSE Server Connection (Optional)
Step 2: Define SSE Server Connection (Optional)
This example shows how to configure MCPServerSse
to connect to a hypothetical web server running locally that provides tools via SSE.
Step 3: Define Streamable HTTP Server Connection (Optional)
Step 3: Define Streamable HTTP Server Connection (Optional)
This example shows how to configure MCPServerStreamableHttp
to connect to a web server that implements the Streamable HTTP transport protocol.
Step 4: Initialize Agent with Servers
Step 4: Initialize Agent with Servers
Pass the list of configured server connections to the mcp_servers
parameter when creating your Agent
.
Runnable Demo
For a practical, runnable example using both MCPServerStdio
and MCPServerSse
, see the demo_mcp.py
script located in the tests/demos/
directory of the Agency Swarm repository.
- Remember: The demo requires you to run the example SSE server (sse_server.py) in a separate terminal first.
Key Takeaways
- MCP connects agents to external tools/data via standard protocols (Stdio, SSE, Streamable HTTP).
- Use
MCPServerStdio
for command-line tool servers,MCPServerSse
for SSE-based web servers, andMCPServerStreamableHttp
for HTTP streaming servers. - Define server connections using the appropriate class, providing
name
,params
, and optionally configuringstrict
andcache_tools_list
. - Pass configured server instances to the
Agent
’smcp_servers
parameter during initialization. - The
name
you give a server connection becomes the prefix for its tools (e.g.,MyServerName.tool_name
). - External MCP servers (especially SSE and streaming HTTP ones) must be running separately for the agent to connect to them.