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
In v1.x, Agency Swarm provides helpers to connect to MCP servers using the Agents SDK. 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.
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.
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 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.
HostedMCPTool: For Public Web Servers
HostedMCPTool: For Public Web Servers
Use this if your tool server is a publicly accessible web service. This approach uses OpenAI’s hosted MCP tool capabilities.
- When to use: Your tool server is accessible from the internet and you want to leverage OpenAI’s infrastructure for MCP connections.
Server specified in the HostedMCPTool should be publicly accessible for the agent to be able to use this tool
Connecting Agents to MCP Servers
To give an agent access to MCP tools, you define the server connections and pass them to the agent’smcp_servers
list or tools
list during initialization. Follow these steps:Step 1: Define Local Server Connections
Step 1: Define Local Server Connections
Configure local MCP servers (stdio, local SSE, or streamable HTTP) that will run on your machine.
Step 2: Define Hosted Server Tools (Optional)
Step 2: Define Hosted Server Tools (Optional)
Configure hosted MCP tools for publicly accessible servers.
Step 3: Initialize Agent with Servers
Step 3: Initialize Agent with Servers
Pass the configured server connections to the appropriate parameter when creating your
Agent
.Runnable Demo
For a practical, runnable example using both local and hosted MCP servers, see the complete example above or themcp_server_example.py
script located in the examples/
directory of the Agency Swarm repository.- Remember: The demo requires you exposing port 8000 through ngrok prior to running it.
Key Takeaways
- MCP connects agents to external tools/data via standard protocols (Stdio, SSE, Streamable HTTP).
- v1.x: Use mcp_server for local mcp servers and
HostedMCPTool
for web-based servers, since the latter allows you to avoid maintaining server lifecycle. - v0.x: Use
MCPServerStdio
for command-line tools,MCPServerSse
for SSE servers, andMCPServerStreamableHttp
for HTTP streaming servers. - External MCP servers must be running separately for the agent to connect to them.