FastMCP Project Structure

Overview

FastMCP is Prefect's Python framework for the Model Context Protocol (MCP), designed to provide a standardized interface for LLM-tool interactions. This analysis examines the project structure and identifies key architectural components.

Project Tree Structure

fastmcp/
β”œβ”€β”€ README.md                       # Project documentation and quick start
β”œβ”€β”€ pyproject.toml                  # Modern Python project configuration
β”œβ”€β”€ LICENSE                         # Apache License 2.0
β”œβ”€β”€ .gitignore                      # Git ignore patterns
β”‚
β”œβ”€β”€ fastmcp/                        # Core framework package
β”‚   β”œβ”€β”€ __init__.py                 # Package initialization and exports
β”‚   β”œβ”€β”€ server.py                   # FastMCP server implementation (Decorator, Builder patterns)
β”‚   β”œβ”€β”€ client.py                   # MCP client implementations
β”‚   β”œβ”€β”€ types.py                    # MCP protocol type definitions
β”‚   β”œβ”€β”€ context.py                  # Context management (Strategy pattern)
β”‚   β”‚
β”‚   β”œβ”€β”€ transports/                 # Communication layer (Adapter pattern)
β”‚   β”‚   β”œβ”€β”€ __init__.py             # Transport interface definitions
β”‚   β”‚   β”œβ”€β”€ stdio.py                # STDIO transport adapter
β”‚   β”‚   β”œβ”€β”€ http.py                 # HTTP transport adapter
β”‚   β”‚   └── sse.py                  # Server-Sent Events transport
β”‚   β”‚
β”‚   β”œβ”€β”€ tools/                      # Tool execution framework
β”‚   β”‚   β”œβ”€β”€ __init__.py             # Tool registry and management
β”‚   β”‚   β”œβ”€β”€ registry.py             # Tool registration system (Registry pattern)
β”‚   β”‚   └── decorators.py           # @mcp.tool decorator implementation
β”‚   β”‚
β”‚   β”œβ”€β”€ resources/                  # Resource management system
β”‚   β”‚   β”œβ”€β”€ __init__.py             # Resource interface definitions
β”‚   β”‚   β”œβ”€β”€ base.py                 # Base resource classes
β”‚   β”‚   └── providers.py            # Resource provider implementations
β”‚   β”‚
β”‚   β”œβ”€β”€ prompts/                    # Prompt template management
β”‚   β”‚   β”œβ”€β”€ __init__.py             # Prompt system initialization
β”‚   β”‚   β”œβ”€β”€ templates.py            # Template management (Template Method pattern)
β”‚   β”‚   └── registry.py             # Prompt registry system
β”‚   β”‚
β”‚   β”œβ”€β”€ proxy/                      # Proxy server capabilities (Proxy pattern)
β”‚   β”‚   β”œβ”€β”€ __init__.py             # Proxy system exports
β”‚   β”‚   β”œβ”€β”€ server.py               # Proxy server implementation
β”‚   β”‚   └── routing.py              # Request routing logic
β”‚   β”‚
β”‚   β”œβ”€β”€ monitoring/                 # Observability system (Observer pattern)
β”‚   β”‚   β”œβ”€β”€ __init__.py             # Monitoring interface
β”‚   β”‚   β”œβ”€β”€ observers.py            # Observer implementations
β”‚   β”‚   β”œβ”€β”€ metrics.py              # Metrics collection
β”‚   β”‚   └── logging.py              # Structured logging
β”‚   β”‚
β”‚   └── utils/                      # Utility functions and helpers
β”‚       β”œβ”€β”€ __init__.py             # Utility exports
β”‚       β”œβ”€β”€ schema.py               # Schema generation utilities
β”‚       β”œβ”€β”€ validation.py           # Type validation helpers
β”‚       └── auth.py                 # Authentication utilities
β”‚
β”œβ”€β”€ examples/                       # Usage examples and demos
β”‚   β”œβ”€β”€ basic_server.py             # Simple MCP server example
β”‚   β”œβ”€β”€ multi_transport.py          # Multi-transport server demo
β”‚   β”œβ”€β”€ proxy_composition.py        # Server composition example
β”‚   β”œβ”€β”€ context_management.py       # Context strategy examples
β”‚   └── production_ready.py         # Production deployment example
β”‚
β”œβ”€β”€ docs/                          # Documentation
β”‚   β”œβ”€β”€ index.md                   # Documentation home
β”‚   β”œβ”€β”€ quickstart.md              # Getting started guide
β”‚   β”œβ”€β”€ patterns.md                # Design patterns documentation
β”‚   β”œβ”€β”€ transports.md              # Transport protocol guide
β”‚   β”œβ”€β”€ deployment.md              # Production deployment guide
β”‚   └── api/                       # API reference documentation
β”‚       β”œβ”€β”€ server.md              # Server API reference
β”‚       β”œβ”€β”€ client.md              # Client API reference
β”‚       └── transports.md          # Transport API reference
β”‚
β”œβ”€β”€ tests/                         # Test suites
β”‚   β”œβ”€β”€ __init__.py               # Test package initialization
β”‚   β”œβ”€β”€ conftest.py               # Pytest configuration and fixtures
β”‚   β”‚
β”‚   β”œβ”€β”€ unit/                     # Unit tests
β”‚   β”‚   β”œβ”€β”€ test_server.py        # Server implementation tests
β”‚   β”‚   β”œβ”€β”€ test_decorators.py    # Decorator pattern tests
β”‚   β”‚   β”œβ”€β”€ test_transports.py    # Transport adapter tests
β”‚   β”‚   └── test_patterns.py      # Design pattern integration tests
β”‚   β”‚
β”‚   β”œβ”€β”€ integration/              # Integration tests
β”‚   β”‚   β”œβ”€β”€ test_workflows.py     # End-to-end workflow tests
β”‚   β”‚   β”œβ”€β”€ test_composition.py   # Server composition tests
β”‚   β”‚   └── test_monitoring.py    # Observability integration tests
β”‚   β”‚
β”‚   └── performance/              # Performance tests
β”‚       β”œβ”€β”€ test_throughput.py    # Throughput benchmarks
β”‚       β”œβ”€β”€ test_latency.py       # Latency measurements
β”‚       └── test_scaling.py       # Scaling behavior tests
β”‚
└── scripts/                      # Development and deployment scripts
    β”œβ”€β”€ setup_dev.py              # Development environment setup
    β”œβ”€β”€ build_docs.py             # Documentation generation
    β”œβ”€β”€ run_benchmarks.py         # Performance benchmarking
    └── deploy.py                 # Deployment automation

Key Architectural Components

Core Framework (fastmcp/)

Server Implementation (server.py)

  • Design Patterns: Decorator, Builder, Template Method

  • Responsibility: Main FastMCP server class with tool/resource registration

  • Key Features: Pythonic API, automatic schema generation, multi-transport support

Transport Layer (transports/)

  • Design Patterns: Adapter, Strategy

  • Responsibility: Protocol abstraction for STDIO, HTTP, SSE communication

  • Key Features: Transport-agnostic server development, pluggable protocols

Tool System (tools/)

  • Design Patterns: Decorator, Registry, Command

  • Responsibility: Tool registration, execution, and management

  • Key Features: @mcp.tool decorator, automatic parameter validation

Proxy System (proxy/)

  • Design Patterns: Proxy, Composite, Chain of Responsibility

  • Responsibility: Server composition, request routing, federation

  • Key Features: Multi-server orchestration, load balancing, authentication

Monitoring System (monitoring/)

  • Design Patterns: Observer, Strategy

  • Responsibility: Observability, metrics collection, event tracking

  • Key Features: Real-time monitoring, performance analytics, debugging support

Pattern Implementation Mapping

Component
Primary Patterns
Pattern Application
Enterprise Value

server.py

Decorator, Builder

Tool registration, server configuration

Developer productivity, type safety

transports/

Adapter, Strategy

Protocol abstraction, pluggable communication

Deployment flexibility, infrastructure independence

tools/

Decorator, Registry

Function-to-tool transformation

Minimal boilerplate, automatic validation

proxy/

Proxy, Composite

Server composition, request federation

Horizontal scaling, complex workflows

context.py

Strategy, State

Session management, context strategies

Stateful AI interactions, persistence flexibility

monitoring/

Observer, Visitor

Event-driven monitoring, metrics collection

Production observability, performance optimization

LLM-Specific Architectural Innovations

AI-Aware Decorators

  • Location: tools/decorators.py

  • Innovation: Automatic schema generation optimized for LLM consumption

  • Benefit: Functions become AI tools with zero configuration

Context-Sensitive Proxying

  • Location: proxy/routing.py

  • Innovation: AI conversation state-aware request routing

  • Benefit: Intelligent tool selection based on interaction context

Semantic Transport Abstraction

  • Location: transports/__init__.py

  • Innovation: Protocol abstraction maintaining semantic consistency

  • Benefit: LLMs interact identically regardless of communication method

Production-Ready Features

Monitoring and Observability

  • Component: monitoring/ directory

  • Features: Real-time metrics, performance tracking, error monitoring

  • Patterns: Observer for event-driven monitoring, Strategy for different metric backends

Authentication and Security

  • Component: utils/auth.py, proxy/server.py

  • Features: Pluggable authentication, request validation, secure proxying

  • Patterns: Strategy for auth methods, Decorator for security enforcement

Configuration Management

  • Component: server.py, context.py

  • Features: Environment-based configuration, runtime customization

  • Patterns: Builder for configuration construction, Strategy for environment-specific settings

Enterprise Deployment Patterns

Single-Server Deployment

FastMCP Server
β”œβ”€β”€ Tools (via @mcp.tool decorators)
β”œβ”€β”€ Resources (data sources)
β”œβ”€β”€ STDIO Transport (direct LLM integration)
└── Monitoring (Observer pattern)

Federated Deployment

FastMCP Proxy Server
β”œβ”€β”€ Authentication Layer
β”œβ”€β”€ Load Balancer
β”œβ”€β”€ Server Federation
β”‚   β”œβ”€β”€ Server A (specialized tools)
β”‚   β”œβ”€β”€ Server B (data resources)
β”‚   └── Server C (computation services)
└── Centralized Monitoring

Multi-Transport Architecture

FastMCP Server
β”œβ”€β”€ Core Logic
β”œβ”€β”€ Transport Layer (Adapter pattern)
β”‚   β”œβ”€β”€ STDIO (development)
β”‚   β”œβ”€β”€ HTTP (web integration)
β”‚   └── SSE (real-time updates)
└── Context Management (Strategy pattern)

Development and Testing Architecture

Test Organization

  • Unit Tests: Pattern implementation verification

  • Integration Tests: Multi-component workflow validation

  • Performance Tests: Throughput and latency benchmarking

Documentation Strategy

  • API Reference: Generated from type hints and docstrings

  • Pattern Guide: Design pattern usage and best practices

  • Deployment Guide: Production configuration and scaling

Development Workflow

  • Pattern-First Development: Design patterns guide implementation structure

  • Type-Driven Development: Full typing support with runtime validation

  • Test-Driven Integration: Pattern behavior verification through comprehensive testing

This structure demonstrates FastMCP's sophisticated application of design patterns to create a production-ready, developer-friendly framework for AI tool integration in the Model Context Protocol ecosystem.

Last updated