Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 14 additions & 96 deletions examples/servers/typescript/everything-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import cors from 'cors';
import { randomUUID, createHmac } from 'crypto';
import { inspectDraftRequest, sendDraftDiscovery } from './helpers/draft-protocol.js';

// Server state
const resourceSubscriptions = new Set<string>();
Expand Down Expand Up @@ -1225,16 +1226,6 @@ app.use(
})
);

// Protocol revisions that use the initialize/session lifecycle. The
// per-request `_meta` and header/body validation requirements apply to
// 2026-07-28 and later, not to traffic from these revisions.
const LEGACY_SESSION_PROTOCOL_VERSIONS = [
'2024-11-05',
'2025-03-26',
'2025-06-18',
'2025-11-25'
];

// Stateless (draft) operations whose results MUST carry the SEP-2549 caching
// hints (`ttlMs`, `cacheScope`).
const STATELESS_CACHEABLE_METHODS: ReadonlySet<string> = new Set([
Expand Down Expand Up @@ -1276,79 +1267,15 @@ function sendStatelessJson(
// Handle POST requests - stateful mode
app.post('/mcp', async (req, res) => {
const sessionId = req.headers['mcp-session-id'] as string | undefined;
const reqVersion = req.headers['mcp-protocol-version'] as string | undefined;
const body = req.body || {};
const method = body.method;
const id = body.id ?? null;
const params = body.params || {};
const meta = params._meta;
const metaVersion = meta?.['io.modelcontextprotocol/protocolVersion'];

// A request that carries no `_meta` and names a legacy session-era revision
// in the header is legacy traffic; it is served by the session path below
// instead of being rejected for missing per-request metadata.
const isLegacySessionEraRequest =
meta === undefined &&
reqVersion !== undefined &&
LEGACY_SESSION_PROTOCOL_VERSIONS.includes(reqVersion);

if (!sessionId && (reqVersion || meta) && !isLegacySessionEraRequest) {
// Missing Transport Header Validation Check
if (!reqVersion) {
return res.status(400).json({
jsonrpc: '2.0',
id,
error: { code: -32020, message: 'Missing MCP-Protocol-Version header' }
});
}

// Per-Request Metadata Integrity Checks (Fields verification).
// A request missing any required `_meta` field is malformed: -32602 and,
// on HTTP, status 400 Bad Request. `clientInfo` is a SHOULD since spec
// PR #3002 and is never required.
if (
!meta ||
!meta['io.modelcontextprotocol/protocolVersion'] ||
!meta['io.modelcontextprotocol/clientCapabilities']
) {
return res.status(400).json({
jsonrpc: '2.0',
id,
error: {
code: -32602,
message: 'Invalid params: missing _meta or required fields'
}
});
}

// Header Mismatch Verification (-32020, HTTP 400)
if (reqVersion !== metaVersion) {
return res.status(400).json({
jsonrpc: '2.0',
id,
error: {
code: -32020,
message: 'Mismatched MCP-Protocol-Version header'
}
});
}

// Protocol Version Negotiation Matrix (-32022, HTTP 400)
if (metaVersion !== '2026-07-28') {
return res.status(400).json({
jsonrpc: '2.0',
id,
error: {
code: -32022,
message: 'UnsupportedProtocolVersionError',
data: {
supported: ['2026-07-28'],
requested: String(metaVersion)
}
}
});
}

const draftRequest = inspectDraftRequest(req, res);
if (draftRequest.kind === 'rejected') return;
if (draftRequest.kind === 'draft') {
// Subscriptions Listening Endpoint Stream Handler (SSE/Chunked Line)
if (method === 'subscriptions/listen') {
res.writeHead(200, {
Expand Down Expand Up @@ -1398,27 +1325,18 @@ app.post('/mcp', async (req, res) => {
}

if (method === 'server/discover') {
return sendStatelessJson(res, method, {
jsonrpc: '2.0',
return sendDraftDiscovery(
res,
id,
result: {
supportedVersions: ['2026-07-28'],
capabilities: {
tools: { listChanged: true }, // Explicitly announce dynamic capabilities matching Section 7 expectations
prompts: { listChanged: true },
// resources/list, resources/templates/list and resources/read are
// served on this path, so the capability must be declared too.
resources: {}
},
// Spec PR #3002: server identity lives in the result `_meta`.
_meta: {
'io.modelcontextprotocol/serverInfo': {
name: 'everything-stateless-server',
version: '1.0.0'
}
}
{ name: 'everything-stateless-server', version: '1.0.0' },
{
tools: { listChanged: true },
prompts: { listChanged: true },
// resources/list, resources/templates/list and resources/read are
// served on this path, so the capability must be declared too.
resources: {}
}
});
);
}

if (method === 'tools/list') {
Expand Down
142 changes: 142 additions & 0 deletions examples/servers/typescript/helpers/draft-protocol.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { describe, expect, it } from 'vitest';
import type { Request, Response } from 'express';
import {
inspectDraftRequest,
sendDraftDiscovery,
sendDraftResult
} from './draft-protocol.js';

type Sent = {
status: number;
payload?: {
id?: unknown;
result?: Record<string, unknown>;
error?: { code: number };
};
};

function mockRequest(
version: string | undefined,
meta: Record<string, unknown> | undefined,
method = 'server/discover',
sessionId?: string
): Request {
return {
headers: {
...(version ? { 'mcp-protocol-version': version } : {}),
...(sessionId ? { 'mcp-session-id': sessionId } : {})
},
body: {
jsonrpc: '2.0',
id: 7,
method,
params: { ...(meta && { _meta: meta }) }
}
} as unknown as Request;
}

function mockResponse(): { res: Response; sent: Sent } {
const sent: Sent = { status: 200 };
const res = {
status(code: number) {
sent.status = code;
return this;
},
json(payload: Record<string, unknown>) {
sent.payload = payload as Sent['payload'];
return this;
}
} as unknown as Response;
return { res, sent };
}

const validMeta = {
'io.modelcontextprotocol/protocolVersion': '2026-07-28',
'io.modelcontextprotocol/clientCapabilities': {}
};

describe('example draft protocol preflight', () => {
it('preserves session-era requests for the official SDK', () => {
expect(
inspectDraftRequest(mockRequest(undefined, undefined), mockResponse().res)
.kind
).toBe('legacy');
expect(
inspectDraftRequest(
mockRequest('2025-11-25', undefined),
mockResponse().res
).kind
).toBe('legacy');
expect(
inspectDraftRequest(
mockRequest('2026-07-28', validMeta, 'tools/list', 'session-123'),
mockResponse().res
).kind
).toBe('legacy');
});

it('accepts a valid draft request without forwarding it to the v1 SDK', () => {
const { res, sent } = mockResponse();
expect(
inspectDraftRequest(mockRequest('2026-07-28', validMeta), res)
).toEqual({
kind: 'draft',
method: 'server/discover',
id: 7
});
expect(sent.payload).toBeUndefined();
});

it.each([
[undefined, validMeta, -32020],
['2026-07-28', undefined, -32602],
['2025-11-25', validMeta, -32020],
[
'2027-01-01',
{ ...validMeta, 'io.modelcontextprotocol/protocolVersion': '2027-01-01' },
-32022
]
])(
'rejects invalid draft request with expected JSON-RPC code',
(version, meta, code) => {
const { res, sent } = mockResponse();
expect(inspectDraftRequest(mockRequest(version, meta), res).kind).toBe(
'rejected'
);
expect(sent.status).toBe(400);
expect(sent.payload?.error?.code).toBe(code);
expect(sent.payload?.id).toBe(7);
}
);

it('advertises draft version and expected server identity in discover', () => {
const { res, sent } = mockResponse();
sendDraftDiscovery(
res,
42,
{ name: 'fixture', version: '1.0.0' },
{ tools: {} }
);
expect(sent.payload?.result).toMatchObject({
resultType: 'complete',
ttlMs: 0,
cacheScope: 'private',
supportedVersions: ['2026-07-28'],
capabilities: { tools: {} },
_meta: {
'io.modelcontextprotocol/serverInfo': { name: 'fixture', version: '1.0.0' }
}
});
});

it('adds required result fields to tools/list responses', () => {
const { res, sent } = mockResponse();
sendDraftResult(res, 1, { tools: [{ name: 'broken-tool-name' }] });
expect(sent.payload?.result).toMatchObject({
resultType: 'complete',
ttlMs: 0,
cacheScope: 'private',
tools: [{ name: 'broken-tool-name' }]
});
});
});
Loading