From 2335e373ec6f1551776b06a2c2f4ecfdf6a6f001 Mon Sep 17 00:00:00 2001 From: VrtxOmega <272921586+VrtxOmega@users.noreply.github.com> Date: Thu, 8 Oct 2026 10:47:35 -0500 Subject: [PATCH] Negotiate supported MCP protocol versions --- docs/MCP.md | 20 ++++++ src/mcp/server.mjs | 8 ++- test/mcp-protocol-negotiation.test.mjs | 94 ++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 test/mcp-protocol-negotiation.test.mjs diff --git a/docs/MCP.md b/docs/MCP.md index 642ad0d..56c4997 100644 --- a/docs/MCP.md +++ b/docs/MCP.md @@ -57,6 +57,26 @@ For an installed npm package, clients that need an executable command can use: } ``` +## Protocol Version Negotiation (Current Source, Unreleased) + +The stdio server negotiates the MCP wire version `2025-06-18`. An initialize +request for that version receives the same version; any other string receives +`2025-06-18` as the supported fallback. This includes an empty string, which the +protocol schema does not prohibit. A client that cannot use the returned version +should disconnect, as described in the +[MCP negotiation rules](https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle#version-negotiation). + +The required `params.protocolVersion` must be a string. Missing, null, boolean, +numeric, object or array values receive JSON-RPC error `-32602`, preserving the +request ID and returning no initialization result. Correct the request before +retrying. This validation is limited to the version field; it does not add a +complete initialization schema validator or enforce session ordering. + +The wire version is separate from `serverInfo.version`, which remains PCF's +implementation version. Run `node --test test/mcp-protocol-negotiation.test.mjs` +to check negotiation, malformed inputs, request correlation and subsequent ping +responses with newline and PCF's Content-Length framing. + ## Submission Readiness Before releasing source changes, run `npm run package:verify` from the checkout. diff --git a/src/mcp/server.mjs b/src/mcp/server.mjs index ca2d9fc..aadf3a7 100644 --- a/src/mcp/server.mjs +++ b/src/mcp/server.mjs @@ -16,6 +16,8 @@ const SERVER_INFO = { name: PCF_MCP_SERVER_NAME, version: PCF_MCP_PROTOCOL_VERSION }; +// The negotiated wire version is separate from PCF's implementation version. +const SUPPORTED_MCP_PROTOCOL_VERSION = "2025-06-18"; const MAX_MCP_FRAME_BYTES = 2_000_000; const MAX_MCP_HEADER_BYTES = 16_384; const EMPTY_FRAME = Symbol("pcf-mcp-empty-frame"); @@ -26,8 +28,12 @@ export async function handleMcpRequest(message) { return errorResult(null, -32600, "Invalid Request"); } if (message.method === "initialize") { + if (typeof message.params?.protocolVersion !== "string") { + return errorResult(message.id, -32602, "initialize.params.protocolVersion must be a string"); + } return result(message.id, { - protocolVersion: message.params?.protocolVersion || "2025-06-18", + // Unsupported strings negotiate to the version this server implements. + protocolVersion: SUPPORTED_MCP_PROTOCOL_VERSION, capabilities: { tools: {}, resources: {}, diff --git a/test/mcp-protocol-negotiation.test.mjs b/test/mcp-protocol-negotiation.test.mjs new file mode 100644 index 0000000..5ded726 --- /dev/null +++ b/test/mcp-protocol-negotiation.test.mjs @@ -0,0 +1,94 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { fileURLToPath } from "node:url"; +import { handleMcpRequest } from "../src/mcp/server.mjs"; +import { PCF_MCP_PROTOCOL_VERSION, PCF_MCP_SERVER_NAME } from "../src/mcp/core.mjs"; + +const repoRoot = fileURLToPath(new URL("..", import.meta.url)); +const wireVersion = "2025-06-18"; +const client = { capabilities: {}, clientInfo: { name: "protocol-control", version: "1" } }; +const cases = [ + ...[wireVersion, "2025-11-25", "2024-11-05", "2099-01-01", "not-a-version", "", " 2025-06-18 "] + .map((version) => ({ label: `string ${JSON.stringify(version)}`, params: { ...client, protocolVersion: version }, valid: true })), + ...[null, true, false, 0, 1, {}, [], [wireVersion]] + .map((version) => ({ label: `non-string ${JSON.stringify(version)}`, params: { ...client, protocolVersion: version }, valid: false })), + { label: "missing version", params: client, valid: false }, + { label: "missing params", valid: false }, + { label: "null params", params: null, valid: false }, + { label: "array params", params: [], valid: false }, + { label: "string params", params: wireVersion, valid: false } +]; + +function requestFor(entry, id) { + return { jsonrpc: "2.0", id, method: "initialize", ...("params" in entry ? { params: entry.params } : {}) }; +} + +function assertNegotiation(response, entry, id) { + assert.equal(response.jsonrpc, "2.0"); + assert.equal(response.id, id); + if (entry.valid) { + assert.equal(response.error, undefined); + assert.equal(response.result.protocolVersion, wireVersion); + assert.deepEqual(response.result.capabilities, { tools: {}, resources: {}, prompts: {} }); + assert.deepEqual(response.result.serverInfo, { name: PCF_MCP_SERVER_NAME, version: PCF_MCP_PROTOCOL_VERSION }); + } else { + assert.equal(response.result, undefined); + assert.equal(response.error.code, -32602); + assert.match(response.error.message, /protocolVersion.*string/); + } +} + +for (const [index, entry] of cases.entries()) { + test(`MCP initialize negotiation: ${entry.label}`, async () => { + const id = index % 2 ? "" : 0; + assertNegotiation(await handleMcpRequest(requestFor(entry, id)), entry, id); + }); +} + +function frame(message, mode) { + const body = JSON.stringify(message); + return mode === "newline" ? `${body}\n` : `Content-Length: ${Buffer.byteLength(body)}\r\n\r\n${body}`; +} + +function decode(output, mode) { + if (mode === "newline") return output.trim().split(/\r?\n/).map((line) => JSON.parse(line)); + const messages = []; + let bytes = Buffer.from(output); + while (bytes.length) { + const separator = bytes.indexOf("\r\n\r\n"); + assert.notEqual(separator, -1, "response header must be complete"); + const header = /^Content-Length: (\d+)$/.exec(bytes.subarray(0, separator).toString()); + assert.ok(header, "unexpected response header"); + const start = separator + 4; + const end = start + Number(header[1]); + assert.ok(end <= bytes.length, "response body must be complete"); + messages.push(JSON.parse(bytes.subarray(start, end).toString())); + bytes = bytes.subarray(end); + } + return messages; +} + +for (const mode of ["newline", "content-length"]) { + test(`MCP stdio initialize negotiation (${mode})`, async (t) => { + for (const [index, entry] of cases.entries()) { + await t.test(entry.label, () => { + const id = index % 2 ? "" : 0; + // Each case starts a fresh session. Ping is allowed during initialization + // and proves that a rejected request does not crash or poison the stream. + const input = frame(requestFor(entry, id), mode) + + frame({ jsonrpc: "2.0", id: "ping", method: "ping" }, mode); + const child = spawnSync(process.execPath, ["src/mcp/server.mjs"], { + cwd: repoRoot, input, encoding: "utf8", timeout: 10_000, windowsHide: true + }); + assert.ifError(child.error); + assert.equal(child.status, 0, child.stderr); + assert.equal(child.stderr, ""); + const responses = decode(child.stdout, mode); + assert.equal(responses.length, 2); + assertNegotiation(responses[0], entry, id); + assert.deepEqual(responses[1], { jsonrpc: "2.0", id: "ping", result: {} }); + }); + } + }); +}