Skip to main content
Everything else in this section connects to somebody else’s server. This one is the other half: putting your functions behind MCP so any agent — or any MCP client, in any language, on any machine — can call them. The reason to do this rather than pass Python functions to Agent(tools=[...]) is process boundaries. A tool that needs a database credential, a GPU, or a private network can live where those things are, and agents reach it over HTTP.

Prerequisites

mcp 2.x renamed FastMCP to MCPServer and moved it to mcp.server.mcpserver. On mcp 1.x, import from mcp.server.fastmcp import FastMCP instead; the decorator and run() call below are the same. The swarms client works with both.

Build it

1

Write the server

An MCPServer is a Python module with decorated functions. The decorator’s name and description are what the model sees, so write them for a reader who has no other context.
Return a string, and make errors part of that string. An agent can reason about “Could not find data for bitcion”; it cannot reason about a traceback.
2

Run it

It serves on http://127.0.0.1:8000/mcp. Leave it running.
3

Point an agent at it

In a second terminal:
Identical to every hosted-server example in this section — only the URL differs.
4

Inspect the server without an agent

When a tool call misbehaves, take the model out of the loop. MCPManager is the class the agent uses internally:
If call_tool returns what you expect and the agent still gets it wrong, the problem is the description or the prompt — not the server.

Tuning a connection to a local server

MCPConnection gives you per-server timeouts and headers:

MCP tools plus your own Python tools

The two coexist on one agent — MCP tools come from the server, and tools_list_dictionary (or tools=[...]) adds your own:

An agent as a tool

To serve a swarms Agent, or a whole swarm, as an MCP tool, you do not need to write a server by hand. MCPDeployer wraps it, puts an auth layer in front, and serves it:
That is how you compose swarms across process or machine boundaries: the calling agent does not know or care that the tool it invoked is itself an agent. See Serve an agent over MCP for the full walkthrough.

Writing tools an agent can use well

Next