FastMCP — Build MCP Servers in Python, Fast
The fast, Pythonic way to build MCP servers and clients. Clean decorator API, automatic type validation, built-in testing, and OpenAPI integration. 24K+ GitHub stars.
Safe staging for this asset
This asset is staged first. The copied prompt tells the agent to inspect the staged files and ask before activating scripts, MCP config, or global config.
npx -y tokrepo@latest install a17a107c-c77e-4c3f-ae09-d2fd985eba93 --target codexStages files first; activation requires review of the staged README and plan.
What it is
FastMCP is a Python framework for building MCP (Model Context Protocol) servers quickly. It provides a decorator-based API similar to FastAPI: you define functions with type hints, decorate them with @mcp.tool(), and FastMCP handles protocol compliance, input validation, and server lifecycle. It also includes a client library for testing and connecting to other MCP servers.
FastMCP targets Python developers who want to expose their code as MCP tools for AI assistants like Claude Code, Cursor, or any MCP-compatible client.
How it saves time or tokens
Building an MCP server from scratch requires implementing the JSON-RPC protocol, handling tool listing, input validation, and error formatting. FastMCP abstracts all of this. You write a Python function, add a decorator, and get a compliant MCP server. The automatic type validation from Python type hints means fewer runtime errors. Built-in testing tools let you verify your server without connecting a real client.
How to use
- Install FastMCP:
pip install fastmcp
- Create a server:
from fastmcp import FastMCP
mcp = FastMCP('My Server')
@mcp.tool()
def add(a: int, b: int) -> int:
'''Add two numbers.'''
return a + b
mcp.run()
- Connect your MCP client to the server.
Example
from fastmcp import FastMCP
import httpx
mcp = FastMCP('Web Scraper')
@mcp.tool()
async def fetch_page(url: str) -> str:
'''Fetch a web page and return its text content.'''
async with httpx.AsyncClient() as client:
response = await client.get(url)
return response.text
@mcp.tool()
def count_words(text: str) -> int:
'''Count words in a text string.'''
return len(text.split())
@mcp.resource('config://app')
def get_config() -> dict:
'''Return application configuration.'''
return {'version': '1.0', 'max_pages': 100}
mcp.run()
This creates an MCP server with two tools and one resource, ready for any MCP client.
Related on TokRepo
- MCP Integrations -- Browse MCP server integrations
- AI Tools for Coding -- Development tools and frameworks
Common pitfalls
- FastMCP uses Python type hints for input validation. Missing or incorrect type hints produce unhelpful errors for the calling AI agent. Always annotate parameters and return types.
- Async tools require an async event loop. If your MCP client connects via stdio, FastMCP handles this. But embedding in an existing async application requires careful loop management.
- Tool descriptions become the documentation AI agents read. Write clear, specific docstrings because the agent uses them to decide when and how to call your tool.
Frequently Asked Questions
The official MCP Python SDK provides low-level protocol primitives. FastMCP adds a high-level decorator API, automatic validation, and testing utilities on top. Think of it as FastAPI vs raw ASGI -- same protocol, better developer experience.
Yes. FastMCP supports all three MCP primitives: tools (functions agents call), resources (data agents read), and prompts (templates agents use). Each has its own decorator.
Yes. FastMCP includes a built-in test client. You can call tools programmatically in unit tests without connecting Claude or Cursor.
Yes. FastMCP can import OpenAPI specifications and automatically generate MCP tools from API endpoints. This lets you expose existing REST APIs as MCP tools without writing wrapper code.
FastMCP supports stdio (standard for local MCP servers) and SSE (Server-Sent Events) for remote connections. The transport is configurable at startup.
Citations (3)
- FastMCP GitHub— Fast Pythonic MCP server framework
- MCP Specification— Model Context Protocol specification
- Anthropic MCP Docs— MCP tools, resources, and prompts
Related on TokRepo
Source & Thanks
Created by PrefectHQ. Licensed under Apache 2.0. PrefectHQ/fastmcp — 24,000+ GitHub stars
Discussion
Related Assets
mcp-use — Fullstack SDK for MCP Servers & Apps
mcp-use is a fullstack SDK to build MCP servers and MCP apps across Claude/ChatGPT, with inspector + quickstarts. Verified 9952★; pushed 2026-05-14.
awesome-mcp-servers — Directory of MCP Servers
Categorized list of Model Context Protocol (MCP) servers. Use it to discover integrations and build a curated allowlist your team can maintain.
Best of MCP Servers — 450 Ranked MCP Servers
A weekly-updated ranked directory of 450 MCP servers across 34 categories with quality scores based on GitHub stars, contributors, and activity. Find the best MCP server for any use case.
Supergateway — Bridge MCP Servers via SSE and HTTP
Run any MCP stdio server over SSE or HTTP transport. Supergateway bridges local MCP servers for remote access, enabling cloud deployment and multi-client sharing.