typescript-sdk/packages/middleware/express
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-08-25 14:23:40 +01:00
..
src fix(server): read HTTP request bodies with a size limit and bound JSON-RPC batch length (#2698) 2026-08-25 14:23:40 +01:00
test fix(server): read HTTP request bodies with a size limit and bound JSON-RPC batch length (#2698) 2026-08-25 14:23:40 +01:00
CHANGELOG.md Version Packages (#2555) 2026-07-28 00:52:57 +01:00
eslint.config.mjs v2: codemod iterations, canonical zod schema exports from sdk-shared (#2354) 2026-06-25 19:42:44 +01:00
package.json Version Packages (#2555) 2026-07-28 00:52:57 +01:00
README.md feat(express): restore Resource-Server auth glue (requireBearerAuth, mcpAuthMetadataRouter) (#1907) 2026-04-28 14:18:33 +00:00
tsconfig.json refactor: move the schema source modules into @modelcontextprotocol/core (#2477) 2026-07-13 17:19:58 +01:00
tsdown.config.ts feat(packaging): ship CommonJS builds alongside ESM for v2 packages (#2405) 2026-07-01 19:40:37 +03:00
typedoc.json Add API documentation with GitHub Pages deployment (#1109) 2026-01-20 17:22:24 +02:00
vitest.config.js [v2] Decouple server from express and hono - http framework-agnostic MCP server (#1326) 2026-01-16 12:13:25 +02:00

@modelcontextprotocol/express

Express adapters for the MCP TypeScript server SDK.

This package is a thin Express integration layer for @modelcontextprotocol/server.

It does not implement MCP itself. Instead, it helps you:

  • create an Express app with sensible defaults for MCP servers
  • add DNS rebinding protection via Host header validation (recommended for localhost servers)
  • protect routes with requireBearerAuth (validates Authorization: Bearer … via your OAuthTokenVerifier)
  • serve OAuth Protected Resource Metadata (RFC 9728) via mcpAuthMetadataRouter

Install

npm install @modelcontextprotocol/server @modelcontextprotocol/express express

# For MCP Streamable HTTP over Node.js (IncomingMessage/ServerResponse):
npm install @modelcontextprotocol/node

Exports

  • createMcpExpressApp(options?)
  • hostHeaderValidation(allowedHostnames)
  • localhostHostValidation()
  • requireBearerAuth(options)
  • mcpAuthMetadataRouter(options)
  • getOAuthProtectedResourceMetadataUrl(serverUrl)
  • OAuthTokenVerifier (interface)

Usage

Create an Express app (localhost DNS rebinding protection by default)

import { createMcpExpressApp } from '@modelcontextprotocol/express';

const app = createMcpExpressApp(); // default host is 127.0.0.1; protection enabled

Streamable HTTP endpoint (Express)

import { createMcpExpressApp } from '@modelcontextprotocol/express';
import { NodeStreamableHTTPServerTransport } from '@modelcontextprotocol/node';
import { McpServer } from '@modelcontextprotocol/server';

const app = createMcpExpressApp();
const server = new McpServer({ name: 'my-server', version: '1.0.0' });

app.post('/mcp', async (req, res) => {
    // Stateless example: create a transport per request.
    // For stateful mode (sessions), keep a transport instance around and reuse it.
    const transport = new NodeStreamableHTTPServerTransport({ sessionIdGenerator: undefined });
    await server.connect(transport);
    await transport.handleRequest(req, res, req.body);
});

Host header validation (DNS rebinding protection)

import { hostHeaderValidation } from '@modelcontextprotocol/express';

app.use(hostHeaderValidation(['localhost', '127.0.0.1', '[::1]']));