|
| 1 | +import type { JSONRPCRequest } from '../types/index.js'; |
| 2 | + |
| 3 | +/** |
| 4 | + * SEP-2243: Methods whose `Mcp-Name` header mirrors a request body field, and which field. |
| 5 | + * Exposed so the client transport (sets headers) and server transports (validate them) |
| 6 | + * agree on the source field. |
| 7 | + */ |
| 8 | +const NAME_FIELD_FOR: Record<string, 'name' | 'uri'> = { |
| 9 | + 'tools/call': 'name', |
| 10 | + 'prompts/get': 'name', |
| 11 | + 'resources/read': 'uri' |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * Returns the SEP-2243 `Mcp-Name` value for a request body, or `undefined` if the method |
| 16 | + * has no name-level field. |
| 17 | + */ |
| 18 | +export function mcpNameForMethod(method: string, params: unknown): string | undefined { |
| 19 | + const field = NAME_FIELD_FOR[method]; |
| 20 | + if (!field || !params || typeof params !== 'object') return undefined; |
| 21 | + const v = (params as Record<string, unknown>)[field]; |
| 22 | + return typeof v === 'string' ? v : undefined; |
| 23 | +} |
| 24 | + |
| 25 | +// HTTP header values must be ISO-8859-1. SEP-2243 specifies RFC-2047-style encoding |
| 26 | +// (`=?base64?<b64>?=`) for values containing characters outside the safe-header range. |
| 27 | +const HEADER_SAFE = /^[ -~]*$/; |
| 28 | + |
| 29 | +/** Encode a value for use as an `Mcp-*` HTTP header per SEP-2243 (RFC-2047 base64 for non-ASCII). */ |
| 30 | +export function encodeMcpHeaderValue(value: string): string { |
| 31 | + if (HEADER_SAFE.test(value)) return value; |
| 32 | + // Byte-level mapping for btoa: each Uint8 byte must become one Latin-1 char. |
| 33 | + // eslint-disable-next-line unicorn/prefer-code-point |
| 34 | + const b64 = btoa(String.fromCharCode(...new TextEncoder().encode(value))); |
| 35 | + return `=?base64?${b64}?=`; |
| 36 | +} |
| 37 | + |
| 38 | +/** Decode an `Mcp-*` HTTP header value, reversing {@linkcode encodeMcpHeaderValue}. */ |
| 39 | +export function decodeMcpHeaderValue(value: string): string { |
| 40 | + const m = /^=\?base64\?(.+)\?=$/.exec(value); |
| 41 | + if (!m) return value; |
| 42 | + // atob output is one Latin-1 char per byte; charCodeAt gives the byte value back. |
| 43 | + // eslint-disable-next-line unicorn/prefer-code-point |
| 44 | + const bytes = Uint8Array.from(atob(m[1]!), c => c.charCodeAt(0)); |
| 45 | + return new TextDecoder().decode(bytes); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * SEP-2243 server-side enforcement: returns a header-mismatch error message if the supplied |
| 50 | + * `Mcp-Method` / `Mcp-Name` headers do not match the body, or `undefined` if they match |
| 51 | + * (or are absent). Per the spec, headers are required for compliance with the version they |
| 52 | + * are introduced in; this validator only rejects on PRESENT-but-mismatched, since absence |
| 53 | + * may indicate a pre-SEP-2243 client. Batch bodies are not validated (no single method). |
| 54 | + */ |
| 55 | +export function validateMcpHeaders(httpReq: Request, body: JSONRPCRequest | JSONRPCRequest[]): string | undefined { |
| 56 | + if (Array.isArray(body)) return undefined; |
| 57 | + const hMethodRaw = httpReq.headers.get('mcp-method'); |
| 58 | + const hMethod = hMethodRaw === null ? null : decodeMcpHeaderValue(hMethodRaw); |
| 59 | + if (hMethod !== null && hMethod !== body.method) { |
| 60 | + return `Mcp-Method header '${hMethod}' does not match request body method '${body.method}'`; |
| 61 | + } |
| 62 | + const hNameRaw = httpReq.headers.get('mcp-name'); |
| 63 | + if (hNameRaw !== null) { |
| 64 | + const hName = decodeMcpHeaderValue(hNameRaw); |
| 65 | + const bodyName = mcpNameForMethod(body.method, body.params); |
| 66 | + if (hName !== bodyName) { |
| 67 | + return `Mcp-Name header '${hName}' does not match request body ${NAME_FIELD_FOR[body.method] ?? 'name'} '${bodyName ?? '(absent)'}'`; |
| 68 | + } |
| 69 | + } |
| 70 | + return undefined; |
| 71 | +} |
0 commit comments