Sub topic 9
Writing your own minimal server
This chapter assumes you can read basic Python, run commands in a terminal, and edit a JSON configuration file. You should also know the three server primitives (tools, resources, prompts) and that a local server talks to the assistant over stdio. By the end you will have a working server that exposes one tool.
What you are building
A minimal MCP server does three things:
- It declares a name, so the client can identify it.
- It registers one or more tools, each with a name, an input schema, and a description.
- It listens on a transport, here stdio, and answers
tools/listandtools/callrequests.
The official Python SDK handles the protocol work. Its FastMCP helper builds the tool definition from your function, so you write ordinary Python and the SDK writes the JSON-RPC layer.
Install the SDK
Install the SDK in a virtual environment:
python -m venv .venv
source .venv/bin/activate
pip install "mcp[cli]"The server
Save this as server.py:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
if __name__ == "__main__":
mcp.run() # stdio transport by defaultThe decorator @mcp.tool() turns the function into a tool. The SDK reads three things from it:
| Part of the function | Becomes |
|---|---|
| Function name `add` | The tool name |
| Type hints `a: int, b: int` | The JSON input schema |
| Docstring | The tool description the model reads |
The description matters more than it looks. The model decides whether to call your tool based on that text, so write it as a clear statement of what the tool does and when to use it.
What happens on the wire
When a client discovers your server, it first sends tools/list and receives the schema generated from your type hints. To use the tool, it sends a tools/call request:
{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}}}The server runs add(2, 3) and replies with a result containing the text 5. Because the transport is stdio, these messages travel over the process's standard input and output.
Register it with the assistant
Add the server to the assistant's configuration, using the same mcpServers shape as in the earlier chapter on configuration:
{
"mcpServers": {
"calculator": {
"command": "python",
"args": ["/absolute/path/to/server.py"]
}
}
}Use an absolute path, because the assistant may launch the process from a different working directory. If you installed the SDK in a virtual environment, set command to that environment's Python, for example /absolute/path/to/.venv/bin/python. Otherwise the assistant may start a Python that does not have mcp installed.
If your server needs a secret, such as an API token, pass it with the env field instead of writing it into the source:
{
"mcpServers": {
"my-api": {
"command": "python",
"args": ["server.py"],
"env": { "API_TOKEN": "<set-in-your-environment>" }
}
}
}Read it in Python with os.environ["API_TOKEN"].
Common mistakes
- Missing type hints. Without them, the schema has no useful types, and the model may pass the wrong kind of value.
- Vague docstrings. "Does math" tells the model little. "Add two integers and return the sum" is specific.
- Printing to stdout. This breaks the stdio stream, as the warning above describes.
- Relative paths or the wrong interpreter. The server fails to start, and the assistant usually shows only a generic connection error.
- Doing too much in one tool. Several small, clearly named tools are easier for a model to choose between than one tool with many modes.
When a server does not appear or a call fails, test it without the assistant. The next chapter covers the MCP Inspector, which starts your server and lets you call add by hand.
Exercise
You add a new tool to server.py and, to debug it, put print("called add") inside the function. The assistant then reports that the server disconnected. Why did this happen, and what should you use instead?
Show answer
With the stdio transport, standard output carries the JSON-RPC messages. The print call wrote plain text into that stream, so the client could not parse it and dropped the connection. Write debug output to standard error instead, for example print("called add", file=sys.stderr) or the logging module, which logs to stderr by default.
Exercise
Add a second tool to server.py named multiply that takes two integers and returns their product. What three parts of the function does FastMCP use to build the tool definition?
Show answer
Add this function above the if __name__ block:
@mcp.tool()
def multiply(a: int, b: int) -> int:
"""Multiply two integers."""
return a * b
FastMCP uses the function name as the tool name, the type hints as the input schema, and the docstring as the description.