typescript-sdk/packages/middleware/node
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Sam Morrow 6032170087
Some checks failed
Conformance Tests / client-conformance (push) Has been cancelled
Conformance Tests / server-conformance (push) Has been cancelled
Deploy Docs / deploy-docs (push) Has been cancelled
Examples / examples (build + e2e) (push) Has been cancelled
/ build (push) Has been cancelled
/ test (20) (push) Has been cancelled
/ test (22) (push) Has been cancelled
/ test (24) (push) Has been cancelled
/ test-e2e (20) (push) Has been cancelled
/ test-e2e (22) (push) Has been cancelled
/ test-e2e (24) (push) Has been cancelled
/ test-runtimes (bun, 1.x) (push) Has been cancelled
/ test-runtimes (deno, v2.x) (push) Has been cancelled
Publish Any Commit / pkg-publish (push) Has been cancelled
Release / Version (push) Has been cancelled
Release / Publish (push) Has been cancelled
feat(server): add request-time OAuth scope challenges (#1624)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-09-16 16:51:56 +00:00
..
src feat(server): add request-time OAuth scope challenges (#1624) 2026-09-16 16:51:56 +00: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 Use protected HTTP wiring in examples and guides (#2445) 2026-07-06 21:03:15 +01: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/node

Node.js adapters for the MCP TypeScript server SDK.

This package is a thin Node.js integration layer for @modelcontextprotocol/server. It provides a Streamable HTTP transport that works with Node’s IncomingMessage / ServerResponse.

For web‑standard runtimes (Cloudflare Workers, Deno, Bun, etc.), use WebStandardStreamableHTTPServerTransport from @modelcontextprotocol/server directly.

Install

npm install @modelcontextprotocol/server @modelcontextprotocol/node

Exports

  • NodeStreamableHTTPServerTransport
  • StreamableHTTPServerTransportOptions (type alias for WebStandardStreamableHTTPServerTransportOptions)
  • toNodeHandler(handler, opts?) — adapt a web-standard { fetch } MCP handler to a Node (req, res, parsedBody?) handler
  • hostHeaderValidation(allowedHostnames) / localhostHostValidation() — Host header guards for hand-wired node:http servers
  • originValidation(allowedOriginHostnames) / localhostOriginValidation() — Origin header guards for hand-wired node:http servers
  • ToNodeHandlerOptions, FetchLikeMcpHandler, NodeMcpRequestHandler (types for toNodeHandler)
  • toWebRequest(req, parsedBody?, opts?) — the Node IncomingMessage → web-standard Request conversion toNodeHandler performs internally, exported on its own (for example to feed isLegacyRequest() from a hand-wired (req, res) handler)
  • ToWebRequestOptions (options type for toWebRequest)
  • NodeIncomingMessageLike, NodeServerResponseLike (structural Node request/response shapes)

Usage

Express + Streamable HTTP

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

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

app.post('/mcp', async (req, res) => {
    const transport = new NodeStreamableHTTPServerTransport({ sessionIdGenerator: undefined });
    await server.connect(transport);

    // If you use Express JSON parsing, pass the pre-parsed body to avoid re-reading the stream.
    await transport.handleRequest(req, res, req.body);
});

Node.js http server

Plain node:http has no middleware chain, so bind loopback explicitly and compose the Host/Origin guards in front of the transport — matching the defaults the framework app factories (createMcpExpressApp, createMcpHonoApp, createMcpFastifyApp) apply for you. The guards answer rejected requests with 403 themselves and return false, so the handler must not touch the request further.

import { createServer } from 'node:http';
import { localhostHostValidation, localhostOriginValidation, NodeStreamableHTTPServerTransport } from '@modelcontextprotocol/node';
import { McpServer } from '@modelcontextprotocol/server';

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

const validateHost = localhostHostValidation();
const validateOrigin = localhostOriginValidation();

createServer(async (req, res) => {
    if (!validateHost(req, res) || !validateOrigin(req, res)) return;
    const transport = new NodeStreamableHTTPServerTransport({ sessionIdGenerator: undefined });
    await server.connect(transport);
    await transport.handleRequest(req, res);
}).listen(3000, '127.0.0.1');