Prerequisites
MCPDeployer needs mcp 2.0.0 or newer.
Build it
1
Write the server
The agent you want to share is an ordinary The tool is named after the agent:
Agent. agent_description becomes the tool description the calling model reads, so write it for a reader with no other context.server.py
Market-Analyst becomes market_analyst.2
Run it
3
Check it from a second terminal
/health is public:401:4
Call it from another agent
The client is an ordinary agent too. The portfolio manager discovers
MCPConnection carries the URL and the key.client.py
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
MCPDeployerwrappedanalystas one MCP tool with the schematask: str,img: str | None.- Every request to
/mcpwent through the auth layer first. The key can arrive asx-api-keyor asAuthorization: Bearer, which is whatMCPConnectionsends. - 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:
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:
auth callable that reads other headers, such as a tenant id, send them with headers:
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:
start() and stop(), or a with block, run it on a background thread:
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 agent fails to build with AgentMCPConnectionError
The client agent fails to build with AgentMCPConnectionError
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.The tool call fails after two minutes
The tool call fails after two minutes
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.A remote client cannot connect
A remote client cannot connect
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.