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
4 changes: 3 additions & 1 deletion src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const REQUEST_LIMIT = 8192;
const RESPONSE_LIMIT = 8 * 1024 * 1024;
const object = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
const identity = (value) => typeof value === "string" || Number.isSafeInteger(value);
// RFC 9110: media types are case-insensitive. Parameters after ';' are ignored.
const mediaType = (value) => value?.split(";")[0].trim().toLowerCase();

class BridgeError extends Error {
constructor(category, code = -32000, status) {
Expand Down Expand Up @@ -75,7 +77,7 @@ async function post(frame, config, protocol, notification) {
throw new BridgeError("http", -32000, response.status);
}
if (response.headers.has("mcp-session-id") || (!notification &&
response.headers.get("content-type")?.split(";")[0].trim() !== "application/json")) {
mediaType(response.headers.get("content-type")) !== "application/json")) {
await response.body?.cancel();
throw new BridgeError("transport");
}
Expand Down
38 changes: 38 additions & 0 deletions tests/bridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,41 @@ test("pre-initialize probes are answered without ending the session", async (t)
assert.deepEqual(outcome.responses[3], { jsonrpc: "2.0", id: 3, result: tools });
assert.deepEqual(fixture.requests.map((request) => request.method), ["initialize", "notifications/initialized", "tools/list"]);
});

test("JSON Content-Type is compared case-insensitively, including a charset parameter", async (t) => {
const tools = { tools: [] };
const result = { content: [{ type: "text", text: "ok" }] };
const fixture = await server(t, (body, response) => {
const type = body.method === "tools/list"
? "Application/JSON"
: "APPLICATION/JSON; charset=utf-8";
response.writeHead(200, { "content-type": type });
response.end(JSON.stringify({
jsonrpc: "2.0",
id: body.id,
result: body.method === "tools/list" ? tools : result,
}));
});
const outcome = await run(fixture.origin, [
initialize, notification,
{ jsonrpc: "2.0", id: 3, method: "tools/list" },
call,
]);
assert.equal(outcome.code, 0);
assert.equal(outcome.stderr, "");
assert.deepEqual(outcome.responses[1].result, tools);
assert.deepEqual(outcome.responses[2].result, result);
});

test("SSE Content-Type is still refused when the media type differs only by case", async (t) => {
const fixture = await server(t, (_body, response) => {
response.writeHead(200, { "content-type": "TEXT/EVENT-STREAM" });
return response.end("private-upstream-canary");
});
const outcome = await run(fixture.origin, [initialize, notification, call]);
assert.equal(outcome.code, 1);
assert.equal(outcome.responses.at(-1).error.code, -32000);
assert.equal(outcome.responses.at(-1).error.data.category, "transport");
assert.ok(!JSON.stringify(outcome).includes("private-upstream-canary"));
});