Skip to main content

Infrastructure Layer Architecture

This document describes the infrastructure layer that separates infrastructure concerns from business logic in the MCP Server codebase.

Overview

The infrastructure layer provides reusable components for:
  • FastAPI application creation
  • Middleware configuration
  • Transport adapters
  • Lifecycle management
  • OpenAPI customization

Architecture

Components

1. App Factory (app_factory.py)

Creates configured FastAPI applications:
Features:
  • CORS middleware configuration
  • Health check endpoint
  • Lifespan management
  • OpenAPI customization
  • Container integration

2. Middleware (middleware.py)

Factory functions for middleware:

3. Transport Adapters (transport_adapters.py)

Utilities for different transport mechanisms:

Usage Patterns

Pattern 1: Test Application

Pattern 2: Development Server

Pattern 3: Production Server

Benefits

Separation of Concerns

  • ✅ Infrastructure code separate from business logic
  • ✅ Easy to test infrastructure independently
  • ✅ Reusable across different server types

Testability

  • ✅ Mock infrastructure in tests
  • ✅ No global state
  • ✅ Easy to create test apps

Flexibility

  • ✅ Swap implementations easily
  • ✅ Support multiple transport mechanisms
  • ✅ Environment-specific configuration

Maintainability

  • ✅ Single responsibility
  • ✅ Clear module boundaries
  • ✅ Easy to extend

Integration with Container Pattern

The infrastructure layer works seamlessly with the container pattern:

Testing

All infrastructure components have comprehensive tests:
Test Coverage:
  • ✅ App creation with container/settings
  • ✅ Health endpoint validation
  • ✅ CORS middleware configuration
  • ✅ Lifespan management
  • ✅ OpenAPI customization
  • ✅ Transport adapters
  • ✅ Environment-specific behavior

Future Enhancements

Short Term

  • Implement production auth middleware
  • Implement rate limiting middleware
  • Add more transport adapters

Long Term

  • Refactor server_streamable.py to use app factory
  • Refactor server_stdio.py to use infrastructure
  • Extract common server logic to base class

Examples

See tests/infrastructure/test_app_factory.py for comprehensive examples.