FastMCP: build your first MCP server in Python

FastMCP is an open-source Python framework (Apache 2.0 license, developed by Prefect) that turns Python functions into an MCP server: a @mcp.tool decorator is all it takes, and the tool's schema, validation, and description are generated from the type annotations and docstring. Version 4.2.1 was released on November 17, 2026, and requires Python 3.11 or later. Installation: uv add fastmcp or pip install fastmcp.
FastMCP lets you write an MCP server in Python, giving Claude, Cursor or a local agent tools you program yourself. This guide starts from scratch with version 4.2.1: installation on Mac, Windows or Linux, your first tool, testing without an assistant, choosing between STDIO and HTTP, registering with a client, and the most common errors.
#What is FastMCP, and which version should you use in November 2026?
FastMCP is an open-source Python framework for writing MCP servers, MCP clients, and interactive applications for AI assistants. As of November 18, 2026, the latest published release is FastMCP 4.2.1, dated November 17, 2026 in the official changelog, under the Apache 2.0 license.
An MCP server is the program that exposes capabilities to an AI application in three forms: tools (functions the model can call), resources (data to read), and prompts (reusable instruction templates).
FastMCP removes the protocol plumbing: you decorate an ordinary Python function, and FastMCP generates the input schema, the argument validation, and the description the model reads. The project is developed by the Prefect team. According to its README, FastMCP 1.0 was merged into the official MCP Python SDK in 2024, and some version of FastMCP reportedly powers 70% of MCP servers across all languages: this figure is the vendor's claim, with no published methodology.
| Point | Value | Source |
|---|---|---|
| Latest version | 4.2.1, released November 17, 2026 | gofastmcp.com changelog |
| Major branch | FastMCP 4, released August 31, 2026 (4.0.0) | gofastmcp.com changelog |
| Python required | 3.11 or later for 4.2; Python 3.10 stays on 4.1.x | "What's New in FastMCP 4" page |
| License | Apache 2.0 | Repository LICENSE file |
| Installation | uv add fastmcp ou pip install fastmcp | "Installation" page |
| Default transport | STDIO (standard input and output) | "Running Your Server" page |
| TypeScript equivalent | FastMCP for TypeScript, same team | Repository README |
#FastMCP, FastAPI or the official MCP SDK: what's the difference?
Benefit from our experience and a guided path to build your local copilot and get comfortable with your coding assistant.
- Lifetime online access
- PDF + files
- Refunded within 30 days
FastMCP and FastAPI serve different purposes: FastAPI builds web APIs for programs, while FastMCP builds servers that AI models can discover and call. The official MCP SDK is the protocol's core library, which FastMCP 4 builds on.
The confusion also stems from a real bridge: FastMCP can generate an MCP server from an existing FastAPI application (FastMCP.from_fastapi), or mount an MCP server inside a FastAPI application. Yet the FastMCP documentation recommends reserving this automatic conversion for prototyping: according to the docs, models get much better results from an MCP server designed for them, with a few well-described tools, than from a copy of every route in a REST API.
On the official SDK side, one issue trips up older projects. Version 2 of the mcp package rebuilt its high-level server under the name MCPServer, and the mcp.server.fastmcp module no longer exists there. A server that starts with from mcp.server.fastmcp import FastMCP therefore fails with a ModuleNotFoundError as soon as an installation pulls in mcp version 2. The FastMCP documentation states that, for most of these servers, moving to standalone FastMCP comes down to changing the import en from fastmcp import FastMCP.
| Need | Right tool | Why |
|---|---|---|
| Give Python tools to an AI assistant | FastMCP | A single decorator is enough; schema and validation are generated |
| Expose a conventional web API to applications | FastAPI | Not an MCP server |
| Prototype an MCP server from an existing FastAPI API | FastMCP.from_fastapi | Fast, but should be replaced with hand-picked tools |
| Control every protocol message | Official MCP SDK (low-level) | More code, more control |
#How do you install FastMCP on Mac, Windows, or Linux?
FastMCP installs like any other Python library: the documentation recommends the uv tool, and pip works too. Version 4.2 requires Python 3.11 or later, and you need uv to then register the server in Claude Desktop or Cursor.
- 01Install uvOn macOS and Linux, run uv's official script with curl; on Windows, use the official PowerShell command from Astral, the company behind uv (see the blocks below).
- 02Create a Python projectRun uv init hello-world, then change into the folder. uv creates the project's virtual environment on the first uv run, uv sync, or uv lock command.
- 03Add FastMCP to the projectRun uv add fastmcp in the project folder. Without uv, the pip install fastmcp command installs the same package into the active Python environment.
- 04Verify the installationRun uv run fastmcp version, which displays the FastMCP, MCP SDK, and Python versions. The uv run command runs a command in the project environment; without it, activate that environment first (source .venv/bin/activate on macOS and Linux, .venv\Scripts\activate on Windows).
- 05Pin the versionKeep the uv.lock file or pin an exact version, for example fastmcp==4.2.1: FastMCP's versioning policy allows breaking changes between two minor versions.
#Building your first FastMCP server in Python: the minimal code
A first FastMCP server fits in seven lines of code: create a FastMCP object, decorate a function with @mcp.tool, then call mcp.run(). The code below comes from the official quickstart; save it in a server.py file at the root of your project.
The @mcp.tool decorator does three things. FastMCP reads the function name to name the tool, reads its type annotations (here name: str) to generate the input schema in JSON Schema format, and reads its docstring to write the description the model consults before choosing the tool.
The model never sees your function, only its name, description, and schema. FastMCP recognizes Google-, NumPy-, and Sphinx-style docstrings: each parameter description in the Args section becomes that parameter's description in the schema. The following official example, a dice roller, shows a short but explicit docstring.
- Synchronous or asynchronous function
- FastMCP accepts both def and async def. A synchronous function automatically runs in a thread pool so it doesn't block other calls.
- Forbidden parameters
- A function with variable arguments (*args, or double-star kwargs) can't become a tool, because FastMCP has to generate a complete schema for every parameter.
- Validation constraints
- Pydantic's Annotated and Field add a description or bounds (for example, a width between 1 and 2000) to each parameter's schema.
- Resources and prompts
- The @mcp.resource and @mcp.prompt decorators expose readable data and prompt templates, following the same principle as @mcp.tool.
#How do you run and test a FastMCP server?
You start a FastMCP server with python server.py or the fastmcp run command, then test it without an AI assistant using MCP Inspector and the fastmcp list and fastmcp call commands. In a uv project, prefix these commands with uv run.
- fastmcp run server.py
- Starts the server by automatically finding the object named mcp, server, or app.
- --reload
- Restarts the server every time a source file is saved.
- fastmcp dev inspector
- Opens MCP Inspector, a browser interface for calling tools manually.
- fastmcp list
- Displays the server's tools as function signatures, with types and descriptions.
- fastmcp call
- Calls a tool with key=value arguments; a tool error returns a nonzero exit code.
With the code above, fastmcp call server.py greet name=World should return the text produced by the greet function, namely Hello, World!. If fastmcp list doesn't show the tool, first check where it's declared: the fastmcp run command imports the server object without executing the if __name__ == "__main__" block, so a tool registered in that block stays invisible to the CLI and to MCP Inspector.
For automated tests, the FastMCP documentation suggests passing the server object directly to the FastMCP client in a pytest fixture, with pytest-asyncio: tools are called in memory, with no separate process or network port.
#STDIO or HTTP: which transport should you choose for FastMCP?
For local use on your own machine, stick with STDIO, FastMCP's default transport: the client launches the server itself for each session. Choose HTTP only if multiple clients or another machine need to reach the server, and secure it if you do.
With STDIO, the client starts one server process per session and communicates with it over standard input and output. With the HTTP transport, called Streamable HTTP, the server becomes a web service reachable at an address and serves multiple clients at once. The SSE transport is still available for older clients, but the documentation advises against it for any new project.
With this setting, the MCP endpoint is http://localhost:8000/mcp, and FastMCP's Python client connects to it like this, according to the official quickstart.
| Situation | Transport | Key consideration |
|---|---|---|
| Personal tool in Claude Desktop, Cursor, or Claude Code | STDIO | Nothing may write to standard output |
| Multiple workstations or multiple agents on the same server | HTTP (Streamable HTTP) | Authentication and Host and Origin protection |
| Server exposed on the internet | HTTP behind a proxy | Authentication strongly recommended by the documentation |
| Older client that only speaks SSE | SSE | Legacy transport, avoid for new projects |
#Connect a FastMCP server to Claude Desktop, Claude Code, Cursor, or a local model
The fastmcp install command registers a local FastMCP server with an MCP client, which can then launch it on its own. It supports Claude Desktop, Claude Code, Cursor, Gemini CLI, and Goose, and can generate a standard JSON configuration for any other MCP-compatible client.
Each client launches the server in its own isolated environment managed by uv, so uv must be installed and on the system PATH, and the server's dependencies must be declared with --with, --with-requirements, or a fastmcp.json file. On macOS, the documentation recommends installing uv with Homebrew for Claude Desktop. After installing in Claude Desktop, fully restart the app so it loads the server.
For a client FastMCP doesn't know about, fastmcp install mcp-json prints the JSON configuration block to paste: the uv command, its arguments, and the environment variables.
A FastMCP server doesn't depend on any model: the client chooses the model and decides when to call the tools. To have your tools called by a local model served by Ollama, you need an MCP client connected to that model, for example the Python bridge described in the site's MCP and local LLM guide.
#Common FastMCP errors and how to fix them
Most failures in a first FastMCP server come from four causes: a print that pollutes standard output, a tool declared in the main block, an unpinned version, and an old SDK import. The table maps each symptom to its documented cause.
| Symptom | Documented cause | Correction |
|---|---|---|
| The client shows protocol errors or disconnects | A print() writes to stdout, which corrupts JSON-RPC messages over STDIO | Remove print calls; log to stderr or through the client's logs |
| Tool missing from fastmcp list or MCP Inspector | The CLI doesn't run the if __name__ == "__main__" block | Declare tools at module level or in a factory function |
| ModuleNotFoundError: No module named 'mcp.server.fastmcp' | The mcp package has moved to version 2 | Import from fastmcp import FastMCP, or pin mcp<2 in the meantime |
| A server breaks after a minor update | Possible breaking changes between 4.1.x and 4.2.0 | Lockfile or exact version |
| The server won't start in Claude Desktop | uv introuvable ou dépendance non déclarée | Install uv globally, declare packages with --with |
The standard output rule comes from the official MCP protocol documentation: with STDIO, writing to stdout corrupts JSON-RPC messages and breaks the server. With HTTP, logging to standard output isn't a problem.
What is the definition of the Model Context Protocol (MCP)?+
Is FastMCP free?+
Which Python version does FastMCP 4.2 require?+
Can you turn an existing FastAPI API into an MCP server with FastMCP?+
Does a FastMCP server work with Ollama or another local LLM?+
#Sources and verification as of November 18, 2026
Versions, dates, commands and default settings were checked on November 18, 2026 against the official FastMCP documentation, its GitHub repository, the MCP protocol documentation and the uv documentation. FastMCP releases often and allows breaking changes between minor versions, so check your version before copying a command.
- Source: PrefectHQ/fastmcp GitHub repository (Prefect)
- Source: FastMCP installation (official documentation)
- Source: FastMCP quickstart (official documentation)
- Source: running a server and transports (official documentation)
- Source: fastmcp install for Claude, Cursor and other clients
- Source: FastMCP changelog
- Source: building an MCP server (protocol documentation)
Feedback, an error, or a clarification? Let us know—it improves the guide for everyone.