@kya-os/mcp-i-runtime 1.3.1 → 1.3.3
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/.turbo/turbo-build.log +1 -1
- package/dist/auth/handshake.d.ts +23 -43
- package/dist/auth/handshake.d.ts.map +1 -1
- package/dist/auth/handshake.js +51 -82
- package/dist/auth/handshake.js.map +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/resume-token.d.ts +52 -0
- package/dist/auth/resume-token.d.ts.map +1 -0
- package/dist/auth/resume-token.js +89 -0
- package/dist/auth/resume-token.js.map +1 -0
- package/dist/auth/types.d.ts +15 -0
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -3
- package/dist/index.js.map +1 -1
- package/dist/proof/companion.d.ts +25 -0
- package/dist/proof/companion.d.ts.map +1 -0
- package/dist/proof/companion.js +14 -0
- package/dist/proof/companion.js.map +1 -0
- package/dist/proof/generator.d.ts +22 -9
- package/dist/proof/generator.d.ts.map +1 -1
- package/dist/proof/generator.js +20 -27
- package/dist/proof/generator.js.map +1 -1
- package/dist/proof/index.d.ts +2 -1
- package/dist/proof/index.d.ts.map +1 -1
- package/dist/proof/index.js +3 -1
- package/dist/proof/index.js.map +1 -1
- package/dist/runtime/audit.d.ts.map +1 -1
- package/dist/runtime/audit.js +78 -18
- package/dist/runtime/audit.js.map +1 -1
- package/dist/runtime/base.d.ts +6 -1
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js +65 -17
- package/dist/runtime/base.js.map +1 -1
- package/dist/runtime/dif-core.d.ts +8 -0
- package/dist/runtime/dif-core.d.ts.map +1 -1
- package/dist/runtime/dif-core.js +10 -0
- package/dist/runtime/dif-core.js.map +1 -1
- package/dist/services/access-control.service.d.ts.map +1 -1
- package/dist/services/access-control.service.js +12 -5
- package/dist/services/access-control.service.js.map +1 -1
- package/dist/services/claims-first-guard.d.ts +33 -0
- package/dist/services/claims-first-guard.d.ts.map +1 -0
- package/dist/services/claims-first-guard.js +77 -0
- package/dist/services/claims-first-guard.js.map +1 -0
- package/dist/services/holder-keys.d.ts +95 -0
- package/dist/services/holder-keys.d.ts.map +1 -0
- package/dist/services/holder-keys.js +281 -0
- package/dist/services/holder-keys.js.map +1 -0
- package/dist/services/holder-read-slots.d.ts +42 -0
- package/dist/services/holder-read-slots.d.ts.map +1 -0
- package/dist/services/holder-read-slots.js +82 -0
- package/dist/services/holder-read-slots.js.map +1 -0
- package/dist/services/proof-verifier.d.ts +13 -35
- package/dist/services/proof-verifier.d.ts.map +1 -1
- package/dist/services/proof-verifier.js +16 -74
- package/dist/services/proof-verifier.js.map +1 -1
- package/dist/services/request-holder.d.ts +78 -0
- package/dist/services/request-holder.d.ts.map +1 -0
- package/dist/services/request-holder.js +81 -0
- package/dist/services/request-holder.js.map +1 -0
- package/dist/services/tool-context-builder.d.ts.map +1 -1
- package/dist/services/tool-context-builder.js +16 -4
- package/dist/services/tool-context-builder.js.map +1 -1
- package/dist/services/tool-protection.service.d.ts +7 -0
- package/dist/services/tool-protection.service.d.ts.map +1 -1
- package/dist/services/tool-protection.service.js +34 -15
- package/dist/services/tool-protection.service.js.map +1 -1
- package/dist/types/tool-protection.d.ts +94 -0
- package/dist/types/tool-protection.d.ts.map +1 -1
- package/dist/types/tool-protection.js +113 -1
- package/dist/types/tool-protection.js.map +1 -1
- package/eslint.config.js +20 -0
- package/package.json +8 -2
- package/src/__tests__/runtime/amount-limit-enforcement.test.ts +269 -0
- package/src/__tests__/runtime/amount-limit-result.test.ts +61 -0
- package/src/__tests__/runtime/audit-contracts-compat.test.ts +20 -5
- package/src/__tests__/runtime/audit.test.ts +363 -53
- package/src/__tests__/runtime/process-tool-call-security.test.ts +97 -13
- package/src/__tests__/runtime/provider-selection.test.ts +4 -6
- package/src/__tests__/runtime/route-interception.test.ts +215 -214
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +89 -6
- package/src/__tests__/services/tool-protection-amount.test.ts +107 -0
- package/src/__tests__/services/tool-protection-failsafe.test.ts +71 -4
- package/src/__tests__/utils/tool-protection-runtime.ts +3 -0
- package/src/auth/__tests__/verify-or-hints-presented.test.ts +155 -0
- package/src/auth/handshake.ts +80 -125
- package/src/auth/index.ts +1 -0
- package/src/auth/resume-token.ts +134 -0
- package/src/auth/types.ts +20 -2
- package/src/index.ts +40 -1
- package/src/proof/__tests__/proof-generator.test.ts +80 -13
- package/src/proof/companion.ts +35 -0
- package/src/proof/generator.ts +51 -33
- package/src/proof/index.ts +3 -0
- package/src/runtime/__tests__/ext-apps-consent.test.ts +87 -2
- package/src/runtime/audit.ts +170 -65
- package/src/runtime/base.ts +95 -31
- package/src/runtime/dif-core.ts +16 -0
- package/src/services/__tests__/access-control.service.test.ts +44 -0
- package/src/services/__tests__/claims-first-guard.test.ts +93 -0
- package/src/services/__tests__/holder-fixtures.ts +135 -0
- package/src/services/__tests__/holder-keys.test.ts +257 -0
- package/src/services/__tests__/holder-read-slots.test.ts +117 -0
- package/src/services/__tests__/holder-reads.test.ts +284 -0
- package/src/services/__tests__/proof-verifier.test.ts +286 -195
- package/src/services/__tests__/request-holder.test.ts +210 -0
- package/src/services/__tests__/tool-context-builder-authtype.test.ts +79 -0
- package/src/services/__tests__/tool-context-builder.test.ts +4 -7
- package/src/services/__tests__/tool-protection-served-revision.test.ts +102 -0
- package/src/services/access-control.service.ts +11 -5
- package/src/services/claims-first-guard.ts +89 -0
- package/src/services/holder-keys.ts +379 -0
- package/src/services/holder-read-slots.ts +85 -0
- package/src/services/proof-verifier.ts +18 -79
- package/src/services/request-holder.ts +146 -0
- package/src/services/tool-context-builder.ts +19 -4
- package/src/services/tool-protection.service.ts +62 -19
- package/src/types/tool-protection.ts +161 -0
package/src/proof/generator.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* (Node.js, Cloudflare Workers) inject a CryptoProvider and delegate here.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import {
|
|
11
|
+
import { CompactSign, importPKCS8 } from "jose";
|
|
12
12
|
import { canonicalize } from "json-canonicalize";
|
|
13
13
|
import {
|
|
14
14
|
DetachedProof,
|
|
@@ -18,6 +18,15 @@ import {
|
|
|
18
18
|
import { SessionContext } from "@kya-os/contracts/handshake";
|
|
19
19
|
import { CryptoProvider } from "../providers/base";
|
|
20
20
|
import { CryptoService, type Ed25519JWK } from "../services/crypto.service";
|
|
21
|
+
import { base64urlEncodeFromBytes } from "../utils/base64";
|
|
22
|
+
import { loadDifCore } from "../runtime/dif-core";
|
|
23
|
+
import {
|
|
24
|
+
presentValues,
|
|
25
|
+
type GeneratedProof,
|
|
26
|
+
type ProofCompanion,
|
|
27
|
+
} from "./companion";
|
|
28
|
+
|
|
29
|
+
export type { GeneratedProof, ProofCompanion } from "./companion";
|
|
21
30
|
|
|
22
31
|
/**
|
|
23
32
|
* Minimal agent identity required by ProofGenerator.
|
|
@@ -51,13 +60,26 @@ export interface ToolResponse {
|
|
|
51
60
|
}
|
|
52
61
|
|
|
53
62
|
/**
|
|
54
|
-
* Proof generation options
|
|
63
|
+
* Proof generation options. Each claim is signed when it has a value; an
|
|
64
|
+
* empty one (undefined or "") is never written.
|
|
55
65
|
*/
|
|
56
|
-
export interface ProofOptions {
|
|
66
|
+
export interface ProofOptions extends ProofCompanion {
|
|
67
|
+
/** Authorization decision this proof attests to (signed). */
|
|
68
|
+
outcome?: ProofMeta["outcome"];
|
|
57
69
|
scopeId?: string;
|
|
58
70
|
delegationRef?: string;
|
|
59
71
|
clientDid?: string;
|
|
72
|
+
/**
|
|
73
|
+
* @deprecated Not a proof claim. Returned beside the proof as
|
|
74
|
+
* `proof.toolName`, never on `meta`. Prefer naming the tool where the
|
|
75
|
+
* proof is submitted: `ProofBatchQueue.enqueue({ proof, toolName, outcome })`.
|
|
76
|
+
*/
|
|
60
77
|
toolName?: string;
|
|
78
|
+
/**
|
|
79
|
+
* @deprecated Not a proof claim. Returned beside the proof as
|
|
80
|
+
* `proof.submissionOutcome`, never on `meta`. Prefer
|
|
81
|
+
* `ProofBatchQueue.enqueue({ proof, toolName, outcome })`.
|
|
82
|
+
*/
|
|
61
83
|
submissionOutcome?: "success" | "failure" | "denied";
|
|
62
84
|
}
|
|
63
85
|
|
|
@@ -83,24 +105,34 @@ export class ProofGenerator {
|
|
|
83
105
|
response: ToolResponse,
|
|
84
106
|
session: SessionContext,
|
|
85
107
|
options: ProofOptions = {}
|
|
86
|
-
): Promise<
|
|
108
|
+
): Promise<GeneratedProof> {
|
|
87
109
|
const hashes = await this.generateCanonicalHashes(request, response);
|
|
110
|
+
const { toolName, submissionOutcome, ...claims } = options;
|
|
88
111
|
|
|
89
112
|
const meta: ProofMeta = {
|
|
90
113
|
did: this.identity.did,
|
|
91
114
|
kid: this.identity.kid,
|
|
92
115
|
ts: Math.floor(Date.now() / 1000),
|
|
93
|
-
nonce:
|
|
116
|
+
// A nonce of this proof's own, as @kya-os/mcp's generator mints it: a
|
|
117
|
+
// replay check spends a nonce once per signer, so the session's
|
|
118
|
+
// handshake nonce would refuse every proof after its first.
|
|
119
|
+
nonce: base64urlEncodeFromBytes(
|
|
120
|
+
await this.cryptoProvider.randomBytes(16)
|
|
121
|
+
),
|
|
94
122
|
audience: session.audience,
|
|
95
123
|
sessionId: session.sessionId,
|
|
96
124
|
requestHash: hashes.requestHash,
|
|
97
125
|
responseHash: hashes.responseHash,
|
|
98
|
-
...
|
|
126
|
+
...presentValues(claims),
|
|
99
127
|
};
|
|
100
128
|
|
|
101
129
|
const jws = await this.generateJWS(meta);
|
|
102
130
|
|
|
103
|
-
return {
|
|
131
|
+
return {
|
|
132
|
+
jws,
|
|
133
|
+
meta,
|
|
134
|
+
...presentValues({ toolName, submissionOutcome }),
|
|
135
|
+
};
|
|
104
136
|
}
|
|
105
137
|
|
|
106
138
|
/**
|
|
@@ -146,37 +178,23 @@ export class ProofGenerator {
|
|
|
146
178
|
* Generate Ed25519 JWS in compact format (header.payload.signature).
|
|
147
179
|
* Requirement: 5.3
|
|
148
180
|
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* (did, kid, nonce, ts, audience) would otherwise see them all as
|
|
153
|
-
* undefined, since none of the JWT-standard aliases match those names.
|
|
181
|
+
* The payload is exactly `@kya-os/mcp`'s `buildProofJwsPayload(meta)`,
|
|
182
|
+
* serialized per RFC 8785; the verifier rebuilds the same bytes from `meta`.
|
|
183
|
+
* `kid` lives in the protected header only.
|
|
154
184
|
*/
|
|
155
185
|
private async generateJWS(meta: ProofMeta): Promise<string> {
|
|
156
186
|
try {
|
|
157
|
-
const privateKeyPem = this.formatPrivateKeyAsPEM(
|
|
187
|
+
const privateKeyPem = this.formatPrivateKeyAsPEM(
|
|
188
|
+
this.identity.privateKey
|
|
189
|
+
);
|
|
158
190
|
const privateKey = await importPKCS8(privateKeyPem, "EdDSA");
|
|
159
191
|
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
audience: meta.audience,
|
|
167
|
-
requestHash: meta.requestHash,
|
|
168
|
-
responseHash: meta.responseHash,
|
|
169
|
-
ts: meta.ts,
|
|
170
|
-
nonce: meta.nonce,
|
|
171
|
-
sessionId: meta.sessionId,
|
|
172
|
-
...(meta.scopeId && { scopeId: meta.scopeId }),
|
|
173
|
-
...(meta.delegationRef && { delegationRef: meta.delegationRef }),
|
|
174
|
-
...(meta.clientDid && { clientDid: meta.clientDid }),
|
|
175
|
-
...(meta.toolName && { toolName: meta.toolName }),
|
|
176
|
-
...(meta.submissionOutcome && { outcome: meta.submissionOutcome }),
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
const jwt = await new SignJWT(payload)
|
|
192
|
+
const { buildProofJwsPayload, canonicalizeJSON } = await loadDifCore();
|
|
193
|
+
const payload = new TextEncoder().encode(
|
|
194
|
+
canonicalizeJSON(buildProofJwsPayload(meta))
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const jwt = await new CompactSign(payload)
|
|
180
198
|
.setProtectedHeader({
|
|
181
199
|
alg: "EdDSA",
|
|
182
200
|
kid: this.identity.kid,
|
package/src/proof/index.ts
CHANGED
|
@@ -123,6 +123,89 @@ describe("ext-apps-constants", () => {
|
|
|
123
123
|
});
|
|
124
124
|
});
|
|
125
125
|
|
|
126
|
+
describe("MCPIRuntimeBase - consent link from configured authorization URL", () => {
|
|
127
|
+
const session = { id: "sess-1", agentDid: "did:key:agent1" };
|
|
128
|
+
|
|
129
|
+
function runtimeWith(authorizationUrl?: string): TestableRuntime {
|
|
130
|
+
const runtime = new TestableRuntime(
|
|
131
|
+
createTestConfig({
|
|
132
|
+
inlineConsent: true,
|
|
133
|
+
...(authorizationUrl === undefined
|
|
134
|
+
? {}
|
|
135
|
+
: { delegation: { authorization: { authorizationUrl } } as any }),
|
|
136
|
+
})
|
|
137
|
+
);
|
|
138
|
+
runtime.setExtAppsAvailable(true);
|
|
139
|
+
return runtime;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const build = (runtime: TestableRuntime) =>
|
|
143
|
+
(runtime as any).buildConsentUrl(
|
|
144
|
+
"tool-a",
|
|
145
|
+
["s:read", "s:write"],
|
|
146
|
+
session,
|
|
147
|
+
"resume-1",
|
|
148
|
+
"proj-1",
|
|
149
|
+
"github"
|
|
150
|
+
) as string | undefined;
|
|
151
|
+
|
|
152
|
+
it("returns undefined when no authorization URL is configured", () => {
|
|
153
|
+
const runtime = runtimeWith();
|
|
154
|
+
expect(build(runtime)).toBeUndefined();
|
|
155
|
+
|
|
156
|
+
const result = runtime.testBuildConsentUIResult(
|
|
157
|
+
"tool-a",
|
|
158
|
+
["s:read"],
|
|
159
|
+
session,
|
|
160
|
+
undefined,
|
|
161
|
+
undefined,
|
|
162
|
+
undefined,
|
|
163
|
+
buildUICapabilities()
|
|
164
|
+
);
|
|
165
|
+
const content = result!.content as Array<{ text: string }>;
|
|
166
|
+
expect(content[0].text).toBe(
|
|
167
|
+
'Authorization required for tool "tool-a". Please approve in the consent form below.'
|
|
168
|
+
);
|
|
169
|
+
expect(result!.structuredContent).not.toHaveProperty("consentUrl");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("keeps existing query parameters and adds the request's parameters", () => {
|
|
173
|
+
const runtime = runtimeWith("https://auth.example.com/consent?x=1");
|
|
174
|
+
const url = new URL(build(runtime)!);
|
|
175
|
+
expect(url.origin + url.pathname).toBe("https://auth.example.com/consent");
|
|
176
|
+
expect(url.searchParams.get("x")).toBe("1");
|
|
177
|
+
expect(url.searchParams.get("tool")).toBe("tool-a");
|
|
178
|
+
expect(url.searchParams.get("scopes")).toBe("s:read,s:write");
|
|
179
|
+
expect(url.searchParams.get("session_id")).toBe("sess-1");
|
|
180
|
+
expect(url.searchParams.get("agent_did")).toBe("did:key:agent1");
|
|
181
|
+
expect(url.searchParams.get("project_id")).toBe("proj-1");
|
|
182
|
+
expect(url.searchParams.get("resume_token")).toBe("resume-1");
|
|
183
|
+
expect(url.searchParams.get("provider")).toBe("github");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("links the configured URL from the consent UI result", () => {
|
|
187
|
+
const runtime = runtimeWith("https://auth.example.com/consent");
|
|
188
|
+
const result = runtime.testBuildConsentUIResult(
|
|
189
|
+
"tool-a",
|
|
190
|
+
["s:read"],
|
|
191
|
+
session,
|
|
192
|
+
undefined,
|
|
193
|
+
undefined,
|
|
194
|
+
undefined,
|
|
195
|
+
buildUICapabilities()
|
|
196
|
+
);
|
|
197
|
+
const sc = result!.structuredContent as Record<string, unknown>;
|
|
198
|
+
expect(sc.consentUrl).toContain("https://auth.example.com/consent?");
|
|
199
|
+
const content = result!.content as Array<{ text: string }>;
|
|
200
|
+
expect(content[0].text).toContain(sc.consentUrl as string);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("returns undefined when the configured URL is not an absolute URL", () => {
|
|
204
|
+
expect(build(runtimeWith("not a url"))).toBeUndefined();
|
|
205
|
+
expect(build(runtimeWith("/relative/consent"))).toBeUndefined();
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
126
209
|
describe("MCPIRuntimeBase - MCP Apps consent UI", () => {
|
|
127
210
|
let runtime: TestableRuntime;
|
|
128
211
|
|
|
@@ -278,8 +361,10 @@ describe("MCPIRuntimeBase - MCP Apps consent UI", () => {
|
|
|
278
361
|
expect(sc.projectId).toBe("proj-xyz");
|
|
279
362
|
expect(sc.resumeToken).toBe("resume-abc");
|
|
280
363
|
expect(sc.serverUrl).toBe("https://mcp.example.com");
|
|
281
|
-
|
|
282
|
-
expect(
|
|
364
|
+
// No authorization URL is configured, so there is no browser link.
|
|
365
|
+
expect(sc).not.toHaveProperty("consentUrl");
|
|
366
|
+
expect(content[0].text).not.toContain("open this link");
|
|
367
|
+
expect(content[0].text).not.toContain("http");
|
|
283
368
|
|
|
284
369
|
// Verify _meta with resourceUri
|
|
285
370
|
const meta = result!._meta as Record<string, unknown>;
|