Skip to main content

Overview

MCPManager is the one class handling MCP in Swarms. Point it at one or more servers and it takes care of transport selection, authentication, tool discovery, schema caching, and routing each tool call to the server that owns it. An Agent builds one as agent.mcp_manager whenever you set mcp_url or mcp_urls. You can also use it directly, with no agent involved.
swarms.tools.mcp_client_tools and its standalone functions have been removed. See Migration for direct replacements.

Import

Constructor

Union[str, MCPConnection, Dict]
A single server — URL string, connection object, or dict.
List[Union[str, MCPConnection, Dict]]
Several servers. Entries may mix forms, so different servers can use different authentication.
Union[MCPConnection, Dict]
Full configuration for one server.
List[Union[MCPConnection, Dict]]
Full configuration for several servers.
str
API key applied to every server that does not define its own.
str
Bearer token applied to every server that does not define its own.
Union[MCPOAuthConfig, Dict]
OAuth 2.1 configuration.
Dict[str, str]
Extra headers merged into every request.
str
Force streamable_http, sse, or stdio. Auto-detected otherwise; hyphenated forms like streamable-http are normalized.
int
default:"None"
Request timeout in seconds, applied to every configured connection. Left unset by default — a None value falls through to MCPConnection.timeout’s own default of 30 seconds.
str
default:"agent"
Name used in log messages.
bool
default:"false"
Verbose logging.
int
default:"3"
Retries per operation before raising.

Discovery

get_tools

Fetch tools from every configured server. Returns OpenAI function-calling schemas by default, ready to hand to an LLM. Results are cached; pass force_refresh=True to re-fetch.
Async form: await manager.aget_tools(...).
aget_tools only populates self._tools_cache / self._tool_routes when format == "openai". On a multi-server manager, calling get_tools(format="mcp") alone leaves tool routing unpopulated, and a later call_tool() can fail with “No configured MCP server exposes a tool named …” until get_tools() (the default "openai" format) has been called at least once.

list_tool_names

Every tool name across all configured servers — the cheapest way to see what is available.

Execution

call_tool

Call one tool directly, no LLM involved. Returns a result envelope:
Async form: await manager.acall_tool(...).

execute_tool_calls

Run the tool calls contained in an LLM response. Each call is routed to the server that advertised the tool, and results come back in call order. This is the step an Agent performs between LLM turns.
Any
required
An LLM response, a single call dict, or a list of calls.
"dict" | "json" | "str"
default:"dict"
Result format.
Async form: await manager.aexecute_tool_calls(...). To re-render results you already hold, use the static MCPManager.format_results(results, output_type).
Under the default output_type="dict", result is already a native Python value, not a JSON string — a str for text content, or a dict for structured content (structuredContent / structured_content, or a text+content mix). Use it directly:
Calling json.loads() on it will raise TypeError when result is a dict. JSON-string framing only applies when the whole envelope is serialized via output_type="json" (or MCPManager.format_results(..., output_type="json")).

Configuration management

Authentication

Both env:NAME and ${NAME} indirection are resolved when the connection is made, so secrets stay out of source.

Mixed auth across servers

Errors

An execution or session failure on the server side is never raised — it’s captured and returned as an is_error: True result envelope (from call_tool/execute_tool_calls and their async forms) so a batch of calls can’t be aborted by one failing tool. All live in swarms.schemas.agent_mcp_errors. Operations retry with backoff up to retry_attempts before raising.

Migration

The standalone functions in swarms.tools.mcp_client_tools were removed once everything moved onto this class. Two behavioral differences to be aware of:
  1. output_type on tool fetching is gone. get_tools_for_multiple_mcp_servers accepted the argument but never applied it. Use format="openai" | "mcp" to pick the schema shape.
  2. Execution results are wrapped. The old functions returned the raw MCP CallToolResult dump. execute_tool_calls returns one envelope per call, so results from several servers stay attributable. Read result["result"] for the tool’s own payload.

MCP Integration

Using MCP servers from an agent

Agent

Set mcp_url / mcp_urls to wire this in automatically