@hop-top/kit 0.5.0-alpha.3 → 0.5.0-alpha.5
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 +52 -0
- package/LICENSE +1 -1
- package/README.md +47 -575
- package/dist/{aim-CTp-kJW2.d.ts → aim-DZHwwiIc.d.ts} +1 -1
- package/dist/aim.d.ts +1 -1
- package/dist/aim.js +13 -414
- package/dist/{alias-dhhr08Ei.d.ts → alias-BOj0-VAr.d.ts} +1 -1
- package/dist/alias.d.ts +1 -1
- package/dist/alias.js +18 -206
- package/dist/{api-miiso81x.d.ts → api-DAexsu2H.d.ts} +1 -1
- package/dist/api.d.ts +1 -1
- package/dist/api.js +10 -164
- package/dist/auth.js +6 -40
- 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-BRNEKPWX.js +37 -0
- package/dist/chunk-CSJWB6D3.js +151 -0
- package/dist/chunk-GGWJRWWA.js +309 -0
- package/dist/chunk-IQXEISMC.js +142 -0
- package/dist/chunk-IXVQ62NZ.js +334 -0
- package/dist/chunk-JTMPNXVQ.js +1491 -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-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-R5U7XKVJ.js +16 -0
- package/dist/chunk-S45UHXQF.js +182 -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-YGYQUFGY.js +219 -0
- package/dist/chunk-YOBAUCVN.js +583 -0
- package/dist/chunk-ZC5TZYZJ.js +609 -0
- package/dist/{cli-DLygZLi9.d.ts → cli-8rfITnv4.d.ts} +13 -4
- package/dist/cli.d.ts +4 -4
- package/dist/cli.js +51 -1057
- package/dist/{config-BUY7_W2n.d.ts → config-Bm9fBXch.d.ts} +35 -4
- package/dist/config.d.ts +1 -1
- package/dist/config.js +6 -78
- package/dist/errcorrect.js +8 -68
- package/dist/{error-DISJKeDu.d.ts → error-Do8Qji43.d.ts} +88 -1
- package/dist/id/index.d.ts +1 -1
- package/dist/id/index.js +14 -65
- package/dist/{index-MLrRDvEG.d.ts → index-B-pqGHhU.d.ts} +113 -19
- package/dist/{index-Bc5c7Ty5.d.ts → index-DEyjGoo2.d.ts} +1 -1
- package/dist/{index-QSffbLnj.d.ts → index-VLDcI5NF.d.ts} +3 -2
- package/dist/index.d.ts +18 -15
- package/dist/index.js +39 -6722
- 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 -1
- package/dist/llm.js +101 -356
- package/dist/mcp/index.d.ts +2 -1
- package/dist/mcp/index.js +155 -1453
- package/dist/{netpolicy-asvCfwi8.d.ts → netpolicy-Brx5XZUB.d.ts} +16 -3
- package/dist/netpolicy.d.ts +1 -1
- package/dist/netpolicy.js +25 -118
- package/dist/{output-4aXs91GY.d.ts → output-CjZWmL8s.d.ts} +28 -4
- package/dist/output.d.ts +2 -2
- package/dist/output.js +137 -971
- package/dist/progress.js +6 -55
- package/dist/provenance.js +6 -31
- package/dist/routellm-B0nv59mH.d.ts +111 -0
- package/dist/routellm.d.ts +2 -2
- package/dist/routellm.js +13 -255
- package/dist/{rpc-D5xLEf8V.d.ts → rpc-D31Lg0Kc.d.ts} +1 -1
- package/dist/rpc.d.ts +2 -2
- package/dist/rpc.js +4 -98
- package/dist/safety.js +6 -43
- package/dist/scope.js +20 -377
- package/dist/{serve-DE-jscpb.d.ts → serve-D8yWMrTc.d.ts} +2 -2
- package/dist/serve.d.ts +2 -2
- package/dist/serve.js +75 -1019
- package/dist/sqlstore.js +6 -39
- package/dist/stream.js +8 -51
- package/dist/telemetry/index.js +24 -608
- package/dist/tui/index.d.ts +5 -5
- package/dist/tui/index.js +17 -374
- package/dist/upgrade.js +6 -114
- package/dist/xdg.js +14 -128
- package/package.json +130 -25
- package/src/README.md +15 -0
- package/src/cli.help-command.test.ts +105 -0
- package/src/cli.test.ts +95 -1
- package/src/cli.ts +78 -13
- package/src/completion-values.test.ts +10 -10
- package/src/compliance.test.ts +196 -2
- package/src/compliance.ts +440 -20
- package/src/config.test.ts +187 -1
- package/src/config.ts +173 -7
- 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 +12 -0
- package/src/index.ts +2 -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/mcp/README.md +57 -0
- package/src/mcp/conformance.test.ts +24 -3
- package/src/mcp/dispatch.ts +4 -4
- package/src/mcp/identity.test.ts +166 -0
- package/src/mcp/identity.ts +72 -0
- package/src/mcp/index.ts +6 -0
- package/src/mcp/legacy.ts +21 -8
- package/src/mcp/mcp.test.ts +14 -4
- package/src/mcp/modern-confirm.ts +1 -1
- package/src/mcp/modern.ts +12 -11
- package/src/mcp/tasks.ts +1 -1
- package/src/mcp/types.ts +80 -8
- package/src/netpolicy.test.ts +78 -0
- package/src/netpolicy.ts +28 -3
- package/src/output/README.md +46 -0
- package/src/output/builtins.ts +5 -5
- package/src/output/error.ts +144 -1
- package/src/output/formatters/README.md +50 -0
- package/src/output.ts +24 -1
- package/src/published-types.test.ts +115 -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.ts +1 -1
- package/src/telemetry/README.md +48 -0
- package/src/telemetry/client.ts +5 -5
- package/src/toolspec.test.ts +2 -2
- package/src/triton/README.md +34 -0
- package/src/tui/README.md +42 -0
- package/src/wizard.test.ts +7 -7
- package/test/llm/conformance-helpers.ts +100 -0
- package/test/output/dispatch.test.ts +1 -2
- package/test/output/error.test.ts +33 -2
- 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/tsconfig.json +3 -1
- package/tsconfig.test.json +10 -0
- package/vitest.config.ts +9 -0
- package/dist/llm-B5XUWH8T.d.ts +0 -231
- package/dist/routellm-PGSt2D1y.d.ts +0 -75
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# mcp
|
|
2
|
+
|
|
3
|
+
## What it answers
|
|
4
|
+
|
|
5
|
+
How a Commander command tree is served over the Model Context Protocol from one mount that answers both the `2024-11-05` handshake era and the `2026-07-28` stateless era, choosing per request. Wrong module for REST or ConnectRPC surfaces (`@hop-top/kit/api`, `@hop-top/kit/rpc`) and for the service lifecycle that hosts the mount (`@hop-top/kit/serve`).
|
|
6
|
+
|
|
7
|
+
## Use it when
|
|
8
|
+
|
|
9
|
+
- expose leaves as MCP tools: `commanderBridge(root, { run })` then `createMcpHandler(bridge, opts)`
|
|
10
|
+
- bind to your own HTTP stack: the handler is `(McpHttpRequest) => Promise<McpHttpResponse>`; node:http, hono, express, fastify or a Worker is your call
|
|
11
|
+
- gate destructive tools: `policy` option, `defaultPolicy()` blocks remote destructive calls
|
|
12
|
+
- require confirmation on the modern era: `headerConfirmationGate` or `ElicitationConfirmGate`
|
|
13
|
+
|
|
14
|
+
## Quick start
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { Command } from 'commander';
|
|
18
|
+
import { createMcpHandler, commanderBridge } from '@hop-top/kit/mcp';
|
|
19
|
+
|
|
20
|
+
const root = new Command('app');
|
|
21
|
+
root.command('ping').description('Ping the server');
|
|
22
|
+
const bridge = commanderBridge(root, {
|
|
23
|
+
run: async () => ({ stdout: 'pong\n', exitCode: 0 }),
|
|
24
|
+
});
|
|
25
|
+
const handler = createMcpHandler(bridge, {
|
|
26
|
+
serverInfo: { name: 'app', version: '0.1.0' },
|
|
27
|
+
});
|
|
28
|
+
const res = await handler({
|
|
29
|
+
method: 'POST',
|
|
30
|
+
headers: { 'content-type': 'application/json' },
|
|
31
|
+
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/list' }),
|
|
32
|
+
});
|
|
33
|
+
console.log(res.status, res.body);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Contract
|
|
37
|
+
|
|
38
|
+
- Wire bytes are pinned by [`sdk/tests/cross-lang/fixtures/mcp-wire.json`](../../../tests/cross-lang/fixtures/mcp-wire.json), compared as raw bytes; [`conformance.test.ts`](conformance.test.ts) replays both `cases` and `sequences`.
|
|
39
|
+
- Protocol layer is `@modelcontextprotocol/core` + `@modelcontextprotocol/server` (v2). The v1 `@modelcontextprotocol/sdk` cannot serve `2026-07-28` and is not a dependency.
|
|
40
|
+
- Era detection follows the marker set and precedence chain in [`dispatch.ts`](dispatch.ts), not the SDK's own classifier.
|
|
41
|
+
- `tools/list` re-reads the bridge's leaves per request; caching the leaf set fails the sequence fixture.
|
|
42
|
+
- `commanderBridge` requires `run`: kit does not capture a command's output for you.
|
|
43
|
+
- Tasks extension: `TASKS_SUPPORTED` is `false`; `discoverCapabilities` reports it.
|
|
44
|
+
- Parity: [`contracts/parity/README.md`](../../../../contracts/parity/README.md), section "MCP wire contract".
|
|
45
|
+
|
|
46
|
+
## Neighbours
|
|
47
|
+
|
|
48
|
+
- `@hop-top/kit/serve`: service lifecycle hosting the mount
|
|
49
|
+
- `@hop-top/kit/api`, `@hop-top/kit/rpc`: REST and ConnectRPC clients
|
|
50
|
+
- `@hop-top/kit/safety`: `--force` delegation guard the policy builds on
|
|
51
|
+
- Go reference: [`go/transport/mcpsdk`](../../../../go/transport/mcpsdk/README.md); Python port: [`hop_top_kit/mcp`](../../../py/hop_top_kit/mcp/README.md)
|
|
52
|
+
|
|
53
|
+
## See also
|
|
54
|
+
|
|
55
|
+
- [`sdk/ts/README.md`](../../README.md), section "MCP surface": options, safety, confirmation, scope
|
|
56
|
+
- [`docs/adopters/guides/serve-mcp-from-any-sdk.md`](../../../../docs/adopters/guides/serve-mcp-from-any-sdk.md)
|
|
57
|
+
- [`docs/adopters/guides/expose-cli-over-mcp.md`](../../../../docs/adopters/guides/expose-cli-over-mcp.md): the Go reference
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* and a trailing newline; a serializer that differs must reorder to
|
|
12
12
|
* match, not normalize the comparison away.
|
|
13
13
|
*
|
|
14
|
-
* The fixtures are the parity contract. Where
|
|
14
|
+
* The fixtures are the parity contract. Where the MCP guides and these
|
|
15
15
|
* bytes disagree, the bytes win.
|
|
16
16
|
*/
|
|
17
17
|
|
|
@@ -20,6 +20,8 @@ import { join } from 'node:path';
|
|
|
20
20
|
import { describe, expect, it } from 'vitest';
|
|
21
21
|
|
|
22
22
|
import { createMcpHandler } from './dispatch.js';
|
|
23
|
+
import { headerValue } from './legacy.js';
|
|
24
|
+
import type { McpMountOptions } from './types.js';
|
|
23
25
|
import {
|
|
24
26
|
legacyLockBridge,
|
|
25
27
|
modernLockBridge,
|
|
@@ -110,16 +112,35 @@ function bridgeFor(c: { name: string }) {
|
|
|
110
112
|
return c.name.startsWith('legacy/') ? legacyLockBridge() : modernLockBridge();
|
|
111
113
|
}
|
|
112
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Maps a case's `mount` tokens onto mount options. The only token the
|
|
117
|
+
* cases carry is `verifier=bearer:<token>`: a verifier that establishes
|
|
118
|
+
* a request whose Authorization is exactly `Bearer <token>` and
|
|
119
|
+
* refuses every other. An unknown token fails the case rather than
|
|
120
|
+
* being silently ignored.
|
|
121
|
+
*/
|
|
122
|
+
function mountOptionsFor(c: { mount?: string[] }): McpMountOptions {
|
|
123
|
+
const opts: McpMountOptions = {};
|
|
124
|
+
for (const tok of c.mount ?? []) {
|
|
125
|
+
const bearer = /^verifier=bearer:(.+)$/.exec(tok);
|
|
126
|
+
if (bearer === null) throw new Error(`unknown mount token: ${tok}`);
|
|
127
|
+
const want = `Bearer ${bearer[1]}`;
|
|
128
|
+
opts.verifier = (req) =>
|
|
129
|
+
headerValue(req, 'authorization') === want ? { caller: 'fixture' } : null;
|
|
130
|
+
}
|
|
131
|
+
return opts;
|
|
132
|
+
}
|
|
133
|
+
|
|
113
134
|
describe('MCP wire conformance (cross-language fixtures)', () => {
|
|
114
135
|
const doc = loadFixtures();
|
|
115
136
|
|
|
116
137
|
it('loads the full fixture set', () => {
|
|
117
|
-
expect(doc.cases.length).toBe(
|
|
138
|
+
expect(doc.cases.length).toBe(23);
|
|
118
139
|
});
|
|
119
140
|
|
|
120
141
|
for (const c of doc.cases) {
|
|
121
142
|
it(`${c.name} — ${c.why ?? ''}`, async () => {
|
|
122
|
-
const handler = createMcpHandler(bridgeFor(c));
|
|
143
|
+
const handler = createMcpHandler(bridgeFor(c), mountOptionsFor(c));
|
|
123
144
|
const res = await handler({
|
|
124
145
|
method: 'POST',
|
|
125
146
|
headers: { 'Content-Type': 'application/json', ...(c.headers ?? {}) },
|
package/src/mcp/dispatch.ts
CHANGED
|
@@ -5,20 +5,20 @@
|
|
|
5
5
|
* Era detection + dispatch for the dual-spec MCP surface, plus the
|
|
6
6
|
* framework-free handler export.
|
|
7
7
|
*
|
|
8
|
-
* Hosting model (
|
|
8
|
+
* Hosting model (serve-mcp-from-any-sdk.md): this module exports a
|
|
9
9
|
* transport-agnostic request handler, not a server. Binding it to
|
|
10
10
|
* node:http, hono, express, fastify, or a Worker is the adopter's
|
|
11
11
|
* job — kit does not own the adopter's HTTP stack, and a pure
|
|
12
12
|
* request→response function is directly testable against the wire
|
|
13
13
|
* fixtures with no socket.
|
|
14
14
|
*
|
|
15
|
-
* Detection implements
|
|
15
|
+
* Detection implements expose-cli-over-mcp.md's normative rules literally: the
|
|
16
16
|
* marker set M1-M4, the two deliberate non-markers, and the
|
|
17
17
|
* precedence chain D1-D4. It deliberately does NOT delegate to the
|
|
18
18
|
* v2 SDK's own `classifyInboundRequest`: that classifier treats a
|
|
19
19
|
* modern `MCP-Protocol-Version` header as a routing signal and
|
|
20
20
|
* routes `initialize`-plus-modern-envelope to the modern era, both
|
|
21
|
-
* of which contradict
|
|
21
|
+
* of which contradict those rules (and the wire fixtures, which are the
|
|
22
22
|
* parity contract). The SDK is still the source of the protocol
|
|
23
23
|
* constants and error codes — see types.ts.
|
|
24
24
|
*/
|
|
@@ -98,7 +98,7 @@ export interface JsonRpcRequest {
|
|
|
98
98
|
export type McpEra = 'legacy' | 'modern';
|
|
99
99
|
|
|
100
100
|
/**
|
|
101
|
-
* Implements
|
|
101
|
+
* Implements per-request era detection, precedence D1-D4.
|
|
102
102
|
* Never fails — it only classifies an already-parsed request; D1
|
|
103
103
|
* (parse) is the caller's responsibility.
|
|
104
104
|
*
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kit/auth-required gate on both eras: only the mount's verifier
|
|
3
|
+
* establishes a caller. An Authorization header is presence, not
|
|
4
|
+
* verification, and a caller or scopes the request claims never
|
|
5
|
+
* become identity.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, it } from 'vitest';
|
|
9
|
+
|
|
10
|
+
import { createMcpHandler, type McpHttpRequest } from './dispatch.js';
|
|
11
|
+
import { headerValue } from './legacy.js';
|
|
12
|
+
import { legacyLockBridge, modernLockBridge } from './testtree.js';
|
|
13
|
+
import type {
|
|
14
|
+
Invocation,
|
|
15
|
+
McpBridge,
|
|
16
|
+
McpMountOptions,
|
|
17
|
+
McpVerifier,
|
|
18
|
+
} from './types.js';
|
|
19
|
+
|
|
20
|
+
const MODERN_META = {
|
|
21
|
+
'io.modelcontextprotocol/clientCapabilities': {},
|
|
22
|
+
'io.modelcontextprotocol/protocolVersion': '2026-07-28',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** A bridge that records every invocation it runs. */
|
|
26
|
+
function recording(inner: McpBridge): { bridge: McpBridge; calls: Invocation[] } {
|
|
27
|
+
const calls: Invocation[] = [];
|
|
28
|
+
return {
|
|
29
|
+
calls,
|
|
30
|
+
bridge: {
|
|
31
|
+
leaves: () => inner.leaves(),
|
|
32
|
+
invoke(inv) {
|
|
33
|
+
calls.push(inv);
|
|
34
|
+
return inner.invoke(inv);
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Accepts exactly `Bearer good` as alice in acme with two scopes. */
|
|
41
|
+
const verifyGood: McpVerifier = (req) =>
|
|
42
|
+
headerValue(req, 'authorization') === 'Bearer good'
|
|
43
|
+
? { caller: 'alice', tenant: 'acme', scopes: ['read', 'write'] }
|
|
44
|
+
: null;
|
|
45
|
+
|
|
46
|
+
type Era = 'legacy' | 'modern';
|
|
47
|
+
|
|
48
|
+
function call(
|
|
49
|
+
era: Era,
|
|
50
|
+
name: string,
|
|
51
|
+
headers: Record<string, string> = {},
|
|
52
|
+
extraParams: Record<string, unknown> = {},
|
|
53
|
+
): McpHttpRequest {
|
|
54
|
+
if (era === 'legacy') {
|
|
55
|
+
return {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
headers,
|
|
58
|
+
body: JSON.stringify({
|
|
59
|
+
jsonrpc: '2.0',
|
|
60
|
+
id: 1,
|
|
61
|
+
method: 'tools/call',
|
|
62
|
+
params: { name, ...extraParams },
|
|
63
|
+
}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const { _meta: claimed, ...rest } = extraParams as { _meta?: object };
|
|
67
|
+
return {
|
|
68
|
+
method: 'POST',
|
|
69
|
+
headers: {
|
|
70
|
+
'MCP-Protocol-Version': '2026-07-28',
|
|
71
|
+
'Mcp-Method': 'tools/call',
|
|
72
|
+
'Mcp-Name': name,
|
|
73
|
+
...headers,
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
jsonrpc: '2.0',
|
|
77
|
+
id: 1,
|
|
78
|
+
method: 'tools/call',
|
|
79
|
+
params: { name, ...rest, _meta: { ...MODERN_META, ...(claimed ?? {}) } },
|
|
80
|
+
}),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function mount(era: Era, opts: McpMountOptions = {}) {
|
|
85
|
+
const rec = recording(era === 'legacy' ? legacyLockBridge() : modernLockBridge());
|
|
86
|
+
return { handler: createMcpHandler(rec.bridge, opts), calls: rec.calls };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const CLAIMS = { caller: 'admin', tenant: 'root', scopes: ['admin'] };
|
|
90
|
+
|
|
91
|
+
for (const era of ['legacy', 'modern'] as const) {
|
|
92
|
+
describe(`${era} tools/call auth gate`, () => {
|
|
93
|
+
it('refuses an auth-required leaf on a bare Authorization header', async () => {
|
|
94
|
+
const { handler, calls } = mount(era);
|
|
95
|
+
const res = await handler(
|
|
96
|
+
call(era, 'secret', { Authorization: 'Bearer anything' }, { _meta: CLAIMS }),
|
|
97
|
+
);
|
|
98
|
+
expect(res.status).toBe(401);
|
|
99
|
+
expect(res.headers['WWW-Authenticate']).toBe('Bearer');
|
|
100
|
+
expect(res.body).toContain('"text":"authentication required"');
|
|
101
|
+
expect(res.body).toContain('"isError":true');
|
|
102
|
+
expect(calls).toHaveLength(0);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('refuses when the verifier refuses the credential', async () => {
|
|
106
|
+
const { handler, calls } = mount(era, { verifier: verifyGood });
|
|
107
|
+
const res = await handler(call(era, 'secret', { Authorization: 'Bearer bad' }));
|
|
108
|
+
expect(res.status).toBe(401);
|
|
109
|
+
expect(res.headers['WWW-Authenticate']).toBe('Bearer');
|
|
110
|
+
expect(calls).toHaveLength(0);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('refuses when the verifier throws', async () => {
|
|
114
|
+
const { handler, calls } = mount(era, {
|
|
115
|
+
verifier: () => {
|
|
116
|
+
throw new Error('verifier down');
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
const res = await handler(call(era, 'secret', { Authorization: 'Bearer good' }));
|
|
120
|
+
expect(res.status).toBe(401);
|
|
121
|
+
expect(calls).toHaveLength(0);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('admits once the verifier establishes the caller, async included', async () => {
|
|
125
|
+
const { handler, calls } = mount(era, {
|
|
126
|
+
verifier: async (req) => verifyGood(req),
|
|
127
|
+
});
|
|
128
|
+
const res = await handler(
|
|
129
|
+
call(era, 'secret', { Authorization: 'Bearer good' }, { _meta: CLAIMS }),
|
|
130
|
+
);
|
|
131
|
+
expect(res.status).toBe(200);
|
|
132
|
+
expect(res.body).toContain('"isError":false');
|
|
133
|
+
expect(calls).toHaveLength(1);
|
|
134
|
+
const meta = calls[0].meta;
|
|
135
|
+
expect(meta.established).toBe('verified');
|
|
136
|
+
expect(meta.caller).toBe('alice');
|
|
137
|
+
expect(meta.tenant).toBe('acme');
|
|
138
|
+
expect(meta.extra?.scopes).toBe('read,write');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('never takes identity from the request on an open leaf', async () => {
|
|
142
|
+
const { handler, calls } = mount(era);
|
|
143
|
+
const res = await handler(
|
|
144
|
+
call(era, 'ping', { Authorization: 'Bearer good' }, {
|
|
145
|
+
_meta: CLAIMS,
|
|
146
|
+
arguments: {},
|
|
147
|
+
}),
|
|
148
|
+
);
|
|
149
|
+
expect(res.status).toBe(200);
|
|
150
|
+
expect(calls).toHaveLength(1);
|
|
151
|
+
const meta = calls[0].meta;
|
|
152
|
+
expect(meta.established).toBeUndefined();
|
|
153
|
+
expect(meta.caller).toBeUndefined();
|
|
154
|
+
expect(meta.tenant).toBeUndefined();
|
|
155
|
+
expect(meta.extra?.scopes).toBeUndefined();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('runs an open leaf unestablished when the verifier refuses', async () => {
|
|
159
|
+
const { handler, calls } = mount(era, { verifier: verifyGood });
|
|
160
|
+
const res = await handler(call(era, 'ping', { Authorization: 'Bearer bad' }));
|
|
161
|
+
expect(res.status).toBe(200);
|
|
162
|
+
expect(calls[0].meta.established).toBeUndefined();
|
|
163
|
+
expect(calls[0].meta.caller).toBeUndefined();
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module mcp/identity
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Who is calling, as both eras' `tools/call` gate reads it. The only
|
|
6
|
+
* source of identity is the mount's verifier: an `Authorization`
|
|
7
|
+
* header is presence, not verification, and a caller or scopes the
|
|
8
|
+
* request claims never become identity. Mirrors the Go surfaces,
|
|
9
|
+
* where only an established `Meta` admits a `kit/auth-required` leaf.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { McpHttpRequest } from './dispatch.js';
|
|
13
|
+
import {
|
|
14
|
+
SURFACE_MCP,
|
|
15
|
+
type InvocationMeta,
|
|
16
|
+
type McpIdentity,
|
|
17
|
+
type McpVerifier,
|
|
18
|
+
} from './types.js';
|
|
19
|
+
|
|
20
|
+
/** The key the verifier's scopes are recorded under in `InvocationMeta.extra`. */
|
|
21
|
+
export const SCOPES_EXTRA_KEY = 'scopes';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Runs the mount's verifier over `req`. Returns the identity it
|
|
25
|
+
* established, or `undefined` when there is no verifier, it refused
|
|
26
|
+
* (`null`/`undefined`), returned something that is not an object, or
|
|
27
|
+
* threw: every failure fails closed.
|
|
28
|
+
*/
|
|
29
|
+
export async function establishCaller(
|
|
30
|
+
verifier: McpVerifier | undefined,
|
|
31
|
+
req: McpHttpRequest,
|
|
32
|
+
): Promise<McpIdentity | undefined> {
|
|
33
|
+
if (verifier === undefined) return undefined;
|
|
34
|
+
let ident: McpIdentity | null | undefined;
|
|
35
|
+
try {
|
|
36
|
+
ident = await verifier(req);
|
|
37
|
+
} catch {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
if (ident === null || typeof ident !== 'object') return undefined;
|
|
41
|
+
return ident;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Builds one `tools/call` invocation's meta: the MCP surface, the
|
|
46
|
+
* request time, the audit `extra` the era supplies, and the identity
|
|
47
|
+
* the verifier established — caller, tenant, `established`, and the
|
|
48
|
+
* comma-joined `scopes` extra entry. Nothing from `ident` is set when
|
|
49
|
+
* it is `undefined`, and nothing here reads the request.
|
|
50
|
+
*/
|
|
51
|
+
export function invocationMeta(
|
|
52
|
+
ident: McpIdentity | undefined,
|
|
53
|
+
extra?: Record<string, string>,
|
|
54
|
+
): InvocationMeta {
|
|
55
|
+
const meta: InvocationMeta = { surface: SURFACE_MCP, requestedAt: new Date() };
|
|
56
|
+
const bag: Record<string, string> = { ...(extra ?? {}) };
|
|
57
|
+
delete bag[SCOPES_EXTRA_KEY];
|
|
58
|
+
if (ident !== undefined) {
|
|
59
|
+
meta.established = 'verified';
|
|
60
|
+
if (typeof ident.caller === 'string' && ident.caller !== '') {
|
|
61
|
+
meta.caller = ident.caller;
|
|
62
|
+
}
|
|
63
|
+
if (typeof ident.tenant === 'string' && ident.tenant !== '') {
|
|
64
|
+
meta.tenant = ident.tenant;
|
|
65
|
+
}
|
|
66
|
+
if (Array.isArray(ident.scopes) && ident.scopes.length > 0) {
|
|
67
|
+
bag[SCOPES_EXTRA_KEY] = ident.scopes.join(',');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (Object.keys(bag).length > 0) meta.extra = bag;
|
|
71
|
+
return meta;
|
|
72
|
+
}
|
package/src/mcp/index.ts
CHANGED
|
@@ -106,6 +106,7 @@ export {
|
|
|
106
106
|
META_KEY_CLIENT_INFO,
|
|
107
107
|
META_KEY_PROTOCOL_VERSION,
|
|
108
108
|
META_KEY_SERVER_INFO,
|
|
109
|
+
MCP_AUTH_CHALLENGE,
|
|
109
110
|
pathFromToolName,
|
|
110
111
|
policyAllowed,
|
|
111
112
|
RawJSON,
|
|
@@ -127,12 +128,17 @@ export {
|
|
|
127
128
|
type Leaf,
|
|
128
129
|
type McpBridge,
|
|
129
130
|
type McpCacheScope,
|
|
131
|
+
type McpEstablishment,
|
|
132
|
+
type McpIdentity,
|
|
130
133
|
type McpMountOptions,
|
|
131
134
|
type McpSpecVersion,
|
|
135
|
+
type McpVerifier,
|
|
132
136
|
type Policy,
|
|
133
137
|
type ResolvedMcpConfig,
|
|
134
138
|
type SafetyClass,
|
|
135
139
|
type Surface,
|
|
136
140
|
} from './types.js';
|
|
137
141
|
|
|
142
|
+
export { establishCaller, invocationMeta, SCOPES_EXTRA_KEY } from './identity.js';
|
|
143
|
+
|
|
138
144
|
export { commanderBridge, type CommanderBridgeOptions } from './bridge.js';
|
package/src/mcp/legacy.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* `tools/call`, plain JSON-RPC over one POST path, no sessions, no
|
|
7
7
|
* SSE.
|
|
8
8
|
*
|
|
9
|
-
* This module is byte-for-byte frozen.
|
|
9
|
+
* This module is byte-for-byte frozen. The dual-spec hard invariant is
|
|
10
10
|
* "2024-11-05 behavior is preserved byte-for-byte; additive only; no
|
|
11
11
|
* deprecation", and the three-way module split (legacy / modern /
|
|
12
12
|
* dispatch) exists precisely so a reviewer can confirm a modern-era
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
errorResultBlock,
|
|
19
19
|
encodeEnvelope,
|
|
20
20
|
buildToolEnvelope,
|
|
21
|
+
MCP_AUTH_CHALLENGE,
|
|
21
22
|
MCP_ERR_INVALID_PARAMS,
|
|
22
23
|
MCP_ERR_INVALID_REQUEST,
|
|
23
24
|
MCP_ERR_METHOD_NOT_FOUND,
|
|
@@ -39,6 +40,7 @@ import {
|
|
|
39
40
|
} from './types.js';
|
|
40
41
|
import type { JsonRpcRequest, McpHttpRequest, McpHttpResponse } from './dispatch.js';
|
|
41
42
|
import { jsonResponse } from './dispatch.js';
|
|
43
|
+
import { establishCaller, invocationMeta } from './identity.js';
|
|
42
44
|
|
|
43
45
|
/** Writes a successful JSON-RPC envelope. */
|
|
44
46
|
export function writeResult(
|
|
@@ -210,12 +212,12 @@ export class LegacyMcpHandler {
|
|
|
210
212
|
|
|
211
213
|
// Auth + confirmation gating, mirrored onto the result envelope so
|
|
212
214
|
// MCP-aware clients see isError while HTTP-only clients see the
|
|
213
|
-
// matching status code.
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
401,
|
|
215
|
+
// matching status code. Only the mount's verifier authenticates:
|
|
216
|
+
// a bare Authorization header does not.
|
|
217
|
+
const ident = await establishCaller(this.cfg.verifier, req);
|
|
218
|
+
if (leaf.class?.authRequired && ident === undefined) {
|
|
219
|
+
return unauthenticated(
|
|
220
|
+
writeResult(rpc.id, errorResultBlock('authentication required'), 401),
|
|
219
221
|
);
|
|
220
222
|
}
|
|
221
223
|
if (
|
|
@@ -233,7 +235,7 @@ export class LegacyMcpHandler {
|
|
|
233
235
|
const res = await this.bridge.invoke({
|
|
234
236
|
path: [...leaf.path],
|
|
235
237
|
flags: params.arguments,
|
|
236
|
-
meta:
|
|
238
|
+
meta: invocationMeta(ident),
|
|
237
239
|
});
|
|
238
240
|
return writeResult(rpc.id, renderCallResult(res), 200);
|
|
239
241
|
} catch (err) {
|
|
@@ -258,6 +260,17 @@ export class LegacyMcpHandler {
|
|
|
258
260
|
}
|
|
259
261
|
}
|
|
260
262
|
|
|
263
|
+
/**
|
|
264
|
+
* Stamps the WWW-Authenticate challenge on a 401 refusal, as Go's
|
|
265
|
+
* api.WriteUnauthenticated does at the edge.
|
|
266
|
+
*/
|
|
267
|
+
export function unauthenticated(res: McpHttpResponse): McpHttpResponse {
|
|
268
|
+
return {
|
|
269
|
+
...res,
|
|
270
|
+
headers: { ...res.headers, 'WWW-Authenticate': MCP_AUTH_CHALLENGE },
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
261
274
|
/** Reports whether a leaf is exposed on the MCP surface. */
|
|
262
275
|
export function isExposed(leaf: Leaf): boolean {
|
|
263
276
|
// Absent `enabled` means the bridge applies its policy defaults,
|
package/src/mcp/mcp.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Unit coverage for behavior the 17 shared wire fixtures do not
|
|
3
|
-
* reach: the era-detection edge cases in
|
|
3
|
+
* reach: the era-detection edge cases in expose-cli-over-mcp.md's table,
|
|
4
4
|
* the modern validation chain V1-V9, mount-option refusals, cache
|
|
5
5
|
* hints, the tasks-extension slot, and the safety gate itself.
|
|
6
6
|
*
|
|
@@ -58,7 +58,7 @@ function parsed(body: unknown) {
|
|
|
58
58
|
);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
describe('era detection (
|
|
61
|
+
describe('era detection (D1-D4, markers M1-M4)', () => {
|
|
62
62
|
it('D2: initialize is legacy even with every modern marker present', () => {
|
|
63
63
|
const req = post(
|
|
64
64
|
{ jsonrpc: '2.0', id: 1, method: 'initialize', params: { _meta: MODERN_META } },
|
|
@@ -305,7 +305,7 @@ describe('modern validation chain (V1-V9)', () => {
|
|
|
305
305
|
expect(differing.body).toContain('conflicting duplicate values');
|
|
306
306
|
});
|
|
307
307
|
|
|
308
|
-
it('auth-required leaves are gated on the Authorization header', async () => {
|
|
308
|
+
it('auth-required leaves are gated on a verified caller, not the Authorization header', async () => {
|
|
309
309
|
const blocked = await handler()(
|
|
310
310
|
post(
|
|
311
311
|
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'secret', _meta: MODERN_META } },
|
|
@@ -317,7 +317,17 @@ describe('modern validation chain (V1-V9)', () => {
|
|
|
317
317
|
// The refusal is still a complete modern result envelope.
|
|
318
318
|
expect(blocked.body).toContain('"resultType":"complete"');
|
|
319
319
|
|
|
320
|
-
const
|
|
320
|
+
const presence = await handler()(
|
|
321
|
+
post(
|
|
322
|
+
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'secret', _meta: MODERN_META } },
|
|
323
|
+
{ ...MODERN_HEADERS('tools/call', 'secret'), Authorization: 'Bearer t' },
|
|
324
|
+
),
|
|
325
|
+
);
|
|
326
|
+
expect(presence.status).toBe(401);
|
|
327
|
+
|
|
328
|
+
const allowed = await createMcpHandler(modernLockBridge(), {
|
|
329
|
+
verifier: () => ({ caller: 'alice' }),
|
|
330
|
+
})(
|
|
321
331
|
post(
|
|
322
332
|
{ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'secret', _meta: MODERN_META } },
|
|
323
333
|
{ ...MODERN_HEADERS('tools/call', 'secret'), Authorization: 'Bearer t' },
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
* this surface deliberately does not keep, so the short TTL bounds the
|
|
30
30
|
* window instead.
|
|
31
31
|
*
|
|
32
|
-
* Two verification failures are deliberately distinct
|
|
32
|
+
* Two verification failures are deliberately distinct: an
|
|
33
33
|
* expired-but-authentic state is a routine re-prompt; a state failing
|
|
34
34
|
* HMAC verification is never honored — the rejection is recorded as a
|
|
35
35
|
* security-relevant audit event first, and only then is a fresh prompt
|
package/src/mcp/modern.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @package @hop-top/kit
|
|
4
4
|
*
|
|
5
5
|
* The 2026-07-28 MCP handler: the stateless request core behind the
|
|
6
|
-
* era dispatcher. Implements
|
|
6
|
+
* era dispatcher. Implements expose-cli-over-mcp.md's validation order V1-V9,
|
|
7
7
|
* `server/discover`, the modern error writers (-32020 / -32021 /
|
|
8
8
|
* -32022 with their status mapping), result-envelope stamping
|
|
9
9
|
* (`resultType` + serverInfo `_meta`), and cache-hint application.
|
|
@@ -34,7 +34,6 @@ import {
|
|
|
34
34
|
META_KEY_SERVER_INFO,
|
|
35
35
|
RESULT_TYPE_COMPLETE,
|
|
36
36
|
renderCallResult,
|
|
37
|
-
SURFACE_MCP,
|
|
38
37
|
type Leaf,
|
|
39
38
|
type McpBridge,
|
|
40
39
|
type RawJSON,
|
|
@@ -51,9 +50,11 @@ import {
|
|
|
51
50
|
headerValues,
|
|
52
51
|
isExposed,
|
|
53
52
|
resolveLeaf,
|
|
53
|
+
unauthenticated,
|
|
54
54
|
writeError,
|
|
55
55
|
writeResult,
|
|
56
56
|
} from './legacy.js';
|
|
57
|
+
import { establishCaller, invocationMeta } from './identity.js';
|
|
57
58
|
import { isTaskMethod, taskMethodNotFound } from './tasks.js';
|
|
58
59
|
import {
|
|
59
60
|
ElicitationConfirmGate,
|
|
@@ -109,7 +110,7 @@ export class ModernMcpHandler {
|
|
|
109
110
|
}
|
|
110
111
|
|
|
111
112
|
/**
|
|
112
|
-
* The modern entry point. The validation chain runs in
|
|
113
|
+
* The modern entry point. The validation chain runs in the guide's
|
|
113
114
|
* order V1-V9; the first failure responds and stops. HTTP status is
|
|
114
115
|
* 400/404 only where the spec mandates it — application-level
|
|
115
116
|
* JSON-RPC errors ride HTTP 200, matching legacy's convention.
|
|
@@ -309,9 +310,13 @@ export class ModernMcpHandler {
|
|
|
309
310
|
);
|
|
310
311
|
}
|
|
311
312
|
|
|
312
|
-
// Pre-flight gates, mirroring legacy exactly
|
|
313
|
-
|
|
314
|
-
|
|
313
|
+
// Pre-flight gates, mirroring legacy exactly: only the mount's
|
|
314
|
+
// verifier authenticates, never a bare Authorization header.
|
|
315
|
+
const ident = await establishCaller(this.cfg.verifier, req);
|
|
316
|
+
if (leaf.class?.authRequired && ident === undefined) {
|
|
317
|
+
return unauthenticated(
|
|
318
|
+
this.writeCallError(rpc, 'authentication required', 401),
|
|
319
|
+
);
|
|
315
320
|
}
|
|
316
321
|
// Confirmation gate. With a mounted key this is the MRTR
|
|
317
322
|
// elicitation loop, which falls back to the header gate for
|
|
@@ -332,11 +337,7 @@ export class ModernMcpHandler {
|
|
|
332
337
|
const res = await this.bridge.invoke({
|
|
333
338
|
path: [...leaf.path],
|
|
334
339
|
flags: params.arguments,
|
|
335
|
-
meta:
|
|
336
|
-
surface: SURFACE_MCP,
|
|
337
|
-
requestedAt: new Date(),
|
|
338
|
-
extra: modernInvocationExtra(meta),
|
|
339
|
-
},
|
|
340
|
+
meta: invocationMeta(ident, modernInvocationExtra(meta)),
|
|
340
341
|
});
|
|
341
342
|
const out = renderCallResult(res);
|
|
342
343
|
if (res.data !== undefined && res.data !== null) {
|
package/src/mcp/tasks.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* The `io.modelcontextprotocol/tasks` extension slot.
|
|
6
6
|
*
|
|
7
|
-
* kit does not implement the tasks extension (
|
|
7
|
+
* kit does not implement the tasks extension (design gap matrix:
|
|
8
8
|
* "Not implemented. capabilities.extensions omitted from
|
|
9
9
|
* server/discover (= unsupported); tasks/* → -32601 @ 404"). This
|
|
10
10
|
* module makes that decision explicit and testable rather than
|