or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

authentication.mdclient.mdcontext.mdindex.mdprompts.mdresources.mdserver.mdtools.mdtransports.mdutilities.md
tile.json

tessl/pypi-fastmcp

The fast, Pythonic way to build MCP servers and clients with minimal boilerplate code.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/fastmcp@2.12.x

To install, run

npx @tessl/cli install tessl/pypi-fastmcp@2.12.0

index.mddocs/

FastMCP

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

Package Information

  • Package Name: fastmcp
  • Language: Python
  • Installation: pip install fastmcp
  • Python Requirements: >=3.10

Core Imports

from fastmcp import FastMCP, Context, Client

Basic Usage

Creating a Simple MCP Server

from 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 transport

Creating a Client

from 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)

Architecture

FastMCP is built around several core components that work together to provide a complete MCP framework:

  • FastMCP Server: The main server class that manages tools, resources, prompts, and handles MCP protocol communication
  • Context: Execution context providing capabilities like logging, LLM sampling, and HTTP requests to tools/resources/prompts
  • Client: Full-featured client supporting multiple transports and authentication methods
  • Component System: Modular architecture for tools, resources, and prompts with both decorator and class-based approaches
  • Transport Layer: Multiple transport protocols (stdio, HTTP, SSE, WebSocket) for flexible deployment
  • Authentication: Comprehensive auth system supporting OAuth, JWT, Bearer tokens, and custom providers
  • Middleware: Extensible middleware system for request/response processing

Capabilities

Server Implementation

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

Server Implementation

Client Library

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): ...

Client Library

Tools System

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

Tools System

Resources System

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

Resources System

Prompts System

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
): ...

Prompts System

Context and Dependencies

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

Context and Dependencies

Transport Layer

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): ...

Transport Layer

Authentication

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

Authentication

Utilities and Types

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

Utilities and Types

Types

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