@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.
Files changed (122) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/auth/handshake.d.ts +23 -43
  3. package/dist/auth/handshake.d.ts.map +1 -1
  4. package/dist/auth/handshake.js +51 -82
  5. package/dist/auth/handshake.js.map +1 -1
  6. package/dist/auth/index.d.ts +1 -1
  7. package/dist/auth/index.d.ts.map +1 -1
  8. package/dist/auth/index.js.map +1 -1
  9. package/dist/auth/resume-token.d.ts +52 -0
  10. package/dist/auth/resume-token.d.ts.map +1 -0
  11. package/dist/auth/resume-token.js +89 -0
  12. package/dist/auth/resume-token.js.map +1 -0
  13. package/dist/auth/types.d.ts +15 -0
  14. package/dist/auth/types.d.ts.map +1 -1
  15. package/dist/index.d.ts +7 -3
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +25 -3
  18. package/dist/index.js.map +1 -1
  19. package/dist/proof/companion.d.ts +25 -0
  20. package/dist/proof/companion.d.ts.map +1 -0
  21. package/dist/proof/companion.js +14 -0
  22. package/dist/proof/companion.js.map +1 -0
  23. package/dist/proof/generator.d.ts +22 -9
  24. package/dist/proof/generator.d.ts.map +1 -1
  25. package/dist/proof/generator.js +20 -27
  26. package/dist/proof/generator.js.map +1 -1
  27. package/dist/proof/index.d.ts +2 -1
  28. package/dist/proof/index.d.ts.map +1 -1
  29. package/dist/proof/index.js +3 -1
  30. package/dist/proof/index.js.map +1 -1
  31. package/dist/runtime/audit.d.ts.map +1 -1
  32. package/dist/runtime/audit.js +78 -18
  33. package/dist/runtime/audit.js.map +1 -1
  34. package/dist/runtime/base.d.ts +6 -1
  35. package/dist/runtime/base.d.ts.map +1 -1
  36. package/dist/runtime/base.js +65 -17
  37. package/dist/runtime/base.js.map +1 -1
  38. package/dist/runtime/dif-core.d.ts +8 -0
  39. package/dist/runtime/dif-core.d.ts.map +1 -1
  40. package/dist/runtime/dif-core.js +10 -0
  41. package/dist/runtime/dif-core.js.map +1 -1
  42. package/dist/services/access-control.service.d.ts.map +1 -1
  43. package/dist/services/access-control.service.js +12 -5
  44. package/dist/services/access-control.service.js.map +1 -1
  45. package/dist/services/claims-first-guard.d.ts +33 -0
  46. package/dist/services/claims-first-guard.d.ts.map +1 -0
  47. package/dist/services/claims-first-guard.js +77 -0
  48. package/dist/services/claims-first-guard.js.map +1 -0
  49. package/dist/services/holder-keys.d.ts +95 -0
  50. package/dist/services/holder-keys.d.ts.map +1 -0
  51. package/dist/services/holder-keys.js +281 -0
  52. package/dist/services/holder-keys.js.map +1 -0
  53. package/dist/services/holder-read-slots.d.ts +42 -0
  54. package/dist/services/holder-read-slots.d.ts.map +1 -0
  55. package/dist/services/holder-read-slots.js +82 -0
  56. package/dist/services/holder-read-slots.js.map +1 -0
  57. package/dist/services/proof-verifier.d.ts +13 -35
  58. package/dist/services/proof-verifier.d.ts.map +1 -1
  59. package/dist/services/proof-verifier.js +16 -74
  60. package/dist/services/proof-verifier.js.map +1 -1
  61. package/dist/services/request-holder.d.ts +78 -0
  62. package/dist/services/request-holder.d.ts.map +1 -0
  63. package/dist/services/request-holder.js +81 -0
  64. package/dist/services/request-holder.js.map +1 -0
  65. package/dist/services/tool-context-builder.d.ts.map +1 -1
  66. package/dist/services/tool-context-builder.js +16 -4
  67. package/dist/services/tool-context-builder.js.map +1 -1
  68. package/dist/services/tool-protection.service.d.ts +7 -0
  69. package/dist/services/tool-protection.service.d.ts.map +1 -1
  70. package/dist/services/tool-protection.service.js +34 -15
  71. package/dist/services/tool-protection.service.js.map +1 -1
  72. package/dist/types/tool-protection.d.ts +94 -0
  73. package/dist/types/tool-protection.d.ts.map +1 -1
  74. package/dist/types/tool-protection.js +113 -1
  75. package/dist/types/tool-protection.js.map +1 -1
  76. package/eslint.config.js +20 -0
  77. package/package.json +8 -2
  78. package/src/__tests__/runtime/amount-limit-enforcement.test.ts +269 -0
  79. package/src/__tests__/runtime/amount-limit-result.test.ts +61 -0
  80. package/src/__tests__/runtime/audit-contracts-compat.test.ts +20 -5
  81. package/src/__tests__/runtime/audit.test.ts +363 -53
  82. package/src/__tests__/runtime/process-tool-call-security.test.ts +97 -13
  83. package/src/__tests__/runtime/provider-selection.test.ts +4 -6
  84. package/src/__tests__/runtime/route-interception.test.ts +215 -214
  85. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +89 -6
  86. package/src/__tests__/services/tool-protection-amount.test.ts +107 -0
  87. package/src/__tests__/services/tool-protection-failsafe.test.ts +71 -4
  88. package/src/__tests__/utils/tool-protection-runtime.ts +3 -0
  89. package/src/auth/__tests__/verify-or-hints-presented.test.ts +155 -0
  90. package/src/auth/handshake.ts +80 -125
  91. package/src/auth/index.ts +1 -0
  92. package/src/auth/resume-token.ts +134 -0
  93. package/src/auth/types.ts +20 -2
  94. package/src/index.ts +40 -1
  95. package/src/proof/__tests__/proof-generator.test.ts +80 -13
  96. package/src/proof/companion.ts +35 -0
  97. package/src/proof/generator.ts +51 -33
  98. package/src/proof/index.ts +3 -0
  99. package/src/runtime/__tests__/ext-apps-consent.test.ts +87 -2
  100. package/src/runtime/audit.ts +170 -65
  101. package/src/runtime/base.ts +95 -31
  102. package/src/runtime/dif-core.ts +16 -0
  103. package/src/services/__tests__/access-control.service.test.ts +44 -0
  104. package/src/services/__tests__/claims-first-guard.test.ts +93 -0
  105. package/src/services/__tests__/holder-fixtures.ts +135 -0
  106. package/src/services/__tests__/holder-keys.test.ts +257 -0
  107. package/src/services/__tests__/holder-read-slots.test.ts +117 -0
  108. package/src/services/__tests__/holder-reads.test.ts +284 -0
  109. package/src/services/__tests__/proof-verifier.test.ts +286 -195
  110. package/src/services/__tests__/request-holder.test.ts +210 -0
  111. package/src/services/__tests__/tool-context-builder-authtype.test.ts +79 -0
  112. package/src/services/__tests__/tool-context-builder.test.ts +4 -7
  113. package/src/services/__tests__/tool-protection-served-revision.test.ts +102 -0
  114. package/src/services/access-control.service.ts +11 -5
  115. package/src/services/claims-first-guard.ts +89 -0
  116. package/src/services/holder-keys.ts +379 -0
  117. package/src/services/holder-read-slots.ts +85 -0
  118. package/src/services/proof-verifier.ts +18 -79
  119. package/src/services/request-holder.ts +146 -0
  120. package/src/services/tool-context-builder.ts +19 -4
  121. package/src/services/tool-protection.service.ts +62 -19
  122. package/src/types/tool-protection.ts +161 -0
@@ -8,7 +8,7 @@
8
8
  * (Node.js, Cloudflare Workers) inject a CryptoProvider and delegate here.
9
9
  */
10
10
 
11
- import { SignJWT, importPKCS8 } from "jose";
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<DetachedProof> {
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: session.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
- ...options,
126
+ ...presentValues(claims),
99
127
  };
100
128
 
101
129
  const jws = await this.generateJWS(meta);
102
130
 
103
- return { jws, meta };
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
- * Includes both the JWT-standard claims (aud, sub, iss) and the literal
150
- * did/kid/audience field names verifiers check for -- a verifier that
151
- * compares payload fields against proof metadata by exact property name
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(this.identity.privateKey);
187
+ const privateKeyPem = this.formatPrivateKeyAsPEM(
188
+ this.identity.privateKey
189
+ );
158
190
  const privateKey = await importPKCS8(privateKeyPem, "EdDSA");
159
191
 
160
- const payload = {
161
- aud: meta.audience,
162
- sub: meta.did,
163
- iss: meta.did,
164
- did: meta.did,
165
- kid: this.identity.kid,
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,
@@ -9,4 +9,7 @@ export {
9
9
  type ToolRequest,
10
10
  type ToolResponse,
11
11
  type ProofOptions,
12
+ type ProofCompanion,
13
+ type GeneratedProof,
12
14
  } from "./generator";
15
+ export { presentValues } from "./companion";
@@ -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
- expect(sc.consentUrl).toBeDefined();
282
- expect(typeof sc.consentUrl).toBe("string");
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>;