Sub topic 5
Your first tool
This chapter builds a minimal MCP server with one tool and shows how the SDK turns a Python function into a tool definition. It assumes you can read Python type hints, know what JSON Schema is, and understand the host, client, and server roles from earlier chapters.
What a tool is
A tool is a function the server exposes so the model can ask the host to call it. The server describes each tool with three things: a name, a description, and an input schema. The model reads those, decides whether to call the tool, and fills in the arguments. The host runs the call, and the server returns a result.
Tools are model-controlled. The model chooses when to call them. Resources and prompts, covered later, are controlled by the application and the user.
Install the SDK
Install the official Python SDK with its command-line extras:
pip install "mcp[cli]"Write the server
This server uses the FastMCP helper from the SDK. It exposes one tool that looks up which team owns a service.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("team-tools")
OWNERS = {"billing": "payments-team", "search": "discovery-team"}
@mcp.tool()
def get_service_owner(service: str) -> str:
"""Return the team that owns a service, given its short name."""
if service not in OWNERS:
raise ValueError(f"Unknown service: {service}")
return OWNERS[service]
if __name__ == "__main__":
mcp.run() # stdio transport by defaultSave it as server.py. The string passed to FastMCP is the server name, which the host shows to users.
How the function becomes a tool
The @mcp.tool() decorator registers the function. The SDK reads the function and builds the tool definition from it:
| Part of the function | Part of the tool definition |
|---|---|
| Function name | Tool name: `get_service_owner` |
| Docstring | Tool description the model reads |
| Parameter names and type hints | Input schema (JSON Schema) |
| Return type | Shape of the result |
Here the schema says the tool takes one required string named service. Because the model relies on the description to choose the tool, write the docstring for the model, not only for maintainers.
Run it over stdio
With no arguments, mcp.run() uses the stdio transport. The host launches your script as a subprocess and exchanges JSON-RPC messages over its standard input and output. You normally do not run the script by hand to use it. A host starts it for you, and a later chapter covers that setup.
Because stdout carries protocol messages, never use print() in a stdio server. Any stray text on stdout corrupts the stream and the client will fail to parse it. Send logs to stderr instead:
import logging
import sys
# With the stdio transport, stdout carries protocol messages.
# Send all logs to stderr so they do not corrupt the stream.
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
logger = logging.getLogger("team-tools")Common mistakes
- Missing docstring. The tool still works, but the model gets no description and may never choose it.
- Missing type hints. Without them the schema cannot describe the arguments, so the model has to guess.
- Vague names.
lookuptells the model less thanget_service_owner. - Relative paths in host config. The host may launch the script from a different working directory, so use absolute paths.
Try it
You can check the tool before wiring up a host. The next chapters cover typed inputs and the Inspector in detail. For now, confirm that the file starts without errors:
python server.pyThe process waits for input on stdin and prints nothing. Press Ctrl+C to stop it. If you see a traceback, fix it before continuing.
Exercise
Your server's get_service_owner tool has no docstring, and a print("starting") call at module level. Name one problem each causes.
Show answer
Without a docstring, the tool has no description, so the model has less information for deciding when to call it. The print writes to stdout, which the stdio transport uses for protocol messages, so it can corrupt the stream and break the connection. Log to stderr instead.