Sub topic 7
Building a small server to test against
This chapter assumes you can read async Python, know what JSON Schema is, and have called an LLM API with tools before. You will build one small MCP server. Later chapters connect a client and an agent loop to it.
Why build your own server first
Third-party servers change, need credentials, and may touch real data. A server you wrote is predictable. You know what its tools return, so a wrong result points at your client code and not at the server.
Install the SDK
The official Python SDK ships a helper called FastMCP that handles the protocol details for you. Install it with pip install "mcp[cli]". The cli extra also installs the mcp command, which you will use for the Inspector in a later chapter.
The server
Save this as server.py.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calc")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
if __name__ == "__main__":
mcp.run(transport="stdio")Each part does one job:
FastMCP("calc")creates a server namedcalc. The name is reported to clients during initialization.@mcp.tool()registers the function as a tool. The function name becomes the tool name.- The docstring becomes the tool description. The model reads this text to decide when to call the tool, so write it for the model.
- The type hints
a: int, b: intbecome the tool'sinputSchema, which is a JSON Schema. Both parameters are required because neither has a default value. transport="stdio"makes the server read requests from standard input and write responses to standard output. A client starts it as a subprocess.
What the SDK builds for you
The decorator produces a schema like this for add:
{
"type": "object",
"properties": {
"a": {"title": "A", "type": "integer"},
"b": {"title": "B", "type": "integer"}
},
"required": ["a", "b"]
}If a client sends a string for a, the SDK rejects the call with an error result. Your function never runs with bad input.
Running it
You normally do not start this server by hand. A client launches it. If you run python server.py in a terminal, it appears to hang. It is waiting for JSON-RPC messages on standard input. Press Ctrl+C to stop it.
A common error is a command that points at the wrong Python. A client that runs python may pick an interpreter without the mcp package. Use the interpreter from your virtual environment, for example by passing sys.executable as the command.
Make it a useful test target
A single tool that always succeeds cannot test error handling. Once the basics work, consider adding a second tool that raises an exception. The SDK turns the exception into a result with isError set to true. The error chapters use this to check that your loop reports failures to the model without crashing.
Exercise
Why does the add tool in this server require both a and b, and where does that requirement come from?
Show answer
The requirement comes from the function signature. FastMCP builds the inputSchema from the type hints, and parameters without default values are listed under required. If you gave b a default such as b: int = 0, it would become optional.
Exercise
A client connects to your stdio server but fails with a JSON parse error. You recently added print("starting") at the top of server.py. What is the cause and the fix?
Show answer
Standard output is the protocol channel under stdio, so the printed text is read as a malformed JSON-RPC message. Remove the print or send it to standard error with print("starting", file=sys.stderr), or use logging.