typescript-sdk/packages/middleware/express
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Ramesh Reddy Adutla 1fe9eda4a7
Add jsonLimit option to createMcpExpressApp (#1625)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Konstantin Konstantinov <KKonstantinov@users.noreply.github.com>
2026-03-05 08:57:40 +02:00
..
src Add jsonLimit option to createMcpExpressApp (#1625) 2026-03-05 08:57:40 +02:00
test Add jsonLimit option to createMcpExpressApp (#1625) 2026-03-05 08:57:40 +02:00
eslint.config.mjs [v2] Decouple server from express and hono - http framework-agnostic MCP server (#1326) 2026-01-16 12:13:25 +02:00
package.json remove references to npm, clean up package manager usage in workspaces (#1534) 2026-03-02 12:13:44 +00:00
README.md fix: resolve TypeDoc warnings from directory-pointing relative links (#1546) 2026-02-17 23:25:54 +00:00
tsconfig.json feat: auto-select JSON Schema validator based on runtime (#1456) 2026-02-03 19:22:39 +00:00
tsdown.config.ts [v2] Decouple server from express and hono - http framework-agnostic MCP server (#1326) 2026-01-16 12:13:25 +02: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)

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()

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]']));