Skip to main content
Every other page in this section points an agent at an MCP server. This one makes an agent into one. Once it is served, any MCP client can call it as a tool: another Swarms agent, Claude Desktop, Cursor, or a script in another language.

Prerequisites

MCPDeployer needs mcp 2.0.0 or newer.

Build it

1

Write the server

The agent you want to share is an ordinary Agent. agent_description becomes the tool description the calling model reads, so write it for a reader with no other context.
server.py
The tool is named after the agent: Market-Analyst becomes market_analyst.
2

Run it

It prints a banner with the URL and keeps serving. Leave it running.
3

Check it from a second terminal

/health is public:
The MCP endpoint is not. A request with no key, or the wrong one, gets 401:
4

Call it from another agent

The client is an ordinary agent too. MCPConnection carries the URL and the key.
client.py
The portfolio manager discovers market_analyst, calls it with the question as task, and summarises what the analyst returned. The server terminal logs each request.The client lists the server’s tools while it is being constructed, so start the server first. A server that is down, or a wrong key, makes the Agent(...) call itself raise AgentMCPConnectionError.

What just happened

  1. MCPDeployer wrapped analyst as one MCP tool with the schema task: str, img: str | None.
  2. Every request to /mcp went through the auth layer first. The key can arrive as x-api-key or as Authorization: Bearer, which is what MCPConnection sends.
  3. The tool call ran analyst.run(task) on a worker thread and returned its answer as the tool result.

Other ways to connect a client

MCPConnection(url=..., api_key=...) sends Authorization: Bearer <key>, which the deployer accepts. These forms work too. The examples read the key from the environment, so export it first with export ANALYST_MCP_KEY="sk-local-dev". A URL string plus an agent-level key. mcp_api_key applies to every server that has no key of its own, and env: reads the key from the environment when the connection is made:
The x-api-key header, with a longer tool timeout. mcp_config takes a full MCPConnection. A swarms client waits tool_timeout seconds for one tool call, 120 by default, so raise it when the served agent or swarm is slow:
Extra headers for custom auth. When the server uses an auth callable that reads other headers, such as a tenant id, send them with headers:
An SSE server. A deployer built with transport="sse" serves http://127.0.0.1:8000/sse. Clients default to streamable HTTP, so set the transport on the client too:

Variations

Keys from the environment instead of the code. api_key_env reads a comma-separated list, so you can rotate keys without a code change:
Run the server inside your own script. start() and stop(), or a with block, run it on a background thread:
Reachable from other machines. Bind to all interfaces and put TLS in front of it:
Launched by an MCP host. Claude Desktop and similar hosts start servers as subprocesses over stdio. There are no HTTP headers on stdio, so the host itself is the security boundary. Stdout carries the protocol, so turn the banner off:
A fresh agent per call. One served Agent handles every call and keeps its conversation between them. To give each call a clean history, serve a function that builds the agent. The function’s name and docstring become the tool’s name and description:

Troubleshooting

The client could not list the server’s tools. Check that the server is running and the key matches. With mcp 2.x the client reports a rejected key as Server returned an error response rather than 401; the server log shows MCPDeployer rejected request to /mcp for each refused request.
That is the client’s tool_timeout, 120 seconds by default. Raise it on the MCPConnection, and set the server’s timeout to match if you use one.
The default host="127.0.0.1" only accepts local connections. Bind with host="0.0.0.0" and give clients the machine’s address, not 0.0.0.0.

Next

Serve a team from one server

Several agents, a workflow, and a function, each its own tool.

MCPDeployer reference

Every parameter, auth mode, and method.