@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
|
@@ -4,13 +4,16 @@
|
|
|
4
4
|
* Tests for route interception, resume tokens, and tool call resumption.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { describe, it, expect, beforeEach, vi } from
|
|
8
|
-
import { MCPIRuntimeBase } from
|
|
9
|
-
import { ProviderRuntimeConfig } from
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
8
|
+
import { MCPIRuntimeBase } from "../../runtime/base";
|
|
9
|
+
import { ProviderRuntimeConfig } from "../../config";
|
|
10
|
+
import {
|
|
11
|
+
createMockProviders,
|
|
12
|
+
MockClockProvider,
|
|
13
|
+
} from "../utils/mock-providers";
|
|
14
|
+
import { DelegationRequiredError } from "../../types/tool-protection";
|
|
15
|
+
|
|
16
|
+
describe("MCPIRuntimeBase - Route Interception", () => {
|
|
14
17
|
let runtime: MCPIRuntimeBase;
|
|
15
18
|
let config: ProviderRuntimeConfig;
|
|
16
19
|
let mockProviders: ReturnType<typeof createMockProviders>;
|
|
@@ -21,19 +24,22 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
21
24
|
beforeEach(async () => {
|
|
22
25
|
vi.clearAllMocks();
|
|
23
26
|
mockProviders = createMockProviders();
|
|
24
|
-
|
|
27
|
+
|
|
25
28
|
mockToolProtectionService = {
|
|
26
|
-
checkToolProtection: vi.fn()
|
|
29
|
+
checkToolProtection: vi.fn(),
|
|
27
30
|
};
|
|
28
31
|
|
|
29
32
|
config = {
|
|
30
33
|
...mockProviders,
|
|
31
|
-
environment:
|
|
34
|
+
environment: "development",
|
|
32
35
|
session: {
|
|
33
36
|
timestampSkewSeconds: 120,
|
|
34
|
-
ttlMinutes: 30
|
|
37
|
+
ttlMinutes: 30,
|
|
35
38
|
},
|
|
36
|
-
toolProtectionService: mockToolProtectionService as any
|
|
39
|
+
toolProtectionService: mockToolProtectionService as any,
|
|
40
|
+
delegation: {
|
|
41
|
+
authorization: { authorizationUrl: "https://auth.example.com/consent" },
|
|
42
|
+
} as any,
|
|
37
43
|
};
|
|
38
44
|
runtime = new MCPIRuntimeBase(config);
|
|
39
45
|
(runtime as any).accessControlService = {
|
|
@@ -46,22 +52,22 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
46
52
|
await runtime.initialize();
|
|
47
53
|
});
|
|
48
54
|
|
|
49
|
-
describe(
|
|
50
|
-
it(
|
|
55
|
+
describe("generateResumeToken", () => {
|
|
56
|
+
it("should generate unique resume tokens for different calls", async () => {
|
|
51
57
|
const call1 = {
|
|
52
|
-
toolName:
|
|
53
|
-
args: { arg:
|
|
54
|
-
sessionId:
|
|
58
|
+
toolName: "tool1",
|
|
59
|
+
args: { arg: "value1" },
|
|
60
|
+
sessionId: "session1",
|
|
55
61
|
timestamp: Date.now(),
|
|
56
|
-
expiresAt: Date.now() + 1800000
|
|
62
|
+
expiresAt: Date.now() + 1800000,
|
|
57
63
|
};
|
|
58
64
|
|
|
59
65
|
const call2 = {
|
|
60
|
-
toolName:
|
|
61
|
-
args: { arg:
|
|
62
|
-
sessionId:
|
|
66
|
+
toolName: "tool2",
|
|
67
|
+
args: { arg: "value2" },
|
|
68
|
+
sessionId: "session2",
|
|
63
69
|
timestamp: Date.now(),
|
|
64
|
-
expiresAt: Date.now() + 1800000
|
|
70
|
+
expiresAt: Date.now() + 1800000,
|
|
65
71
|
};
|
|
66
72
|
|
|
67
73
|
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
@@ -75,13 +81,13 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
75
81
|
expect(token2).toMatch(/^resume_/);
|
|
76
82
|
});
|
|
77
83
|
|
|
78
|
-
it(
|
|
84
|
+
it("should generate cryptographically random tokens", async () => {
|
|
79
85
|
const call = {
|
|
80
|
-
toolName:
|
|
81
|
-
args: { arg:
|
|
82
|
-
sessionId:
|
|
86
|
+
toolName: "tool1",
|
|
87
|
+
args: { arg: "value" },
|
|
88
|
+
sessionId: "session1",
|
|
83
89
|
timestamp: 1000,
|
|
84
|
-
expiresAt: 1801000
|
|
90
|
+
expiresAt: 1801000,
|
|
85
91
|
};
|
|
86
92
|
|
|
87
93
|
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
@@ -101,21 +107,21 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
101
107
|
expect(tokens.size).toBe(10);
|
|
102
108
|
});
|
|
103
109
|
|
|
104
|
-
it(
|
|
110
|
+
it("should generate unique tokens even for identical calls", async () => {
|
|
105
111
|
const call1 = {
|
|
106
|
-
toolName:
|
|
107
|
-
args: { arg:
|
|
108
|
-
sessionId:
|
|
112
|
+
toolName: "tool1",
|
|
113
|
+
args: { arg: "value1" },
|
|
114
|
+
sessionId: "session1",
|
|
109
115
|
timestamp: 1000,
|
|
110
|
-
expiresAt: 1801000
|
|
116
|
+
expiresAt: 1801000,
|
|
111
117
|
};
|
|
112
118
|
|
|
113
119
|
const call2 = {
|
|
114
|
-
toolName:
|
|
115
|
-
args: { arg:
|
|
116
|
-
sessionId:
|
|
120
|
+
toolName: "tool1",
|
|
121
|
+
args: { arg: "value1" }, // Same args
|
|
122
|
+
sessionId: "session1",
|
|
117
123
|
timestamp: 1000,
|
|
118
|
-
expiresAt: 1801000
|
|
124
|
+
expiresAt: 1801000,
|
|
119
125
|
};
|
|
120
126
|
|
|
121
127
|
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
@@ -127,133 +133,131 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
127
133
|
});
|
|
128
134
|
});
|
|
129
135
|
|
|
130
|
-
describe(
|
|
131
|
-
it(
|
|
136
|
+
describe("buildConsentUrl", () => {
|
|
137
|
+
it("should build consent URL with tool and scopes", () => {
|
|
132
138
|
const session = {
|
|
133
|
-
id:
|
|
134
|
-
agentDid:
|
|
135
|
-
audience:
|
|
139
|
+
id: "session123",
|
|
140
|
+
agentDid: "did:key:zagent123",
|
|
141
|
+
audience: "https://client.example.com",
|
|
136
142
|
};
|
|
137
143
|
|
|
138
144
|
const buildUrl = (runtime as any).buildConsentUrl.bind(runtime);
|
|
139
145
|
const url = buildUrl(
|
|
140
|
-
|
|
141
|
-
[
|
|
146
|
+
"protectedTool",
|
|
147
|
+
["files:write", "files:read"],
|
|
142
148
|
session,
|
|
143
|
-
|
|
149
|
+
"resume_token_123"
|
|
144
150
|
);
|
|
145
151
|
|
|
146
|
-
expect(url).toContain(
|
|
147
|
-
expect(url).toContain(
|
|
148
|
-
expect(url).toContain(
|
|
149
|
-
expect(url).toContain(
|
|
150
|
-
expect(url).toContain(
|
|
151
|
-
expect(url).toContain(
|
|
152
|
+
expect(url).toContain("https://auth.example.com/consent");
|
|
153
|
+
expect(url).toContain("tool=protectedTool");
|
|
154
|
+
expect(url).toContain("scopes=files%3Awrite%2Cfiles%3Aread");
|
|
155
|
+
expect(url).toContain("session_id=session123");
|
|
156
|
+
expect(url).toContain("agent_did=did%3Akey%3Azagent123");
|
|
157
|
+
expect(url).toContain("resume_token=resume_token_123");
|
|
152
158
|
});
|
|
153
159
|
|
|
154
|
-
it(
|
|
160
|
+
it("should build consent URL without resume token if not provided", () => {
|
|
155
161
|
const session = {
|
|
156
|
-
id:
|
|
157
|
-
agentDid:
|
|
158
|
-
audience:
|
|
162
|
+
id: "session123",
|
|
163
|
+
agentDid: "did:key:zagent123",
|
|
164
|
+
audience: "https://client.example.com",
|
|
159
165
|
};
|
|
160
166
|
|
|
161
167
|
const buildUrl = (runtime as any).buildConsentUrl.bind(runtime);
|
|
162
|
-
const url = buildUrl(
|
|
163
|
-
'protectedTool',
|
|
164
|
-
['files:write'],
|
|
165
|
-
session
|
|
166
|
-
);
|
|
168
|
+
const url = buildUrl("protectedTool", ["files:write"], session);
|
|
167
169
|
|
|
168
|
-
expect(url).toContain(
|
|
169
|
-
expect(url).not.toContain(
|
|
170
|
+
expect(url).toContain("https://auth.example.com/consent");
|
|
171
|
+
expect(url).not.toContain("resume_token=");
|
|
170
172
|
});
|
|
171
173
|
|
|
172
|
-
it(
|
|
174
|
+
it("should handle missing session fields gracefully", () => {
|
|
173
175
|
const buildUrl = (runtime as any).buildConsentUrl.bind(runtime);
|
|
174
176
|
const url = buildUrl(
|
|
175
|
-
|
|
176
|
-
[
|
|
177
|
+
"protectedTool",
|
|
178
|
+
["files:write"],
|
|
177
179
|
{},
|
|
178
|
-
|
|
180
|
+
"resume_token_123"
|
|
179
181
|
);
|
|
180
182
|
|
|
181
|
-
expect(url).toContain(
|
|
182
|
-
expect(url).toContain(
|
|
183
|
-
expect(url).toContain(
|
|
184
|
-
expect(url).toContain(
|
|
183
|
+
expect(url).toContain("https://auth.example.com/consent");
|
|
184
|
+
expect(url).toContain("tool=protectedTool");
|
|
185
|
+
expect(url).toContain("session_id=");
|
|
186
|
+
expect(url).toContain("agent_did=");
|
|
185
187
|
});
|
|
186
188
|
|
|
187
|
-
it(
|
|
189
|
+
it("should URL encode special characters in scopes", () => {
|
|
188
190
|
const session = {
|
|
189
|
-
id:
|
|
190
|
-
agentDid:
|
|
191
|
+
id: "session123",
|
|
192
|
+
agentDid: "did:key:zagent123",
|
|
191
193
|
};
|
|
192
194
|
|
|
193
195
|
const buildUrl = (runtime as any).buildConsentUrl.bind(runtime);
|
|
194
196
|
const url = buildUrl(
|
|
195
|
-
|
|
196
|
-
[
|
|
197
|
+
"protectedTool",
|
|
198
|
+
["files:write", "scope with spaces", "scope:with:colons"],
|
|
197
199
|
session
|
|
198
200
|
);
|
|
199
201
|
|
|
200
|
-
expect(url).toContain(
|
|
202
|
+
expect(url).toContain(
|
|
203
|
+
"scopes=files%3Awrite%2Cscope+with+spaces%2Cscope%3Awith%3Acolons"
|
|
204
|
+
);
|
|
201
205
|
});
|
|
202
206
|
});
|
|
203
207
|
|
|
204
|
-
describe(
|
|
205
|
-
const mockHandler = vi.fn().mockResolvedValue({ result:
|
|
208
|
+
describe("intercepted calls storage", () => {
|
|
209
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: "success" });
|
|
206
210
|
const session = {
|
|
207
|
-
id:
|
|
208
|
-
audience:
|
|
209
|
-
nonce:
|
|
211
|
+
id: "session123",
|
|
212
|
+
audience: "https://client.example.com",
|
|
213
|
+
nonce: "test-nonce",
|
|
210
214
|
};
|
|
211
215
|
|
|
212
|
-
it(
|
|
216
|
+
it("should store intercepted call when delegation required", async () => {
|
|
213
217
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
214
218
|
requiresDelegation: true,
|
|
215
|
-
requiredScopes: [
|
|
219
|
+
requiredScopes: ["files:write"],
|
|
216
220
|
});
|
|
217
221
|
|
|
218
222
|
try {
|
|
219
223
|
await runtime.processToolCall(
|
|
220
|
-
|
|
221
|
-
{ arg:
|
|
224
|
+
"protectedTool",
|
|
225
|
+
{ arg: "value" },
|
|
222
226
|
mockHandler,
|
|
223
227
|
session
|
|
224
228
|
);
|
|
225
|
-
expect.fail(
|
|
229
|
+
expect.fail("Should have thrown DelegationRequiredError");
|
|
226
230
|
} catch (error: any) {
|
|
227
231
|
expect(error.resumeToken).toBeDefined();
|
|
228
|
-
|
|
232
|
+
|
|
229
233
|
// Check that intercepted call is stored
|
|
230
234
|
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
231
235
|
const storedCall = interceptedCalls.get(error.resumeToken);
|
|
232
236
|
expect(storedCall).toBeDefined();
|
|
233
|
-
expect(storedCall.toolName).toBe(
|
|
234
|
-
expect(storedCall.args).toEqual({ arg:
|
|
235
|
-
expect(storedCall.sessionId).toBe(
|
|
237
|
+
expect(storedCall.toolName).toBe("protectedTool");
|
|
238
|
+
expect(storedCall.args).toEqual({ arg: "value" });
|
|
239
|
+
expect(storedCall.sessionId).toBe("session123");
|
|
236
240
|
}
|
|
237
241
|
});
|
|
238
242
|
|
|
239
|
-
it(
|
|
243
|
+
it("should set expiry time for intercepted calls", async () => {
|
|
240
244
|
const clock = mockProviders.clockProvider as MockClockProvider;
|
|
241
245
|
const currentTime = Date.now();
|
|
242
246
|
clock.setTime(currentTime);
|
|
243
247
|
|
|
244
248
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
245
249
|
requiresDelegation: true,
|
|
246
|
-
requiredScopes: [
|
|
250
|
+
requiredScopes: ["files:write"],
|
|
247
251
|
});
|
|
248
252
|
|
|
249
253
|
try {
|
|
250
254
|
await runtime.processToolCall(
|
|
251
|
-
|
|
252
|
-
{ arg:
|
|
255
|
+
"protectedTool",
|
|
256
|
+
{ arg: "value" },
|
|
253
257
|
mockHandler,
|
|
254
258
|
session
|
|
255
259
|
);
|
|
256
|
-
expect.fail(
|
|
260
|
+
expect.fail("Should have thrown DelegationRequiredError");
|
|
257
261
|
} catch (error: any) {
|
|
258
262
|
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
259
263
|
const storedCall = interceptedCalls.get(error.resumeToken);
|
|
@@ -262,92 +266,88 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
262
266
|
});
|
|
263
267
|
});
|
|
264
268
|
|
|
265
|
-
describe(
|
|
266
|
-
const mockHandler = vi.fn().mockResolvedValue({ result:
|
|
269
|
+
describe("resumeToolCall", () => {
|
|
270
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: "success" });
|
|
267
271
|
let session: any;
|
|
268
272
|
let resumeToken: string;
|
|
269
273
|
|
|
270
274
|
beforeEach(async () => {
|
|
271
275
|
// Create a handshake session first
|
|
272
276
|
const handshakeResponse = await runtime.handleHandshake({
|
|
273
|
-
clientDid:
|
|
274
|
-
audience:
|
|
277
|
+
clientDid: "did:key:zclient123",
|
|
278
|
+
audience: "https://client.example.com",
|
|
275
279
|
});
|
|
276
280
|
|
|
277
281
|
session = {
|
|
278
282
|
id: handshakeResponse.sessionId, // Use actual session ID from handshake
|
|
279
|
-
audience:
|
|
280
|
-
nonce:
|
|
283
|
+
audience: "https://client.example.com",
|
|
284
|
+
nonce: "test-nonce",
|
|
281
285
|
};
|
|
282
286
|
|
|
283
287
|
// Intercept a tool call
|
|
284
288
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
285
289
|
requiresDelegation: true,
|
|
286
|
-
requiredScopes: [
|
|
290
|
+
requiredScopes: ["files:write"],
|
|
287
291
|
});
|
|
288
292
|
|
|
289
293
|
try {
|
|
290
294
|
await runtime.processToolCall(
|
|
291
|
-
|
|
292
|
-
{ arg:
|
|
295
|
+
"protectedTool",
|
|
296
|
+
{ arg: "value" },
|
|
293
297
|
mockHandler,
|
|
294
298
|
session
|
|
295
299
|
);
|
|
296
|
-
expect.fail(
|
|
300
|
+
expect.fail("Should have thrown DelegationRequiredError");
|
|
297
301
|
} catch (error: any) {
|
|
298
302
|
resumeToken = error.resumeToken;
|
|
299
303
|
}
|
|
300
304
|
});
|
|
301
305
|
|
|
302
|
-
it(
|
|
306
|
+
it("should resume tool call with valid resume token", async () => {
|
|
303
307
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
304
308
|
requiresDelegation: true,
|
|
305
|
-
requiredScopes: [
|
|
309
|
+
requiredScopes: ["files:write"],
|
|
306
310
|
});
|
|
307
311
|
|
|
308
312
|
const result = await runtime.resumeToolCall(
|
|
309
313
|
resumeToken,
|
|
310
314
|
mockHandler,
|
|
311
|
-
|
|
315
|
+
"delegation-token-123"
|
|
312
316
|
);
|
|
313
317
|
|
|
314
|
-
expect(mockHandler).toHaveBeenCalledWith({ arg:
|
|
315
|
-
expect(result).toEqual({ result:
|
|
318
|
+
expect(mockHandler).toHaveBeenCalledWith({ arg: "value" });
|
|
319
|
+
expect(result).toEqual({ result: "success" });
|
|
316
320
|
});
|
|
317
321
|
|
|
318
|
-
it(
|
|
322
|
+
it("should throw error for invalid resume token", async () => {
|
|
319
323
|
await expect(
|
|
320
324
|
runtime.resumeToolCall(
|
|
321
|
-
|
|
325
|
+
"invalid-resume-token",
|
|
322
326
|
mockHandler,
|
|
323
|
-
|
|
327
|
+
"delegation-token-123"
|
|
324
328
|
)
|
|
325
|
-
).rejects.toThrow(
|
|
329
|
+
).rejects.toThrow("Invalid or expired resume token");
|
|
326
330
|
});
|
|
327
331
|
|
|
328
|
-
it(
|
|
332
|
+
it("should throw error for expired resume token", async () => {
|
|
329
333
|
const clock = mockProviders.clockProvider as MockClockProvider;
|
|
330
|
-
|
|
334
|
+
|
|
331
335
|
// Advance time past expiry (30 minutes + 1 second)
|
|
332
|
-
clock.setTime(Date.now() +
|
|
336
|
+
clock.setTime(Date.now() + 30 * 60 * 1000 + 1000);
|
|
333
337
|
|
|
334
338
|
await expect(
|
|
335
|
-
runtime.resumeToolCall(
|
|
336
|
-
|
|
337
|
-
mockHandler,
|
|
338
|
-
'delegation-token-123'
|
|
339
|
-
)
|
|
340
|
-
).rejects.toThrow('Resume token expired');
|
|
339
|
+
runtime.resumeToolCall(resumeToken, mockHandler, "delegation-token-123")
|
|
340
|
+
).rejects.toThrow("Resume token expired");
|
|
341
341
|
});
|
|
342
342
|
|
|
343
|
-
it(
|
|
343
|
+
it("should throw error if session not found", async () => {
|
|
344
344
|
// Create intercepted call with non-existent session ID
|
|
345
345
|
const call = {
|
|
346
|
-
toolName:
|
|
347
|
-
args: { arg:
|
|
348
|
-
sessionId:
|
|
346
|
+
toolName: "protectedTool",
|
|
347
|
+
args: { arg: "value" },
|
|
348
|
+
sessionId: "non-existent-session",
|
|
349
349
|
timestamp: Date.now(),
|
|
350
|
-
expiresAt: Date.now() + 1800000
|
|
350
|
+
expiresAt: Date.now() + 1800000,
|
|
351
351
|
};
|
|
352
352
|
|
|
353
353
|
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
@@ -360,37 +360,37 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
360
360
|
runtime.resumeToolCall(
|
|
361
361
|
invalidToken,
|
|
362
362
|
mockHandler,
|
|
363
|
-
|
|
363
|
+
"delegation-token-123"
|
|
364
364
|
)
|
|
365
|
-
).rejects.toThrow(
|
|
365
|
+
).rejects.toThrow("Session not found");
|
|
366
366
|
});
|
|
367
367
|
|
|
368
|
-
it(
|
|
368
|
+
it("should add delegation token to session when resuming", async () => {
|
|
369
369
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
370
370
|
requiresDelegation: true,
|
|
371
|
-
requiredScopes: [
|
|
371
|
+
requiredScopes: ["files:write"],
|
|
372
372
|
});
|
|
373
373
|
|
|
374
374
|
await runtime.resumeToolCall(
|
|
375
375
|
resumeToken,
|
|
376
376
|
mockHandler,
|
|
377
|
-
|
|
377
|
+
"delegation-token-123"
|
|
378
378
|
);
|
|
379
379
|
|
|
380
380
|
// Verify that processToolCall was called with session containing delegationToken
|
|
381
381
|
expect(mockHandler).toHaveBeenCalled();
|
|
382
382
|
});
|
|
383
383
|
|
|
384
|
-
it(
|
|
384
|
+
it("should clean up intercepted call after successful resumption", async () => {
|
|
385
385
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
386
386
|
requiresDelegation: true,
|
|
387
|
-
requiredScopes: [
|
|
387
|
+
requiredScopes: ["files:write"],
|
|
388
388
|
});
|
|
389
389
|
|
|
390
390
|
await runtime.resumeToolCall(
|
|
391
391
|
resumeToken,
|
|
392
392
|
mockHandler,
|
|
393
|
-
|
|
393
|
+
"delegation-token-123"
|
|
394
394
|
);
|
|
395
395
|
|
|
396
396
|
// Call should be removed from storage
|
|
@@ -398,16 +398,18 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
398
398
|
expect(interceptedCalls.has(resumeToken)).toBe(false);
|
|
399
399
|
});
|
|
400
400
|
|
|
401
|
-
it(
|
|
402
|
-
const errorHandler = vi
|
|
401
|
+
it("should not clean up intercepted call if resumption fails", async () => {
|
|
402
|
+
const errorHandler = vi
|
|
403
|
+
.fn()
|
|
404
|
+
.mockRejectedValue(new Error("Handler failed"));
|
|
403
405
|
|
|
404
406
|
try {
|
|
405
407
|
await runtime.resumeToolCall(
|
|
406
408
|
resumeToken,
|
|
407
409
|
errorHandler,
|
|
408
|
-
|
|
410
|
+
"delegation-token-123"
|
|
409
411
|
);
|
|
410
|
-
expect.fail(
|
|
412
|
+
expect.fail("Should have thrown error");
|
|
411
413
|
} catch (error) {
|
|
412
414
|
// Expected
|
|
413
415
|
}
|
|
@@ -418,26 +420,26 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
418
420
|
});
|
|
419
421
|
});
|
|
420
422
|
|
|
421
|
-
describe(
|
|
422
|
-
it(
|
|
423
|
+
describe("cleanupExpiredInterceptedCalls", () => {
|
|
424
|
+
it("should remove expired intercepted calls", async () => {
|
|
423
425
|
const clock = mockProviders.clockProvider as MockClockProvider;
|
|
424
426
|
const currentTime = Date.now();
|
|
425
427
|
clock.setTime(currentTime);
|
|
426
428
|
|
|
427
429
|
const expiredCall = {
|
|
428
|
-
toolName:
|
|
429
|
-
args: { arg:
|
|
430
|
-
sessionId:
|
|
430
|
+
toolName: "expiredTool",
|
|
431
|
+
args: { arg: "value" },
|
|
432
|
+
sessionId: "session123",
|
|
431
433
|
timestamp: currentTime - 1000,
|
|
432
|
-
expiresAt: currentTime - 500 // Already expired
|
|
434
|
+
expiresAt: currentTime - 500, // Already expired
|
|
433
435
|
};
|
|
434
436
|
|
|
435
437
|
const validCall = {
|
|
436
|
-
toolName:
|
|
437
|
-
args: { arg:
|
|
438
|
-
sessionId:
|
|
438
|
+
toolName: "validTool",
|
|
439
|
+
args: { arg: "value" },
|
|
440
|
+
sessionId: "session123",
|
|
439
441
|
timestamp: currentTime,
|
|
440
|
-
expiresAt: currentTime + 1800000 // Not expired
|
|
442
|
+
expiresAt: currentTime + 1800000, // Not expired
|
|
441
443
|
};
|
|
442
444
|
|
|
443
445
|
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
@@ -449,25 +451,27 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
449
451
|
interceptedCalls.set(validToken, validCall);
|
|
450
452
|
|
|
451
453
|
// Trigger cleanup
|
|
452
|
-
const cleanup = (runtime as any).cleanupExpiredInterceptedCalls.bind(
|
|
454
|
+
const cleanup = (runtime as any).cleanupExpiredInterceptedCalls.bind(
|
|
455
|
+
runtime
|
|
456
|
+
);
|
|
453
457
|
cleanup();
|
|
454
458
|
|
|
455
459
|
expect(interceptedCalls.has(expiredToken)).toBe(false);
|
|
456
460
|
expect(interceptedCalls.has(validToken)).toBe(true);
|
|
457
461
|
});
|
|
458
462
|
|
|
459
|
-
it(
|
|
463
|
+
it("should be called automatically when intercepting calls", async () => {
|
|
460
464
|
const clock = mockProviders.clockProvider as MockClockProvider;
|
|
461
465
|
const currentTime = Date.now();
|
|
462
466
|
clock.setTime(currentTime);
|
|
463
467
|
|
|
464
468
|
// Add an expired call
|
|
465
469
|
const expiredCall = {
|
|
466
|
-
toolName:
|
|
467
|
-
args: { arg:
|
|
468
|
-
sessionId:
|
|
470
|
+
toolName: "expiredTool",
|
|
471
|
+
args: { arg: "value" },
|
|
472
|
+
sessionId: "session123",
|
|
469
473
|
timestamp: currentTime - 1000,
|
|
470
|
-
expiresAt: currentTime - 500
|
|
474
|
+
expiresAt: currentTime - 500,
|
|
471
475
|
};
|
|
472
476
|
|
|
473
477
|
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
@@ -479,17 +483,14 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
479
483
|
// Intercept a new call (should trigger cleanup)
|
|
480
484
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
481
485
|
requiresDelegation: true,
|
|
482
|
-
requiredScopes: [
|
|
486
|
+
requiredScopes: ["files:write"],
|
|
483
487
|
});
|
|
484
488
|
|
|
485
489
|
try {
|
|
486
|
-
await runtime.processToolCall(
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
{ id: 'session123' }
|
|
491
|
-
);
|
|
492
|
-
expect.fail('Should have thrown DelegationRequiredError');
|
|
490
|
+
await runtime.processToolCall("newTool", { arg: "value" }, vi.fn(), {
|
|
491
|
+
id: "session123",
|
|
492
|
+
});
|
|
493
|
+
expect.fail("Should have thrown DelegationRequiredError");
|
|
493
494
|
} catch (error) {
|
|
494
495
|
// Expected
|
|
495
496
|
}
|
|
@@ -499,42 +500,42 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
499
500
|
});
|
|
500
501
|
});
|
|
501
502
|
|
|
502
|
-
describe(
|
|
503
|
-
it(
|
|
504
|
-
const mockHandler = vi.fn().mockResolvedValue({ result:
|
|
503
|
+
describe("integration: full interception flow", () => {
|
|
504
|
+
it("should complete full flow: intercept -> authorize -> resume", async () => {
|
|
505
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: "success" });
|
|
505
506
|
const session = {
|
|
506
|
-
id:
|
|
507
|
-
audience:
|
|
508
|
-
nonce:
|
|
507
|
+
id: "session123",
|
|
508
|
+
audience: "https://client.example.com",
|
|
509
|
+
nonce: "test-nonce",
|
|
509
510
|
};
|
|
510
511
|
|
|
511
512
|
// Step 1: Create session
|
|
512
513
|
const handshakeResponse = await runtime.handleHandshake({
|
|
513
|
-
clientDid:
|
|
514
|
-
audience:
|
|
514
|
+
clientDid: "did:key:zclient123",
|
|
515
|
+
audience: "https://client.example.com",
|
|
515
516
|
});
|
|
516
517
|
|
|
517
518
|
const sessionWithId = {
|
|
518
519
|
id: handshakeResponse.sessionId,
|
|
519
|
-
audience:
|
|
520
|
-
nonce:
|
|
520
|
+
audience: "https://client.example.com",
|
|
521
|
+
nonce: "test-nonce",
|
|
521
522
|
};
|
|
522
523
|
|
|
523
524
|
// Step 2: Intercept tool call
|
|
524
525
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
525
526
|
requiresDelegation: true,
|
|
526
|
-
requiredScopes: [
|
|
527
|
+
requiredScopes: ["files:write"],
|
|
527
528
|
});
|
|
528
529
|
|
|
529
530
|
let resumeToken: string;
|
|
530
531
|
try {
|
|
531
532
|
await runtime.processToolCall(
|
|
532
|
-
|
|
533
|
-
{ arg:
|
|
533
|
+
"protectedTool",
|
|
534
|
+
{ arg: "value" },
|
|
534
535
|
mockHandler,
|
|
535
536
|
sessionWithId
|
|
536
537
|
);
|
|
537
|
-
expect.fail(
|
|
538
|
+
expect.fail("Should have thrown DelegationRequiredError");
|
|
538
539
|
} catch (error: any) {
|
|
539
540
|
resumeToken = error.resumeToken;
|
|
540
541
|
expect(error).toBeInstanceOf(DelegationRequiredError);
|
|
@@ -546,12 +547,12 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
546
547
|
const result = await runtime.resumeToolCall(
|
|
547
548
|
resumeToken!,
|
|
548
549
|
mockHandler,
|
|
549
|
-
|
|
550
|
+
"delegation-token-123"
|
|
550
551
|
);
|
|
551
552
|
|
|
552
553
|
// Step 5: Verify successful execution
|
|
553
|
-
expect(mockHandler).toHaveBeenCalledWith({ arg:
|
|
554
|
-
expect(result).toEqual({ result:
|
|
554
|
+
expect(mockHandler).toHaveBeenCalledWith({ arg: "value" });
|
|
555
|
+
expect(result).toEqual({ result: "success" });
|
|
555
556
|
|
|
556
557
|
// Step 6: Verify cleanup
|
|
557
558
|
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
@@ -559,36 +560,36 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
559
560
|
});
|
|
560
561
|
});
|
|
561
562
|
|
|
562
|
-
describe(
|
|
563
|
-
it(
|
|
564
|
-
const mockHandler = vi.fn().mockResolvedValue({ result:
|
|
565
|
-
|
|
563
|
+
describe("edge cases", () => {
|
|
564
|
+
it("should handle resume token reuse attempt", async () => {
|
|
565
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: "success" });
|
|
566
|
+
|
|
566
567
|
// Create session first
|
|
567
568
|
const handshakeResponse = await runtime.handleHandshake({
|
|
568
|
-
clientDid:
|
|
569
|
-
audience:
|
|
569
|
+
clientDid: "did:key:zclient123",
|
|
570
|
+
audience: "https://client.example.com",
|
|
570
571
|
});
|
|
571
572
|
|
|
572
573
|
const sessionWithId = {
|
|
573
574
|
id: handshakeResponse.sessionId,
|
|
574
|
-
audience:
|
|
575
|
-
nonce:
|
|
575
|
+
audience: "https://client.example.com",
|
|
576
|
+
nonce: "test-nonce",
|
|
576
577
|
};
|
|
577
578
|
|
|
578
579
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
579
580
|
requiresDelegation: true,
|
|
580
|
-
requiredScopes: [
|
|
581
|
+
requiredScopes: ["files:write"],
|
|
581
582
|
});
|
|
582
583
|
|
|
583
584
|
let resumeToken: string;
|
|
584
585
|
try {
|
|
585
586
|
await runtime.processToolCall(
|
|
586
|
-
|
|
587
|
-
{ arg:
|
|
587
|
+
"protectedTool",
|
|
588
|
+
{ arg: "value" },
|
|
588
589
|
mockHandler,
|
|
589
590
|
sessionWithId
|
|
590
591
|
);
|
|
591
|
-
expect.fail(
|
|
592
|
+
expect.fail("Should have thrown DelegationRequiredError");
|
|
592
593
|
} catch (error: any) {
|
|
593
594
|
resumeToken = error.resumeToken;
|
|
594
595
|
}
|
|
@@ -597,7 +598,7 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
597
598
|
await runtime.resumeToolCall(
|
|
598
599
|
resumeToken!,
|
|
599
600
|
mockHandler,
|
|
600
|
-
|
|
601
|
+
"delegation-token-123"
|
|
601
602
|
);
|
|
602
603
|
|
|
603
604
|
// Second resume should fail (token already consumed)
|
|
@@ -605,57 +606,57 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
605
606
|
runtime.resumeToolCall(
|
|
606
607
|
resumeToken!,
|
|
607
608
|
mockHandler,
|
|
608
|
-
|
|
609
|
+
"delegation-token-123"
|
|
609
610
|
)
|
|
610
|
-
).rejects.toThrow(
|
|
611
|
+
).rejects.toThrow("Invalid or expired resume token");
|
|
611
612
|
});
|
|
612
613
|
|
|
613
|
-
it(
|
|
614
|
-
const mockHandler = vi.fn().mockResolvedValue({ result:
|
|
614
|
+
it("should handle complex nested args in intercepted calls", async () => {
|
|
615
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: "success" });
|
|
615
616
|
const session = {
|
|
616
|
-
id:
|
|
617
|
-
audience:
|
|
618
|
-
nonce:
|
|
617
|
+
id: "session123",
|
|
618
|
+
audience: "https://client.example.com",
|
|
619
|
+
nonce: "test-nonce",
|
|
619
620
|
};
|
|
620
621
|
|
|
621
622
|
const handshakeResponse = await runtime.handleHandshake({
|
|
622
|
-
clientDid:
|
|
623
|
-
audience:
|
|
623
|
+
clientDid: "did:key:zclient123",
|
|
624
|
+
audience: "https://client.example.com",
|
|
624
625
|
});
|
|
625
626
|
|
|
626
627
|
const sessionWithId = {
|
|
627
628
|
id: handshakeResponse.sessionId,
|
|
628
|
-
audience:
|
|
629
|
-
nonce:
|
|
629
|
+
audience: "https://client.example.com",
|
|
630
|
+
nonce: "test-nonce",
|
|
630
631
|
};
|
|
631
632
|
|
|
632
633
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
633
634
|
requiresDelegation: true,
|
|
634
|
-
requiredScopes: [
|
|
635
|
+
requiredScopes: ["files:write"],
|
|
635
636
|
});
|
|
636
637
|
|
|
637
638
|
const complexArgs = {
|
|
638
639
|
nested: {
|
|
639
640
|
deeply: {
|
|
640
641
|
nested: {
|
|
641
|
-
value:
|
|
642
|
+
value: "test",
|
|
642
643
|
array: [1, 2, 3],
|
|
643
|
-
object: { key:
|
|
644
|
-
}
|
|
645
|
-
}
|
|
644
|
+
object: { key: "value" },
|
|
645
|
+
},
|
|
646
|
+
},
|
|
646
647
|
},
|
|
647
|
-
array: [{ id: 1 }, { id: 2 }]
|
|
648
|
+
array: [{ id: 1 }, { id: 2 }],
|
|
648
649
|
};
|
|
649
650
|
|
|
650
651
|
let resumeToken: string;
|
|
651
652
|
try {
|
|
652
653
|
await runtime.processToolCall(
|
|
653
|
-
|
|
654
|
+
"protectedTool",
|
|
654
655
|
complexArgs,
|
|
655
656
|
mockHandler,
|
|
656
657
|
sessionWithId
|
|
657
658
|
);
|
|
658
|
-
expect.fail(
|
|
659
|
+
expect.fail("Should have thrown DelegationRequiredError");
|
|
659
660
|
} catch (error: any) {
|
|
660
661
|
resumeToken = error.resumeToken;
|
|
661
662
|
}
|
|
@@ -663,11 +664,11 @@ describe('MCPIRuntimeBase - Route Interception', () => {
|
|
|
663
664
|
const result = await runtime.resumeToolCall(
|
|
664
665
|
resumeToken!,
|
|
665
666
|
mockHandler,
|
|
666
|
-
|
|
667
|
+
"delegation-token-123"
|
|
667
668
|
);
|
|
668
669
|
|
|
669
670
|
expect(mockHandler).toHaveBeenCalledWith(complexArgs);
|
|
670
|
-
expect(result).toEqual({ result:
|
|
671
|
+
expect(result).toEqual({ result: "success" });
|
|
671
672
|
});
|
|
672
673
|
});
|
|
673
674
|
});
|