-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathunsupported-protocol-server.js
More file actions
89 lines (76 loc) · 2.2 KB
/
unsupported-protocol-server.js
File metadata and controls
89 lines (76 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import http from "node:http";
import { once } from "node:events";
const ERROR_MESSAGE =
"Unsupported protocol version: 2025-11-25 - supported versions: 2025-06-18,2025-03-26,2024-11-05,2024-10-07";
const applyCors = (res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"content-type, accept, mcp-session-id, mcp-protocol-version, authorization",
);
res.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
};
export async function startUnsupportedProtocolServer() {
const server = http.createServer(async (req, res) => {
applyCors(res);
if (req.method === "OPTIONS") {
res.statusCode = 204;
res.end();
return;
}
// Streamable HTTP transport does an optional GET to establish an SSE stream.
// Returning 405 is an expected case handled by the SDK.
if (req.method === "GET") {
res.statusCode = 405;
res.end();
return;
}
// Accepts any path; the Inspector URL field can include arbitrary endpoints.
if (req.method !== "POST") {
res.statusCode = 404;
res.end();
return;
}
let body = "";
req.setEncoding("utf8");
req.on("data", (chunk) => {
body += chunk;
});
await once(req, "end");
let parsed;
try {
parsed = JSON.parse(body);
} catch {
res.statusCode = 400;
res.setHeader("content-type", "application/json");
res.end(JSON.stringify({ error: "Invalid JSON" }));
return;
}
res.statusCode = 200;
res.setHeader("content-type", "application/json");
res.end(
JSON.stringify({
jsonrpc: "2.0",
id: parsed?.id ?? null,
error: {
code: -32602,
message: ERROR_MESSAGE,
},
}),
);
});
server.listen(0, "127.0.0.1");
await once(server, "listening");
const address = server.address();
if (!address || typeof address === "string") {
throw new Error("Failed to start unsupported protocol fixture server");
}
const baseUrl = `http://127.0.0.1:${address.port}`;
return {
baseUrl,
close: () =>
new Promise((resolve, reject) =>
server.close((e) => (e ? reject(e) : resolve())),
),
};
}