Repository navigation
Expand file tree
/
Copy pathtest-proxy-mcp.ts
More file actions
1504 lines (1399 loc) · 57.3 KB
/
Copy pathtest-proxy-mcp.ts
File metadata and controls
1504 lines (1399 loc) · 57.3 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Integration tests for src/proxy-mcp.ts — the in-process MCP HTTP server.
*
* These stand up a real `createProxyMcpServer` on an ephemeral port and
* drive it over plain HTTP, so they exercise the actual JSON-RPC framing
* (including the catch-block error envelope).
*
* Usage:
* npx tsx --test test-proxy-mcp.ts
*/
import assert from "node:assert/strict"
import { test } from "node:test"
import * as http from "node:http"
import * as fs from "node:fs"
import {
createProxyMcpServer,
buildProxyTimeoutError,
resolveProxyCallTimeoutMs,
resolveProxyClientCeilingMs,
overlayQuestionProxyDescription,
filterQuestionProxyByOpencodeSupport,
resolveMcpProxyToolDefs,
formatTaskBatchResults,
taskBatchChildToolCallId,
taskBatchInputError,
taskBatchTasks,
TASK_BATCH_TOOL_NAME,
DEFAULT_PROXY_TOOLS,
PROXY_DEFAULT_TIMEOUT_MS,
PROXY_NO_DEADLINE_MS,
MAX_PROXY_TIMEOUT_MS,
SERVER_CLOSED_MESSAGE,
type ProxyMcpServer,
type ProxyToolCall,
type ProxyToolResult,
} from "./src/proxy-mcp.js"
/**
* Low-level POST. `headers` REPLACES the default header set, so the
* security tests below can omit Authorization, send a foreign Host, add an
* Origin, or use a non-JSON Content-Type. `rawBody` bypasses JSON encoding
* for the malformed-payload case.
*/
function post(
url: string,
body: unknown,
opts: { headers?: Record<string, string>; rawBody?: string } = {},
): Promise<{
status: number
json: any
}> {
return new Promise((resolve, reject) => {
const payload = opts.rawBody ?? JSON.stringify(body)
const req = http.request(
url,
{
method: "POST",
headers: opts.headers ?? {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payload).toString(),
},
},
(res) => {
const chunks: Buffer[] = []
res.on("data", (c: Buffer) => chunks.push(c))
res.on("end", () => {
const text = Buffer.concat(chunks).toString("utf8")
try {
resolve({ status: res.statusCode ?? 0, json: JSON.parse(text) })
} catch {
resolve({ status: res.statusCode ?? 0, json: text })
}
})
},
)
req.on("error", reject)
req.write(payload)
req.end()
})
}
/** The happy path: a correctly authenticated JSON-RPC POST. */
function authedPost(srv: ProxyMcpServer, body: unknown) {
const payload = JSON.stringify(body)
return post(srv.url, body, {
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payload).toString(),
Authorization: `Bearer ${srv.authToken}`,
},
})
}
async function withServer<T>(
fn: (srv: ProxyMcpServer) => Promise<T>,
): Promise<T> {
const srv = await createProxyMcpServer(DEFAULT_PROXY_TOOLS)
try {
return await fn(srv)
} finally {
await srv.close()
}
}
// Regression for the 2026-07-04 "malformed result that failed schema
// validation" bug: Claude CLI validates tools/call responses against the
// MCP result schema and rejects JSON-RPC error envelopes. Every tools/call
// error path (broker rejection, error result, unknown tool) must return
// an MCP result with `isError: true`, and must echo the request id.
test("tools/call broker rejection returns an MCP result with isError, echoing the id", async () => {
await withServer(async (srv) => {
// Reject every incoming call immediately, simulating a broker
// rejection (the same path a 10-min timeout takes).
srv.calls.on("call", (call: ProxyToolCall) => {
call.reject(new Error("simulated broker rejection"))
})
const res = await authedPost(srv, {
jsonrpc: "2.0",
id: 42,
method: "tools/call",
params: { name: "bash", arguments: { command: "echo hi" } },
})
assert.equal(res.status, 200)
assert.equal(res.json.jsonrpc, "2.0")
assert.equal(res.json.id, 42, "response must echo the request id")
assert.equal(res.json.error, undefined, "must not be a JSON-RPC error envelope")
assert.ok(res.json.result, "expected an MCP result envelope")
assert.equal(res.json.result.isError, true)
assert.match(
res.json.result.content[0].text,
/simulated broker rejection/,
)
})
})
test("tools/call with kind:error result returns an MCP result with isError", async () => {
await withServer(async (srv) => {
srv.calls.on("call", (call: ProxyToolCall) => {
const result: ProxyToolResult = {
kind: "error",
message: "opencode tool execution failed",
}
call.resolve(result)
})
const res = await authedPost(srv, {
jsonrpc: "2.0",
id: "req-7",
method: "tools/call",
params: { name: "bash", arguments: {} },
})
assert.equal(res.json.id, "req-7")
assert.equal(res.json.error, undefined)
assert.equal(res.json.result.isError, true)
assert.match(
res.json.result.content[0].text,
/opencode tool execution failed/,
)
})
})
test("tools/call for an unknown tool returns an MCP result with isError", async () => {
await withServer(async (srv) => {
const res = await authedPost(srv, {
jsonrpc: "2.0",
id: 99,
method: "tools/call",
params: { name: "nonexistent_tool", arguments: {} },
})
assert.equal(res.json.id, 99)
assert.equal(res.json.error, undefined)
assert.equal(res.json.result.isError, true)
assert.match(res.json.result.content[0].text, /Unknown proxy tool/)
})
})
test("tools/call success preserves isError:false and the result text", async () => {
await withServer(async (srv) => {
srv.calls.on("call", (call: ProxyToolCall) => {
call.resolve({ kind: "text", text: "done" })
})
const res = await authedPost(srv, {
jsonrpc: "2.0",
id: 3,
method: "tools/call",
params: { name: "bash", arguments: {} },
})
assert.equal(res.json.result.isError, false)
assert.equal(res.json.result.content[0].text, "done")
})
})
test("malformed JSON still responds (with null id when unparseable)", async () => {
await withServer(async (srv) => {
// Send invalid JSON so parsing throws before requestId is set. The
// request is otherwise well-formed and authenticated, so it reaches
// the parser rather than being rejected by the entry guards.
const res = await post(srv.url, null, {
rawBody: "{not json",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength("{not json").toString(),
Authorization: `Bearer ${srv.authToken}`,
},
})
// When the body never parsed, null id is the only honest answer and
// is correct JSON-RPC (no request id was ever seen).
assert.equal(res.json.id, null)
assert.ok(res.json.error)
})
})
test("tools/list exposes the default proxy defs", async () => {
await withServer(async (srv) => {
const res = await authedPost(srv, {
jsonrpc: "2.0",
id: 1,
method: "tools/list",
})
const names = res.json.result.tools.map((t: any) => t.name)
assert.ok(names.includes("question"))
assert.ok(names.includes("task"))
assert.ok(names.includes("bash"))
})
})
// --- per-tool proxy timeouts ------------------------------------------------
const MIN = 60 * 1000
test("resolveProxyCallTimeoutMs: unknown tool uses the flat 10-min default", () => {
assert.equal(
resolveProxyCallTimeoutMs("edit", undefined, undefined),
PROXY_DEFAULT_TIMEOUT_MS,
)
})
test("resolveProxyCallTimeoutMs: task and task_batch have no deadline by default", () => {
// A subagent runs as long as it runs; the call waits for it. Abandoned
// calls are released by lifecycle events, not by the clock.
assert.equal(resolveProxyCallTimeoutMs("task", undefined, undefined), PROXY_NO_DEADLINE_MS)
assert.equal(
resolveProxyCallTimeoutMs(TASK_BATCH_TOOL_NAME, undefined, undefined),
PROXY_NO_DEADLINE_MS,
)
assert.equal(PROXY_NO_DEADLINE_MS, 0)
})
test("resolveProxyCallTimeoutMs: a 0 override disables a tool's deadline", () => {
assert.equal(resolveProxyCallTimeoutMs("edit", undefined, { edit: 0 }), 0)
assert.equal(resolveProxyCallTimeoutMs("question", undefined, { Question: 0 }), 0)
})
test("resolveProxyClientCeilingMs covers the largest effective deadline", () => {
// No overrides: task has no deadline, and the CLI rejects `timeout: 0`,
// so the client ceiling is the largest value it (and Node's timers) accept.
assert.equal(resolveProxyClientCeilingMs(undefined), MAX_PROXY_TIMEOUT_MS)
// Once every unlimited tool has a positive override, the ceiling tracks
// the largest effective deadline so Claude's HTTP MCP client never aborts
// before the broker deadline fires.
assert.equal(
resolveProxyClientCeilingMs({ task: 90 * MIN, task_batch: 90 * MIN }),
90 * MIN,
)
// ...and it is the per-tool default that counts when it is the largest.
assert.equal(
resolveProxyClientCeilingMs({ task: 5 * MIN, task_batch: 5 * MIN, bash: 1 * MIN }),
30 * MIN,
)
// One unlimited tool is enough to need the maximum: overriding `task`
// alone leaves `task_batch` without a deadline.
assert.equal(resolveProxyClientCeilingMs({ task: 90 * MIN }), MAX_PROXY_TIMEOUT_MS)
// A 0 override on any tool does the same.
assert.equal(
resolveProxyClientCeilingMs({ task: 5 * MIN, task_batch: 5 * MIN, bash: 0 }),
MAX_PROXY_TIMEOUT_MS,
)
// Absurd values are clamped to Node's timer max; invalid ones are ignored.
assert.equal(
resolveProxyClientCeilingMs({ task: 2 ** 40, task_batch: 2 ** 40 }),
MAX_PROXY_TIMEOUT_MS,
)
assert.equal(
resolveProxyClientCeilingMs({ task: 5 * MIN, task_batch: 5 * MIN, bash: -1, edit: NaN }),
30 * MIN,
)
})
test("resolveProxyCallTimeoutMs: user override replaces the default", () => {
assert.equal(
resolveProxyCallTimeoutMs("task", undefined, { task: 5 * MIN }),
5 * MIN,
)
})
test("resolveProxyCallTimeoutMs: override key is case-insensitive", () => {
// Users configure proxyTools with capitalised names ("Task", "Bash"); the
// override map must match regardless of case.
assert.equal(
resolveProxyCallTimeoutMs("task", undefined, { Task: 7 * MIN }),
7 * MIN,
)
assert.equal(
resolveProxyCallTimeoutMs("bash", undefined, { Bash: 9 * MIN }),
9 * MIN,
)
})
test("resolveProxyCallTimeoutMs: bash input.timeout only ever raises", () => {
// The bash proxy def advertises a `timeout` field; the proxy must not
// undercut a build the caller explicitly asked to run long.
assert.equal(
resolveProxyCallTimeoutMs("bash", { timeout: 25 * MIN }, undefined),
25 * MIN,
)
// A smaller input.timeout never lowers the resolved deadline.
assert.equal(
resolveProxyCallTimeoutMs("bash", { timeout: 1000 }, { bash: 5 * MIN }),
5 * MIN,
)
// And it raises above an override too.
assert.equal(
resolveProxyCallTimeoutMs("bash", { timeout: 12 * MIN }, { bash: 5 * MIN }),
12 * MIN,
)
})
test("resolveProxyCallTimeoutMs: invalid overrides are ignored", () => {
// Negative / NaN / Infinity must not replace the default: a misformed
// config entry should never collapse a deadline, nor silently remove one.
assert.equal(
resolveProxyCallTimeoutMs("edit", undefined, { edit: -100 }),
PROXY_DEFAULT_TIMEOUT_MS,
)
assert.equal(
resolveProxyCallTimeoutMs("edit", undefined, { edit: NaN as any }),
PROXY_DEFAULT_TIMEOUT_MS,
)
assert.equal(
resolveProxyCallTimeoutMs("edit", undefined, { edit: Infinity }),
PROXY_DEFAULT_TIMEOUT_MS,
)
assert.equal(
resolveProxyCallTimeoutMs("question", undefined, { question: -1 }),
30 * MIN,
)
assert.equal(
resolveProxyCallTimeoutMs("task", undefined, { task: "60" as any }),
PROXY_NO_DEADLINE_MS,
)
})
test("resolveProxyCallTimeoutMs: a bash input.timeout restores a deadline the override disabled", () => {
// The floor only ever raises, and a disabled deadline is the lowest value
// there is, so the caller's own timeout wins over `bash: 0`.
assert.equal(
resolveProxyCallTimeoutMs("bash", { timeout: 30_000 }, { bash: 0 }),
30_000,
)
assert.equal(resolveProxyCallTimeoutMs("bash", undefined, { bash: 0 }), 0)
})
test("resolveProxyCallTimeoutMs: absurd values are clamped to Node's timer max", () => {
// Node setTimeout overflows past 2^31-1 ms (~24.85 days), firing at ~1ms.
// Both an override and a bash input.timeout above the cap must clamp.
assert.equal(
resolveProxyCallTimeoutMs("task", undefined, { task: 2 ** 33 }),
MAX_PROXY_TIMEOUT_MS,
)
assert.equal(
resolveProxyCallTimeoutMs("bash", { timeout: 2 ** 33 }, undefined),
MAX_PROXY_TIMEOUT_MS,
)
})
test("buildProxyTimeoutError: generic message keeps the catch-block substrings", () => {
// proxy-mcp's catch block classifies "timed out after" + "waiting for
// opencode to resolve" as expected cleanup (notice, not warn). The Task
// variant must keep both substrings too.
const generic = buildProxyTimeoutError("bash", 600000)
assert.match(generic.message, /timed out after 600000ms/)
assert.match(generic.message, /waiting for opencode to resolve/)
assert.doesNotMatch(generic.message, /wake-up/)
})
test("buildProxyTimeoutError: task message warns against scheduling a wake-up", () => {
const task = buildProxyTimeoutError("task", 3600000)
assert.match(task.message, /timed out after 3600000ms/)
assert.match(task.message, /waiting for opencode to resolve/)
assert.match(task.message, /may still be running/)
assert.match(task.message, /wake-up/)
})
test("buildProxyTimeoutError: task guidance is case-insensitive on the tool name", () => {
// Config / call sites use mixed casing ("Task"); the matcher lowercases.
const task = buildProxyTimeoutError("Task", 60000)
assert.match(task.message, /wake-up/)
// And a non-task tool with unusual casing stays generic.
const generic = buildProxyTimeoutError("BASH", 60000)
assert.doesNotMatch(generic.message, /wake-up/)
})
test("tools/call timeout uses the per-tool override and surfaces the task-specific text", async () => {
// Stand up a server with a tiny Task deadline and never resolve the call,
// so the proxy-mcp timer fires and we see the real error envelope that
// Claude would receive.
const srv = await createProxyMcpServer(DEFAULT_PROXY_TOOLS, { task: 50 })
try {
// Intentionally do NOT attach a calls listener — let the deadline fire.
const res = await authedPost(srv, {
jsonrpc: "2.0",
id: "timeout-1",
method: "tools/call",
params: {
name: "task",
arguments: { description: "x", subagent_type: "gpt", prompt: "y" },
},
})
assert.equal(res.json.id, "timeout-1")
assert.equal(res.json.result.isError, true)
const text = res.json.result.content[0].text
assert.match(text, /timed out after 50ms/)
assert.match(text, /wake-up/)
} finally {
await srv.close()
}
})
test("tools/call bash timeout honours input.timeout over a shorter override", async () => {
// Override says 40ms but the call asks for a 30s bash timeout — the
// effective deadline must be 30s, so the call must NOT time out within a
// short window. Resolve it ourselves to end the test promptly.
const srv = await createProxyMcpServer(DEFAULT_PROXY_TOOLS, { bash: 40 })
try {
let resolved = false
srv.calls.on("call", (call: ProxyToolCall) => {
// Defer resolution past the 40ms override deadline to prove the
// input.timeout (30s) is what governs.
setTimeout(() => {
resolved = true
call.resolve({ kind: "text", text: "built" })
}, 120)
})
const res = await authedPost(srv, {
jsonrpc: "2.0",
id: "bash-1",
method: "tools/call",
params: { name: "bash", arguments: { command: "xcodebuild ...", timeout: 30000 } },
})
assert.equal(resolved, true, "call should resolve, not time out")
assert.equal(res.json.result.isError, false)
assert.equal(res.json.result.content[0].text, "built")
} finally {
await srv.close()
}
})
// --- question proxy: version gate + description overlay ---------------------
test("question gets a 30-min default deadline (a human has to read the form)", () => {
assert.equal(
resolveProxyCallTimeoutMs("question", undefined, undefined),
30 * MIN,
)
})
test("resolveProxyClientCeilingMs is always a positive, timer-safe value", () => {
// The ceiling is written into Claude's --mcp-config entry. It can never be
// 0 (the CLI rejects the server config) and never above Node's timer max,
// whatever the overrides say.
for (const overrides of [undefined, {}, { task: 0 }, { task: 2 ** 40 }, { bash: 1 }]) {
const ceiling = resolveProxyClientCeilingMs(overrides)
assert.ok(ceiling > 0, `ceiling must be positive for ${JSON.stringify(overrides)}`)
assert.ok(ceiling <= MAX_PROXY_TIMEOUT_MS)
assert.ok(ceiling >= 30 * MIN, "never below the longest positive per-tool default")
}
})
test("tools/call with no deadline stays pending instead of timing out on the next tick", async () => {
// A zero deadline must mean "no timer", not `setTimeout(fn, 0)`: the
// latter rejects the call immediately with "timed out after 0ms".
const srv = await createProxyMcpServer(DEFAULT_PROXY_TOOLS)
try {
let settled: string | null = null
const callReceived = new Promise<void>((resolve) => srv.calls.once("call", () => resolve()))
const response = authedPost(srv, {
jsonrpc: "2.0",
id: "unlimited-1",
method: "tools/call",
params: {
name: "task",
arguments: { description: "x", subagent_type: "general", prompt: "y" },
},
}).then((res) => {
settled = String(res.json.result.content[0].text)
return res
})
await callReceived
await new Promise((r) => setTimeout(r, 100))
assert.equal(settled, null, "an unlimited call must not be rejected by a timer")
// Closing the server is one of the lifecycle events that releases it.
await srv.close()
const res = await response
assert.equal(res.json.id, "unlimited-1")
assert.equal(res.json.result.isError, true)
assert.equal(res.json.result.content[0].text, SERVER_CLOSED_MESSAGE)
} finally {
await srv.close()
}
})
// --- JSON-only long calls -----------------------------------------------------
//
// Claude's MCP client used to abandon a silent JSON reply at its own HTTP
// timers (~300 s) whatever the per-tool deadline said. SSE clients got
// immediate headers and keepalive comments in 0.15.0; a client that only
// accepts JSON now gets the same liveness as a chunked JSON body.
/** POST and hand back the response as soon as its headers arrive. */
function openPost(
srv: ProxyMcpServer,
body: unknown,
extraHeaders: Record<string, string> = {},
): Promise<http.IncomingMessage> {
const payload = JSON.stringify(body)
return new Promise((resolve, reject) => {
const req = http.request(
srv.url,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payload).toString(),
Authorization: `Bearer ${srv.authToken}`,
...extraHeaders,
},
},
resolve,
)
req.on("error", reject)
req.end(payload)
})
}
function readAll(res: http.IncomingMessage): Promise<string> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = []
res.on("data", (c: Buffer) => chunks.push(c))
res.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")))
res.on("error", reject)
})
}
test("a JSON-only tools/call gets its headers and keepalive whitespace before the result", async () => {
const srv = await createProxyMcpServer(DEFAULT_PROXY_TOOLS, undefined, undefined, { keepaliveMs: 20 })
try {
let call: ProxyToolCall | undefined
srv.calls.once("call", (c: ProxyToolCall) => { call = c })
const res = await openPost(srv, {
jsonrpc: "2.0",
id: "json-keepalive-1",
method: "tools/call",
params: { name: "bash", arguments: { command: "sleep 600" } },
})
// Headers are in hand while the call is still pending, with no
// Content-Length: chunked is what lets whitespace precede the result.
assert.equal(res.statusCode, 200)
assert.match(String(res.headers["content-type"]), /^application\/json/)
assert.equal(res.headers["content-length"], undefined)
assert.equal(res.headers["transfer-encoding"], "chunked")
assert.ok(call, "the call reached the broker before the reply finished")
const chunks: string[] = []
res.setEncoding("utf8")
res.on("data", (chunk: string) => chunks.push(chunk))
await new Promise((r) => setTimeout(r, 90))
assert.ok(
chunks.length > 0 && chunks.every((chunk) => chunk.trim() === ""),
`expected only keepalive whitespace before the result, got ${JSON.stringify(chunks)}`,
)
call!.resolve({ kind: "text", text: "late but fine" })
await new Promise<void>((resolve) => res.once("end", resolve))
const parsed = JSON.parse(chunks.join(""))
assert.equal(parsed.id, "json-keepalive-1")
assert.equal(parsed.result.isError, false)
assert.equal(parsed.result.content[0].text, "late but fine")
} finally {
await srv.close()
}
})
test("a JSON-only tools/call that fails after its headers went out still ends as valid JSON", async () => {
const srv = await createProxyMcpServer(DEFAULT_PROXY_TOOLS, undefined, undefined, { keepaliveMs: 10 })
try {
srv.calls.once("call", (c: ProxyToolCall) => {
setTimeout(() => c.reject(new Error("simulated late broker rejection")), 40)
})
const res = await openPost(srv, {
jsonrpc: "2.0",
id: "json-keepalive-err",
method: "tools/call",
params: { name: "bash", arguments: { command: "false" } },
})
assert.equal(res.statusCode, 200)
const body = await readAll(res)
assert.match(body, /^\s+\{/, "keepalive whitespace precedes the envelope")
const parsed = JSON.parse(body)
assert.equal(parsed.id, "json-keepalive-err")
assert.equal(parsed.error, undefined, "still an MCP result, never a JSON-RPC error envelope")
assert.equal(parsed.result.isError, true)
assert.match(parsed.result.content[0].text, /simulated late broker rejection/)
} finally {
await srv.close()
}
})
test("a JSON-only client that hangs up stops its keepalive and the result is dropped, not thrown", async () => {
const srv = await createProxyMcpServer(DEFAULT_PROXY_TOOLS, undefined, undefined, { keepaliveMs: 10 })
try {
const callReceived = new Promise<ProxyToolCall>((resolve) => srv.calls.once("call", resolve))
const res = await openPost(srv, {
jsonrpc: "2.0",
id: "json-keepalive-gone",
method: "tools/call",
params: { name: "bash", arguments: { command: "sleep 600" } },
})
const call = await callReceived
assert.equal(call.channel?.closed, false)
res.destroy()
await new Promise((r) => setTimeout(r, 50))
// The reply channel is what the language model reads before answering;
// the entry itself stays so a late result can still be recovered.
assert.equal(call.channel?.closed, true)
// Resolving now must not throw into the server (no write to a dead socket).
call.resolve({ kind: "text", text: "nobody is listening" })
await new Promise((r) => setTimeout(r, 30))
} finally {
await srv.close()
}
})
test("protocol methods keep the single-shot JSON reply with a Content-Length", async () => {
// Only broker-backed tools/call replies are streamed; initialize and
// tools/list are answered in one write as before.
await withServer(async (srv) => {
for (const body of [
{ jsonrpc: "2.0", id: "init", method: "initialize", params: {} },
{ jsonrpc: "2.0", id: "list", method: "tools/list" },
]) {
const res = await openPost(srv, body)
assert.equal(res.statusCode, 200)
assert.ok(res.headers["content-length"], `${body.method} must carry a Content-Length`)
assert.equal(res.headers["transfer-encoding"], undefined)
const parsed = JSON.parse(await readAll(res))
assert.equal(parsed.id, body.id)
assert.ok(parsed.result)
}
})
})
test("an SSE client still gets the event-stream reply", async () => {
await withServer(async (srv) => {
srv.calls.once("call", (c: ProxyToolCall) => c.resolve({ kind: "text", text: "over sse" }))
const res = await openPost(
srv,
{ jsonrpc: "2.0", id: "sse-1", method: "tools/call", params: { name: "bash", arguments: { command: "true" } } },
{ Accept: "application/json, text/event-stream" },
)
assert.match(String(res.headers["content-type"]), /^text\/event-stream/)
const body = await readAll(res)
const data = body.split("\n").find((line) => line.startsWith("data: "))
assert.ok(data, "SSE reply carries the JSON-RPC result as a data line")
assert.equal(JSON.parse(data!.slice(6)).result.content[0].text, "over sse")
})
})
test("filterQuestionProxyByOpencodeSupport drops the def on older opencode", () => {
const tools = DEFAULT_PROXY_TOOLS
assert.ok(tools.some((t) => t.name === "question"))
const kept = filterQuestionProxyByOpencodeSupport(tools, true)
assert.ok(kept.some((t) => t.name === "question"))
const dropped = filterQuestionProxyByOpencodeSupport(tools, false)
assert.equal(
dropped.some((t) => t.name === "question"),
false,
"no registry entry means a forwarded call would render as invalid",
)
// Only `question` is gated; everything else survives untouched.
assert.ok(dropped.some((t) => t.name === "task"))
assert.ok(dropped.some((t) => t.name === "bash"))
})
test("overlayQuestionProxyDescription prefers opencode's live description", () => {
const overlaid = overlayQuestionProxyDescription(
DEFAULT_PROXY_TOOLS,
"LIVE question description from opencode",
)
const question = overlaid.find((t) => t.name === "question")
assert.ok(question)
assert.ok(question.description.startsWith("LIVE question description"))
// The disambiguation note must survive, it is what tells the model the
// built-in AskUserQuestion is disabled.
assert.ok(question.description.includes("AskUserQuestion is disabled"))
})
test("overlayQuestionProxyDescription is a no-op without a live description", () => {
const before = DEFAULT_PROXY_TOOLS.find((t) => t.name === "question")
const after = overlayQuestionProxyDescription(
DEFAULT_PROXY_TOOLS,
undefined,
).find((t) => t.name === "question")
assert.equal(after?.description, before?.description)
})
// ---------------------------------------------------------------------------
// Entry-guard security tests.
//
// This endpoint executes bash/edit/write through opencode's executor, so an
// unauthenticated caller on loopback would have arbitrary command execution
// as the user. These pin every guard in front of the JSON-RPC body parser.
// ---------------------------------------------------------------------------
const LIST_REQ = { jsonrpc: "2.0", id: 1, method: "tools/list" }
function jsonHeaders(
payload: string,
extra: Record<string, string> = {},
): Record<string, string> {
return {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payload).toString(),
...extra,
}
}
test("security: a correctly authenticated request is accepted", async () => {
await withServer(async (srv) => {
const res = await authedPost(srv, LIST_REQ)
assert.equal(res.status, 200)
assert.ok(res.json.result.tools.length > 0)
})
})
test("security: a wrong bearer token of equal length is rejected with 401", async () => {
await withServer(async (srv) => {
// Same length as the real token, so this exercises timingSafeEqual
// rather than the cheap length short-circuit in front of it.
const forged = "0".repeat(srv.authToken.length)
assert.equal(forged.length, srv.authToken.length)
const payload = JSON.stringify(LIST_REQ)
const res = await post(srv.url, LIST_REQ, {
headers: jsonHeaders(payload, { Authorization: `Bearer ${forged}` }),
})
assert.equal(res.status, 401)
})
})
test("security: a short/garbage bearer token is rejected with 401", async () => {
await withServer(async (srv) => {
const payload = JSON.stringify(LIST_REQ)
const res = await post(srv.url, LIST_REQ, {
headers: jsonHeaders(payload, { Authorization: "Bearer nope" }),
})
assert.equal(res.status, 401)
})
})
test("security: an absent Authorization header is rejected with 401", async () => {
await withServer(async (srv) => {
const payload = JSON.stringify(LIST_REQ)
const res = await post(srv.url, LIST_REQ, { headers: jsonHeaders(payload) })
assert.equal(res.status, 401)
})
})
test("security: a foreign Host header is rejected with 403 (DNS rebinding)", async () => {
await withServer(async (srv) => {
const payload = JSON.stringify(LIST_REQ)
const res = await post(srv.url, LIST_REQ, {
headers: jsonHeaders(payload, {
Host: "attacker.example",
Authorization: `Bearer ${srv.authToken}`,
}),
})
assert.equal(res.status, 403)
})
})
test("security: any Origin header is rejected with 403 (browser context)", async () => {
await withServer(async (srv) => {
const payload = JSON.stringify(LIST_REQ)
const res = await post(srv.url, LIST_REQ, {
headers: jsonHeaders(payload, {
Origin: "https://attacker.example",
Authorization: `Bearer ${srv.authToken}`,
}),
})
assert.equal(res.status, 403)
})
})
test("security: text/plain is rejected with 415 (CORS simple-request bypass)", async () => {
await withServer(async (srv) => {
// text/plain is a CORS "simple request" content type, so a cross-origin
// page can send it with no preflight. Requiring application/json forces
// a preflight that then fails.
const payload = JSON.stringify(LIST_REQ)
const res = await post(srv.url, LIST_REQ, {
headers: {
"Content-Type": "text/plain",
"Content-Length": Buffer.byteLength(payload).toString(),
Authorization: `Bearer ${srv.authToken}`,
},
})
assert.equal(res.status, 415)
})
})
test("security: a Content-Type with charset parameters is still accepted", async () => {
await withServer(async (srv) => {
const payload = JSON.stringify(LIST_REQ)
const res = await post(srv.url, LIST_REQ, {
headers: jsonHeaders(payload, {
"Content-Type": "application/json; charset=utf-8",
Authorization: `Bearer ${srv.authToken}`,
}),
})
assert.equal(res.status, 200)
})
})
test("security: the 401 path answers without reading the request body", async () => {
await withServer(async (srv) => {
const status = await new Promise<number>((resolve, reject) => {
const req = http.request(
srv.url,
{
method: "POST",
headers: {
"Content-Type": "application/json",
// Declare a large body that we never finish sending, and send
// no Authorization. If the handler read the body before
// authenticating it would block here and no response would
// ever arrive.
"Content-Length": "10000000",
},
},
(res) => {
clearTimeout(timer)
res.resume()
resolve(res.statusCode ?? 0)
req.destroy()
},
)
const timer = setTimeout(() => {
req.destroy()
reject(
new Error(
"no response while the body was still incomplete — the handler appears to read the body before authenticating",
),
)
}, 5000)
req.on("error", () => {})
req.write("{") // one byte; req.end() is deliberately never called
})
assert.equal(status, 401)
})
})
test("security: the generated MCP config carries the token, 0600, and never in the URL", async () => {
await withServer(async (srv) => {
const cfgPath = srv.configPath()
const cfg = JSON.parse(fs.readFileSync(cfgPath, "utf8"))
const entry = cfg.mcpServers[srv.serverName]
assert.equal(entry.type, "http")
assert.equal(entry.headers.Authorization, `Bearer ${srv.authToken}`)
// The file now holds a secret, so its mode is load-bearing -- ON POSIX.
// Node does not implement owner/group/other mode bits on Windows, where
// this commonly reads back 0o666 and confidentiality instead depends on
// the inherited ACL of os.tmpdir(). Asserting 0o600 there would be a
// test that cannot pass, and claiming it in the README would be a
// guarantee we do not provide.
if (process.platform !== "win32") {
assert.equal(fs.statSync(cfgPath).mode & 0o777, 0o600)
}
// A token in the URL would leak into logs and process listings.
assert.ok(!srv.url.includes(srv.authToken))
assert.ok(!entry.url.includes(srv.authToken))
})
})
// A rejected request must not leave the connection usable. Without an
// explicit close, a peer can declare a large Content-Length, send one byte,
// take the 401, and hold the socket -- and `server.close()` does NOT reap
// connections that are still sending, so shutdown would block behind an
// unauthenticated caller for Node's five-minute request timeout.
//
// This test deliberately never finishes the body. An earlier version of the
// suite masked the defect by destroying the socket client-side as soon as the
// response arrived, which is exactly the cleanup the server must not depend on.
test("security: rejecting an unauthenticated request does not leave shutdown hostage to an unfinished body", async () => {
const net = await import("node:net")
const srv = await createProxyMcpServer(DEFAULT_PROXY_TOOLS)
const { port } = new URL(srv.url)
const sock = net.connect({ host: "127.0.0.1", port: Number(port) })
await new Promise<void>((resolve) => sock.once("connect", () => resolve()))
// Announce a large body, then send a single byte and stop.
sock.write(
"POST /mcp HTTP/1.1\r\n" +
`Host: 127.0.0.1:${port}\r\n` +
"Content-Type: application/json\r\n" +
"Content-Length: 1048576\r\n" +
"\r\n" +
"{",
)
const status = await new Promise<string>((resolve) => {
sock.once("data", (chunk) => resolve(chunk.toString("utf8").split("\r\n")[0]))
})
assert.match(status, /401/, "the unauthenticated request should be rejected")
// The body is still unfinished here, on purpose. close() must not hang.
const closed = srv.close().then(() => "closed" as const)
const timedOut = new Promise<"hung">((resolve) =>
setTimeout(() => resolve("hung"), 4000).unref(),
)
assert.equal(await Promise.race([closed, timedOut]), "closed")
sock.destroy()
})
test("security: a client using only the generated config's header is accepted (round-trip)", async () => {
await withServer(async (srv) => {
// Proves config generation and request validation agree: read the
// header out of the file Claude is handed, and use nothing else.
const cfg = JSON.parse(fs.readFileSync(srv.configPath(), "utf8"))
const auth = cfg.mcpServers[srv.serverName].headers.Authorization
const payload = JSON.stringify(LIST_REQ)
const res = await post(srv.url, LIST_REQ, {
headers: jsonHeaders(payload, { Authorization: auth }),
})
assert.equal(res.status, 200)
assert.ok(res.json.result.tools.length > 0)
})
})
test("security: two servers get distinct tokens, and one's token is rejected by the other", async () => {
const a = await createProxyMcpServer(DEFAULT_PROXY_TOOLS)
const b = await createProxyMcpServer(DEFAULT_PROXY_TOOLS)
try {
assert.notEqual(a.authToken, b.authToken)
const payload = JSON.stringify(LIST_REQ)
const res = await post(b.url, LIST_REQ, {
headers: jsonHeaders(payload, { Authorization: `Bearer ${a.authToken}` }),
})
assert.equal(res.status, 401)
} finally {
await a.close()
await b.close()
}
})
// ---------------------------------------------------------------------------
// SSE reply channel. Claude Code's MCP client aborts a tools/call request
// that has produced no bytes for about five minutes (measured on 2.1.258),
// which is how a long `task` ended up answered to a client that had already
// given up. A client that accepts text/event-stream must get headers and a
// first byte immediately and the JSON-RPC result as the final event.
// ---------------------------------------------------------------------------
type SseCapture = {
status: number
contentType: string
chunks: Array<{ at: number; text: string }>
done: Promise<void>
destroy(): void
}
function openSse(srv: ProxyMcpServer, body: unknown): Promise<SseCapture> {
const payload = JSON.stringify(body)
return new Promise((resolve, reject) => {
const req = http.request(
srv.url,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payload).toString(),
Accept: "application/json, text/event-stream",
Authorization: `Bearer ${srv.authToken}`,