Repository navigation
Expand file tree
/
Copy pathprobe-protocol.mjs
More file actions
161 lines (146 loc) · 6.1 KB
/
Copy pathprobe-protocol.mjs
File metadata and controls
161 lines (146 loc) · 6.1 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Raw JSON-RPC stdio probe. Deliberately does NOT use the MCP client SDK, so the
// evidence is independent of the library under test: it writes bytes, reads bytes.
//
// node probe.mjs <command> [args...]
//
// The era is pinned per CONNECTION by the opening message, so each arm gets its
// own process:
// ARM 1 server/discover WITH the 2026-07-28 version claim in _meta
// ARM 2 server/discover WITHOUT a claim (spec: a claim-less message is a 2025 opening)
// ARM 3 initialize — the 2025-11-25 handshake, proving the legacy era still serves
import { spawn } from 'node:child_process';
// --assert must be stripped before the command is read, or it becomes the command.
const ASSERT = process.argv.includes('--assert');
const argv = process.argv.slice(2).filter((a) => a !== '--assert');
const [cmd, ...args] = argv;
if (!cmd) {
console.error(
'usage: node scripts/probe-protocol.mjs [--assert] <command> [args...]\n\n' +
'examples:\n' +
' node scripts/probe-protocol.mjs node dist/index.js\n' +
' node scripts/probe-protocol.mjs --assert node dist/index.js # exit 1 unless both eras answer\n' +
' node scripts/probe-protocol.mjs npx -y [email protected] # control arm: the previous release\n',
);
process.exit(2);
}
const PV = 'io.modelcontextprotocol/protocolVersion';
const CC = 'io.modelcontextprotocol/clientCapabilities';
const CI = 'io.modelcontextprotocol/clientInfo';
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
// On Windows npx/npm/pnpm/yarn are .cmd shims. spawn() cannot exec them directly (ENOENT),
// and since the CVE-2024-27980 mitigation Node refuses `.cmd` outright (EINVAL) -- so the
// shell is genuinely required there. Without this every arm dies before the server starts
// and reports "NO RESPONSE", which is indistinguishable from a server that ran and stayed
// silent. That false negative reads exactly like a finding; it is the control arm measuring
// nothing at all.
//
// The command goes through as ONE string with no separate args array, because passing args
// *alongside* shell:true is what triggers DEP0190.
const WIN = process.platform === 'win32';
const spawnProbe = () =>
WIN
? spawn([cmd, ...args].join(' '), { stdio: ['pipe', 'pipe', 'pipe'], shell: true })
: spawn(cmd, args, { stdio: ['pipe', 'pipe', 'pipe'] });
async function ask(request, label) {
const child = spawnProbe();
const seen = [];
const stderr = [];
let buf = '';
child.stdout.on('data', (d) => {
buf += d.toString();
let i;
while ((i = buf.indexOf('\n')) >= 0) {
const line = buf.slice(0, i).trim();
buf = buf.slice(i + 1);
if (line) {
try {
seen.push(JSON.parse(line));
} catch {
/* non-JSON line on stdout is itself a finding; ignored here */
}
}
}
});
child.stderr.on('data', (d) => stderr.push(d.toString()));
child.on('error', (e) => stderr.push(`spawn error: ${e.message}`));
// Wait for the server to actually be listening rather than for a fixed delay. A cold
// `npx` downloads the package first, which blew past a flat 1200ms and made every arm
// report NO RESPONSE -- a timing artifact that reads as "the old version ignores the
// request". Poll for the readiness banner on stderr, with a generous ceiling.
const READY_MS = 30_000;
for (let waited = 0; waited < READY_MS; waited += 100) {
if (stderr.join('').includes('ready on stdio')) break;
await wait(100);
}
await wait(300);
child.stdin.write(JSON.stringify(request) + '\n');
await wait(2500);
child.kill();
await wait(120);
const r = seen.find((m) => m.id === request.id);
let verdict;
if (!r) verdict = 'NO RESPONSE';
else if (r.error) verdict = `ERROR ${r.error.code} ${r.error.message}`;
else verdict = 'OK';
console.log(`\n### ${label}`);
console.log(` verdict: ${verdict}`);
if (r?.result) {
const keys = Object.keys(r.result);
console.log(` result keys: ${keys.join(', ')}`);
for (const k of ['protocolVersions', 'protocolVersion', 'serverInfo', 'capabilities', 'resultType']) {
if (r.result[k] !== undefined) console.log(` ${k}: ${JSON.stringify(r.result[k])}`);
}
if (r.result._meta) console.log(` _meta: ${JSON.stringify(r.result._meta)}`);
}
const err = stderr.join('').trim();
if (err) console.log(` stderr: ${err.split('\n')[0]}`);
return verdict;
}
const modern = {
jsonrpc: '2.0',
id: 1,
method: 'server/discover',
params: {
_meta: {
[PV]: '2026-07-28',
[CC]: {},
[CI]: { name: 'raw-probe', version: '0.0.0' },
},
},
};
const claimless = { jsonrpc: '2.0', id: 2, method: 'server/discover', params: {} };
const legacy = {
jsonrpc: '2.0',
id: 3,
method: 'initialize',
params: {
protocolVersion: '2025-11-25',
capabilities: {},
clientInfo: { name: 'raw-probe', version: '0.0.0' },
},
};
console.log(`=== probing: ${argv.join(' ')} ===`);
const a = await ask(modern, 'ARM 1 server/discover WITH 2026-07-28 claim');
const b = await ask(claimless, 'ARM 2 server/discover, claim-less');
const c = await ask(legacy, 'ARM 3 initialize (2025-11-25)');
console.log(`\nSUMMARY modern=${a} claimless=${b} legacy=${c}`);
// Without --assert this script is a printer, not a check. Measured 2026-08-01: it
// exited 0 for a command that does not exist and for a program that is not an MCP
// server at all. A CI step built on that reports success for a server that never
// started, which is worse than having no step -- it turns "untested" into a green
// mark. Control arm for the flag: `node scripts/probe-protocol.mjs --assert node
// ./no-such-file.js` must exit 1.
//
// ARM 2 is deliberately NOT asserted: a claim-less `server/discover` is specified
// as a 2025-era opening, so -32601 there is the correct answer and asserting OK
// would fail a compliant server.
if (ASSERT) {
const failures = [];
if (a !== 'OK') failures.push(`modern server/discover: ${a}`);
if (c !== 'OK') failures.push(`legacy initialize: ${c}`);
if (failures.length) {
console.error(`\nFAIL: ${failures.join(' | ')}`);
process.exit(1);
}
console.log('ASSERT OK: both protocol eras answered');
}