"I built FastAPI-MCP, a library that automatically converts FastAPI REST APIs into Model Context Protocol tools. It enables AI agents to interact with existing APIs with zero configuration, preserving authentication and all FastAPI features. The project demonstrates protocol implementation, async Python, and developer tool design."
- ASGI Transport: Zero-overhead in-process communication (no HTTP calls)
- OpenAPI Converter: Custom engine preserving FastAPI features
- Dual Transport: HTTP (new) + SSE (backwards compatible)
server.py(657 lines): Main MCP server implementationopenapi/convert.py(268 lines): Schema conversion enginetransport/http.py(137 lines): HTTP transport with session managementauth/proxy.py(267 lines): OAuth 2.1 implementation
- FastAPI-Native: Direct integration vs generic converters
- OAuth Proxy Pattern: Works with Auth0/Okta without modification
- Flexible Mounting: Same app, separate app, or APIRouter
- Lazy Session Management: Async initialization with proper cleanup
- Schema Reference Resolution: Handles circular/nested
$refreferences - Parameter Type Inference: Extracts types from complex schemas
- Async Session Management: Thread-safe lazy initialization
- Request Context Preservation: Forwards headers/cookies from MCP to API
- Backwards Compatibility: Maintains SSE while adding HTTP transport
- RESTful API support (GET/POST/PUT/DELETE/PATCH)
- Microservices architecture support
- Dependency injection integration
- OpenAPI/Swagger documentation
- Works with FastAPI apps using any database
- Preserves database transactions via dependency injection
- Session management for stateful operations
- CI/CD: GitHub Actions (multi-version testing, linting, type checking)
- Packaging: PyPI distribution (hatchling build system)
- Deployment: Unified or separate deployment options
- Testing: Comprehensive test suite (9 test files)
- Documentation: Full docs site + examples
- Backend library (no frontend code)
- Enables AI-powered frontends via MCP clients
- Generates API documentation for frontend developers
- Extensive
async/awaitusage - Background task management
- Proper cleanup on shutdown
- Model Context Protocol (MCP)
- JSON-RPC (underlying messaging)
- OAuth 2.1 (authentication)
- OpenAPI 3.0 (schema parsing)
- ASGI transport = zero network overhead
- Lazy initialization
- Efficient schema resolution
"I built FastAPI-MCP, a library that automatically converts FastAPI REST APIs into Model Context Protocol tools. It enables AI agents to interact with existing APIs with zero configuration, while preserving authentication, documentation, and all FastAPI features."
"Three key architectural decisions:
- ASGI Transport for zero-overhead in-process communication
- Custom OpenAPI converter preserving FastAPI-specific features
- Dual transport support (HTTP + SSE) for compatibility
Most complex part: OpenAPI-to-MCP conversion engine handling schema references, parameter inference, and tool description generation."
"Session management in async contexts required lazy initialization with asyncio locks. OAuth integration uses proxy pattern to work with existing providers without requiring full OAuth implementation."
"While primarily backend, demonstrates fullstack thinking: API design patterns, complete CI/CD infrastructure, PyPI distribution, and deployment strategies. Integrates with database-backed FastAPI apps through dependency injection."
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
app = FastAPI()
mcp = FastApiMCP(app)
mcp.mount_http() # Done!mcp = FastApiMCP(
app,
auth_config=AuthConfig(dependencies=[Depends(authenticate)])
)- LOC: ~2,000+ core code
- Tests: 9 test files
- Version: 0.4.0 (active)
- Python: 3.10, 3.11, 3.12
- License: MIT
Q: Why build this? A: Existing tools were generic converters losing FastAPI features. Wanted native integration preserving dependency injection.
Q: Hardest challenge? A: OpenAPI schema conversion with circular references. Session management in async contexts.
Q: How does it scale? A: ASGI transport scales with FastAPI app. Supports separate deployment for microservices.
Q: What to improve? A: WebSocket support, caching layer, metrics hooks, GraphQL support.
- GitHub: github.com/tadata-org/fastapi_mcp
- PyPI: pypi.org/project/fastapi-mcp
- Docs: fastapi-mcp.tadata.com
- MCP Spec: modelcontextprotocol.io