@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
@@ -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 'vitest';
8
- import { MCPIRuntimeBase } from '../../runtime/base';
9
- import { ProviderRuntimeConfig } from '../../config';
10
- import { createMockProviders, MockClockProvider } from '../utils/mock-providers';
11
- import { DelegationRequiredError } from '../../types/tool-protection';
12
-
13
- describe('MCPIRuntimeBase - Route Interception', () => {
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: 'development',
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('generateResumeToken', () => {
50
- it('should generate unique resume tokens for different calls', async () => {
55
+ describe("generateResumeToken", () => {
56
+ it("should generate unique resume tokens for different calls", async () => {
51
57
  const call1 = {
52
- toolName: 'tool1',
53
- args: { arg: 'value1' },
54
- sessionId: 'session1',
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: 'tool2',
61
- args: { arg: 'value2' },
62
- sessionId: 'session2',
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('should generate cryptographically random tokens', async () => {
84
+ it("should generate cryptographically random tokens", async () => {
79
85
  const call = {
80
- toolName: 'tool1',
81
- args: { arg: 'value' },
82
- sessionId: 'session1',
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('should generate unique tokens even for identical calls', async () => {
110
+ it("should generate unique tokens even for identical calls", async () => {
105
111
  const call1 = {
106
- toolName: 'tool1',
107
- args: { arg: 'value1' },
108
- sessionId: 'session1',
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: 'tool1',
115
- args: { arg: 'value1' }, // Same args
116
- sessionId: 'session1',
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('buildConsentUrl', () => {
131
- it('should build consent URL with tool and scopes', () => {
136
+ describe("buildConsentUrl", () => {
137
+ it("should build consent URL with tool and scopes", () => {
132
138
  const session = {
133
- id: 'session123',
134
- agentDid: 'did:key:zagent123',
135
- audience: 'https://client.example.com'
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
- 'protectedTool',
141
- ['files:write', 'files:read'],
146
+ "protectedTool",
147
+ ["files:write", "files:read"],
142
148
  session,
143
- 'resume_token_123'
149
+ "resume_token_123"
144
150
  );
145
151
 
146
- expect(url).toContain('https://kya.vouched.id/bouncer/consent');
147
- expect(url).toContain('tool=protectedTool');
148
- expect(url).toContain('scopes=files%3Awrite%2Cfiles%3Aread');
149
- expect(url).toContain('session_id=session123');
150
- expect(url).toContain('agent_did=did%3Akey%3Azagent123');
151
- expect(url).toContain('resume_token=resume_token_123');
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('should build consent URL without resume token if not provided', () => {
160
+ it("should build consent URL without resume token if not provided", () => {
155
161
  const session = {
156
- id: 'session123',
157
- agentDid: 'did:key:zagent123',
158
- audience: 'https://client.example.com'
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('https://kya.vouched.id/bouncer/consent');
169
- expect(url).not.toContain('resume_token=');
170
+ expect(url).toContain("https://auth.example.com/consent");
171
+ expect(url).not.toContain("resume_token=");
170
172
  });
171
173
 
172
- it('should handle missing session fields gracefully', () => {
174
+ it("should handle missing session fields gracefully", () => {
173
175
  const buildUrl = (runtime as any).buildConsentUrl.bind(runtime);
174
176
  const url = buildUrl(
175
- 'protectedTool',
176
- ['files:write'],
177
+ "protectedTool",
178
+ ["files:write"],
177
179
  {},
178
- 'resume_token_123'
180
+ "resume_token_123"
179
181
  );
180
182
 
181
- expect(url).toContain('https://kya.vouched.id/bouncer/consent');
182
- expect(url).toContain('tool=protectedTool');
183
- expect(url).toContain('session_id=');
184
- expect(url).toContain('agent_did=');
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('should URL encode special characters in scopes', () => {
189
+ it("should URL encode special characters in scopes", () => {
188
190
  const session = {
189
- id: 'session123',
190
- agentDid: 'did:key:zagent123'
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
- 'protectedTool',
196
- ['files:write', 'scope with spaces', 'scope:with:colons'],
197
+ "protectedTool",
198
+ ["files:write", "scope with spaces", "scope:with:colons"],
197
199
  session
198
200
  );
199
201
 
200
- expect(url).toContain('scopes=files%3Awrite%2Cscope+with+spaces%2Cscope%3Awith%3Acolons');
202
+ expect(url).toContain(
203
+ "scopes=files%3Awrite%2Cscope+with+spaces%2Cscope%3Awith%3Acolons"
204
+ );
201
205
  });
202
206
  });
203
207
 
204
- describe('intercepted calls storage', () => {
205
- const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
208
+ describe("intercepted calls storage", () => {
209
+ const mockHandler = vi.fn().mockResolvedValue({ result: "success" });
206
210
  const session = {
207
- id: 'session123',
208
- audience: 'https://client.example.com',
209
- nonce: 'test-nonce'
211
+ id: "session123",
212
+ audience: "https://client.example.com",
213
+ nonce: "test-nonce",
210
214
  };
211
215
 
212
- it('should store intercepted call when delegation required', async () => {
216
+ it("should store intercepted call when delegation required", async () => {
213
217
  mockToolProtectionService.checkToolProtection.mockResolvedValue({
214
218
  requiresDelegation: true,
215
- requiredScopes: ['files:write']
219
+ requiredScopes: ["files:write"],
216
220
  });
217
221
 
218
222
  try {
219
223
  await runtime.processToolCall(
220
- 'protectedTool',
221
- { arg: 'value' },
224
+ "protectedTool",
225
+ { arg: "value" },
222
226
  mockHandler,
223
227
  session
224
228
  );
225
- expect.fail('Should have thrown DelegationRequiredError');
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('protectedTool');
234
- expect(storedCall.args).toEqual({ arg: 'value' });
235
- expect(storedCall.sessionId).toBe('session123');
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('should set expiry time for intercepted calls', async () => {
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: ['files:write']
250
+ requiredScopes: ["files:write"],
247
251
  });
248
252
 
249
253
  try {
250
254
  await runtime.processToolCall(
251
- 'protectedTool',
252
- { arg: 'value' },
255
+ "protectedTool",
256
+ { arg: "value" },
253
257
  mockHandler,
254
258
  session
255
259
  );
256
- expect.fail('Should have thrown DelegationRequiredError');
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('resumeToolCall', () => {
266
- const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
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: 'did:key:zclient123',
274
- audience: 'https://client.example.com'
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: 'https://client.example.com',
280
- nonce: 'test-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: ['files:write']
290
+ requiredScopes: ["files:write"],
287
291
  });
288
292
 
289
293
  try {
290
294
  await runtime.processToolCall(
291
- 'protectedTool',
292
- { arg: 'value' },
295
+ "protectedTool",
296
+ { arg: "value" },
293
297
  mockHandler,
294
298
  session
295
299
  );
296
- expect.fail('Should have thrown DelegationRequiredError');
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('should resume tool call with valid resume token', async () => {
306
+ it("should resume tool call with valid resume token", async () => {
303
307
  mockToolProtectionService.checkToolProtection.mockResolvedValue({
304
308
  requiresDelegation: true,
305
- requiredScopes: ['files:write']
309
+ requiredScopes: ["files:write"],
306
310
  });
307
311
 
308
312
  const result = await runtime.resumeToolCall(
309
313
  resumeToken,
310
314
  mockHandler,
311
- 'delegation-token-123'
315
+ "delegation-token-123"
312
316
  );
313
317
 
314
- expect(mockHandler).toHaveBeenCalledWith({ arg: 'value' });
315
- expect(result).toEqual({ result: 'success' });
318
+ expect(mockHandler).toHaveBeenCalledWith({ arg: "value" });
319
+ expect(result).toEqual({ result: "success" });
316
320
  });
317
321
 
318
- it('should throw error for invalid resume token', async () => {
322
+ it("should throw error for invalid resume token", async () => {
319
323
  await expect(
320
324
  runtime.resumeToolCall(
321
- 'invalid-resume-token',
325
+ "invalid-resume-token",
322
326
  mockHandler,
323
- 'delegation-token-123'
327
+ "delegation-token-123"
324
328
  )
325
- ).rejects.toThrow('Invalid or expired resume token');
329
+ ).rejects.toThrow("Invalid or expired resume token");
326
330
  });
327
331
 
328
- it('should throw error for expired resume token', async () => {
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() + (30 * 60 * 1000) + 1000);
336
+ clock.setTime(Date.now() + 30 * 60 * 1000 + 1000);
333
337
 
334
338
  await expect(
335
- runtime.resumeToolCall(
336
- resumeToken,
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('should throw error if session not found', async () => {
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: 'protectedTool',
347
- args: { arg: 'value' },
348
- sessionId: 'non-existent-session',
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
- 'delegation-token-123'
363
+ "delegation-token-123"
364
364
  )
365
- ).rejects.toThrow('Session not found');
365
+ ).rejects.toThrow("Session not found");
366
366
  });
367
367
 
368
- it('should add delegation token to session when resuming', async () => {
368
+ it("should add delegation token to session when resuming", async () => {
369
369
  mockToolProtectionService.checkToolProtection.mockResolvedValue({
370
370
  requiresDelegation: true,
371
- requiredScopes: ['files:write']
371
+ requiredScopes: ["files:write"],
372
372
  });
373
373
 
374
374
  await runtime.resumeToolCall(
375
375
  resumeToken,
376
376
  mockHandler,
377
- 'delegation-token-123'
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('should clean up intercepted call after successful resumption', async () => {
384
+ it("should clean up intercepted call after successful resumption", async () => {
385
385
  mockToolProtectionService.checkToolProtection.mockResolvedValue({
386
386
  requiresDelegation: true,
387
- requiredScopes: ['files:write']
387
+ requiredScopes: ["files:write"],
388
388
  });
389
389
 
390
390
  await runtime.resumeToolCall(
391
391
  resumeToken,
392
392
  mockHandler,
393
- 'delegation-token-123'
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('should not clean up intercepted call if resumption fails', async () => {
402
- const errorHandler = vi.fn().mockRejectedValue(new Error('Handler failed'));
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
- 'delegation-token-123'
410
+ "delegation-token-123"
409
411
  );
410
- expect.fail('Should have thrown error');
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('cleanupExpiredInterceptedCalls', () => {
422
- it('should remove expired intercepted calls', async () => {
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: 'expiredTool',
429
- args: { arg: 'value' },
430
- sessionId: 'session123',
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: 'validTool',
437
- args: { arg: 'value' },
438
- sessionId: 'session123',
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(runtime);
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('should be called automatically when intercepting calls', async () => {
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: 'expiredTool',
467
- args: { arg: 'value' },
468
- sessionId: 'session123',
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: ['files:write']
486
+ requiredScopes: ["files:write"],
483
487
  });
484
488
 
485
489
  try {
486
- await runtime.processToolCall(
487
- 'newTool',
488
- { arg: 'value' },
489
- vi.fn(),
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('integration: full interception flow', () => {
503
- it('should complete full flow: intercept -> authorize -> resume', async () => {
504
- const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
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: 'session123',
507
- audience: 'https://client.example.com',
508
- nonce: 'test-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: 'did:key:zclient123',
514
- audience: 'https://client.example.com'
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: 'https://client.example.com',
520
- nonce: 'test-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: ['files:write']
527
+ requiredScopes: ["files:write"],
527
528
  });
528
529
 
529
530
  let resumeToken: string;
530
531
  try {
531
532
  await runtime.processToolCall(
532
- 'protectedTool',
533
- { arg: 'value' },
533
+ "protectedTool",
534
+ { arg: "value" },
534
535
  mockHandler,
535
536
  sessionWithId
536
537
  );
537
- expect.fail('Should have thrown DelegationRequiredError');
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
- 'delegation-token-123'
550
+ "delegation-token-123"
550
551
  );
551
552
 
552
553
  // Step 5: Verify successful execution
553
- expect(mockHandler).toHaveBeenCalledWith({ arg: 'value' });
554
- expect(result).toEqual({ result: 'success' });
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('edge cases', () => {
563
- it('should handle resume token reuse attempt', async () => {
564
- const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
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: 'did:key:zclient123',
569
- audience: 'https://client.example.com'
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: 'https://client.example.com',
575
- nonce: 'test-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: ['files:write']
581
+ requiredScopes: ["files:write"],
581
582
  });
582
583
 
583
584
  let resumeToken: string;
584
585
  try {
585
586
  await runtime.processToolCall(
586
- 'protectedTool',
587
- { arg: 'value' },
587
+ "protectedTool",
588
+ { arg: "value" },
588
589
  mockHandler,
589
590
  sessionWithId
590
591
  );
591
- expect.fail('Should have thrown DelegationRequiredError');
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
- 'delegation-token-123'
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
- 'delegation-token-123'
609
+ "delegation-token-123"
609
610
  )
610
- ).rejects.toThrow('Invalid or expired resume token');
611
+ ).rejects.toThrow("Invalid or expired resume token");
611
612
  });
612
613
 
613
- it('should handle complex nested args in intercepted calls', async () => {
614
- const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
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: 'session123',
617
- audience: 'https://client.example.com',
618
- nonce: 'test-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: 'did:key:zclient123',
623
- audience: 'https://client.example.com'
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: 'https://client.example.com',
629
- nonce: 'test-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: ['files:write']
635
+ requiredScopes: ["files:write"],
635
636
  });
636
637
 
637
638
  const complexArgs = {
638
639
  nested: {
639
640
  deeply: {
640
641
  nested: {
641
- value: 'test',
642
+ value: "test",
642
643
  array: [1, 2, 3],
643
- object: { key: 'value' }
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
- 'protectedTool',
654
+ "protectedTool",
654
655
  complexArgs,
655
656
  mockHandler,
656
657
  sessionWithId
657
658
  );
658
- expect.fail('Should have thrown DelegationRequiredError');
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
- 'delegation-token-123'
667
+ "delegation-token-123"
667
668
  );
668
669
 
669
670
  expect(mockHandler).toHaveBeenCalledWith(complexArgs);
670
- expect(result).toEqual({ result: 'success' });
671
+ expect(result).toEqual({ result: "success" });
671
672
  });
672
673
  });
673
674
  });