Intermediate 12 minMCP

FastMCP: build your first MCP server in Python

BestLLMfor — FastMCP — Python MCP server
Direct response

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.

By Mohamed Meguedmi·Update 2026-12-04·Tested on Windows, macOS, and Linux

#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.

FastMCP at a glance, verified on November 18, 2026 against the official documentation and repository
PointValueSource
Latest version4.2.1, released November 17, 2026gofastmcp.com changelog
Major branchFastMCP 4, released August 31, 2026 (4.0.0)gofastmcp.com changelog
Python required3.11 or later for 4.2; Python 3.10 stays on 4.1.x"What's New in FastMCP 4" page
LicenseApache 2.0Repository LICENSE file
Installationuv add fastmcp ou pip install fastmcp"Installation" page
Default transportSTDIO (standard input and output)"Running Your Server" page
TypeScript equivalentFastMCP for TypeScript, same teamRepository README

#FastMCP, FastAPI or the official MCP SDK: what's the difference?

The complete kit for building your own AI copilot.

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.

Which tool for which need
NeedRight toolWhy
Give Python tools to an AI assistantFastMCPA single decorator is enough; schema and validation are generated
Expose a conventional web API to applicationsFastAPINot an MCP server
Prototype an MCP server from an existing FastAPI APIFastMCP.from_fastapiFast, but should be replaced with hand-picked tools
Control every protocol messageOfficial 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.

  1. 01
    Install uv
    On 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).
  2. 02
    Create a Python project
    Run 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.
  3. 03
    Add FastMCP to the project
    Run uv add fastmcp in the project folder. Without uv, the pip install fastmcp command installs the same package into the active Python environment.
  4. 04
    Verify the installation
    Run 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).
  5. 05
    Pin the version
    Keep 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.
macOS and Linux (Terminal): install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
Windows (PowerShell): install uv
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
macOS, Linux, and Windows: create the project and add FastMCP
uv init hello-world
cd hello-world
uv add fastmcp
uv run fastmcp version

#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.

server.py (all systems)
from fastmcp import FastMCP

mcp = FastMCP("My MCP Server")

@mcp.tool
def greet(name: str) -> str:
    return f"Hello, {name}!"

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

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.

server.py with docstring (all systems)
import random
from fastmcp import FastMCP

mcp = FastMCP(name="Dice Roller")

@mcp.tool
def roll_dice(n_dice: int) -> list[int]:
    """Roll `n_dice` 6-sided dice and return the results."""
    return [random.randint(1, 6) for _ in range(n_dice)]

if __name__ == "__main__":
    mcp.run()
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.

macOS, Linux, and Windows: run and test the server
fastmcp run server.py
fastmcp run server.py --reload
fastmcp dev inspector server.py
fastmcp list server.py
fastmcp call server.py greet name=World
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.

server.py over HTTP (all systems)
if __name__ == "__main__":
    # Start an HTTP server on port 8000
    mcp.run(transport="http", host="127.0.0.1", port=8000)

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.

client.py (all systems)
import asyncio
from fastmcp import Client

client = Client("http://localhost:8000/mcp")

async def call_tool(name: str):
    async with client:
        result = await client.call_tool("greet", {"name": name})
        print(result)

asyncio.run(call_tool("Ford"))
Choosing a transport for a FastMCP server
SituationTransportKey consideration
Personal tool in Claude Desktop, Cursor, or Claude CodeSTDIONothing may write to standard output
Multiple workstations or multiple agents on the same serverHTTP (Streamable HTTP)Authentication and Host and Origin protection
Server exposed on the internetHTTP behind a proxyAuthentication strongly recommended by the documentation
Older client that only speaks SSESSELegacy transport, avoid for new projects
!
DNS rebinding protection is disabled by default
According to FastMCP's HTTP Deployment page, the default setting host_origin_protection=False validates neither the Host header nor the Origin header, and therefore offers no protection against DNS rebinding, an attack in which a malicious web page targets a server listening on localhost. For an HTTP server bound to localhost, the documentation suggests host_origin_protection="auto".

#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.

macOS, Linux and Windows: registering the server with a client
fastmcp install claude-desktop server.py
fastmcp install claude-code server.py
fastmcp install cursor server.py --with-editable .
fastmcp install mcp-json server.py

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.

i
What leaves your machine
With STDIO, the FastMCP server runs on your machine and doesn't contact any service on its own. However, each tool's output is passed to the client's model: with Claude Desktop or Cursor, that output goes to the model provider; with a client connected to a local model, it stays on the machine.

#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, cause and fix for a FastMCP server
SymptomDocumented causeCorrection
The client shows protocol errors or disconnectsA print() writes to stdout, which corrupts JSON-RPC messages over STDIORemove print calls; log to stderr or through the client's logs
Tool missing from fastmcp list or MCP InspectorThe CLI doesn't run the if __name__ == "__main__" blockDeclare tools at module level or in a factory function
ModuleNotFoundError: No module named 'mcp.server.fastmcp'The mcp package has moved to version 2Import from fastmcp import FastMCP, or pin mcp<2 in the meantime
A server breaks after a minor updatePossible breaking changes between 4.1.x and 4.2.0Lockfile or exact version
The server won't start in Claude Desktopuv introuvable ou dépendance non déclaréeInstall 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.

→
A few well-named tools
Start with a few clearly named tools, each with a docstring that says when to use it, what it expects, and what it returns, then check against real requests that the model picks the right tool before adding more.
Frequently asked questions
What is the definition of the Model Context Protocol (MCP)?+
The Model Context Protocol (MCP) is an open standard that connects AI applications such as Claude or ChatGPT to external systems: data sources (local files, databases), tools (search engines, calculators), and workflows (specialized prompts). An MCP server exposes these capabilities, and an MCP client makes them available to the model. FastMCP lets you write either one in Python.
Is FastMCP free?+
Yes. FastMCP is open-source software under the Apache 2.0 license, so you can use it for free, including in commercial projects, as long as you comply with that license's terms. Prefect's FastMCP Gateway hosting service, advertised as free for personal projects, isn't required for a local server.
Which Python version does FastMCP 4.2 require?+
FastMCP 4.2 requires Python 3.11 or later, according to the FastMCP 4 What's New page. Projects that need to stay on Python 3.10 still receive the 4.1.x branch.
Can you turn an existing FastAPI API into an MCP server with FastMCP?+
Yes. FastMCP.from_fastapi converts a FastAPI application's routes into MCP tools, and http_app lets you mount an MCP server inside that application. However, the FastMCP documentation recommends this conversion for getting started or prototyping, not for replicating an entire API, because models perform better with tools designed for them.
Does a FastMCP server work with Ollama or another local LLM?+
A FastMCP server works with any model, because it's the MCP client that connects the model to the tools. With Ollama, you need an MCP client connected to Ollama, such as a Python bridge or a compatible agent, and a model that handles tool calling well. The server itself doesn't change.

#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.

Did this guide help you?

Feedback, an error, or a clarification? Let us know—it improves the guide for everyone.