@kya-os/mcp-i-runtime 1.3.0 → 1.3.2
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/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -2
- package/dist/index.js.map +1 -1
- package/dist/proof/generator.d.ts +8 -6
- package/dist/proof/generator.d.ts.map +1 -1
- package/dist/proof/generator.js +7 -24
- package/dist/proof/generator.js.map +1 -1
- package/dist/runtime/audit.d.ts.map +1 -1
- package/dist/runtime/audit.js +66 -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/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/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/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/package.json +4 -3
- 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 +255 -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/index.ts +15 -1
- package/src/proof/__tests__/proof-generator.test.ts +35 -12
- package/src/proof/generator.ts +18 -27
- package/src/runtime/__tests__/ext-apps-consent.test.ts +87 -2
- package/src/runtime/audit.ts +155 -62
- package/src/runtime/base.ts +95 -31
- package/src/services/__tests__/access-control.service.test.ts +44 -0
- package/src/services/__tests__/proof-verifier.test.ts +283 -195
- 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/proof-verifier.ts +18 -79
- 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
|
@@ -8,7 +8,12 @@ import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
|
8
8
|
import { MCPIRuntimeBase } from "../../runtime/base";
|
|
9
9
|
import { ProviderRuntimeConfig } from "../../config";
|
|
10
10
|
import { createToolProtectionRuntime } from "../utils/tool-protection-runtime";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
DelegationRequiredError,
|
|
13
|
+
isToolProtectionUnenforceableError,
|
|
14
|
+
type ToolProtectionUnenforceableError,
|
|
15
|
+
unavailableToolProtection,
|
|
16
|
+
} from "../../types/tool-protection";
|
|
12
17
|
import type { VerifyDelegationAPIResponse } from "@kya-os/contracts/checkpoint-api";
|
|
13
18
|
|
|
14
19
|
describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
|
|
@@ -681,7 +686,7 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
|
|
|
681
686
|
} catch (error: any) {
|
|
682
687
|
expect(error).toBeInstanceOf(DelegationRequiredError);
|
|
683
688
|
expect(error.consentUrl).toBeDefined();
|
|
684
|
-
expect(error.consentUrl).toContain("
|
|
689
|
+
expect(error.consentUrl).toContain("https://auth.example.com/consent?");
|
|
685
690
|
expect(error.consentUrl).toContain("tool=protectedTool");
|
|
686
691
|
expect(error.consentUrl).toContain("scopes=files%3Awrite");
|
|
687
692
|
}
|
|
@@ -900,20 +905,30 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
|
|
|
900
905
|
nonce: "test-nonce",
|
|
901
906
|
};
|
|
902
907
|
|
|
903
|
-
it("
|
|
908
|
+
it("refuses a protected tool whose protection names no scope, before any consent", async () => {
|
|
904
909
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
905
910
|
requiresDelegation: true,
|
|
906
911
|
requiredScopes: [],
|
|
907
912
|
});
|
|
908
913
|
|
|
909
|
-
await
|
|
910
|
-
|
|
914
|
+
const error = await runtime
|
|
915
|
+
.processToolCall(
|
|
911
916
|
"protectedTool",
|
|
912
917
|
{ arg: "value" },
|
|
913
918
|
mockHandler,
|
|
914
919
|
session
|
|
915
920
|
)
|
|
916
|
-
|
|
921
|
+
.catch((e: unknown) => e);
|
|
922
|
+
|
|
923
|
+
expect(isToolProtectionUnenforceableError(error)).toBe(true);
|
|
924
|
+
expect(
|
|
925
|
+
(error as ToolProtectionUnenforceableError).toJSON()
|
|
926
|
+
).toMatchObject({
|
|
927
|
+
error: "tool_protection_unenforceable",
|
|
928
|
+
toolName: "protectedTool",
|
|
929
|
+
missing: ["requiredScopes"],
|
|
930
|
+
});
|
|
931
|
+
expect(mockHandler).not.toHaveBeenCalled();
|
|
917
932
|
});
|
|
918
933
|
|
|
919
934
|
it("should handle multiple required scopes", async () => {
|
|
@@ -960,6 +975,74 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
|
|
|
960
975
|
}
|
|
961
976
|
});
|
|
962
977
|
|
|
978
|
+
it.each([
|
|
979
|
+
["an empty list", []],
|
|
980
|
+
["only empty strings", ["", ""]],
|
|
981
|
+
["no list at all", undefined],
|
|
982
|
+
])(
|
|
983
|
+
"refuses a presented credential for a protection naming %s, without asking the verifier",
|
|
984
|
+
async (_label, requiredScopes) => {
|
|
985
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
986
|
+
requiresDelegation: true,
|
|
987
|
+
requiredScopes,
|
|
988
|
+
});
|
|
989
|
+
mockAccessControlService.verifyDelegation.mockResolvedValue({
|
|
990
|
+
success: true,
|
|
991
|
+
data: {
|
|
992
|
+
valid: true,
|
|
993
|
+
credential: {
|
|
994
|
+
agent_did: "did:key:zmock123",
|
|
995
|
+
scopes: ["anything"],
|
|
996
|
+
issued_at: Date.now(),
|
|
997
|
+
created_at: Date.now(),
|
|
998
|
+
},
|
|
999
|
+
},
|
|
1000
|
+
} as VerifyDelegationAPIResponse);
|
|
1001
|
+
|
|
1002
|
+
const error = await runtime
|
|
1003
|
+
.processToolCall("protectedTool", { arg: "value" }, mockHandler, {
|
|
1004
|
+
...session,
|
|
1005
|
+
delegationToken: "valid-delegation-token",
|
|
1006
|
+
})
|
|
1007
|
+
.catch((e: unknown) => e);
|
|
1008
|
+
|
|
1009
|
+
expect(isToolProtectionUnenforceableError(error)).toBe(true);
|
|
1010
|
+
expect((error as ToolProtectionUnenforceableError).missing).toEqual([
|
|
1011
|
+
"requiredScopes",
|
|
1012
|
+
]);
|
|
1013
|
+
expect(
|
|
1014
|
+
mockAccessControlService.verifyDelegation
|
|
1015
|
+
).not.toHaveBeenCalled();
|
|
1016
|
+
expect(mockHandler).not.toHaveBeenCalled();
|
|
1017
|
+
}
|
|
1018
|
+
);
|
|
1019
|
+
|
|
1020
|
+
it("refuses every call while the service stands in for unavailable protection", async () => {
|
|
1021
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue(
|
|
1022
|
+
unavailableToolProtection()
|
|
1023
|
+
);
|
|
1024
|
+
|
|
1025
|
+
for (const delegationToken of [undefined, "valid-delegation-token"]) {
|
|
1026
|
+
const error = await runtime
|
|
1027
|
+
.processToolCall("anyTool", { arg: "value" }, mockHandler, {
|
|
1028
|
+
...session,
|
|
1029
|
+
...(delegationToken ? { delegationToken } : {}),
|
|
1030
|
+
})
|
|
1031
|
+
.catch((e: unknown) => e);
|
|
1032
|
+
|
|
1033
|
+
expect(isToolProtectionUnenforceableError(error)).toBe(true);
|
|
1034
|
+
expect(
|
|
1035
|
+
(error as ToolProtectionUnenforceableError).toJSON()
|
|
1036
|
+
).toMatchObject({
|
|
1037
|
+
error: "tool_protection_unenforceable",
|
|
1038
|
+
toolName: "anyTool",
|
|
1039
|
+
missing: ["toolProtectionConfig"],
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
expect(mockAccessControlService.verifyDelegation).not.toHaveBeenCalled();
|
|
1043
|
+
expect(mockHandler).not.toHaveBeenCalled();
|
|
1044
|
+
});
|
|
1045
|
+
|
|
963
1046
|
it("should handle handler errors independently of protection", async () => {
|
|
964
1047
|
mockToolProtectionService.checkToolProtection.mockResolvedValue(null);
|
|
965
1048
|
const errorHandler = vi
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
2
|
+
import { ToolProtectionService } from "../../services/tool-protection.service";
|
|
3
|
+
import { InMemoryToolProtectionCache } from "../../cache/tool-protection-cache";
|
|
4
|
+
|
|
5
|
+
const agentDid = "did:key:zTestAgent123";
|
|
6
|
+
const amount = { argument: "amount", currency: "USD" };
|
|
7
|
+
const remoteAmount = { argument: "order.total", currency: { argument: "ccy" } };
|
|
8
|
+
|
|
9
|
+
const respond = (mockFetch: ReturnType<typeof vi.fn>, tools: unknown) => {
|
|
10
|
+
const body = {
|
|
11
|
+
success: true,
|
|
12
|
+
data: { config: { toolProtection: { source: "agentshield", tools } } },
|
|
13
|
+
};
|
|
14
|
+
mockFetch.mockResolvedValue({
|
|
15
|
+
ok: true,
|
|
16
|
+
json: async () => body,
|
|
17
|
+
text: async () => JSON.stringify(body),
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
describe("ToolProtectionService amount config", () => {
|
|
22
|
+
let mockFetch: ReturnType<typeof vi.fn>;
|
|
23
|
+
let service: ToolProtectionService;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
mockFetch = vi.fn();
|
|
27
|
+
(globalThis as Record<string, unknown>).fetch = mockFetch;
|
|
28
|
+
service = new ToolProtectionService(
|
|
29
|
+
{
|
|
30
|
+
projectId: "p",
|
|
31
|
+
apiUrl: "https://kya.vouched.id",
|
|
32
|
+
apiKey: "k",
|
|
33
|
+
localConfig: {
|
|
34
|
+
toolProtections: {
|
|
35
|
+
pay: {
|
|
36
|
+
requiresDelegation: true,
|
|
37
|
+
requiredScopes: ["payments:send"],
|
|
38
|
+
amount,
|
|
39
|
+
},
|
|
40
|
+
plain: { requiresDelegation: true, requiredScopes: ["a:b"] },
|
|
41
|
+
both: {
|
|
42
|
+
requiresDelegation: true,
|
|
43
|
+
requiredScopes: ["payments:send"],
|
|
44
|
+
amount,
|
|
45
|
+
},
|
|
46
|
+
keep: {
|
|
47
|
+
requiresDelegation: true,
|
|
48
|
+
requiredScopes: ["payments:send"],
|
|
49
|
+
amount,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
new InMemoryToolProtectionCache()
|
|
55
|
+
);
|
|
56
|
+
respond(mockFetch, {
|
|
57
|
+
remote: {
|
|
58
|
+
requiresDelegation: true,
|
|
59
|
+
requiredScopes: ["payments:refund"],
|
|
60
|
+
amount: remoteAmount,
|
|
61
|
+
},
|
|
62
|
+
remotePlain: { requiresDelegation: true, requiredScopes: [] },
|
|
63
|
+
both: {
|
|
64
|
+
requiresDelegation: true,
|
|
65
|
+
requiredScopes: ["payments:send"],
|
|
66
|
+
amount: remoteAmount,
|
|
67
|
+
},
|
|
68
|
+
keep: { requiresDelegation: true, requiredScopes: ["payments:send"] },
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("keeps a local amount config and reads a remote one", async () => {
|
|
73
|
+
const { toolProtections } = await service.getToolProtectionConfig(agentDid);
|
|
74
|
+
expect(toolProtections.pay.amount).toEqual(amount);
|
|
75
|
+
expect(toolProtections.remote.amount).toEqual(remoteAmount);
|
|
76
|
+
expect(toolProtections.plain).not.toHaveProperty("amount");
|
|
77
|
+
expect(toolProtections.remotePlain).not.toHaveProperty("amount");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("lets a remote amount win and keeps the local one when the remote entry omits it", async () => {
|
|
81
|
+
const { toolProtections } = await service.getToolProtectionConfig(agentDid);
|
|
82
|
+
expect(toolProtections.both.amount).toEqual(remoteAmount);
|
|
83
|
+
expect(toolProtections.keep.amount).toEqual(amount);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("keeps a local amount config when the cache is cleared and refreshed", async () => {
|
|
87
|
+
const { config } = await service.clearAndRefresh(agentDid);
|
|
88
|
+
expect(config.toolProtections.pay.amount).toEqual(amount);
|
|
89
|
+
expect(config.toolProtections.remote.amount).toEqual(remoteAmount);
|
|
90
|
+
expect(config.toolProtections.plain).not.toHaveProperty("amount");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("applies the same remote-wins and local-kept rules on clear and refresh", async () => {
|
|
94
|
+
const { config } = await service.clearAndRefresh(agentDid);
|
|
95
|
+
expect(config.toolProtections.both.amount).toEqual(remoteAmount);
|
|
96
|
+
expect(config.toolProtections.keep.amount).toEqual(amount);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("hands the amount config to the caller through checkToolProtection", async () => {
|
|
100
|
+
await expect(
|
|
101
|
+
service.checkToolProtection("pay", agentDid)
|
|
102
|
+
).resolves.toMatchObject({ amount });
|
|
103
|
+
await expect(
|
|
104
|
+
service.checkToolProtection("keep", agentDid)
|
|
105
|
+
).resolves.toMatchObject({ amount });
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -11,7 +11,10 @@
|
|
|
11
11
|
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
12
12
|
import { ToolProtectionService } from "../../services/tool-protection.service";
|
|
13
13
|
import { InMemoryToolProtectionCache } from "../../cache/tool-protection-cache";
|
|
14
|
-
import
|
|
14
|
+
import {
|
|
15
|
+
isUnavailableToolProtection,
|
|
16
|
+
type ToolProtectionConfig,
|
|
17
|
+
} from "../../types/tool-protection";
|
|
15
18
|
import type { ToolProtection } from "@kya-os/contracts/tool-protection";
|
|
16
19
|
|
|
17
20
|
/**
|
|
@@ -265,9 +268,73 @@ describe("ToolProtectionService Fail-Safe Behaviors", () => {
|
|
|
265
268
|
|
|
266
269
|
const config = await service.getToolProtectionConfig(agentDid);
|
|
267
270
|
|
|
268
|
-
//
|
|
269
|
-
|
|
270
|
-
|
|
271
|
+
// Every tool resolves to the protection-unavailable stand-in
|
|
272
|
+
const protection = await service.checkToolProtection(
|
|
273
|
+
"any-tool",
|
|
274
|
+
agentDid
|
|
275
|
+
);
|
|
276
|
+
expect(protection).not.toBeNull();
|
|
277
|
+
expect(isUnavailableToolProtection(protection!)).toBe(true);
|
|
278
|
+
expect(Object.keys(config.toolProtections)).toEqual(["*"]);
|
|
279
|
+
expect(isUnavailableToolProtection(config.toolProtections["*"])).toBe(
|
|
280
|
+
true
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("never caches the stand-in, so the first config that arrives replaces it", async () => {
|
|
285
|
+
service = new ToolProtectionService(
|
|
286
|
+
{ projectId, apiUrl, apiKey, failSafeBehavior: "deny-all" },
|
|
287
|
+
cache
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
mockFetch.mockRejectedValue(new Error("Network error"));
|
|
291
|
+
const unavailable = await service.checkToolProtection("tool-a", agentDid);
|
|
292
|
+
expect(isUnavailableToolProtection(unavailable!)).toBe(true);
|
|
293
|
+
expect(
|
|
294
|
+
await cache.get(`config:tool-protections:${projectId}`)
|
|
295
|
+
).toBeNull();
|
|
296
|
+
|
|
297
|
+
// The API answers again: the next lookup reads it.
|
|
298
|
+
createMockFetchResponse(mockFetch, {
|
|
299
|
+
"tool-a": { requiresDelegation: true, requiredScopes: ["read"] },
|
|
300
|
+
});
|
|
301
|
+
const served = await service.checkToolProtection("tool-a", agentDid);
|
|
302
|
+
expect(served).toMatchObject({
|
|
303
|
+
requiresDelegation: true,
|
|
304
|
+
requiredScopes: ["read"],
|
|
305
|
+
});
|
|
306
|
+
expect(isUnavailableToolProtection(served!)).toBe(false);
|
|
307
|
+
expect(mockFetch).toHaveBeenCalledTimes(2);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("still prefers a stale cache over the stand-in", async () => {
|
|
311
|
+
const staleCache = new StaleCacheTestImpl();
|
|
312
|
+
await staleCache.set(
|
|
313
|
+
`config:tool-protections:${projectId}`,
|
|
314
|
+
{
|
|
315
|
+
toolProtections: {
|
|
316
|
+
"tool-a": { requiresDelegation: true, requiredScopes: ["read"] },
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
100
|
|
320
|
+
);
|
|
321
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
322
|
+
|
|
323
|
+
service = new ToolProtectionService(
|
|
324
|
+
{
|
|
325
|
+
projectId,
|
|
326
|
+
apiUrl,
|
|
327
|
+
apiKey,
|
|
328
|
+
allowStaleCache: true,
|
|
329
|
+
maxStaleCacheAge: 60000,
|
|
330
|
+
},
|
|
331
|
+
staleCache
|
|
332
|
+
);
|
|
333
|
+
mockFetch.mockRejectedValue(new Error("Network error"));
|
|
334
|
+
|
|
335
|
+
const protection = await service.checkToolProtection("tool-a", agentDid);
|
|
336
|
+
expect(protection).toMatchObject({ requiredScopes: ["read"] });
|
|
337
|
+
expect(isUnavailableToolProtection(protection!)).toBe(false);
|
|
271
338
|
});
|
|
272
339
|
|
|
273
340
|
it("should allow-all when configured and API fails", async () => {
|
|
@@ -12,6 +12,9 @@ export async function createToolProtectionRuntime() {
|
|
|
12
12
|
environment: "development",
|
|
13
13
|
session: { timestampSkewSeconds: 120, ttlMinutes: 30 },
|
|
14
14
|
toolProtectionService: mockToolProtectionService as any,
|
|
15
|
+
delegation: {
|
|
16
|
+
authorization: { authorizationUrl: "https://auth.example.com/consent" },
|
|
17
|
+
} as any,
|
|
15
18
|
};
|
|
16
19
|
const runtime = new MCPIRuntimeBase(config);
|
|
17
20
|
(runtime as any).accessControlService = mockAccessControlService;
|
package/src/index.ts
CHANGED
|
@@ -229,7 +229,21 @@ export type {
|
|
|
229
229
|
ToolProtectionServiceConfig,
|
|
230
230
|
} from "./types/tool-protection";
|
|
231
231
|
|
|
232
|
-
export {
|
|
232
|
+
export {
|
|
233
|
+
AmountLimitError,
|
|
234
|
+
amountLimitToolResult,
|
|
235
|
+
amountRefusalCode,
|
|
236
|
+
DelegationRequiredError,
|
|
237
|
+
isToolProtectionUnenforceableError,
|
|
238
|
+
isUnavailableToolProtection,
|
|
239
|
+
namesRequiredScope,
|
|
240
|
+
TOOL_PROTECTION_UNENFORCEABLE,
|
|
241
|
+
ToolProtectionUnenforceableError,
|
|
242
|
+
unavailableToolProtection,
|
|
243
|
+
unavailableToolProtectionError,
|
|
244
|
+
unscopedToolProtectionError,
|
|
245
|
+
} from "./types/tool-protection";
|
|
246
|
+
export type { UnavailableToolProtection } from "./types/tool-protection";
|
|
233
247
|
export { OAuthRequiredError } from "./types/oauth-required-error";
|
|
234
248
|
export type { OAuthRequiredErrorOptions } from "./types/oauth-required-error";
|
|
235
249
|
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
} from "../generator";
|
|
18
18
|
import type { ProofAgentIdentity } from "../generator";
|
|
19
19
|
import { SessionContext } from "@kya-os/contracts/handshake";
|
|
20
|
+
import { loadDifCore } from "../../runtime/dif-core";
|
|
20
21
|
|
|
21
22
|
// NodeCryptoProvider is imported from mcp-i (test environment — Node.js)
|
|
22
23
|
import { NodeCryptoProvider } from "../../../../mcp-i/src/providers/node-providers";
|
|
@@ -273,7 +274,7 @@ describe("ProofGenerator", () => {
|
|
|
273
274
|
expect(payload.clientDid).toBe("did:key:zClientPayload");
|
|
274
275
|
});
|
|
275
276
|
|
|
276
|
-
it("
|
|
277
|
+
it("signs exactly @kya-os/mcp's buildProofJwsPayload(meta), RFC 8785 serialized, with no did/kid/audience literals", async () => {
|
|
277
278
|
const request: ToolRequest = {
|
|
278
279
|
method: "test-tool",
|
|
279
280
|
params: { input: "hello" },
|
|
@@ -286,13 +287,29 @@ describe("ProofGenerator", () => {
|
|
|
286
287
|
mockSession
|
|
287
288
|
);
|
|
288
289
|
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
|
|
290
|
+
const { buildProofJwsPayload, canonicalizeJSON } = await loadDifCore();
|
|
291
|
+
const signedPayload = Buffer.from(
|
|
292
|
+
proof.jws.split(".")[1],
|
|
293
|
+
"base64url"
|
|
294
|
+
).toString("utf8");
|
|
295
|
+
|
|
296
|
+
expect(signedPayload).toBe(
|
|
297
|
+
canonicalizeJSON(buildProofJwsPayload(proof.meta))
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
const payload = JSON.parse(signedPayload) as Record<string, unknown>;
|
|
301
|
+
expect(payload.iss).toBe(mockIdentity.did);
|
|
302
|
+
expect(payload.sub).toBe(mockIdentity.did);
|
|
303
|
+
expect(payload.aud).toBe(mockSession.audience);
|
|
304
|
+
expect(payload).not.toHaveProperty("did");
|
|
305
|
+
expect(payload).not.toHaveProperty("kid");
|
|
306
|
+
expect(payload).not.toHaveProperty("audience");
|
|
292
307
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
308
|
+
// kid lives in the protected header only.
|
|
309
|
+
const header = JSON.parse(
|
|
310
|
+
Buffer.from(proof.jws.split(".")[0], "base64url").toString()
|
|
311
|
+
) as { kid: string };
|
|
312
|
+
expect(header.kid).toBe(mockIdentity.kid);
|
|
296
313
|
});
|
|
297
314
|
|
|
298
315
|
it("omits toolName and outcome from the JWS payload when not provided", async () => {
|
|
@@ -316,7 +333,7 @@ describe("ProofGenerator", () => {
|
|
|
316
333
|
expect(payload).not.toHaveProperty("outcome");
|
|
317
334
|
});
|
|
318
335
|
|
|
319
|
-
it("
|
|
336
|
+
it("signs the authorization outcome and keeps toolName/submissionOutcome on meta, unsigned", async () => {
|
|
320
337
|
const request: ToolRequest = {
|
|
321
338
|
method: "test-tool",
|
|
322
339
|
params: { input: "hello" },
|
|
@@ -327,15 +344,21 @@ describe("ProofGenerator", () => {
|
|
|
327
344
|
request,
|
|
328
345
|
response,
|
|
329
346
|
mockSession,
|
|
330
|
-
{
|
|
347
|
+
{
|
|
348
|
+
outcome: "allowed",
|
|
349
|
+
toolName: "get_weather",
|
|
350
|
+
submissionOutcome: "failure",
|
|
351
|
+
}
|
|
331
352
|
);
|
|
332
353
|
|
|
333
354
|
const payload = JSON.parse(
|
|
334
355
|
Buffer.from(proof.jws.split(".")[1], "base64url").toString()
|
|
335
|
-
) as
|
|
356
|
+
) as Record<string, unknown>;
|
|
336
357
|
|
|
337
|
-
expect(payload.
|
|
338
|
-
expect(payload
|
|
358
|
+
expect(payload.outcome).toBe("allowed");
|
|
359
|
+
expect(payload).not.toHaveProperty("toolName");
|
|
360
|
+
expect(proof.meta.toolName).toBe("get_weather");
|
|
361
|
+
expect(proof.meta.submissionOutcome).toBe("failure");
|
|
339
362
|
});
|
|
340
363
|
});
|
|
341
364
|
|
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,7 @@ 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 { loadDifCore } from "../runtime/dif-core";
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* Minimal agent identity required by ProofGenerator.
|
|
@@ -54,10 +55,14 @@ export interface ToolResponse {
|
|
|
54
55
|
* Proof generation options
|
|
55
56
|
*/
|
|
56
57
|
export interface ProofOptions {
|
|
58
|
+
/** Authorization decision this proof attests to (signed). */
|
|
59
|
+
outcome?: ProofMeta["outcome"];
|
|
57
60
|
scopeId?: string;
|
|
58
61
|
delegationRef?: string;
|
|
59
62
|
clientDid?: string;
|
|
63
|
+
/** xmcp-i-only metadata: travels beside the proof, never signed. */
|
|
60
64
|
toolName?: string;
|
|
65
|
+
/** xmcp-i-only execution result: travels beside the proof, never signed. */
|
|
61
66
|
submissionOutcome?: "success" | "failure" | "denied";
|
|
62
67
|
}
|
|
63
68
|
|
|
@@ -146,37 +151,23 @@ export class ProofGenerator {
|
|
|
146
151
|
* Generate Ed25519 JWS in compact format (header.payload.signature).
|
|
147
152
|
* Requirement: 5.3
|
|
148
153
|
*
|
|
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.
|
|
154
|
+
* The payload is exactly `@kya-os/mcp`'s `buildProofJwsPayload(meta)`,
|
|
155
|
+
* serialized per RFC 8785; the verifier rebuilds the same bytes from `meta`.
|
|
156
|
+
* `kid` lives in the protected header only.
|
|
154
157
|
*/
|
|
155
158
|
private async generateJWS(meta: ProofMeta): Promise<string> {
|
|
156
159
|
try {
|
|
157
|
-
const privateKeyPem = this.formatPrivateKeyAsPEM(
|
|
160
|
+
const privateKeyPem = this.formatPrivateKeyAsPEM(
|
|
161
|
+
this.identity.privateKey
|
|
162
|
+
);
|
|
158
163
|
const privateKey = await importPKCS8(privateKeyPem, "EdDSA");
|
|
159
164
|
|
|
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)
|
|
165
|
+
const { buildProofJwsPayload, canonicalizeJSON } = await loadDifCore();
|
|
166
|
+
const payload = new TextEncoder().encode(
|
|
167
|
+
canonicalizeJSON(buildProofJwsPayload(meta))
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
const jwt = await new CompactSign(payload)
|
|
180
171
|
.setProtectedHeader({
|
|
181
172
|
alg: "EdDSA",
|
|
182
173
|
kid: this.identity.kid,
|
|
@@ -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>;
|