Lesson 11 / 25

Your First Server and Tool

Create a server with the official Python SDK and expose a typed function as a tool.

FastMCP in a few lines

The official Python SDK includes FastMCP. You create a server object and decorate normal functions with @mcp.tool(). The SDK builds the JSON schema from your type hints and uses the docstring as the description, so good types and docstrings are your tool documentation.

From function to server

Decorate plain functions, choose a transport, run the server and test it with an inspector.

Four steps: write, expose, run, inspect.
Figure 4.1 — Write, expose, run and inspect.

A calculator server

Install with pip install "mcp[cli]". The default transport is stdio, so the host starts this script as a subprocess.

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("calculator")

@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two integers and return the sum."""
    return a + b

if __name__ == "__main__":
    mcp.run()          # stdio transport by default

Never print to stdout on stdio

With the stdio transport, standard output carries the protocol messages. A stray print() corrupts the stream. Log to standard error or a file instead.

Quick check: Where does FastMCP get a tool's input schema from?

  • From the function's type hints
  • From a random guess
  • From the filename
  • From the operating system
Answer

From the function's type hints — Type hints define parameter types and the docstring becomes the description.