MCP 2025-11-25 Without Infrastructure Sprawl: OAuth 2.1 PKCE, Session Replay, and SQLite FTS5 RRF in Pure Stdlib Python

When teams integrate Model Context Protocol (MCP) tools into production workflows, they frequently pull in Redis for session replay, Qdrant or Pinecone for vector search, and heavy web frameworks for OAuth 2.1 token validation. Each external service adds operational failure modes, container bloat, and key-rotation surface area.

In T0, the full protocol stack runs on Python 3.12 standard library modules and embedded SQLite WAL (FTS5 + cosine similarity): 42 offline pytest tests and a 200-query golden benchmark finish in under 5 seconds with zero external dependencies.

1. Audience-Bound OAuth 2.1 Discovery and PKCE S256

Unscoped bearer tokens invite confused-deputy replay across tool servers. Implementing RFC 9728 Protected Resource Metadata (/.well-known/oauth-protected-resource) pairs cleanly with RFC 8707 resource indicators and RFC 7636 PKCE S256 verification:

2. Streamable HTTP Replay via Mcp-Session-Id & Last-Event-ID

Network drops during Server-Sent Events (SSE) streams break long-running tool calls unless events are indexed in a bounded ring buffer. Tagging each outgoing SSE frame with a reproducible event ID and scoping replay rings by client-bound CRC32C Mcp-Session-Id guarantees two properties:

  1. Reconnecting clients recover missed frames cleanly via Last-Event-ID.
  2. Evicted event cursors trigger explicit HTTP 409 Conflict signals rather than silent data truncation, while cross-session probes fail immediately with HTTP 404.

3. Hybrid Search (FTS5 BM25 + Cosine RRF, k=60) Before LLM Escalation

Lexical search alone misses paraphrases; dense similarity alone misses exact symbols such as error codes (0xE3069283) or flag names (--allow-write). Merging Okapi BM25 rankings with dense cosine rankings via Reciprocal Rank Fusion (1 / (60 + rank)) achieves 1.0000 Recall@1 on the 100-query architecture corpus in 0.27 ms p95 on commodity hardware.

Try the routing and RRF scoring formulas directly in the interactive browser workbench: https://nahianakhtar.github.io/demos/moe-rag/.