# vercel/mcp-handler architecture

> Architecture diagram of the GitHub repository vercel/mcp-handler, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vercel/mcp-handler
- Repository: https://github.com/vercel/mcp-handler

## Overview

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: [`src/handler/mcp-api-handler.ts`](https://github.com/vercel/mcp-handler/blob/main/src/handler/mcp-api-handler.ts) (MCP Handler), [`src/auth/auth-wrapper.ts`](https://github.com/vercel/mcp-handler/blob/main/src/auth/auth-wrapper.ts) (Auth Wrapper), [`src/index.ts`](https://github.com/vercel/mcp-handler/blob/main/src/index.ts) (Entrypoint), [`src/handler/server-response-adapter.ts`](https://github.com/vercel/mcp-handler/blob/main/src/handler/server-response-adapter.ts) (Response Adapter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public_api["Public API"]
  node_src_index["Entrypoint<br/>exports<br/>[index.ts]"]
  node_handler_index["Handler API<br/>factory<br/>[index.ts]"]
  node_cli_index["CLI<br/>tooling entrypoint<br/>[index.ts]"]
end

subgraph group_handler_core["Handler Core"]
  node_mcp_api_handler["MCP Handler<br/>pipeline<br/>[mcp-api-handler.ts]"]
  node_response_adapter["Response Adapter<br/>boundary"]
end

subgraph group_auth["Auth"]
  node_auth_wrapper["Auth Wrapper<br/>middleware<br/>[auth-wrapper.ts]"]
  node_auth_context["Auth Context<br/>[auth-context.ts]"]
  node_auth_metadata["Auth Metadata<br/>[auth-metadata.ts]"]
end

subgraph group_shared["Shared Utils"]
  node_event_emitter(("Event Emitter<br/>streaming util<br/>[event-emitter.ts]"))
  node_log_helper["Log Helper<br/>observability util<br/>[log-helper.ts]"]
  node_url_util["URL Helper<br/>routing util<br/>[url.ts]"]
end

subgraph group_examples_tests["Examples & Tests"]
  node_example_route["Route Example<br/>usage example<br/>[route.ts]"]
  node_example_auth_route["Auth Example<br/>usage example<br/>[route.ts]"]
  node_mcp_test["MCP Tests<br/>test suite<br/>[mcp.test.ts]"]
  node_auth_test["Auth Tests<br/>test suite<br/>[auth.test.ts]"]
  node_e2e_test["E2E Tests<br/>test suite<br/>[e2e.test.ts]"]
end

node_consumer_route["Framework Route<br/>consumer app"]
node_framework_runtime["Web Runtime"]
node_mcp_sdk["MCP SDK<br/>external dependency"]
node_redis[("Redis<br/>optional dependency")]

node_consumer_route -->|"imports handler"| node_src_index
node_src_index -->|"re-exports"| node_handler_index
node_handler_index -->|"creates"| node_mcp_api_handler
node_mcp_api_handler -->|"adapts response"| node_response_adapter
node_mcp_api_handler -->|"wraps auth"| node_auth_wrapper
node_auth_wrapper -->|"propagates"| node_auth_context
node_auth_wrapper -->|"emits metadata"| node_auth_metadata
node_mcp_api_handler -->|"streams events"| node_event_emitter
node_mcp_api_handler -->|"logs"| node_log_helper
node_mcp_api_handler -->|"matches routes"| node_url_util
node_mcp_api_handler -->|"invokes server"| node_mcp_sdk
node_mcp_sdk -.->|"protocol callbacks"| node_mcp_api_handler
node_mcp_api_handler -.->|"optional resume store"| node_redis
node_example_route -->|"demonstrates use"| node_src_index
node_example_auth_route -->|"demonstrates use"| node_src_index
node_mcp_test -->|"verifies"| node_mcp_api_handler
node_auth_test -->|"verifies"| node_auth_wrapper
node_e2e_test -->|"verifies"| node_response_adapter
node_e2e_test -.->|"runs in"| node_framework_runtime
node_consumer_route -->|"executes in"| node_framework_runtime
node_response_adapter -->|"targets"| node_framework_runtime
node_cli_index -.->|"secondary entrypoint"| node_mcp_api_handler

click node_src_index "https://github.com/vercel/mcp-handler/blob/main/src/index.ts"
click node_handler_index "https://github.com/vercel/mcp-handler/blob/main/src/handler/index.ts"
click node_mcp_api_handler "https://github.com/vercel/mcp-handler/blob/main/src/handler/mcp-api-handler.ts"
click node_response_adapter "https://github.com/vercel/mcp-handler/blob/main/src/handler/server-response-adapter.ts"
click node_auth_wrapper "https://github.com/vercel/mcp-handler/blob/main/src/auth/auth-wrapper.ts"
click node_auth_context "https://github.com/vercel/mcp-handler/blob/main/src/auth/auth-context.ts"
click node_auth_metadata "https://github.com/vercel/mcp-handler/blob/main/src/auth/auth-metadata.ts"
click node_event_emitter "https://github.com/vercel/mcp-handler/blob/main/src/lib/event-emitter.ts"
click node_log_helper "https://github.com/vercel/mcp-handler/blob/main/src/lib/log-helper.ts"
click node_url_util "https://github.com/vercel/mcp-handler/blob/main/src/lib/url.ts"
click node_cli_index "https://github.com/vercel/mcp-handler/blob/main/src/cli/index.ts"
click node_example_route "https://github.com/vercel/mcp-handler/blob/main/examples/route.ts"
click node_example_auth_route "https://github.com/vercel/mcp-handler/blob/main/examples/auth/route.ts"
click node_mcp_test "https://github.com/vercel/mcp-handler/blob/main/tests/mcp.test.ts"
click node_auth_test "https://github.com/vercel/mcp-handler/blob/main/tests/auth.test.ts"
click node_e2e_test "https://github.com/vercel/mcp-handler/blob/main/tests/e2e.test.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_index,node_handler_index,node_cli_index toneBlue
class node_mcp_api_handler,node_response_adapter toneAmber
class node_auth_wrapper,node_auth_context,node_auth_metadata toneMint
class node_event_emitter,node_log_helper,node_url_util toneRose
class node_example_route,node_example_auth_route,node_mcp_test,node_auth_test,node_e2e_test toneIndigo
class node_consumer_route,node_framework_runtime,node_mcp_sdk,node_redis toneNeutral
```

## Components

### Public API

Library entrypoints and consumer-facing handler creation

- [Entrypoint](https://github.com/vercel/mcp-handler/blob/main/src/index.ts): exports · Primary library surface that re-exports the handler factory · `src/index.ts`
- [Handler API](https://github.com/vercel/mcp-handler/blob/main/src/handler/index.ts): factory · Handler-facing exports for consumers and route files · `src/handler/index.ts`
- [CLI](https://github.com/vercel/mcp-handler/blob/main/src/cli/index.ts): tooling entrypoint · Secondary command-line entrypoint for library tooling · `src/cli/index.ts`

### Handler Core

MCP request processing and response adaptation

- [MCP Handler](https://github.com/vercel/mcp-handler/blob/main/src/handler/mcp-api-handler.ts): pipeline · Core HTTP-to-MCP request pipeline and transport coordination · `src/handler/mcp-api-handler.ts`
- [Response Adapter](https://github.com/vercel/mcp-handler/blob/main/src/handler/server-response-adapter.ts): boundary · Builds framework/runtime responses from protocol outcomes · `src/handler/server-response-adapter.ts`

### Auth

Authorization context, metadata, and wrapping

- [Auth Wrapper](https://github.com/vercel/mcp-handler/blob/main/src/auth/auth-wrapper.ts): middleware · Wraps handler execution with auth-aware request handling · `src/auth/auth-wrapper.ts`
- [Auth Context](https://github.com/vercel/mcp-handler/blob/main/src/auth/auth-context.ts): context · Carries authenticated request state through the pipeline · `src/auth/auth-context.ts`
- [Auth Metadata](https://github.com/vercel/mcp-handler/blob/main/src/auth/auth-metadata.ts): 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](https://github.com/vercel/mcp-handler/blob/main/src/lib/event-emitter.ts): streaming util · Coordinates event delivery for streaming transports · `src/lib/event-emitter.ts`
- [Log Helper](https://github.com/vercel/mcp-handler/blob/main/src/lib/log-helper.ts): observability util · Optional verbose logging for handler activity · `src/lib/log-helper.ts`
- [URL Helper](https://github.com/vercel/mcp-handler/blob/main/src/lib/url.ts): routing util · Base-path and route matching helpers · `src/lib/url.ts`

### Examples & Tests

Usage samples and architectural verification

- [Route Example](https://github.com/vercel/mcp-handler/blob/main/examples/route.ts): usage example · Minimal route handler integration example · `examples/route.ts`
- [Auth Example](https://github.com/vercel/mcp-handler/blob/main/examples/auth/route.ts): usage example · Authenticated route-handler integration example · `examples/auth/route.ts`
- [MCP Tests](https://github.com/vercel/mcp-handler/blob/main/tests/mcp.test.ts): test suite · Protocol behavior coverage · `tests/mcp.test.ts`
- [Auth Tests](https://github.com/vercel/mcp-handler/blob/main/tests/auth.test.ts): test suite · Authorization behavior coverage · `tests/auth.test.ts`
- [E2E Tests](https://github.com/vercel/mcp-handler/blob/main/tests/e2e.test.ts): test suite · End-to-end transport coverage · `tests/e2e.test.ts`

### Other components

- **Framework Route**: consumer app · Next.js or Nuxt route handler that exports GET and POST
- **Web Runtime**: runtime · Next.js, Nuxt, and Vercel-style request/response environment
- **MCP SDK**: external dependency · Model Context Protocol server and transport primitives
- **Redis**: optional dependency · Optional SSE resumability backing store

## Connections

- 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
