Skip to content

Commit bb0592b

Browse files
committed
add supported versions to error message
1 parent 84971c8 commit bb0592b

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

src/server/streamableHttp.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ describe("StreamableHTTPServerTransport", () => {
788788

789789
expect(response.status).toBe(400);
790790
const errorData = await response.json();
791-
expectErrorResponse(errorData, -32000, /Bad Request: Unsupported protocol version/);
791+
expectErrorResponse(errorData, -32000, /Bad Request: Unsupported protocol version \(supported versions: .+\)/);
792792
});
793793

794794
it("should accept but warn when protocol version differs from negotiated version", async () => {
@@ -835,7 +835,7 @@ describe("StreamableHTTPServerTransport", () => {
835835

836836
expect(response.status).toBe(400);
837837
const errorData = await response.json();
838-
expectErrorResponse(errorData, -32000, /Bad Request: Unsupported protocol version/);
838+
expectErrorResponse(errorData, -32000, /Bad Request: Unsupported protocol version \(supported versions: .+\)/);
839839
});
840840

841841
it("should handle protocol version validation for DELETE requests", async () => {
@@ -852,7 +852,7 @@ describe("StreamableHTTPServerTransport", () => {
852852

853853
expect(response.status).toBe(400);
854854
const errorData = await response.json();
855-
expectErrorResponse(errorData, -32000, /Bad Request: Unsupported protocol version/);
855+
expectErrorResponse(errorData, -32000, /Bad Request: Unsupported protocol version \(supported versions: .+\)/);
856856
});
857857
});
858858
});

src/server/streamableHttp.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IncomingMessage, ServerResponse } from "node:http";
22
import { Transport } from "../shared/transport.js";
3-
import { isInitializeRequest, isJSONRPCError, isJSONRPCRequest, isJSONRPCResponse, JSONRPCMessage, JSONRPCMessageSchema, RequestId, SUPPORTED_PROTOCOL_VERSIONS } from "../types.js";
3+
import { isInitializeRequest, isJSONRPCError, isJSONRPCRequest, isJSONRPCResponse, JSONRPCMessage, JSONRPCMessageSchema, RequestId, SUPPORTED_PROTOCOL_VERSIONS, DEFAULT_NEGOTIATED_PROTOCOL_VERSION } from "../types.js";
44
import getRawBody from "raw-body";
55
import contentType from "content-type";
66
import { randomUUID } from "node:crypto";
@@ -538,17 +538,12 @@ export class StreamableHTTPServerTransport implements Transport {
538538
}
539539

540540
private validateProtocolVersion(req: IncomingMessage, res: ServerResponse): boolean {
541-
let protocolVersion = req.headers["mcp-protocol-version"];
541+
let protocolVersion = req.headers["mcp-protocol-version"] ?? DEFAULT_NEGOTIATED_PROTOCOL_VERSION;
542542
if (Array.isArray(protocolVersion)) {
543543
protocolVersion = protocolVersion[protocolVersion.length - 1];
544544
}
545-
546-
if (protocolVersion == null || protocolVersion === undefined) {
547-
// If the protocol version is not set, we assume the client supports the implicit protocol version
548-
return true;
549-
}
550-
551-
protocolVersion = String(protocolVersion).trim();
545+
546+
552547
if (this.protocolVersion !== undefined && this.protocolVersion !== protocolVersion) {
553548
console.warn(`Request has header with protocol version ${protocolVersion}, but version previously negotiated is ${this.protocolVersion}.`);
554549
}
@@ -557,7 +552,7 @@ export class StreamableHTTPServerTransport implements Transport {
557552
jsonrpc: "2.0",
558553
error: {
559554
code: -32000,
560-
message: 'Bad Request: Unsupported protocol version'
555+
message: `Bad Request: Unsupported protocol version (supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(", ")})`
561556
},
562557
id: null
563558
}));

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z, ZodTypeAny } from "zod";
22

33
export const LATEST_PROTOCOL_VERSION = "2025-03-26";
4+
export const DEFAULT_NEGOTIATED_PROTOCOL_VERSION = "2025-03-26";
45
export const SUPPORTED_PROTOCOL_VERSIONS = [
56
LATEST_PROTOCOL_VERSION,
67
"2024-11-05",

0 commit comments

Comments
 (0)