FastMCP: the high-level server API

The official Python MCP SDK ships a class called FastMCP that does for servers what @tool did for LangChain functions: you write a normal Python function, decorate it, and the SDK handles turning it into something a client can discover and call.

from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator-server")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers together."""
return a + b

FastMCP("calculator-server") creates the server object. The string is the server's name, this is what a host displays when it lists connected servers, similar to how calculator.name was what an AI saw for a LangChain tool.

@mcp.tool() vs. @tool

If this looks almost identical to @tool from the LangChain course, that's the point. Both wrap a plain function so something else, an AI, a client, can be told its name, description, and argument schema without seeing the implementation. The difference is where that description travels: @tool keeps it in the same Python process; @mcp.tool() sends it over a protocol to a completely separate program.

Running the server

A server needs a run loop. mcp.run() starts it and blocks, listening for a client to connect.

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

transport="stdio" means the server reads requests from standard input and writes responses to standard output. That's the default for local servers a host launches as a subprocess, more on this in Lesson 8, and what every server in this course uses until Lesson 20.

This server has no client yet, so running it directly would just sit there waiting for stdio input. Instead, lesson.py calls the tool function directly, the same trick the LangChain course used, so you can see the tool works before any protocol is involved.

Checkpoint

  • FastMCP: the high-level class for building an MCP server, imported from mcp.server.fastmcp.
  • @mcp.tool(): decorates a function so it's exposed to clients, name/description/schema all pulled from the function itself.
  • mcp.run(transport="stdio"): starts the server's request loop, reading and writing over standard input/output.
  • A tool function still works as a plain function underneath, nothing about calling it directly has changed.

If anything here still feels unclear, ask before moving to Lesson 3, where we look at exactly how that schema gets built.