@hop-top/kit 0.5.0-alpha.1 → 0.5.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +251 -0
- package/LICENSE +1 -1
- package/LICENSE-cobra.txt +174 -0
- package/NOTICE +15 -0
- package/README.md +55 -259
- package/dist/aim-DZHwwiIc.d.ts +139 -0
- package/dist/aim.d.ts +1 -116
- package/dist/aim.js +13 -414
- package/dist/alias-D9_qFlyP.d.ts +174 -0
- package/dist/alias.d.ts +2 -73
- package/dist/alias.js +27 -207
- package/dist/api-DAexsu2H.d.ts +77 -0
- package/dist/api.d.ts +1 -62
- package/dist/api.js +10 -164
- package/dist/auth-2-jfgiHn.d.ts +34 -0
- package/dist/auth.d.ts +1 -26
- package/dist/auth.js +6 -40
- package/dist/chunk-2JV3A7OP.js +1894 -0
- package/dist/chunk-3FIKGFKI.js +54 -0
- package/dist/chunk-4UE5TCMQ.js +104 -0
- package/dist/chunk-6HFAEZ52.js +29 -0
- package/dist/chunk-6SVOHNIC.js +17 -0
- package/dist/chunk-72XZWQX7.js +504 -0
- package/dist/chunk-ABQSE7AI.js +513 -0
- package/dist/chunk-BDNNPRX6.js +41 -0
- package/dist/chunk-BLS2OBIQ.js +263 -0
- package/dist/chunk-BRNEKPWX.js +37 -0
- package/dist/chunk-CSJWB6D3.js +151 -0
- package/dist/chunk-GGWJRWWA.js +309 -0
- package/dist/chunk-GHPJJV5L.js +613 -0
- package/dist/chunk-IXVQ62NZ.js +334 -0
- package/dist/chunk-JKMQSAJP.js +174 -0
- package/dist/chunk-JZ22ZUFA.js +1131 -0
- package/dist/chunk-K7VCKJ27.js +814 -0
- package/dist/chunk-KVG7YCTS.js +115 -0
- package/dist/chunk-L23O2FFE.js +51 -0
- package/dist/chunk-MQXYDYRD.js +968 -0
- package/dist/chunk-MUIQVU6E.js +251 -0
- package/dist/chunk-NLMS7HGP.js +178 -0
- package/dist/chunk-POR5L2DG.js +247 -0
- package/dist/chunk-PSFYJGDG.js +1445 -0
- package/dist/chunk-QJVSKKRQ.js +369 -0
- package/dist/chunk-QPUCXQXT.js +67 -0
- package/dist/chunk-QQQYCWSC.js +240 -0
- package/dist/chunk-R5U7XKVJ.js +16 -0
- package/dist/chunk-SNVGDYX7.js +187 -0
- package/dist/chunk-T46YPHKN.js +669 -0
- package/dist/chunk-UKZIAQA7.js +26 -0
- package/dist/chunk-WBVO43HU.js +355 -0
- package/dist/chunk-WUOAQVOA.js +86 -0
- package/dist/chunk-WWUKGJB5.js +90 -0
- package/dist/chunk-XJTRY453.js +868 -0
- package/dist/chunk-YGYQUFGY.js +219 -0
- package/dist/chunk-YOBAUCVN.js +583 -0
- package/dist/cli-C5B5tnpA.d.ts +400 -0
- package/dist/cli.d.ts +7 -309
- package/dist/cli.js +70 -945
- package/dist/completion-csVun6SP.d.ts +112 -0
- package/dist/completion-values-By-mnjNt.d.ts +142 -0
- package/dist/completion-values.d.ts +1 -0
- package/dist/completion-values.js +29 -0
- package/dist/completion.d.ts +3 -0
- package/dist/completion.js +34 -0
- package/dist/compliance-AxFf-uMy.d.ts +60 -0
- package/dist/compliance.d.ts +1 -0
- package/dist/compliance.js +17 -0
- package/dist/config-CaK60ByQ.d.ts +241 -0
- package/dist/config.d.ts +1 -97
- package/dist/config.js +8 -78
- package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
- package/dist/errcorrect.d.ts +1 -37
- package/dist/errcorrect.js +8 -68
- package/dist/error-Do8Qji43.d.ts +231 -0
- package/dist/id/index.d.ts +2 -83
- package/dist/id/index.js +14 -65
- package/dist/index-BKJZClJx.d.ts +1486 -0
- package/dist/index-C3Zw4wDJ.d.ts +359 -0
- package/dist/index-DCe1boBz.d.ts +328 -0
- package/dist/index-VLDcI5NF.d.ts +95 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +132 -0
- package/dist/llm-DYuR7_uX.d.ts +1069 -0
- package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
- package/dist/llm-anthropic.d.ts +2 -0
- package/dist/llm-anthropic.js +13 -0
- package/dist/llm-openai-DyFrucmV.d.ts +87 -0
- package/dist/llm-openai.d.ts +2 -0
- package/dist/llm-openai.js +19 -0
- package/dist/llm.d.ts +1 -188
- package/dist/llm.js +101 -356
- package/dist/log-CkaxMX3U.d.ts +115 -0
- package/dist/log.d.ts +2 -0
- package/dist/log.js +14 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/index.js +184 -0
- package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
- package/dist/netpolicy.d.ts +1 -0
- package/dist/netpolicy.js +25 -0
- package/dist/output-CjZWmL8s.d.ts +420 -0
- package/dist/output.d.ts +3 -213
- package/dist/output.js +137 -447
- package/dist/parity-CvMuYcVI.d.ts +61 -0
- package/dist/progress-CAzyxeWl.d.ts +38 -0
- package/dist/progress.d.ts +1 -30
- package/dist/progress.js +6 -55
- package/dist/provenance-SMFHm5gx.d.ts +30 -0
- package/dist/provenance.d.ts +1 -24
- package/dist/provenance.js +6 -31
- package/dist/routellm-B0nv59mH.d.ts +111 -0
- package/dist/routellm.d.ts +2 -64
- package/dist/routellm.js +13 -255
- package/dist/rpc-D31Lg0Kc.d.ts +32 -0
- package/dist/rpc.d.ts +2 -25
- package/dist/rpc.js +4 -98
- package/dist/safety-CUaYEjZc.d.ts +30 -0
- package/dist/safety.d.ts +1 -24
- package/dist/safety.js +6 -43
- package/dist/scope-BrhYB4ij.d.ts +141 -0
- package/dist/scope.d.ts +1 -121
- package/dist/scope.js +20 -377
- package/dist/serve-D8yWMrTc.d.ts +646 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +75 -0
- package/dist/sqlstore.js +6 -39
- package/dist/stream-CrjeCp0M.d.ts +44 -0
- package/dist/stream.d.ts +1 -35
- package/dist/stream.js +8 -51
- package/dist/telemetry/index.d.ts +1 -315
- package/dist/telemetry/index.js +26 -599
- package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
- package/dist/toolspec.d.ts +2 -0
- package/dist/toolspec.js +37 -0
- package/dist/tui/index.d.ts +6 -298
- package/dist/tui/index.js +17 -381
- package/dist/upgrade-p_zh3pvV.d.ts +102 -0
- package/dist/upgrade.d.ts +1 -95
- package/dist/upgrade.js +6 -114
- package/dist/xdg-DB7Shoib.d.ts +179 -0
- package/dist/xdg.d.ts +1 -169
- package/dist/xdg.js +14 -128
- package/package.json +192 -24
- package/src/README.md +15 -0
- package/src/alias.conformance.test.ts +250 -0
- package/src/alias.test.ts +206 -2
- package/src/alias.ts +196 -21
- package/src/cli.help-command.test.ts +105 -0
- package/src/cli.offline.test.ts +93 -0
- package/src/cli.parity.test.ts +192 -0
- package/src/cli.test.ts +95 -1
- package/src/cli.ts +195 -25
- package/src/completion-scripts.ts +16 -0
- package/src/completion-values.test.ts +76 -16
- package/src/completion-values.ts +170 -66
- package/src/completion.conformance.test.ts +297 -0
- package/src/completion.test.ts +256 -145
- package/src/completion.ts +767 -121
- package/src/compliance.test.ts +196 -2
- package/src/compliance.ts +440 -20
- package/src/config-merge.test.ts +425 -0
- package/src/config.test.ts +228 -1
- package/src/config.ts +429 -16
- package/src/gen/crud_pb.ts +7 -7
- package/src/id/README.md +38 -0
- package/src/id/contract.test.ts +1 -1
- package/src/id/index.test.ts +1 -1
- package/src/id/index.ts +3 -2
- package/src/index.test.ts +71 -0
- package/src/index.ts +46 -0
- package/src/llm-anthropic.conformance.test.ts +1135 -0
- package/src/llm-anthropic.test.ts +152 -0
- package/src/llm-anthropic.ts +940 -0
- package/src/llm-choice-hermetic.test.ts +335 -0
- package/src/llm-config.test.ts +232 -0
- package/src/llm-config.ts +106 -0
- package/src/llm-endpoint.ts +254 -0
- package/src/llm-error.ts +17 -0
- package/src/llm-errors.ts +152 -0
- package/src/llm-file.ts +126 -0
- package/src/llm-http.ts +317 -0
- package/src/llm-keys.ts +167 -0
- package/src/llm-media.conformance.test.ts +87 -0
- package/src/llm-media.test.ts +279 -0
- package/src/llm-media.ts +322 -0
- package/src/llm-openai.conformance.test.ts +407 -0
- package/src/llm-openai.test.ts +510 -0
- package/src/llm-openai.ts +708 -0
- package/src/llm-payload.ts +155 -0
- package/src/llm-quote.ts +46 -0
- package/src/llm-routellm.conformance.test.ts +44 -0
- package/src/llm-toolnames.conformance.test.ts +112 -0
- package/src/llm-toolnames.test.ts +132 -0
- package/src/llm-toolnames.ts +298 -0
- package/src/llm-uri.ts +99 -0
- package/src/llm.test.ts +43 -15
- package/src/llm.toolcall.test.ts +175 -0
- package/src/llm.ts +602 -190
- package/src/log.parity.test.ts +105 -0
- package/src/log.test.ts +270 -1
- package/src/log.ts +228 -49
- package/src/mcp/README.md +63 -0
- package/src/mcp/args-separator.test.ts +164 -0
- package/src/mcp/args.test.ts +223 -0
- package/src/mcp/args.ts +160 -0
- package/src/mcp/audit.test.ts +520 -0
- package/src/mcp/audit.ts +70 -0
- package/src/mcp/bridge.test.ts +222 -0
- package/src/mcp/bridge.ts +291 -0
- package/src/mcp/conformance.test.ts +357 -0
- package/src/mcp/dispatch.ts +359 -0
- package/src/mcp/identity.test.ts +166 -0
- package/src/mcp/identity.ts +72 -0
- package/src/mcp/index.ts +176 -0
- package/src/mcp/legacy-notify.test.ts +59 -0
- package/src/mcp/legacy.ts +367 -0
- package/src/mcp/mcp.test.ts +707 -0
- package/src/mcp/modern-confirm.test.ts +520 -0
- package/src/mcp/modern-confirm.ts +484 -0
- package/src/mcp/modern.ts +788 -0
- package/src/mcp/stdio.test.ts +313 -0
- package/src/mcp/stdio.ts +254 -0
- package/src/mcp/tasks.ts +82 -0
- package/src/mcp/testtree.ts +250 -0
- package/src/mcp/toolname.test.ts +147 -0
- package/src/mcp/types.ts +689 -0
- package/src/netpolicy.test.ts +280 -0
- package/src/netpolicy.ts +256 -0
- package/src/output/README.md +46 -0
- package/src/output/builtins.ts +5 -5
- package/src/output/dispatch.ts +10 -2
- package/src/output/error.ts +459 -0
- package/src/output/formatter.ts +29 -5
- package/src/output/formatters/README.md +50 -0
- package/src/output/formatters/csv.ts +48 -12
- package/src/output/formatters/json.ts +4 -25
- package/src/output/formatters/project.ts +34 -0
- package/src/output/formatters/table.ts +8 -6
- package/src/output/formatters/text.ts +7 -3
- package/src/output/formatters/yaml.ts +5 -22
- package/src/output/projection.test.ts +298 -0
- package/src/output/projection.ts +33 -59
- package/src/output/template.ts +11 -21
- package/src/output.surface.test.ts +41 -0
- package/src/output.ts +64 -3
- package/src/published-types.test.ts +133 -0
- package/src/routellm.test.ts +209 -28
- package/src/routellm.toolcall.test.ts +521 -0
- package/src/routellm.ts +213 -177
- package/src/router/README.md +44 -0
- package/src/router/server.test.ts +41 -0
- package/src/router/server.ts +11 -0
- package/src/safety.test.ts +19 -9
- package/src/scope-defaults.json +29 -29
- package/src/serve.test.ts +1257 -0
- package/src/serve.ts +1665 -0
- package/src/telemetry/README.md +48 -0
- package/src/telemetry/client.test.ts +31 -0
- package/src/telemetry/client.ts +12 -6
- package/src/telemetry/offline.test.ts +64 -0
- package/src/toolspec-internal.ts +279 -0
- package/src/toolspec-manifest.test.ts +167 -0
- package/src/toolspec-manifest.ts +410 -0
- package/src/toolspec-spec-schemas.ts +12 -0
- package/src/toolspec-spec.test.ts +273 -0
- package/src/toolspec-spec.ts +458 -0
- package/src/toolspec.from-command.test.ts +350 -0
- package/src/toolspec.test.ts +2 -2
- package/src/toolspec.ts +222 -0
- package/src/triton/README.md +34 -0
- package/src/tui/README.md +42 -0
- package/src/tui/parity.test.ts +120 -31
- package/src/tui/parity.ts +58 -7
- package/src/wizard.test.ts +7 -7
- package/test/llm/conformance-helpers.ts +100 -0
- package/test/output/csv.test.ts +164 -0
- package/test/output/dispatch.test.ts +1 -2
- package/test/output/error.test.ts +277 -0
- package/test/output/smoke-capture.ts +2 -2
- package/test/output/smoke.test.ts +2 -2
- package/test/output/taxonomy-contract.test.ts +148 -0
- package/test/setup-env.ts +16 -0
- package/test/toolspec/fixture.ts +79 -0
- package/tsconfig.json +3 -1
- package/tsconfig.test.json +10 -0
- package/vitest.config.ts +9 -0
- package/src/tui/parity.json +0 -74
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The stdio binding: newline framing, notifications never answered,
|
|
3
|
+
* ping, parse / batch refusals, concurrent requests, both eras with no
|
|
4
|
+
* header layer, and a clean end of session.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { PassThrough } from 'node:stream';
|
|
8
|
+
import { describe, expect, it } from 'vitest';
|
|
9
|
+
|
|
10
|
+
import { createMcpHandler, type McpHandler } from './dispatch.js';
|
|
11
|
+
import { serveStdio, type McpStdioServer } from './stdio.js';
|
|
12
|
+
import { legacyLockBridge, mrtrLockBridge } from './testtree.js';
|
|
13
|
+
import type { McpBridge, McpMountOptions } from './types.js';
|
|
14
|
+
|
|
15
|
+
const MODERN_META =
|
|
16
|
+
'"_meta":{"io.modelcontextprotocol/clientCapabilities":{},' +
|
|
17
|
+
'"io.modelcontextprotocol/protocolVersion":"2026-07-28"}';
|
|
18
|
+
|
|
19
|
+
interface Session {
|
|
20
|
+
server: McpStdioServer;
|
|
21
|
+
input: PassThrough;
|
|
22
|
+
/** Every line written to the output stream, in order. */
|
|
23
|
+
lines: () => string[];
|
|
24
|
+
/** Everything written to the log stream. */
|
|
25
|
+
log: () => string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function session(
|
|
29
|
+
target: McpBridge | McpHandler,
|
|
30
|
+
mount?: McpMountOptions,
|
|
31
|
+
): Session {
|
|
32
|
+
const input = new PassThrough();
|
|
33
|
+
const output = new PassThrough();
|
|
34
|
+
const log = new PassThrough();
|
|
35
|
+
let out = '';
|
|
36
|
+
let err = '';
|
|
37
|
+
output.on('data', (c: Buffer) => (out += c.toString('utf8')));
|
|
38
|
+
log.on('data', (c: Buffer) => (err += c.toString('utf8')));
|
|
39
|
+
const server = serveStdio(target, { input, output, log, mount });
|
|
40
|
+
return {
|
|
41
|
+
server,
|
|
42
|
+
input,
|
|
43
|
+
lines: () => out.split('\n').filter((l) => l !== ''),
|
|
44
|
+
log: () => err,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Writes each message as one line, ends input, waits for the session. */
|
|
49
|
+
async function run(s: Session, ...messages: string[]): Promise<string[]> {
|
|
50
|
+
for (const m of messages) s.input.write(`${m}\n`);
|
|
51
|
+
s.input.end();
|
|
52
|
+
await s.server.done;
|
|
53
|
+
return s.lines();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function byId(lines: string[]): Map<unknown, Record<string, unknown>> {
|
|
57
|
+
const out = new Map<unknown, Record<string, unknown>>();
|
|
58
|
+
for (const l of lines) {
|
|
59
|
+
const m = JSON.parse(l) as Record<string, unknown>;
|
|
60
|
+
out.set(m.id, m);
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A bridge whose `slow` leaf answers only after `fast` has. */
|
|
66
|
+
function orderedBridge(): McpBridge {
|
|
67
|
+
let releaseSlow: () => void = () => {};
|
|
68
|
+
const fastDone = new Promise<void>((r) => (releaseSlow = r));
|
|
69
|
+
return {
|
|
70
|
+
leaves: () => [
|
|
71
|
+
{ path: ['slow'], description: 'Slow', properties: {} },
|
|
72
|
+
{ path: ['fast'], description: 'Fast', properties: {} },
|
|
73
|
+
],
|
|
74
|
+
async invoke(inv) {
|
|
75
|
+
if (inv.path[0] === 'slow') {
|
|
76
|
+
await fastDone;
|
|
77
|
+
return { stdout: 'slow\n', exitCode: 0 };
|
|
78
|
+
}
|
|
79
|
+
setTimeout(releaseSlow, 5);
|
|
80
|
+
return { stdout: 'fast\n', exitCode: 0 };
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
describe('serveStdio', () => {
|
|
86
|
+
it('runs the legacy handshake and never answers a notification', async () => {
|
|
87
|
+
const lines = await run(
|
|
88
|
+
session(legacyLockBridge()),
|
|
89
|
+
'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"pipe","version":"0"}}}',
|
|
90
|
+
'{"jsonrpc":"2.0","method":"notifications/initialized"}',
|
|
91
|
+
'{"jsonrpc":"2.0","id":2,"method":"tools/list"}',
|
|
92
|
+
'{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"ping"}}',
|
|
93
|
+
);
|
|
94
|
+
expect(lines).toHaveLength(3);
|
|
95
|
+
const got = byId(lines);
|
|
96
|
+
expect([...got.keys()].sort()).toEqual([1, 2, 3]);
|
|
97
|
+
expect(got.get(1)?.result).toMatchObject({ protocolVersion: '2024-11-05' });
|
|
98
|
+
expect(JSON.stringify(got.get(3)?.result)).toContain('pong');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('answers ping with an empty result', async () => {
|
|
102
|
+
const lines = await run(session(legacyLockBridge()), '{"jsonrpc":"2.0","id":"p1","method":"ping"}');
|
|
103
|
+
expect(lines).toEqual(['{"jsonrpc":"2.0","id":"p1","result":{}}']);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('drops every notification, even a malformed or failing one', async () => {
|
|
107
|
+
const lines = await run(
|
|
108
|
+
session(legacyLockBridge()),
|
|
109
|
+
'{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":1}}',
|
|
110
|
+
'{"jsonrpc":"1.0","method":"notifications/initialized"}',
|
|
111
|
+
'{"jsonrpc":"2.0","method":"tools/call","params":{"name":"ping"}}',
|
|
112
|
+
'{"jsonrpc":"2.0","method":"nope"}',
|
|
113
|
+
);
|
|
114
|
+
expect(lines).toEqual([]);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('answers unparseable input with -32700 and id null', async () => {
|
|
118
|
+
const lines = await run(session(legacyLockBridge()), '{not json');
|
|
119
|
+
expect(lines).toEqual([
|
|
120
|
+
'{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"parse error: invalid character \'n\' looking for beginning of object key string"}}',
|
|
121
|
+
]);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('refuses a batch and a non-object message with -32600 and id null', async () => {
|
|
125
|
+
const lines = await run(
|
|
126
|
+
session(legacyLockBridge()),
|
|
127
|
+
'[{"jsonrpc":"2.0","id":1,"method":"ping"}]',
|
|
128
|
+
'42',
|
|
129
|
+
);
|
|
130
|
+
expect(lines).toEqual([
|
|
131
|
+
'{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request: batch requests are not supported"}}',
|
|
132
|
+
'{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request: not a JSON-RPC object"}}',
|
|
133
|
+
]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('answers a request without a method with -32600 and its id', async () => {
|
|
137
|
+
const lines = await run(session(legacyLockBridge()), '{"jsonrpc":"2.0","id":9}');
|
|
138
|
+
expect(lines).toEqual([
|
|
139
|
+
'{"jsonrpc":"2.0","id":9,"error":{"code":-32600,"message":"invalid request: missing method"}}',
|
|
140
|
+
]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('ignores a client response on stdout and logs it to the log stream', async () => {
|
|
144
|
+
const s = session(legacyLockBridge());
|
|
145
|
+
const lines = await run(s, '{"jsonrpc":"2.0","id":1,"result":{}}');
|
|
146
|
+
expect(lines).toEqual([]);
|
|
147
|
+
expect(s.log()).toContain('ignored a JSON-RPC response');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('skips blank lines and accepts CRLF framing', async () => {
|
|
151
|
+
const s = session(legacyLockBridge());
|
|
152
|
+
s.input.write('\n \n{"jsonrpc":"2.0","id":1,"method":"ping"}\r\n');
|
|
153
|
+
s.input.end();
|
|
154
|
+
await s.server.done;
|
|
155
|
+
expect(s.lines()).toEqual(['{"jsonrpc":"2.0","id":1,"result":{}}']);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('runs requests concurrently; replies arrive out of order, each with its id', async () => {
|
|
159
|
+
const lines = await run(
|
|
160
|
+
session(orderedBridge()),
|
|
161
|
+
'{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"slow"}}',
|
|
162
|
+
'{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"fast"}}',
|
|
163
|
+
);
|
|
164
|
+
expect(lines.map((l) => (JSON.parse(l) as { id: number }).id)).toEqual([2, 1]);
|
|
165
|
+
expect(lines[0]).toContain('fast');
|
|
166
|
+
expect(lines[1]).toContain('slow');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('serves 2026-07-28 from the body alone: no header layer on stdio', async () => {
|
|
170
|
+
const lines = await run(
|
|
171
|
+
session(legacyLockBridge()),
|
|
172
|
+
`{"jsonrpc":"2.0","id":1,"method":"server/discover","params":{${MODERN_META}}}`,
|
|
173
|
+
`{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"ping",${MODERN_META}}}`,
|
|
174
|
+
`{"jsonrpc":"2.0","id":3,"method":"ping","params":{${MODERN_META}}}`,
|
|
175
|
+
);
|
|
176
|
+
const got = byId(lines);
|
|
177
|
+
expect(got.get(1)?.result).toMatchObject({
|
|
178
|
+
resultType: 'complete',
|
|
179
|
+
supportedVersions: ['2026-07-28'],
|
|
180
|
+
});
|
|
181
|
+
expect(got.get(2)?.result).toMatchObject({ resultType: 'complete', isError: false });
|
|
182
|
+
// 2026-07-28 removed ping: a modern ping is an unknown method.
|
|
183
|
+
expect(got.get(3)?.error).toMatchObject({ code: -32601 });
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('keeps the remaining modern validation (unsupported version)', async () => {
|
|
187
|
+
const lines = await run(
|
|
188
|
+
session(legacyLockBridge()),
|
|
189
|
+
'{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/protocolVersion":"2099-01-01"}}}',
|
|
190
|
+
);
|
|
191
|
+
expect(JSON.parse(lines[0] ?? '{}')).toMatchObject({ id: 1, error: { code: -32022 } });
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('reuses a createMcpHandler handler, mount options included', async () => {
|
|
195
|
+
const handler = createMcpHandler(legacyLockBridge(), {
|
|
196
|
+
serverInfo: { name: 'stdio-tool', version: '9.9.9' },
|
|
197
|
+
});
|
|
198
|
+
const lines = await run(
|
|
199
|
+
session(handler),
|
|
200
|
+
'{"jsonrpc":"2.0","id":1,"method":"initialize"}',
|
|
201
|
+
`{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{${MODERN_META}}}`,
|
|
202
|
+
);
|
|
203
|
+
const got = byId(lines);
|
|
204
|
+
expect(got.get(1)?.result).toMatchObject({ serverInfo: { name: 'stdio-tool', version: '9.9.9' } });
|
|
205
|
+
expect(got.get(2)?.result).toMatchObject({ resultType: 'complete' });
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('applies mount options when serving a bridge', async () => {
|
|
209
|
+
const lines = await run(
|
|
210
|
+
session(legacyLockBridge(), { specVersions: ['2026-07-28'] }),
|
|
211
|
+
'{"jsonrpc":"2.0","id":1,"method":"initialize"}',
|
|
212
|
+
);
|
|
213
|
+
expect(lines[0]).toContain('supported protocol versions: 2026-07-28');
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('refuses mount options alongside a handler', () => {
|
|
217
|
+
expect(() =>
|
|
218
|
+
serveStdio(createMcpHandler(legacyLockBridge()), {
|
|
219
|
+
input: new PassThrough(),
|
|
220
|
+
output: new PassThrough(),
|
|
221
|
+
mount: {},
|
|
222
|
+
}),
|
|
223
|
+
).toThrow(TypeError);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('drives a foreign handler as an HTTP-shaped POST', async () => {
|
|
227
|
+
const inner = createMcpHandler(legacyLockBridge());
|
|
228
|
+
const foreign: McpHandler = (req) => inner(req);
|
|
229
|
+
const lines = await run(
|
|
230
|
+
session(foreign),
|
|
231
|
+
'{"jsonrpc":"2.0","id":1,"method":"ping"}',
|
|
232
|
+
'{"jsonrpc":"2.0","method":"notifications/initialized"}',
|
|
233
|
+
);
|
|
234
|
+
expect(lines).toEqual(['{"jsonrpc":"2.0","id":1,"result":{}}']);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('answers -32603 when the handler throws, and logs the cause', async () => {
|
|
238
|
+
const s = session((() => Promise.reject(new Error('boom'))) as McpHandler);
|
|
239
|
+
const lines = await run(s, '{"jsonrpc":"2.0","id":4,"method":"ping"}');
|
|
240
|
+
expect(lines).toEqual(['{"jsonrpc":"2.0","id":4,"error":{"code":-32603,"message":"internal error"}}']);
|
|
241
|
+
expect(s.log()).toContain('boom');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('waits for in-flight requests before done resolves', async () => {
|
|
245
|
+
let release: () => void = () => {};
|
|
246
|
+
const gate = new Promise<void>((r) => (release = r));
|
|
247
|
+
const bridge: McpBridge = {
|
|
248
|
+
leaves: () => [{ path: ['wait'], description: 'Wait', properties: {} }],
|
|
249
|
+
async invoke() {
|
|
250
|
+
await gate;
|
|
251
|
+
return { stdout: 'done\n', exitCode: 0 };
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
const s = session(bridge);
|
|
255
|
+
s.input.write('{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"wait"}}\n');
|
|
256
|
+
s.input.end();
|
|
257
|
+
let finished = false;
|
|
258
|
+
void s.server.done.then(() => (finished = true));
|
|
259
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
260
|
+
expect(finished).toBe(false);
|
|
261
|
+
release();
|
|
262
|
+
await s.server.done;
|
|
263
|
+
expect(s.lines()).toHaveLength(1);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('confirms in-band over MRTR; a client without elicitation is refused', async () => {
|
|
267
|
+
const { bridge, executions } = mrtrLockBridge();
|
|
268
|
+
const s = session(bridge, { confirmationKey: 'stdio-mrtr-suite-shared-secret-32' });
|
|
269
|
+
const meta = (caps: Record<string, unknown>) => ({
|
|
270
|
+
'io.modelcontextprotocol/clientCapabilities': caps,
|
|
271
|
+
'io.modelcontextprotocol/protocolVersion': '2026-07-28',
|
|
272
|
+
});
|
|
273
|
+
const call = (id: number, extra: Record<string, unknown>) =>
|
|
274
|
+
JSON.stringify({ jsonrpc: '2.0', id, method: 'tools/call', params: { name: 'purge', ...extra } });
|
|
275
|
+
const reply = async (n: number) => {
|
|
276
|
+
while (s.lines().length < n) await new Promise((r) => setTimeout(r, 1));
|
|
277
|
+
return JSON.parse(s.lines()[n - 1] ?? '{}') as { id: number; result: Record<string, unknown> };
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
s.input.write(`${call(1, { _meta: meta({ elicitation: { form: {} } }) })}\n`);
|
|
281
|
+
const r1 = await reply(1);
|
|
282
|
+
expect(r1.result.resultType).toBe('input_required');
|
|
283
|
+
expect(Object.keys(r1.result.inputRequests as object)).toEqual(['confirm']);
|
|
284
|
+
expect(executions()).toBe(0);
|
|
285
|
+
|
|
286
|
+
s.input.write(
|
|
287
|
+
`${call(2, {
|
|
288
|
+
_meta: meta({ elicitation: { form: {} } }),
|
|
289
|
+
requestState: r1.result.requestState,
|
|
290
|
+
inputResponses: { confirm: { action: 'accept' } },
|
|
291
|
+
})}\n`,
|
|
292
|
+
);
|
|
293
|
+
const r2 = await reply(2);
|
|
294
|
+
expect(r2.result).toMatchObject({ resultType: 'complete', isError: false });
|
|
295
|
+
expect(executions()).toBe(1);
|
|
296
|
+
|
|
297
|
+
// No elicitation declared: the header gate applies, and stdio has no headers.
|
|
298
|
+
const lines = await run(s, call(3, { _meta: meta({}) }));
|
|
299
|
+
const r3 = JSON.parse(lines[2] ?? '{}') as { result: Record<string, unknown> };
|
|
300
|
+
expect(r3.result).toMatchObject({ resultType: 'complete', isError: true });
|
|
301
|
+
expect(executions()).toBe(1);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it('close() stops reading and resolves once idle', async () => {
|
|
305
|
+
const s = session(legacyLockBridge());
|
|
306
|
+
s.input.write('{"jsonrpc":"2.0","id":1,"method":"ping"}\n');
|
|
307
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
308
|
+
await s.server.close();
|
|
309
|
+
s.input.write('{"jsonrpc":"2.0","id":2,"method":"ping"}\n');
|
|
310
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
311
|
+
expect(s.lines()).toEqual(['{"jsonrpc":"2.0","id":1,"result":{}}']);
|
|
312
|
+
});
|
|
313
|
+
});
|
package/src/mcp/stdio.ts
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module mcp/stdio
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* The stdio binding for the dual-spec MCP surface: newline-delimited
|
|
6
|
+
* JSON-RPC over a pair of byte streams (by default the process's
|
|
7
|
+
* stdin/stdout), driving the same era dispatch the HTTP handler uses.
|
|
8
|
+
*
|
|
9
|
+
* Wire rules (stdio binding, 2024-11-05 and 2026-07-28):
|
|
10
|
+
* - one JSON-RPC message per line; output lines carry no embedded
|
|
11
|
+
* newline, and the output stream carries nothing but protocol
|
|
12
|
+
* messages — diagnostics go to the log stream (stderr);
|
|
13
|
+
* - a notification (no `id` member) is never answered;
|
|
14
|
+
* - each message is a single request: a JSON array (a batch) is
|
|
15
|
+
* refused with -32600 and `id: null`;
|
|
16
|
+
* - a line that is not JSON is answered -32700 with `id: null`;
|
|
17
|
+
* - requests run concurrently, so replies may arrive out of order —
|
|
18
|
+
* each carries its request's id.
|
|
19
|
+
*
|
|
20
|
+
* stdio has no header layer: era detection uses the body markers only
|
|
21
|
+
* (`params._meta` protocolVersion, `server/discover`), and the
|
|
22
|
+
* 2026-07-28 header checks (V4, V6, V7) do not apply. Gates that read
|
|
23
|
+
* headers see none — see the MCP guide for what that means for
|
|
24
|
+
* auth-required and confirmation-gated tools.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { createInterface } from 'node:readline';
|
|
28
|
+
import type { Readable, Writable } from 'node:stream';
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
dispatcherFactoryOf,
|
|
32
|
+
McpDispatcher,
|
|
33
|
+
parseJsonRpcRequest,
|
|
34
|
+
type JsonRpcRequest,
|
|
35
|
+
type McpHandler,
|
|
36
|
+
type McpHttpRequest,
|
|
37
|
+
type McpHttpResponse,
|
|
38
|
+
} from './dispatch.js';
|
|
39
|
+
import { errorMessage, goJSONParseMessage } from './legacy.js';
|
|
40
|
+
import {
|
|
41
|
+
encodeEnvelope,
|
|
42
|
+
MCP_ERR_INTERNAL,
|
|
43
|
+
MCP_ERR_INVALID_REQUEST,
|
|
44
|
+
MCP_ERR_PARSE,
|
|
45
|
+
RawJSON,
|
|
46
|
+
resolveMcpConfig,
|
|
47
|
+
type JsonRpcErrorObject,
|
|
48
|
+
type McpBridge,
|
|
49
|
+
type McpMountOptions,
|
|
50
|
+
} from './types.js';
|
|
51
|
+
|
|
52
|
+
/** Options for {@link serveStdio}. */
|
|
53
|
+
export interface McpStdioOptions {
|
|
54
|
+
/** Where requests are read from. Default: `process.stdin`. */
|
|
55
|
+
input?: Readable;
|
|
56
|
+
/** Where replies are written. Default: `process.stdout`. */
|
|
57
|
+
output?: Writable;
|
|
58
|
+
/** Where diagnostics are written. Default: `process.stderr`. */
|
|
59
|
+
log?: Writable;
|
|
60
|
+
/**
|
|
61
|
+
* Mount options, when serving a bridge. A handler from
|
|
62
|
+
* createMcpHandler already carries its own; passing both throws.
|
|
63
|
+
*/
|
|
64
|
+
mount?: McpMountOptions;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A running stdio session. */
|
|
68
|
+
export interface McpStdioServer {
|
|
69
|
+
/**
|
|
70
|
+
* Resolves once input has ended (or {@link close} was called) and
|
|
71
|
+
* every in-flight request has been answered.
|
|
72
|
+
*/
|
|
73
|
+
readonly done: Promise<void>;
|
|
74
|
+
/** Stops reading input; resolves like {@link done}. */
|
|
75
|
+
close(): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type Serve = (req: McpHttpRequest, rpc: JsonRpcRequest) => Promise<McpHttpResponse>;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Serves MCP over stdio for a bridge, or for a handler built by
|
|
82
|
+
* createMcpHandler (its bridge and mount options are reused).
|
|
83
|
+
*
|
|
84
|
+
* A handler built some other way is called with an HTTP-shaped POST
|
|
85
|
+
* per message; that serves the 2024-11-05 era, but such a handler
|
|
86
|
+
* cannot know the request came over stdio, so 2026-07-28 requests fail
|
|
87
|
+
* its header checks. Pass the bridge or a createMcpHandler handler to
|
|
88
|
+
* serve both eras.
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { serveStdio } from '@hop-top/kit/mcp';
|
|
92
|
+
*
|
|
93
|
+
* const server = serveStdio(bridge);
|
|
94
|
+
* await server.done; // the host closed stdin
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export function serveStdio(
|
|
98
|
+
target: McpBridge | McpHandler,
|
|
99
|
+
opts: McpStdioOptions = {},
|
|
100
|
+
): McpStdioServer {
|
|
101
|
+
const serve = resolveServe(target, opts.mount);
|
|
102
|
+
const input = opts.input ?? process.stdin;
|
|
103
|
+
const output = opts.output ?? process.stdout;
|
|
104
|
+
const log = opts.log ?? process.stderr;
|
|
105
|
+
|
|
106
|
+
const inFlight = new Set<Promise<void>>();
|
|
107
|
+
const write = (line: string): void => {
|
|
108
|
+
output.write(line.endsWith('\n') ? line : `${line}\n`);
|
|
109
|
+
};
|
|
110
|
+
const warn = (msg: string): void => {
|
|
111
|
+
log.write(`mcp stdio: ${msg}\n`);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const rl = createInterface({ input, crlfDelay: Infinity, terminal: false });
|
|
115
|
+
rl.on('line', (line) => {
|
|
116
|
+
const task = handleLine(line, serve, write, warn);
|
|
117
|
+
if (task === undefined) return;
|
|
118
|
+
inFlight.add(task);
|
|
119
|
+
void task.finally(() => inFlight.delete(task));
|
|
120
|
+
});
|
|
121
|
+
// A broken stream ends the session instead of crashing the process.
|
|
122
|
+
const onInputError = (err: unknown): void => {
|
|
123
|
+
warn(`input error: ${errorMessage(err)}`);
|
|
124
|
+
rl.close();
|
|
125
|
+
};
|
|
126
|
+
const onOutputError = (err: unknown): void => {
|
|
127
|
+
warn(`output error: ${errorMessage(err)}`);
|
|
128
|
+
rl.close();
|
|
129
|
+
};
|
|
130
|
+
input.on('error', onInputError);
|
|
131
|
+
output.on('error', onOutputError);
|
|
132
|
+
|
|
133
|
+
const done = new Promise<void>((resolve) => {
|
|
134
|
+
rl.once('close', () => {
|
|
135
|
+
void drain(inFlight).then(() => {
|
|
136
|
+
input.off('error', onInputError);
|
|
137
|
+
output.off('error', onOutputError);
|
|
138
|
+
resolve();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
done,
|
|
145
|
+
close() {
|
|
146
|
+
rl.close();
|
|
147
|
+
return done;
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Picks the dispatch function for a bridge or a handler. */
|
|
153
|
+
function resolveServe(
|
|
154
|
+
target: McpBridge | McpHandler,
|
|
155
|
+
mount: McpMountOptions | undefined,
|
|
156
|
+
): Serve {
|
|
157
|
+
if (typeof target === 'function') {
|
|
158
|
+
if (mount !== undefined) {
|
|
159
|
+
throw new TypeError(
|
|
160
|
+
'serveStdio: mount options apply to a bridge; a handler carries its own',
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
const factory = dispatcherFactoryOf(target);
|
|
164
|
+
if (factory !== undefined) {
|
|
165
|
+
const dispatcher = factory('stdio');
|
|
166
|
+
return (req, rpc) => dispatcher.dispatch(req, rpc);
|
|
167
|
+
}
|
|
168
|
+
return (req) => target(req);
|
|
169
|
+
}
|
|
170
|
+
const dispatcher = new McpDispatcher(target, resolveMcpConfig(mount ?? {}), 'stdio');
|
|
171
|
+
return (req, rpc) => dispatcher.dispatch(req, rpc);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Waits until no request is in flight, including ones added meanwhile. */
|
|
175
|
+
async function drain(inFlight: Set<Promise<void>>): Promise<void> {
|
|
176
|
+
while (inFlight.size > 0) {
|
|
177
|
+
await Promise.allSettled([...inFlight]);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Frames, parses and dispatches one input line. Returns the pending
|
|
183
|
+
* work for a dispatched message, or undefined when the line was
|
|
184
|
+
* answered (or dropped) synchronously.
|
|
185
|
+
*/
|
|
186
|
+
function handleLine(
|
|
187
|
+
line: string,
|
|
188
|
+
serve: Serve,
|
|
189
|
+
write: (line: string) => void,
|
|
190
|
+
warn: (msg: string) => void,
|
|
191
|
+
): Promise<void> | undefined {
|
|
192
|
+
const text = line.trim();
|
|
193
|
+
if (text === '') return undefined;
|
|
194
|
+
|
|
195
|
+
let decoded: unknown;
|
|
196
|
+
try {
|
|
197
|
+
decoded = JSON.parse(text);
|
|
198
|
+
} catch {
|
|
199
|
+
write(errorLine(null, { code: MCP_ERR_PARSE, message: `parse error: ${goJSONParseMessage(text)}` }));
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
if (Array.isArray(decoded)) {
|
|
203
|
+
write(errorLine(null, {
|
|
204
|
+
code: MCP_ERR_INVALID_REQUEST,
|
|
205
|
+
message: 'invalid request: batch requests are not supported',
|
|
206
|
+
}));
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
if (decoded === null || typeof decoded !== 'object') {
|
|
210
|
+
write(errorLine(null, {
|
|
211
|
+
code: MCP_ERR_INVALID_REQUEST,
|
|
212
|
+
message: 'invalid request: not a JSON-RPC object',
|
|
213
|
+
}));
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const obj = decoded as Record<string, unknown>;
|
|
218
|
+
const rpc = parseJsonRpcRequest(text);
|
|
219
|
+
const isNotification = rpc.id === undefined;
|
|
220
|
+
|
|
221
|
+
if (typeof obj.method !== 'string') {
|
|
222
|
+
// A response (result / error, no method): this server sends no
|
|
223
|
+
// requests, so there is nothing to correlate it with.
|
|
224
|
+
if ('result' in obj || 'error' in obj) {
|
|
225
|
+
warn('ignored a JSON-RPC response from the client');
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
if (isNotification) return undefined;
|
|
229
|
+
write(errorLine(rpc.id, {
|
|
230
|
+
code: MCP_ERR_INVALID_REQUEST,
|
|
231
|
+
message: 'invalid request: missing method',
|
|
232
|
+
}));
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const req: McpHttpRequest = { method: 'POST', body: text };
|
|
237
|
+
return serve(req, rpc).then(
|
|
238
|
+
(res) => {
|
|
239
|
+
// A notification is never answered, whatever the handler said.
|
|
240
|
+
if (isNotification || res.body === '') return;
|
|
241
|
+
write(res.body);
|
|
242
|
+
},
|
|
243
|
+
(err: unknown) => {
|
|
244
|
+
warn(`${rpc.method}: ${errorMessage(err)}`);
|
|
245
|
+
if (isNotification) return;
|
|
246
|
+
write(errorLine(rpc.id, { code: MCP_ERR_INTERNAL, message: 'internal error' }));
|
|
247
|
+
},
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** One JSON-RPC error line; `null` renders `"id":null`. */
|
|
252
|
+
function errorLine(id: RawJSON | null | undefined, error: JsonRpcErrorObject): string {
|
|
253
|
+
return encodeEnvelope({ id: id === null ? new RawJSON('null') : id, error });
|
|
254
|
+
}
|
package/src/mcp/tasks.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module mcp/tasks
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* The `io.modelcontextprotocol/tasks` extension slot.
|
|
6
|
+
*
|
|
7
|
+
* kit does not implement the tasks extension (design gap matrix:
|
|
8
|
+
* "Not implemented. capabilities.extensions omitted from
|
|
9
|
+
* server/discover (= unsupported); tasks/* → -32601 @ 404"). This
|
|
10
|
+
* module makes that decision explicit and testable rather than
|
|
11
|
+
* implicit in a `default:` branch, and marks the designated slot if
|
|
12
|
+
* the extension is ever added.
|
|
13
|
+
*
|
|
14
|
+
* The negotiation contract matters for parity: because
|
|
15
|
+
* server/discover omits `capabilities.extensions` entirely, a
|
|
16
|
+
* conforming client never negotiates the extension and never sends
|
|
17
|
+
* `tasks/*`. A client that sends it anyway gets the same
|
|
18
|
+
* method-not-found response any other unknown modern method gets —
|
|
19
|
+
* NOT a distinct "extension unsupported" error, which would be a wire
|
|
20
|
+
* behavior the Go reference does not produce.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { MCP_ERR_METHOD_NOT_FOUND } from './types.js';
|
|
24
|
+
|
|
25
|
+
/** The reserved extension identifier, as named by the spec. */
|
|
26
|
+
export const TASKS_EXTENSION = 'io.modelcontextprotocol/tasks';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The task-extension methods. Listed so the surface can recognize
|
|
30
|
+
* them for documentation and testing purposes; recognizing one does
|
|
31
|
+
* NOT change its response.
|
|
32
|
+
*/
|
|
33
|
+
export const TASK_METHODS = [
|
|
34
|
+
'tasks/get',
|
|
35
|
+
'tasks/update',
|
|
36
|
+
'tasks/list',
|
|
37
|
+
'tasks/result',
|
|
38
|
+
'tasks/cancel',
|
|
39
|
+
] as const;
|
|
40
|
+
|
|
41
|
+
export type TaskMethod = (typeof TASK_METHODS)[number];
|
|
42
|
+
|
|
43
|
+
/** Reports whether a method name belongs to the tasks extension. */
|
|
44
|
+
export function isTaskMethod(method: string): method is TaskMethod {
|
|
45
|
+
return (TASK_METHODS as readonly string[]).includes(method);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Whether this mount supports the tasks extension. Always false: the
|
|
50
|
+
* extension is a non-goal, and `server/discover` therefore omits the
|
|
51
|
+
* `capabilities.extensions` map entirely (an empty map would
|
|
52
|
+
* advertise the *ability* to carry extensions, which is a different
|
|
53
|
+
* claim than making none).
|
|
54
|
+
*/
|
|
55
|
+
export const TASKS_SUPPORTED = false as const;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The response for an unsupported tasks method: identical to the
|
|
59
|
+
* generic modern method-not-found (-32601 @ 404). Kept as a named
|
|
60
|
+
* function so the "tasks are unsupported" decision has one call site
|
|
61
|
+
* a reviewer can find, and so a future implementation replaces this
|
|
62
|
+
* rather than editing a catch-all branch.
|
|
63
|
+
*/
|
|
64
|
+
export function taskMethodNotFound(method: string): {
|
|
65
|
+
code: number;
|
|
66
|
+
msg: string;
|
|
67
|
+
status: number;
|
|
68
|
+
} {
|
|
69
|
+
return {
|
|
70
|
+
code: MCP_ERR_METHOD_NOT_FOUND,
|
|
71
|
+
msg: `method not found: ${method}`,
|
|
72
|
+
status: 404,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The `capabilities` object `server/discover` reports. The extensions
|
|
78
|
+
* map is omitted rather than emitted empty — see TASKS_SUPPORTED.
|
|
79
|
+
*/
|
|
80
|
+
export function discoverCapabilities(): Record<string, unknown> {
|
|
81
|
+
return { tools: {} };
|
|
82
|
+
}
|