How vercel/mcp-handler works
1. What this repo is
mcp-handler is a small TypeScript adapter library, not an application. Its job is to let a Next.js or Nuxt route handler expose a Model Context Protocol (MCP) server over HTTP transports, especially for Vercel-style deployments.
2. Primary public entrypoint
The library surface is centered in src/index.ts, which exports the handler factory used by consumers in their route files. The README examples show the intended shape: create one handler, then re-export it as GET and POST from a framework route.
3. Core request pipeline
The heart of the repo is src/handler/mcp-api-handler.ts. This is where inbound HTTP requests are interpreted as MCP transport traffic, the MCP server callback is invoked, and the protocol interaction is driven end-to-end.
4. Framework response boundary
src/handler/server-response-adapter.ts isolates framework-specific response construction. This is the boundary between MCP protocol handling and the web framework/runtime response object, which is important because the same MCP logic needs to work in both Next.js and Nuxt environments.
5. MCP server setup model
The consumer provides a function that receives an MCP server instance and registers tools/resources/prompts. The repository does not define application tools itself; it provides the infrastructure for those registrations to be exposed over the selected transport.
6. Transport and streaming support
The package supports Streamable HTTP and SSE, with the README calling out optional Redis for SSE resumability. Architecturally, this means the handler must manage both request/response style interactions and long-lived event streams, likely with transport-specific branching inside the handler layer.
7. Authentication subsystem
Auth is a distinct concern in src/auth/, split into auth-context.ts, auth-metadata.ts, and auth-wrapper.ts. This suggests the library wraps MCP handling with auth-aware metadata and request context propagation rather than mixing auth logic directly into protocol handling.
8. Shared utilities
src/lib/ contains small cross-cutting helpers: event-emitter.ts for stream/event coordination, log-helper.ts for optional verbose logging, and url.ts for path/base-path handling. These support transport negotiation, observability, and route matching.
9. CLI and packaging surface
src/cli/index.ts indicates there is also a command-line entrypoint, but it is secondary to the handler API. The build uses TypeScript with tsup, and versioning/release automation is handled via Changesets and GitHub Actions, which matters for how the library is published rather than how it runs.
10. Examples, docs, and tests as architecture documentation
examples/ shows concrete route-handler integration patterns, including an auth example. tests/auth.test.ts, tests/e2e.test.ts, and tests/mcp.test.ts indicate coverage across auth, protocol behavior, and end-to-end transport behavior, which maps directly to the main architectural seams.
11. External dependencies that shape the design
The repo depends on @modelcontextprotocol/sdk and zod, so protocol behavior and schema validation are delegated to those libraries. The intended deployment targets are Next.js 13+ and Nuxt 3+ on Node.js 18+, with optional Redis for SSE replay/resume support.
12. How to draw the architecture
A good diagram would place the consumer’s framework route on the left, then the createMcpHandler/handler pipeline in the middle, with branches for auth and transport handling, and the MCP server registration callback as the internal application logic boundary. External nodes should include the MCP SDK, optional Redis for SSE, and the framework runtime (Next.js/Nuxt/Vercel-style hosting).
Main entry points: mcp-api-handler.ts MCP Handler · auth-wrapper.ts Auth Wrapper · index.ts Entrypoint · server-response-adapter.ts Response Adapter
Components 20
Public API
Library entrypoints and consumer-facing handler creation
- Entrypoint · Primary library surface that re-exports the handler factory
src/index.ts - Handler API · Handler-facing exports for consumers and route files
src/handler/index.ts - CLI · Secondary command-line entrypoint for library tooling
src/cli/index.ts
Handler Core
MCP request processing and response adaptation
- MCP Handler · Core HTTP-to-MCP request pipeline and transport coordination
src/handler/mcp-api-handler.ts - Response Adapter · Builds framework/runtime responses from protocol outcomes
src/handler/server-response-adapter.ts
Auth
Authorization context, metadata, and wrapping
- Auth Wrapper · Wraps handler execution with auth-aware request handling
src/auth/auth-wrapper.ts - Auth Context · Carries authenticated request state through the pipeline
src/auth/auth-context.ts - Auth Metadata · Protocol metadata and auth-related response shaping
src/auth/auth-metadata.ts
Shared Utils
Cross-cutting helpers used by the handler pipeline
- Event Emitter · Coordinates event delivery for streaming transports
src/lib/event-emitter.ts - Log Helper · Optional verbose logging for handler activity
src/lib/log-helper.ts - URL Helper · Base-path and route matching helpers
src/lib/url.ts
Examples & Tests
Usage samples and architectural verification
- Route Example · Minimal route handler integration example
examples/route.ts - Auth Example · Authenticated route-handler integration example
examples/auth/route.ts - MCP Tests · Protocol behavior coverage
tests/mcp.test.ts - Auth Tests · Authorization behavior coverage
tests/auth.test.ts - E2E Tests · End-to-end transport coverage
tests/e2e.test.ts
Other components
- Framework Route · Next.js or Nuxt route handler that exports GET and POST
- Web Runtime · Next.js, Nuxt, and Vercel-style request/response environment
- MCP SDK · Model Context Protocol server and transport primitives
- Redis · Optional SSE resumability backing store
Connections 22
- Framework Route → Entrypoint: imports handler · Consumer route pulls the handler factory from the public entrypoint
- Entrypoint → Handler API: re-exports · Top-level entrypoint exposes handler APIs
- Handler API → MCP Handler: creates · Factory wires consumer configuration into the core request pipeline
- MCP Handler → Response Adapter: adapts response · Converts protocol results into framework responses
- MCP Handler → Auth Wrapper: wraps auth · Auth is applied around request handling when enabled
- Auth Wrapper → Auth Context: propagates · Authenticated request state is carried into downstream handling
- Auth Wrapper → Auth Metadata: emits metadata · Authorization state influences MCP metadata and response behavior
- MCP Handler → Event Emitter: streams events · Streaming transports use event coordination for live responses
- MCP Handler → Log Helper: logs · Optional diagnostics are emitted during handling
- MCP Handler → URL Helper: matches routes · Request paths and base paths are normalized for transport selection
- MCP Handler → MCP SDK: invokes server · Handler drives the MCP server callback and transport protocol
- MCP SDK → MCP Handler: protocol callbacks · SDK supplies server-side MCP execution primitives
- MCP Handler → Redis: optional resume store · SSE resumability can be backed by Redis
- Route Example → Entrypoint: demonstrates use · Example shows the standard consumer integration shape
- Auth Example → Entrypoint: demonstrates use · Auth example shows wrapped handler integration
- MCP Tests → MCP Handler: verifies · Tests exercise protocol and transport behavior
- Auth Tests → Auth Wrapper: verifies · Tests exercise authorization flow and metadata handling
- E2E Tests → Response Adapter: verifies · End-to-end coverage checks framework response behavior
- E2E Tests → Web Runtime: runs in · Transport and integration behavior are validated in runtime-like conditions
- Framework Route → Web Runtime: executes in · Route handlers run inside Next.js or Nuxt style deployments
- Response Adapter → Web Runtime: targets · Adapter bridges protocol results to runtime response objects
- CLI → MCP Handler: secondary entrypoint · CLI entrypoint is separate from the main library path but shares core logic