Skip to main content
The Model Context Protocol (MCP) is a standardized protocol that enables AI agents to interact with external tools and services through MCP servers. Swarms provides first-class support for MCP integration, allowing your agents to dynamically discover and execute tools.

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.
That is the whole integration. Behind it, the agent builds an MCPManager — reachable as agent.mcp_manager — which handles transport, auth, discovery, and routing.

Several servers at once

The agent sees the union of every server’s tools and each call is routed back to the server that owns it. If two servers expose a tool with the same name, the first server’s tool is kept and a warning is logged.

Without an agent

MCPManager works standalone when you want tools, not autonomy:
Every method has an async twin: 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, pass MCPConnection objects:

Secrets from the environment

Keep keys out of source — both env: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:
Valid values are 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:
Hyphenated forms such as streamable-http are normalized automatically.

OAuth 2.1

Full OAuth 2.1 is supported, including PKCE authorization-code flow with RFC 7591 dynamic client registration, headless client credentials, and pre-issued tokens. See MCPManager for every field.

Multi-Server Integration

One manager, many servers, automatic routing:
Servers can be added later; doing so invalidates the tool cache so the next fetch picks them up:

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 an Agent performs between turns.
Each result is an envelope:
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

The agent discovers the server’s tools on startup, decides which to call, executes them, and feeds results back into its own loop.

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.
Another agent connects with the same 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 with api_key_env, your own auth callable, or an mcp TokenVerifier with required_scopes. A server with no auth configured refuses to build unless you pass allow_anonymous=True.
  • Transports. Streamable HTTP (default), SSE, or stdio for desktop MCP hosts.
  • Requirements. swarms 15.0.3 or later and mcp>=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 to retry_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:
Build one manager and reuse it rather than constructing one per call.

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 Timeouts
Authentication Failures
With mcp 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
Agent Not Using the Tools

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