Add two numbers
FastMCP is a powerful Python framework for building Model Context Protocol (MCP) servers and clients. It provides an intuitive, Pythonic interface for creating tools, exposing resources, defining prompts, and connecting components with clean code. FastMCP 2.0 significantly expands on the original version with advanced client capabilities, server proxying, composition features, and OpenAPI/FastAPI integration. The framework makes it simple to build MCP servers that expose data and functionality to LLM applications in a standardized way. With FastMCP, developers can quickly create tools (similar to API endpoints), resources (for loading information into context), prompts (reusable templates), and more - all with minimal boilerplate code and maximum flexibility.
FastMCP is a Python framework for building Model Context Protocol (MCP) servers and clients. This guide will help you get up and running quickly.
Install FastMCP using pip:
pip install fastmcp
For development installations:
pip install -e ".[dev]"
Here's a simple example of creating an MCP server with FastMCP:
# server.py
from fastmcp import FastMCP
mcp = FastMCP("Demo Server")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
if __name__ == "__main__":
mcp.run()
You can run your server using the FastMCP CLI:
fastmcp run server.py
Or directly from Python:
python server.py
By default, the server runs on http://localhost:8000
.
Tools are functions that your LLM can call to perform actions:
@mcp.tool()
def search_database(query: str) -> list[dict]:
"""Search the database for information"""
# Implementation here
return results
Resources provide data to the LLM:
@mcp.resource()
def user_profile() -> dict:
"""Get the current user's profile information"""
return {"name": "Alice", "role": "Admin"}
Prompts are reusable templates:
mcp.prompt(
name="greeting",
content="Hello, {{name}}! Welcome to {{service_name}}.",
parameters={"name": str, "service_name": str}
)
Context provides background information:
mcp.context(
name="company_info",
content="Acme Corp was founded in 2020 and specializes in AI solutions."
)
FastMCP can proxy requests to other MCP servers:
from fastmcp import FastMCP, MCPClient
mcp = FastMCP("Proxy Server")
other_server = MCPClient("http://localhost:8001")
mcp.proxy(other_server)
Combine multiple MCP servers:
from fastmcp import FastMCP
tools_mcp = FastMCP("Tools")
resources_mcp = FastMCP("Resources")
# Add tools and resources to respective servers
main_mcp = FastMCP("Main Server")
main_mcp.include(tools_mcp)
main_mcp.include(resources_mcp)
Generate FastAPI applications from your MCP server:
from fastmcp import FastMCP
mcp = FastMCP("API Server")
# Add tools, resources, etc.
# Generate a FastAPI app
app = mcp.fastapi_app()
FastMCP includes a client for interacting with MCP servers:
from fastmcp import MCPClient
client = MCPClient("http://localhost:8000")
# Call a tool
result = await client.tools.add(a=5, b=3)
print(result) # 8
# Get a resource
profile = await client.resources.user_profile()
FastMCP supports authentication for secure MCP servers:
from fastmcp import FastMCP
from fastmcp.auth import APIKeyAuth
mcp = FastMCP(
"Secure Server",
auth=APIKeyAuth(api_key="your-secret-key")
)
For more detailed documentation, visit gofastmcp.com.