Skip to content

Latest commit

 

History

History
161 lines (117 loc) · 5.14 KB

File metadata and controls

161 lines (117 loc) · 5.14 KB

FastAPI-MCP: Quick Interview Reference

Elevator Pitch (30 seconds)

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


Technical Ownership Highlights

Core Architecture

  1. ASGI Transport: Zero-overhead in-process communication (no HTTP calls)
  2. OpenAPI Converter: Custom engine preserving FastAPI features
  3. Dual Transport: HTTP (new) + SSE (backwards compatible)

Key Files

  • server.py (657 lines): Main MCP server implementation
  • openapi/convert.py (268 lines): Schema conversion engine
  • transport/http.py (137 lines): HTTP transport with session management
  • auth/proxy.py (267 lines): OAuth 2.1 implementation

Creativity & Innovation

  1. FastAPI-Native: Direct integration vs generic converters
  2. OAuth Proxy Pattern: Works with Auth0/Okta without modification
  3. Flexible Mounting: Same app, separate app, or APIRouter
  4. Lazy Session Management: Async initialization with proper cleanup

Real-World Problems Solved

  1. Schema Reference Resolution: Handles circular/nested $ref references
  2. Parameter Type Inference: Extracts types from complex schemas
  3. Async Session Management: Thread-safe lazy initialization
  4. Request Context Preservation: Forwards headers/cookies from MCP to API
  5. Backwards Compatibility: Maintains SSE while adding HTTP transport

Fullstack Capabilities

✅ Backend/API Design

  • RESTful API support (GET/POST/PUT/DELETE/PATCH)
  • Microservices architecture support
  • Dependency injection integration
  • OpenAPI/Swagger documentation

⚠️ Database Usage (Indirect)

  • Works with FastAPI apps using any database
  • Preserves database transactions via dependency injection
  • Session management for stateful operations

✅ Infrastructure

  • 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

❌ Frontend

  • Backend library (no frontend code)
  • Enables AI-powered frontends via MCP clients
  • Generates API documentation for frontend developers

Key Technical Details

Async Patterns

  • Extensive async/await usage
  • Background task management
  • Proper cleanup on shutdown

Protocols Implemented

  • Model Context Protocol (MCP)
  • JSON-RPC (underlying messaging)
  • OAuth 2.1 (authentication)
  • OpenAPI 3.0 (schema parsing)

Performance

  • ASGI transport = zero network overhead
  • Lazy initialization
  • Efficient schema resolution

Interview Talking Points

Opening (30s)

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

Technical Depth (2-3 min)

"Three key architectural decisions:

  1. ASGI Transport for zero-overhead in-process communication
  2. Custom OpenAPI converter preserving FastAPI-specific features
  3. Dual transport support (HTTP + SSE) for compatibility

Most complex part: OpenAPI-to-MCP conversion engine handling schema references, parameter inference, and tool description generation."

Problem Solving (2 min)

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

Fullstack (1-2 min)

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


Code Examples

Basic Usage

from fastapi import FastAPI
from fastapi_mcp import FastApiMCP

app = FastAPI()
mcp = FastApiMCP(app)
mcp.mount_http()  # Done!

With Auth

mcp = FastApiMCP(
    app,
    auth_config=AuthConfig(dependencies=[Depends(authenticate)])
)

Project Stats

  • LOC: ~2,000+ core code
  • Tests: 9 test files
  • Version: 0.4.0 (active)
  • Python: 3.10, 3.11, 3.12
  • License: MIT

Common Questions

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.


Resources

  • GitHub: github.com/tadata-org/fastapi_mcp
  • PyPI: pypi.org/project/fastapi-mcp
  • Docs: fastapi-mcp.tadata.com
  • MCP Spec: modelcontextprotocol.io