Skip to content

Instantly share code, notes, and snippets.

@donbr
Last active June 11, 2025 22:14
Show Gist options
  • Save donbr/f7584443f81102d342aa4b5d33ae031a to your computer and use it in GitHub Desktop.
Save donbr/f7584443f81102d342aa4b5d33ae031a to your computer and use it in GitHub Desktop.
FastMCP Migration & Comparison

Project: FastMCP Migration & Comparison

Objective
Migrate your existing FastAPI services to the Model Context Protocol (MCP) using two different implementations:

Evaluate each on performance, LangChain integration, and production readiness.


Critical Inputs

  1. Existing FastAPI Code (REPLACE WITH YOUR REAL CODE):
    from fastapi import FastAPI
    app = FastAPI()
    
    @app.post("/query")
    async def query_engine(text: str):
        # Your actual retrieval/RAG logic here
        return {"response": f"Processed {text}"}
  2. Project Constraints:
    • Scale: e.g. 5 endpoints, 100+ QPS
    • Non-negotiables: e.g. OAuth2, latency <8 ms, Kubernetes
    • LangChain Usage: e.g. LCEL chain with OpenAI + pgvector
    • Baseline Metric: e.g. Current latency: 15 ms @ 50 QPS
  3. Environment:
    • Python version (e.g. 3.11)
    • FastAPI version (e.g. 0.98)
    • OS/container details if relevant

Response Requirements

A. Code Conversion

  • Produce side-by-side FastAPI → FastMCP examples for your endpoint, including:
    • ASGI mounting & lifespan events
    • @tool definitions
    • Full runnable snippets with robust error handling (retries, circuit-breakers)

B. Architecture Analysis

Comparison Point Official MCP SDK (mcp) FastMCP 2.0 (fastmcp)
Cold start [benchmark vs. baseline] [benchmark vs. baseline]
Async throughput [req/s @ 50 concurrent] [req/s @ 50 concurrent]
License Risk Apache 2.0 (✅) MIT (✅)
PyPI Downloads (30 d) [data] [data]

Evidence Rule: Cite PyPI/GitHub stats; if unavailable, explain how to measure (e.g. locust or k6).

C. LangChain Integration

  • Show how to wrap your LangChain chain using langchain-mcp-adapters
  • Provide a compatibility matrix for both MCP implementations

D. Production Practices

# IMPLEMENT IN CODE:
# 1. OAuth2 middleware ▶ WHY: SSO compliance
# 2. OpenTelemetry hooks          ▶ WHY: distributed tracing
# 3. Async resource cleanup      ▶ WHY: prevent connection leaks
# 4. Retry & circuit-breaker     ▶ WHY: resilience under load

E. Decision & Rollback

  • Final recommendation with one-sentence justification
  • 3-step rollback plan to revert to FastAPI

Tone & Format

  • Max 1,200 words
  • Prioritize: Code > Tables > Bullet lists
  • Annotate every code snippet with ▶ WHY comments
  • Zero fluff—focus on actionable, evidence-backed guidance

Fallback Protocol

Missing benchmarks? Provide ready-to-run locust or k6 scripts to gather the required metrics.

# Example locust task
from locust import FastHttpUser, task, between

class TestUser(FastHttpUser):
    wait_time = between(1, 2)

    @task
    def query(self):
        self.client.post("/query", json={"text": "test"}, name="/query")
# Example k6 script
import http from 'k6/http';
import { sleep } from 'k6s';

export default function () {
  http.post('https://your-host/query', JSON.stringify({ text: 'test' }), { headers: { 'Content-Type': 'application/json' } });
  sleep(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment