The fast, Pythonic way to build MCP servers and clients with minimal boilerplate code.
npx @tessl/cli install tessl/pypi-fastmcp@2.12.0The fast, Pythonic way to build MCP servers and clients. FastMCP is a comprehensive framework for building Model Context Protocol (MCP) servers and clients with minimal boilerplate code, providing a clean, Pythonic API for creating tools, exposing resources, defining prompts, and connecting components in the MCP ecosystem.
pip install fastmcpfrom fastmcp import FastMCP, Context, Clientfrom fastmcp import FastMCP
# Create a server instance
mcp = FastMCP("Demo Server")
@mcp.tool
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
@mcp.resource("config://version")
def get_version():
"""Get server version."""
return "1.0.0"
@mcp.prompt
def summarize_request(text: str) -> str:
"""Generate a prompt asking for a summary."""
return f"Please summarize the following text:\n\n{text}"
if __name__ == "__main__":
mcp.run() # Run with stdio transportfrom fastmcp import Client
async def main():
# Connect to a server via stdio
async with Client("server.py") as client:
# List available tools
tools = await client.list_tools()
# Call a tool
result = await client.call_tool("add", {"a": 5, "b": 3})
print(result.text)
# Read a resource
resource = await client.read_resource("config://version")
print(resource.content)FastMCP is built around several core components that work together to provide a complete MCP framework:
Core server functionality for creating MCP servers with decorators, managing components, and handling protocol communication.
class FastMCP:
def __init__(
self,
name: str,
version: str = "0.1.0",
instructions: str | None = None,
auth_provider: AuthProvider | None = None,
middlewares: list[Middleware] | None = None,
settings: Settings | None = None
): ...
def tool(self, func: Callable | None = None, *, name: str | None = None) -> Callable: ...
def resource(self, uri: str) -> Callable: ...
def prompt(self, func: Callable | None = None, *, name: str | None = None) -> Callable: ...
def run(
self,
transport: Literal["stdio", "sse", "http"] = "stdio",
host: str = "127.0.0.1",
port: int = 8000,
**kwargs
) -> None: ...Full-featured MCP client supporting multiple transports, authentication, and advanced features like LLM sampling.
class Client:
def __init__(
self,
server_or_config: str | FastMCP | dict,
transport: ClientTransport | None = None,
auth: BearerAuth | OAuth | None = None,
sampling_handler: SamplingHandler | None = None,
elicitation_handler: ElicitationHandler | None = None
): ...
async def list_tools(self) -> list: ...
async def call_tool(self, name: str, arguments: dict | None = None): ...
async def list_resources(self) -> list: ...
async def read_resource(self, uri: str): ...
async def list_prompts(self) -> list: ...
async def get_prompt(self, name: str, arguments: dict | None = None): ...Tools allow LLMs to perform actions by executing Python functions, with automatic schema generation and flexible return types.
class Tool:
def __init__(
self,
name: str,
description: str,
func: Callable,
schema: dict | None = None
): ...
class FunctionTool(Tool): ...
class ToolManager:
def add_tool(self, tool: Tool) -> None: ...
def remove_tool(self, name: str) -> None: ...
def get_tool(self, name: str) -> Tool | None: ...Resources expose read-only data sources with support for static resources and dynamic templates with URI parameters.
class Resource:
def __init__(
self,
uri: str,
name: str,
description: str,
mime_type: str | None = None
): ...
class ResourceTemplate:
def __init__(
self,
uri_template: str,
name: str,
description: str,
mime_type: str | None = None
): ...
class ResourceManager:
def add_resource(self, resource: Resource) -> None: ...
def add_template(self, template: ResourceTemplate) -> None: ...Prompts define reusable message templates to guide LLM interactions with parameter support and message formatting.
class Prompt:
def __init__(
self,
name: str,
description: str,
func: Callable,
schema: dict | None = None
): ...
class PromptManager:
def add_prompt(self, prompt: Prompt) -> None: ...
def get_prompt(self, name: str) -> Prompt | None: ...
def Message(
role: Literal["user", "assistant", "system"],
content: str | dict,
name: str | None = None
): ...Execution context providing capabilities like logging, LLM sampling, HTTP requests, and resource access to tools/resources/prompts.
class Context:
async def info(self, message: str) -> None: ...
async def error(self, message: str) -> None: ...
async def debug(self, message: str) -> None: ...
async def sample(
self,
messages: list[dict],
params: dict | None = None
): ...
async def read_resource(self, uri: str): ...
async def http_request(
self,
method: str,
url: str,
headers: dict | None = None,
data: Any | None = None
): ...
async def report_progress(self, progress: int, total: int | None = None) -> None: ...Multiple transport protocols for flexible server deployment and client connections.
class ClientTransport: ...
class StdioTransport(ClientTransport): ...
class SSETransport(ClientTransport): ...
class StreamableHttpTransport(ClientTransport): ...
class WSTransport(ClientTransport): ...
class FastMCPTransport(ClientTransport): ...Comprehensive authentication system supporting multiple providers and tokens for secure server and client connections.
class AuthProvider: ...
class OAuthProvider(AuthProvider): ...
class JWTVerifier(AuthProvider): ...
class TokenVerifier(AuthProvider): ...
class StaticTokenVerifier(AuthProvider): ...
class RemoteAuthProvider(AuthProvider): ...
class BearerAuth: ...
class OAuth: ...
class AccessToken: ...Helper classes and utility functions for enhanced functionality and type support.
class Image:
def __init__(
self,
data: bytes | str,
mime_type: str = "image/png"
): ...
class Audio:
def __init__(
self,
data: bytes | str,
mime_type: str = "audio/wav"
): ...
class File:
def __init__(
self,
data: bytes | str,
name: str,
mime_type: str | None = None
): ...
class Settings: ...
class MCPConfig: ...# Core types
from typing import Callable, Any, Literal
from pydantic import BaseModel
class FastMCPBaseModel(BaseModel): ...
# Function signatures for decorators
ToolFunction = Callable[..., Any]
ResourceFunction = Callable[..., str | bytes | dict]
PromptFunction = Callable[..., str | list[dict]]
# Transport types
TransportType = Literal["stdio", "sse", "http", "ws"]
# Authentication types
SamplingHandler = Callable[[list[dict]], Any]
ElicitationHandler = Callable[[dict], Any]
# Component types
class MCPMixin: ...
ComponentFn = Callable[..., Any]