Skip to content

Commit 78fabea

Browse files
Use protected HTTP wiring in examples and guides (#2445)
1 parent 561c6d8 commit 78fabea

47 files changed

Lines changed: 339 additions & 147 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/node': patch
3+
---
4+
5+
Document composing the host and origin validation guards in front of `toNodeHandler` for hand-wired `node:http` servers, matching the protected wiring the examples and serving guide now demonstrate.

docs/serving/http.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,25 @@ Because no state lives on the instance, the endpoint is stateless and scales hor
6666

6767
## Mount it on your runtime
6868

69-
On a web-standard runtime — Cloudflare Workers, Deno, Bun — `export default handler` is the entire mount. Node frameworks wrap the handler once with `toNodeHandler` from `@modelcontextprotocol/node`; on plain `node:http`:
69+
On a web-standard runtime — Cloudflare Workers, Deno, Bun — `export default handler` is the entire mount. Node frameworks wrap the handler once with `toNodeHandler` from `@modelcontextprotocol/node`; on plain `node:http`, bind loopback explicitly and compose the `localhostHostValidation` / `localhostOriginValidation` guards (also from `@modelcontextprotocol/node`) in front of it, matching the framework factories' defaults:
7070

7171
```ts source="../../examples/guides/serving/http.examples.ts#mountNode"
72-
createServer(toNodeHandler(handler)).listen(3000);
72+
const nodeHandler = toNodeHandler(handler);
73+
const validateHost = localhostHostValidation();
74+
const validateOrigin = localhostOriginValidation();
75+
createServer((req, res) => {
76+
if (!validateHost(req, res) || !validateOrigin(req, res)) return;
77+
void nodeHandler(req, res);
78+
}).listen(3000, '127.0.0.1');
7379
```
7480

75-
`POST http://localhost:3000/mcp` now reaches the factory. The same wrapped handler mounts under [Express](./express.md), [Fastify](./fastify.md), and [Hono](./hono.md); [Serve on web-standard runtimes](./web-standard.md) covers the `export default` side.
81+
`POST http://127.0.0.1:3000/mcp` now reaches the factory; the guards answer anything else with `403` before the handler sees it — [the next section](#validate-host-and-origin-in-front-of-it) explains why they belong in front. The same wrapped handler mounts under [Express](./express.md), [Fastify](./fastify.md), and [Hono](./hono.md); [Serve on web-standard runtimes](./web-standard.md) covers the `export default` side.
7682

7783
## Validate Host and Origin in front of it
7884

7985
The handler trusts its caller: it validates no `Host` header, no `Origin` header, and no token. Mount those checks in front of it — on a localhost bind, the `Host` check is what stops **DNS rebinding**, a malicious page resolving its own domain to `127.0.0.1` so the browser treats your local server as same-origin.
8086

81-
Under a framework you never wire either check by hand: `createMcpExpressApp`, `createMcpHonoApp`, and `createMcpFastifyApp` all arm both by default on localhost binds — the [Express](./express.md), [Hono](./hono.md), and [Fastify](./fastify.md) recipes start there. On a bare fetch runtime, put `hostHeaderValidationResponse` and `originValidationResponse` (from `@modelcontextprotocol/server`) in front of `handler.fetch`[Serve on web-standard runtimes](./web-standard.md#protect-against-dns-rebinding) builds that wrapper.
87+
Under a framework you never wire either check by hand: `createMcpExpressApp`, `createMcpHonoApp`, and `createMcpFastifyApp` all arm both by default on localhost binds — the [Express](./express.md), [Hono](./hono.md), and [Fastify](./fastify.md) recipes start there. On plain `node:http`, compose `localhostHostValidation` and `localhostOriginValidation` (from `@modelcontextprotocol/node`) in front of the wrapped handler, as [the mount above](#mount-it-on-your-runtime) does. On a bare fetch runtime, put `hostHeaderValidationResponse` and `originValidationResponse` (from `@modelcontextprotocol/server`) in front of `handler.fetch`[Serve on web-standard runtimes](./web-standard.md#protect-against-dns-rebinding) builds that wrapper.
8288

8389
## Pass authentication through
8490

examples/CONTRIBUTING.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ it (HTTP-only auth, sessionful transports, framework adapters, etc.).
2626
### `server.ts`
2727

2828
```ts
29-
import { createServer } from 'node:http';
30-
29+
import { serve } from '@hono/node-server';
3130
import { parseExampleArgs } from '@mcp-examples/shared';
32-
import { toNodeHandler } from '@modelcontextprotocol/node';
31+
import { createMcpHonoApp } from '@modelcontextprotocol/hono';
3332
import { createMcpHandler, McpServer } from '@modelcontextprotocol/server';
3433
import { serveStdio } from '@modelcontextprotocol/server/stdio';
3534

@@ -46,12 +45,24 @@ if (transport === 'stdio') {
4645
console.error('[server] serving over stdio');
4746
} else {
4847
const handler = createMcpHandler(buildServer);
49-
createServer(toNodeHandler(handler)).listen(port, () => {
48+
// `createMcpHonoApp()` arms localhost Host/Origin validation by default.
49+
const app = createMcpHonoApp();
50+
app.all('/mcp', c => handler.fetch(c.req.raw));
51+
serve({ fetch: app.fetch, port, hostname: '127.0.0.1' }, () => {
5052
console.error(`[server] listening on http://127.0.0.1:${port}/mcp`);
5153
});
5254
}
5355
```
5456

57+
The HTTP leg binds loopback explicitly and mounts the handler behind
58+
`createMcpHonoApp()`, which applies host/origin validation by default —
59+
matching the framework factories' defaults. A story whose point is the raw
60+
`node:http` wiring (e.g. `gateway/`, `elicitation/`) keeps
61+
`createServer` + `toNodeHandler` but must then bind
62+
`.listen(port, '127.0.0.1')` and compose the `localhostHostValidation()` /
63+
`localhostOriginValidation()` guards from `@modelcontextprotocol/node` in
64+
front of the handler. Either way, no example teaches an unguarded HTTP mount.
65+
5566
### `client.ts`
5667

5768
```ts

examples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pnpm --filter @mcp-examples/<story> client -- --http http://127.0.0.1:3000/mcp
1616

1717
Add `-- --legacy` to the client command for the 2025-era handshake.
1818

19+
Every HTTP leg serves the handler behind host/origin validation — via `createMcpHonoApp()` (or another framework app factory, which arm the checks by default on localhost binds), or, for raw `node:http` stories, via the `localhostHostValidation()` / `localhostOriginValidation()` guards from `@modelcontextprotocol/node` composed in front of `toNodeHandler` on an explicit loopback bind. New stories follow the canonical skeleton — see [CONTRIBUTING.md](./CONTRIBUTING.md).
20+
1921
The one exception to the generic commands is the reference pair: [`cli-client/`](./cli-client/README.md) and [`todos-server/`](./todos-server/README.md) have their own entry points (`pnpm --filter @mcp-examples/cli-client start`, `pnpm --filter @mcp-examples/todos-server start:http`) — see their READMEs.
2022

2123
## Start here

examples/caching/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"client": "tsx client.ts"
88
},
99
"dependencies": {
10+
"@hono/node-server": "catalog:runtimeServerOnly",
1011
"@mcp-examples/shared": "workspace:*",
1112
"@modelcontextprotocol/client": "workspace:*",
12-
"@modelcontextprotocol/node": "workspace:*",
13+
"@modelcontextprotocol/hono": "workspace:*",
1314
"@modelcontextprotocol/server": "workspace:*"
1415
},
1516
"devDependencies": {

examples/caching/server.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
* The fields are emitted ONLY toward 2026-era clients — a 2025-era response
1414
* is byte-for-byte unchanged. One binary, either transport.
1515
*/
16-
import { createServer } from 'node:http';
17-
16+
import { serve } from '@hono/node-server';
1817
import { parseExampleArgs } from '@mcp-examples/shared';
19-
import { toNodeHandler } from '@modelcontextprotocol/node';
18+
import { createMcpHonoApp } from '@modelcontextprotocol/hono';
2019
import { createMcpHandler, McpServer } from '@modelcontextprotocol/server';
2120
import { serveStdio } from '@modelcontextprotocol/server/stdio';
2221

@@ -96,7 +95,11 @@ if (transport === 'stdio') {
9695
console.error('[server] serving over stdio');
9796
} else {
9897
const handler = createMcpHandler(buildServer);
99-
createServer(toNodeHandler(handler)).listen(port, () => {
98+
// `createMcpHonoApp()` binds the endpoint behind localhost host/origin
99+
// validation by default, matching the framework factories' defaults.
100+
const app = createMcpHonoApp();
101+
app.all('/mcp', c => handler.fetch(c.req.raw));
102+
serve({ fetch: app.fetch, port, hostname: '127.0.0.1' }, () => {
100103
console.error(`[server] listening on http://127.0.0.1:${port}/mcp`);
101104
});
102105
}

examples/custom-methods/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"client": "tsx client.ts"
88
},
99
"dependencies": {
10+
"@hono/node-server": "catalog:runtimeServerOnly",
1011
"@mcp-examples/shared": "workspace:*",
1112
"@modelcontextprotocol/client": "workspace:*",
12-
"@modelcontextprotocol/node": "workspace:*",
13+
"@modelcontextprotocol/hono": "workspace:*",
1314
"@modelcontextprotocol/server": "workspace:*",
1415
"zod": "catalog:runtimeShared"
1516
},

examples/custom-methods/server.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
* One binary, either transport — selected by `--http --port <N>` (defaults to
66
* stdio). See `examples/CONTRIBUTING.md` for the canonical shape.
77
*/
8-
import { createServer } from 'node:http';
9-
8+
import { serve } from '@hono/node-server';
109
import { parseExampleArgs } from '@mcp-examples/shared';
11-
import { toNodeHandler } from '@modelcontextprotocol/node';
10+
import { createMcpHonoApp } from '@modelcontextprotocol/hono';
1211
import { createMcpHandler, McpServer } from '@modelcontextprotocol/server';
1312
import { serveStdio } from '@modelcontextprotocol/server/stdio';
1413
import { z } from 'zod/v4';
@@ -36,7 +35,10 @@ if (transport === 'stdio') {
3635
console.error('[server] serving over stdio');
3736
} else {
3837
const handler = createMcpHandler(buildServer);
39-
createServer(toNodeHandler(handler)).listen(port, () => {
38+
// `createMcpHonoApp()` arms localhost host/origin validation by default.
39+
const app = createMcpHonoApp();
40+
app.all('/mcp', c => handler.fetch(c.req.raw));
41+
serve({ fetch: app.fetch, port, hostname: '127.0.0.1' }, () => {
4042
console.error(`[server] listening on http://127.0.0.1:${port}/mcp`);
4143
});
4244
}

examples/custom-version/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"client": "tsx client.ts"
88
},
99
"dependencies": {
10+
"@hono/node-server": "catalog:runtimeServerOnly",
1011
"@mcp-examples/shared": "workspace:*",
1112
"@modelcontextprotocol/client": "workspace:*",
12-
"@modelcontextprotocol/node": "workspace:*",
13+
"@modelcontextprotocol/hono": "workspace:*",
1314
"@modelcontextprotocol/server": "workspace:*"
1415
},
1516
"devDependencies": {

examples/custom-version/server.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
* The first version in the list is the fallback when the client requests an
44
* unsupported one. One binary, either transport.
55
*/
6-
import { createServer } from 'node:http';
7-
6+
import { serve } from '@hono/node-server';
87
import { parseExampleArgs } from '@mcp-examples/shared';
9-
import { toNodeHandler } from '@modelcontextprotocol/node';
8+
import { createMcpHonoApp } from '@modelcontextprotocol/hono';
109
import { createMcpHandler, McpServer, SUPPORTED_PROTOCOL_VERSIONS } from '@modelcontextprotocol/server';
1110
import { serveStdio } from '@modelcontextprotocol/server/stdio';
1211

@@ -33,7 +32,11 @@ if (transport === 'stdio') {
3332
console.error('[server] serving over stdio');
3433
} else {
3534
const handler = createMcpHandler(buildServer);
36-
createServer(toNodeHandler(handler)).listen(port, () => {
35+
// `createMcpHonoApp()` binds the endpoint behind localhost host/origin
36+
// validation by default, matching the framework factories' defaults.
37+
const app = createMcpHonoApp();
38+
app.all('/mcp', c => handler.fetch(c.req.raw));
39+
serve({ fetch: app.fetch, port, hostname: '127.0.0.1' }, () => {
3740
console.error(`[server] listening on http://127.0.0.1:${port}/mcp`);
3841
});
3942
}

0 commit comments

Comments
 (0)