What is MCP?
MCP (Model Context Protocol) provides:- Standardized Tool Interface: Unified protocol for tool integration
- Dynamic Discovery: Automatically discover available tools from MCP servers
- Multi-Server Support: Connect to multiple MCP servers simultaneously
- Type Safety: Automatic schema validation for tool calls
- Flexible Transport: Streamable HTTP, SSE, and stdio transports
Quick Start
Let the agent do it
The simplest integration: give the agent a URL and it discovers and calls the tools itself.MCPManager — reachable as agent.mcp_manager — which handles transport, auth, discovery, and routing.
Several servers at once
Without an agent
MCPManager works standalone when you want tools, not autonomy:
aget_tools, acall_tool, aexecute_tool_calls.
Connection Configuration
Agent-level settings
Authentication and transport can be set directly on the agent and apply to every server it uses:mcp_api_key, mcp_authorization_token, and mcp_oauth only fill in servers that have no credential of their own. mcp_headers is merged into each server’s headers, with the server’s own winning. mcp_transport and mcp_timeout replace every server’s own value.
Per-server settings with MCPConnection
For different credentials per server, passMCPConnection objects:
Secrets from the environment
Keep keys out of source — bothenv:NAME and ${NAME} are resolved when the connection is made. This works for api_key, authorization_token, the OAuth credential fields, and stdio env values; headers are sent as written:
Transport
Every connection uses streamable HTTP unless you say otherwise; the URL alone does not switch it. Set the transport for an SSE server:streamable_http, sse, stdio, and auto. With auto, a URL ending in /sse uses SSE and a value with no scheme, such as "python server.py", uses stdio. A connection with command set always uses stdio:
streamable-http are normalized automatically.
OAuth 2.1
Multi-Server Integration
One manager, many servers, automatic routing:Tool Execution
Executing what a model asked for
When an LLM replies with tool calls, hand the response straight to the manager. Each call is routed and the results come back in order — this is the step anAgent performs between turns.
result is a str when the server returned text, which is how most servers send dicts and numbers, so it may hold JSON. It is a dict when the server returned structured content only, or text mixed with images or resources. Check the type before calling json.loads(); see What result holds.Calling one tool directly
Async
Real-World Example
Serving your own agents over MCP
The integration also runs the other way.MCPDeployer turns an agent, any swarm with a run() method, or a plain function into an MCP server with an auth layer in front of it. Each target becomes one tool that takes task and an optional img, so any MCP client, including another swarms agent, can call it.
MCPConnection used for any other server:
- Several agents, one server. Pass a list, or a dict of tool name to target:
MCPDeployer({"research": researcher, "write": pipeline}, api_keys=[...]). - Auth. Static
api_keys, keys from an environment variable withapi_key_env, your ownauthcallable, or anmcpTokenVerifierwithrequired_scopes. A server with no auth configured refuses to build unless you passallow_anonymous=True. - Transports. Streamable HTTP (default), SSE, or stdio for desktop MCP hosts.
- Requirements.
swarms15.0.3 or later andmcp>=2.0.0.
Serve an agent over MCP
One agent behind an API key, called by a second agent.
MCPDeployer reference
Every parameter, auth mode, transport, and method.
Error Handling
Connection and discovery failures raise the agent MCP exceptions. Tool discovery retries with exponential backoff up toretry_attempts (default 3) before raising; tool calls are not retried:
AgentMCPToolError is raised only by call_tool(), when no configured server exposes the tool name. Per-call failures do not raise — check the envelope instead. A failure reported by the server carries its text in result; a client-side failure carries it in error:
Inspecting Configuration
to_dict() gives a serializable, secret-redacted view — safe to log:
Caching
Tool schemas are cached per manager after the first fetch:Best Practices
Reuse the manager
Tool schemas are cached per manager, so build one and reuse it
Timeout Configuration
Set appropriate timeouts based on server response times
Error Recovery
Implement fallback strategies for MCP server failures
Verbose Logging
Enable verbose mode during development for debugging
Troubleshooting
Common Issues
Connection Timeoutsmcp 2.x a rejected credential shows up as Server returned an error response, not 401. Check the server’s logs for the reason.
Tool Not Found
Next Steps
MCP tutorials
Step-by-step tutorials against real servers: DeepWiki, Exa, Firecrawl, Hugging Face, Semgrep
MCPManager
The full client reference: connections, results, errors, OAuth
MCPDeployer
Serve your own agents and swarms as MCP servers
Model Providers
Configure different LLM providers
Custom Tools
Create your own tool integrations