@kya-os/mcp-i-cloudflare 1.5.8-canary.7 → 1.5.8-canary.71

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 (93) hide show
  1. package/README.md +130 -0
  2. package/dist/__tests__/e2e/test-config.d.ts +37 -0
  3. package/dist/__tests__/e2e/test-config.d.ts.map +1 -0
  4. package/dist/__tests__/e2e/test-config.js +62 -0
  5. package/dist/__tests__/e2e/test-config.js.map +1 -0
  6. package/dist/adapter.d.ts +44 -0
  7. package/dist/adapter.d.ts.map +1 -1
  8. package/dist/adapter.js +677 -88
  9. package/dist/adapter.js.map +1 -1
  10. package/dist/agent.d.ts +79 -25
  11. package/dist/agent.d.ts.map +1 -1
  12. package/dist/agent.js +521 -40
  13. package/dist/agent.js.map +1 -1
  14. package/dist/app.d.ts +0 -8
  15. package/dist/app.d.ts.map +1 -1
  16. package/dist/app.js +208 -55
  17. package/dist/app.js.map +1 -1
  18. package/dist/cache/kv-oauth-config-cache.d.ts +47 -0
  19. package/dist/cache/kv-oauth-config-cache.d.ts.map +1 -0
  20. package/dist/cache/kv-oauth-config-cache.js +82 -0
  21. package/dist/cache/kv-oauth-config-cache.js.map +1 -0
  22. package/dist/config.d.ts.map +1 -1
  23. package/dist/config.js +33 -4
  24. package/dist/config.js.map +1 -1
  25. package/dist/helpers/env-mapper.d.ts +60 -1
  26. package/dist/helpers/env-mapper.d.ts.map +1 -1
  27. package/dist/helpers/env-mapper.js +136 -6
  28. package/dist/helpers/env-mapper.js.map +1 -1
  29. package/dist/index.d.ts +4 -2
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +8 -3
  32. package/dist/index.js.map +1 -1
  33. package/dist/runtime/audit-logger.d.ts +96 -0
  34. package/dist/runtime/audit-logger.d.ts.map +1 -0
  35. package/dist/runtime/audit-logger.js +276 -0
  36. package/dist/runtime/audit-logger.js.map +1 -0
  37. package/dist/runtime/oauth-handler.d.ts +5 -0
  38. package/dist/runtime/oauth-handler.d.ts.map +1 -1
  39. package/dist/runtime/oauth-handler.js +287 -35
  40. package/dist/runtime/oauth-handler.js.map +1 -1
  41. package/dist/runtime.d.ts +12 -1
  42. package/dist/runtime.d.ts.map +1 -1
  43. package/dist/runtime.js +34 -4
  44. package/dist/runtime.js.map +1 -1
  45. package/dist/server.d.ts +7 -0
  46. package/dist/server.d.ts.map +1 -1
  47. package/dist/server.js +93 -3
  48. package/dist/server.js.map +1 -1
  49. package/dist/services/admin.service.d.ts.map +1 -1
  50. package/dist/services/admin.service.js +89 -10
  51. package/dist/services/admin.service.js.map +1 -1
  52. package/dist/services/consent-audit.service.d.ts +91 -0
  53. package/dist/services/consent-audit.service.d.ts.map +1 -0
  54. package/dist/services/consent-audit.service.js +243 -0
  55. package/dist/services/consent-audit.service.js.map +1 -0
  56. package/dist/services/consent-config.service.d.ts +2 -2
  57. package/dist/services/consent-config.service.d.ts.map +1 -1
  58. package/dist/services/consent-config.service.js +55 -24
  59. package/dist/services/consent-config.service.js.map +1 -1
  60. package/dist/services/consent-page-renderer.d.ts +14 -0
  61. package/dist/services/consent-page-renderer.d.ts.map +1 -1
  62. package/dist/services/consent-page-renderer.js +42 -0
  63. package/dist/services/consent-page-renderer.js.map +1 -1
  64. package/dist/services/consent.service.d.ts +82 -4
  65. package/dist/services/consent.service.d.ts.map +1 -1
  66. package/dist/services/consent.service.js +1653 -34
  67. package/dist/services/consent.service.js.map +1 -1
  68. package/dist/services/delegation.service.d.ts.map +1 -1
  69. package/dist/services/delegation.service.js +67 -29
  70. package/dist/services/delegation.service.js.map +1 -1
  71. package/dist/services/idp-token-storage.d.ts +68 -0
  72. package/dist/services/idp-token-storage.d.ts.map +1 -0
  73. package/dist/services/idp-token-storage.js +157 -0
  74. package/dist/services/idp-token-storage.js.map +1 -0
  75. package/dist/services/oauth-service.d.ts +66 -0
  76. package/dist/services/oauth-service.d.ts.map +1 -0
  77. package/dist/services/oauth-service.js +223 -0
  78. package/dist/services/oauth-service.js.map +1 -0
  79. package/dist/services/proof.service.d.ts +8 -6
  80. package/dist/services/proof.service.d.ts.map +1 -1
  81. package/dist/services/proof.service.js +131 -75
  82. package/dist/services/proof.service.js.map +1 -1
  83. package/dist/services/tool-context-builder.d.ts +55 -0
  84. package/dist/services/tool-context-builder.d.ts.map +1 -0
  85. package/dist/services/tool-context-builder.js +124 -0
  86. package/dist/services/tool-context-builder.js.map +1 -0
  87. package/dist/types/tool-context.d.ts +35 -0
  88. package/dist/types/tool-context.d.ts.map +1 -0
  89. package/dist/types/tool-context.js +13 -0
  90. package/dist/types/tool-context.js.map +1 -0
  91. package/dist/types.d.ts +30 -0
  92. package/dist/types.d.ts.map +1 -1
  93. package/package.json +27 -60
package/dist/agent.js CHANGED
@@ -10,8 +10,12 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
10
10
  import { CloudflareRuntime } from "./runtime";
11
11
  import { createCloudflareRuntime } from "./index";
12
12
  import { mapPrefixedEnv } from "./helpers/env-mapper";
13
- // Global registry for agent options (set by createMCPIApp)
14
- const agentOptionsRegistry = new Map();
13
+ import { STORAGE_KEYS } from "./constants/storage-keys";
14
+ import { z } from "zod";
15
+ import { OAuthConfigService, OAuthService, IdpTokenResolver, ToolContextBuilder, DelegationRequiredError, } from "@kya-os/mcp-i-core";
16
+ import { KVOAuthConfigCache } from "./cache/kv-oauth-config-cache";
17
+ import { IdpTokenStorage } from "./services/idp-token-storage";
18
+ import { OAuthSecurityService } from "./services/oauth-security.service";
15
19
  /**
16
20
  * Base class for MCP agents with MCP-I features
17
21
  *
@@ -21,45 +25,45 @@ const agentOptionsRegistry = new Map();
21
25
  * - Session management
22
26
  * - Durable Object routing
23
27
  *
24
- * Users extend this class and implement `registerTools()` to add their custom tools.
28
+ * Users extend this class and implement:
29
+ * - `getAgentName()`: Return the agent name
30
+ * - `getAgentVersion()`: Return the agent version (optional)
31
+ * - `getEnvPrefix()`: Return the KV environment prefix (optional)
32
+ * - `getRuntimeConfig()`: Return the runtime configuration
33
+ * - `registerTools()`: Register tools with the MCP server
25
34
  */
26
35
  export class MCPICloudflareAgent extends McpAgent {
27
- // @ts-ignore - Type incompatibility between different versions of @modelcontextprotocol/sdk
28
- // Runtime behavior is correct, this is a type-only issue
29
36
  // Initialize server as class field (before constructor) so parent class can access it
37
+ // Note: SDK 1.x uses single parameter constructor (server info only)
30
38
  server = new McpServer({
31
39
  name: "MCP-I Agent",
32
40
  version: "1.0.0",
33
41
  });
34
42
  mcpiRuntime;
35
43
  env;
36
- options;
37
44
  constructor(state, env) {
38
45
  // Call super() with just state and env (agents@0.2.21+ only accepts 2 parameters)
39
46
  // The config is no longer passed to the constructor - it's set via the server property
40
47
  super(state, env);
41
- // Get options from registry AFTER calling super() (can now access 'this')
42
- const AgentClass = new.target;
43
- const options = agentOptionsRegistry.get(AgentClass);
44
- // Now we can safely access 'this' and set properties
45
- if (!options) {
46
- throw new Error(`Agent options not found. Make sure to call createMCPIApp() before instantiating ${AgentClass.name}.`);
47
- }
48
48
  this.env = env;
49
- this.options = options;
50
- // Update server with correct config from options (override the default)
49
+ // Get agent configuration from subclass methods
50
+ const agentName = this.getAgentName();
51
+ const agentVersion = this.getAgentVersion();
52
+ const envPrefix = this.getEnvPrefix();
53
+ // Update server with correct config
54
+ // Note: SDK 1.x uses single parameter constructor (server info only)
51
55
  this.server = new McpServer({
52
- name: this.options.name,
53
- version: this.options.version || "1.0.0",
56
+ name: agentName,
57
+ version: agentVersion || "1.0.0",
54
58
  });
55
59
  // Map prefixed environment to standard CloudflareEnv
56
- const mappedEnv = this.options.envPrefix
57
- ? mapPrefixedEnv(env, this.options.envPrefix)
60
+ const mappedEnv = envPrefix
61
+ ? mapPrefixedEnv(env, envPrefix)
58
62
  : env;
59
- // Add DurableObjectState to mapped env
63
+ // Add DurableObjectState to mapped env for identity persistence
60
64
  mappedEnv._durableObjectState = state;
61
- // Load runtime configuration
62
- const runtimeConfig = this.options.getRuntimeConfig(mappedEnv);
65
+ // Load runtime configuration from subclass
66
+ const runtimeConfig = this.getRuntimeConfigInternal(mappedEnv);
63
67
  // Create tool protection service if configured
64
68
  const toolProtectionService = runtimeConfig.toolProtection &&
65
69
  mappedEnv.TOOL_PROTECTION_KV &&
@@ -73,10 +77,13 @@ export class MCPICloudflareAgent extends McpAgent {
73
77
  mappedEnv.AGENTSHIELD_PROJECT_ID,
74
78
  cacheTtl: runtimeConfig.toolProtection.agentShield?.cacheTtl || 300000,
75
79
  debug: runtimeConfig.environment === "development",
80
+ // Wrap fallback ToolProtectionMap into ToolProtectionConfig format
81
+ // Handle both wrapped and unwrapped formats for backward compatibility
76
82
  fallbackConfig: runtimeConfig.toolProtection.fallback
77
- ? {
78
- toolProtections: runtimeConfig.toolProtection.fallback,
79
- }
83
+ ? runtimeConfig.toolProtection.fallback.toolProtections !==
84
+ undefined
85
+ ? runtimeConfig.toolProtection.fallback // Already wrapped
86
+ : { toolProtections: runtimeConfig.toolProtection.fallback } // Wrap it
80
87
  : undefined,
81
88
  })
82
89
  : undefined;
@@ -94,40 +101,185 @@ export class MCPICloudflareAgent extends McpAgent {
94
101
  });
95
102
  }
96
103
  /**
97
- * Register options for an agent class
98
- * Called internally by createMCPIApp
104
+ * Get agent version
105
+ * Subclasses can override this to provide a custom version
106
+ */
107
+ getAgentVersion() {
108
+ return "1.0.0";
109
+ }
110
+ /**
111
+ * Get environment prefix for KV bindings
112
+ * Subclasses can override this for multi-agent deployments
99
113
  */
100
- static registerOptions(AgentClass, options) {
101
- agentOptionsRegistry.set(AgentClass, options);
114
+ getEnvPrefix() {
115
+ return undefined;
102
116
  }
103
117
  /**
104
118
  * Initialize the agent
105
119
  * Call this after construction to set up the runtime and register tools
106
120
  */
107
121
  async init() {
108
- await this.mcpiRuntime?.initialize();
109
- const identity = await this.mcpiRuntime?.getIdentity();
110
- console.log("[MCP-I] Initialized with DID:", identity?.did);
111
- // Register tools (implemented by subclasses)
112
- await this.registerTools();
122
+ try {
123
+ await this.mcpiRuntime?.initialize();
124
+ const identity = await this.mcpiRuntime?.getIdentity();
125
+ console.log("[MCP-I] Initialized with DID:", identity?.did);
126
+ // Ensure server is initialized before registering tools
127
+ if (!this.server) {
128
+ throw new Error("Server not initialized. This should not happen - server is initialized in constructor.");
129
+ }
130
+ // Register tools (implemented by subclasses)
131
+ await this.registerTools();
132
+ }
133
+ catch (error) {
134
+ console.error("[MCPICloudflareAgent] Initialization failed:", error);
135
+ throw error;
136
+ }
137
+ }
138
+ /**
139
+ * Register a tool with automatic session ID extraction and proof generation
140
+ *
141
+ * This helper method automatically extracts `sessionId` from `ToolExtraArguments`
142
+ * and passes it to `executeToolWithProof`, eliminating the need for manual extraction
143
+ * in every tool handler.
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * // For JSON Schema format (from template):
148
+ * this.registerToolWithProof(
149
+ * greetTool.name,
150
+ * greetTool.description,
151
+ * greetTool.inputSchema, // Full JSON Schema object
152
+ * greetTool.handler
153
+ * );
154
+ *
155
+ * // For Zod schema format (from examples):
156
+ * this.registerToolWithProof(
157
+ * greetTool.name,
158
+ * greetTool.description,
159
+ * greetTool.inputSchema.shape, // Zod schema shape
160
+ * greetTool.handler
161
+ * );
162
+ * ```
163
+ *
164
+ * @param name - Tool name
165
+ * @param description - Tool description
166
+ * @param schema - Tool input schema (JSON Schema object OR Zod schema shape)
167
+ * @param handler - Tool handler function that receives args and returns result
168
+ */
169
+ registerToolWithProof(name, description, schema, // Accept JSON Schema object or Zod schema shape
170
+ handler) {
171
+ // Safety check: ensure server is initialized
172
+ if (!this.server) {
173
+ throw new Error(`Cannot register tool "${name}": server not initialized. Make sure to call init() before registering tools.`);
174
+ }
175
+ try {
176
+ // Normalize schema format for MCP SDK
177
+ // MCP SDK expects Zod schema shape (Record<string, ZodType>), not JSON Schema
178
+ // Convert JSON Schema to Zod schema shape if needed
179
+ let zodSchemaShape = {};
180
+ if (schema && typeof schema === "object") {
181
+ // Check if it's already a Zod schema shape (has ZodType instances)
182
+ if (schema.type === "object" && schema.properties) {
183
+ // It's a JSON Schema object - convert to Zod schema shape
184
+ const jsonSchema = schema;
185
+ const requiredFields = new Set(jsonSchema.required || []);
186
+ zodSchemaShape = Object.fromEntries(Object.entries(jsonSchema.properties).map(([key, prop]) => {
187
+ // Convert JSON Schema property to Zod type
188
+ let zodType;
189
+ if (prop.type === "string") {
190
+ zodType = z.string();
191
+ if (prop.description) {
192
+ zodType = zodType.describe(prop.description);
193
+ }
194
+ }
195
+ else if (prop.type === "number") {
196
+ zodType = z.number();
197
+ if (prop.description) {
198
+ zodType = zodType.describe(prop.description);
199
+ }
200
+ }
201
+ else if (prop.type === "boolean") {
202
+ zodType = z.boolean();
203
+ if (prop.description) {
204
+ zodType = zodType.describe(prop.description);
205
+ }
206
+ }
207
+ else if (prop.type === "array") {
208
+ zodType = z.array(z.any());
209
+ if (prop.description) {
210
+ zodType = zodType.describe(prop.description);
211
+ }
212
+ }
213
+ else {
214
+ // Fallback to any for unknown types
215
+ zodType = z.any();
216
+ if (prop.description) {
217
+ zodType = zodType.describe(prop.description);
218
+ }
219
+ }
220
+ // Make optional if not in required array
221
+ if (!requiredFields.has(key)) {
222
+ zodType = zodType.optional();
223
+ }
224
+ return [key, zodType];
225
+ }));
226
+ }
227
+ else {
228
+ // Assume it's already a Zod schema shape or compatible object
229
+ zodSchemaShape = schema;
230
+ }
231
+ }
232
+ this.server.tool(name, description, zodSchemaShape, async (args, extra) => {
233
+ // ✅ Automatically extract sessionId from ToolExtraArguments
234
+ // This ensures delegation tokens stored with the original session ID
235
+ // can be retrieved on subsequent tool calls
236
+ // Note: extra is typed as 'any' to match MCP SDK's ToolExtraArguments type
237
+ const sessionId = extra?.sessionId;
238
+ return this.executeToolWithProof(name, args, handler, sessionId);
239
+ });
240
+ }
241
+ catch (error) {
242
+ console.error(`[MCPICloudflareAgent] Failed to register tool "${name}":`, error);
243
+ throw error;
244
+ }
113
245
  }
114
246
  /**
115
247
  * Execute a tool with automatic proof generation
116
248
  * Use this helper method when registering tools to ensure proofs are generated
249
+ *
250
+ * @param toolName - Name of the tool being executed
251
+ * @param args - Tool arguments
252
+ * @param handler - Tool handler function
253
+ * @param providedSessionId - Optional session ID from MCP request (extracted from ToolExtraArguments.sessionId)
254
+ * If provided, uses this session ID for delegation token lookup.
255
+ * If not provided, falls back to getSessionId() or generates ephemeral ID.
117
256
  */
118
- async executeToolWithProof(toolName, args, handler) {
257
+ async executeToolWithProof(toolName, args, handler, providedSessionId) {
119
258
  if (!this.mcpiRuntime) {
120
259
  throw new Error("MCP-I runtime not initialized. Call init() first.");
121
260
  }
122
261
  // Create session
262
+ // ✅ CRITICAL: Use provided sessionId if available (from MCP request params)
263
+ // This ensures delegation tokens stored with the original session ID can be retrieved
123
264
  const timestamp = Date.now();
124
- const sessionId = this.getSessionId() ||
265
+ const sessionId = providedSessionId ||
266
+ this.getSessionId() ||
125
267
  `ephemeral-${timestamp}-${Math.random().toString(36).substring(2, 10)}`;
268
+ // Log session ID source for debugging
269
+ const envPrefix = this.getEnvPrefix();
270
+ const mappedEnv = envPrefix
271
+ ? mapPrefixedEnv(this.env, envPrefix)
272
+ : this.env;
273
+ if (this.getRuntimeConfigInternal(mappedEnv).environment === "development") {
274
+ console.log("[MCPICloudflareAgent] Session ID source:", {
275
+ provided: !!providedSessionId,
276
+ fromGetSessionId: !!this.getSessionId(),
277
+ isEphemeral: !providedSessionId && !this.getSessionId(),
278
+ sessionId: sessionId.slice(0, 20) + "...",
279
+ });
280
+ }
126
281
  // Get server URL from env var (MCP_SERVER_URL) for consent URL building
127
282
  // In production, this should be set to the deployed worker URL
128
- const mappedEnv = this.options?.envPrefix
129
- ? mapPrefixedEnv(this.env, this.options.envPrefix)
130
- : this.env;
131
283
  let serverUrl = mappedEnv.MCP_SERVER_URL;
132
284
  // Ensure URL has protocol (https://) if provided
133
285
  if (serverUrl &&
@@ -136,6 +288,92 @@ export class MCPICloudflareAgent extends McpAgent {
136
288
  // Auto-add https:// if protocol missing
137
289
  serverUrl = `https://${serverUrl}`;
138
290
  }
291
+ // ✅ Look up delegation token from KV storage (3-priority system)
292
+ let delegationToken;
293
+ let userDid; // Extract userDid for IDP token resolution
294
+ const delegationStorage = mappedEnv.DELEGATION_STORAGE;
295
+ if (delegationStorage) {
296
+ try {
297
+ const agentDid = (await this.mcpiRuntime.getIdentity()).did;
298
+ console.log("[MCPICloudflareAgent] 🔍 Starting delegation token lookup:", {
299
+ sessionId: sessionId.slice(0, 20) + "...",
300
+ agentDid: agentDid.slice(0, 20) + "...",
301
+ hasDelegationStorage: !!delegationStorage,
302
+ });
303
+ // PRIORITY 1: User+Agent scoped token (user-specific, most secure)
304
+ // Try to get userDid from session cache
305
+ const sessionKey = STORAGE_KEYS.session(sessionId);
306
+ const sessionData = (await delegationStorage.get(sessionKey, "json"));
307
+ userDid = sessionData?.userDid;
308
+ if (userDid) {
309
+ const userAgentKey = STORAGE_KEYS.delegation(userDid, agentDid);
310
+ console.log("[MCPICloudflareAgent] 🔍 PRIORITY 1: Checking user+agent scoped key:", {
311
+ key: userAgentKey,
312
+ userDid: userDid.slice(0, 20) + "...",
313
+ agentDid: agentDid.slice(0, 20) + "...",
314
+ });
315
+ const userAgentToken = await delegationStorage.get(userAgentKey, "text");
316
+ console.log("[MCPICloudflareAgent] 🔍 PRIORITY 1: User+agent scoped lookup result:", {
317
+ found: !!userAgentToken,
318
+ tokenLength: userAgentToken?.length || 0,
319
+ });
320
+ if (userAgentToken) {
321
+ delegationToken = userAgentToken;
322
+ }
323
+ }
324
+ // PRIORITY 2: Session-scoped token (if session ID persists)
325
+ if (!delegationToken) {
326
+ console.log("[MCPICloudflareAgent] 🔍 PRIORITY 2: Checking session-scoped key:", {
327
+ key: sessionKey,
328
+ sessionId: sessionId.slice(0, 20) + "...",
329
+ });
330
+ const sessionTokenData = (await delegationStorage.get(sessionKey, "json"));
331
+ console.log("[MCPICloudflareAgent] 🔍 PRIORITY 2: Session-scoped lookup result:", {
332
+ found: !!sessionTokenData,
333
+ hasDelegationToken: !!sessionTokenData?.delegationToken,
334
+ tokenLength: sessionTokenData?.delegationToken?.length || 0,
335
+ });
336
+ if (sessionTokenData?.delegationToken) {
337
+ delegationToken = sessionTokenData.delegationToken;
338
+ }
339
+ }
340
+ // PRIORITY 3: Agent-scoped token (legacy fallback - DEPRECATED)
341
+ // Only use when userDid is unavailable (backward compatibility)
342
+ if (!delegationToken && !userDid) {
343
+ const agentKey = STORAGE_KEYS.legacyDelegation(agentDid);
344
+ console.warn("[MCPICloudflareAgent] ⚠️ DEPRECATION: Using agent-scoped token (legacy format). Migrate to user+agent scoped tokens for proper user isolation.", {
345
+ key: agentKey,
346
+ agentDid: agentDid.slice(0, 20) + "...",
347
+ reason: "userDid unavailable",
348
+ });
349
+ console.log("[MCPICloudflareAgent] 🔍 PRIORITY 3: Checking agent-scoped key (legacy):", {
350
+ key: agentKey,
351
+ agentDid: agentDid.slice(0, 20) + "...",
352
+ });
353
+ delegationToken = await delegationStorage.get(agentKey, "text");
354
+ console.log("[MCPICloudflareAgent] 🔍 PRIORITY 3: Agent-scoped lookup result:", {
355
+ found: !!delegationToken,
356
+ tokenLength: delegationToken?.length || 0,
357
+ });
358
+ }
359
+ if (delegationToken) {
360
+ console.log("[MCPICloudflareAgent] ✅ Delegation token retrieved:", {
361
+ sessionId: sessionId.slice(0, 20) + "...",
362
+ tokenLength: delegationToken.length,
363
+ source: "kv-storage",
364
+ });
365
+ }
366
+ else {
367
+ console.log("[MCPICloudflareAgent] ⚠️ No delegation token found in KV storage:", {
368
+ sessionId: sessionId.slice(0, 20) + "...",
369
+ agentDid: agentDid.slice(0, 20) + "...",
370
+ });
371
+ }
372
+ }
373
+ catch (error) {
374
+ console.warn("[MCPICloudflareAgent] Failed to retrieve delegation token:", error);
375
+ }
376
+ }
139
377
  const session = {
140
378
  id: sessionId,
141
379
  audience: "https://kya.vouched.id",
@@ -143,11 +381,254 @@ export class MCPICloudflareAgent extends McpAgent {
143
381
  createdAt: timestamp,
144
382
  expiresAt: timestamp + 30 * 60 * 1000, // 30 minutes
145
383
  serverOrigin: serverUrl, // Use MCP_SERVER_URL for consent URL building
384
+ delegationToken, // ✅ Include token in session if found
385
+ };
386
+ // ✅ NEW: Build tool execution context with IDP token (Phase 1 - MH-7)
387
+ let toolContext;
388
+ try {
389
+ toolContext = await this.buildToolContext(toolName, userDid, sessionId, delegationToken, mappedEnv);
390
+ }
391
+ catch (error) {
392
+ // Handle OAuthRequiredError - build OAuth URL and throw DelegationRequiredError
393
+ // Use property checks instead of instanceof for Cloudflare Workers compatibility
394
+ if (error instanceof Error &&
395
+ (error.name === "OAuthRequiredError" ||
396
+ error.constructor?.name === "OAuthRequiredError" ||
397
+ ("oauthUrl" in error &&
398
+ "provider" in error &&
399
+ "requiredScopes" in error))) {
400
+ try {
401
+ // Type assertion for OAuthRequiredError properties
402
+ const oauthError = error;
403
+ const oauthUrl = await this.buildOAuthUrlForError(oauthError, sessionId, mappedEnv);
404
+ // Generate resume token using runtime's internal method
405
+ // The runtime will generate it when DelegationRequiredError is thrown
406
+ const resumeToken = `resume_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
407
+ // Throw DelegationRequiredError with OAuth URL
408
+ throw new DelegationRequiredError(oauthError.toolName, oauthError.requiredScopes, oauthUrl, // Use OAuth URL as consent URL
409
+ undefined, // interceptedCall
410
+ resumeToken);
411
+ }
412
+ catch (oauthError) {
413
+ // If OAuth URL building fails, re-throw the original error
414
+ throw oauthError instanceof DelegationRequiredError
415
+ ? oauthError
416
+ : error;
417
+ }
418
+ }
419
+ // Log other errors but don't fail tool execution (backward compatibility)
420
+ console.warn("[MCPICloudflareAgent] Failed to build tool context:", error);
421
+ }
422
+ // Wrap handler to pass context as optional second parameter
423
+ // For backward compatibility, handlers can accept (args) or (args, context)
424
+ // If handler doesn't accept context, it will be ignored
425
+ const handlerWithContext = async (handlerArgs) => {
426
+ // Try to call handler with context if available
427
+ // TypeScript function types don't preserve length, so we try both signatures
428
+ try {
429
+ // Try calling with context (for handlers that accept it)
430
+ if (toolContext) {
431
+ return handler(handlerArgs, toolContext);
432
+ }
433
+ else {
434
+ return handler(handlerArgs);
435
+ }
436
+ }
437
+ catch (error) {
438
+ // If handler doesn't accept context parameter, fall back to args-only call
439
+ // This maintains backward compatibility
440
+ return handler(handlerArgs);
441
+ }
146
442
  };
147
443
  // Execute tool with automatic proof generation
148
- const result = await this.mcpiRuntime.processToolCall(toolName, args, handler, session);
444
+ const result = await this.mcpiRuntime.processToolCall(toolName, args, handlerWithContext, session);
149
445
  return result;
150
446
  }
447
+ /**
448
+ * Build tool execution context with IDP token (Phase 1 - MH-7)
449
+ *
450
+ * @private
451
+ */
452
+ async buildToolContext(toolName, userDid, sessionId, delegationToken, env) {
453
+ // Only build context if userDid is available and services are configured
454
+ if (!userDid || !env.AGENTSHIELD_API_KEY || !env.DELEGATION_STORAGE) {
455
+ return undefined;
456
+ }
457
+ // Check if tool protection service is available
458
+ const toolProtectionService = this.mcpiRuntime.config
459
+ ?.toolProtectionService;
460
+ if (!toolProtectionService) {
461
+ return undefined;
462
+ }
463
+ try {
464
+ // Get tool protection configuration
465
+ const agentDid = (await this.mcpiRuntime.getIdentity()).did;
466
+ const protection = await toolProtectionService.checkToolProtection(toolName, agentDid);
467
+ // Only build context if tool requires OAuth
468
+ if (!protection?.requiredScopes?.length) {
469
+ return undefined;
470
+ }
471
+ // Get project ID from tool protection service
472
+ const projectId = toolProtectionService.getProjectId();
473
+ if (!projectId) {
474
+ return undefined;
475
+ }
476
+ // Initialize OAuth services lazily
477
+ const fetchProvider = this.mcpiRuntime.fetch;
478
+ if (!fetchProvider) {
479
+ return undefined;
480
+ }
481
+ // Use KV cache for OAuth config if available, otherwise in-memory
482
+ const oauthConfigCache = env.TOOL_PROTECTION_KV
483
+ ? new KVOAuthConfigCache({ kv: env.TOOL_PROTECTION_KV })
484
+ : undefined;
485
+ const oauthConfigService = new OAuthConfigService({
486
+ baseUrl: env.AGENTSHIELD_API_URL || "https://kya.vouched.id",
487
+ apiKey: env.AGENTSHIELD_API_KEY,
488
+ fetchProvider,
489
+ cache: oauthConfigCache,
490
+ });
491
+ // Phase 2: Initialize provider registry and resolver
492
+ const { OAuthProviderRegistry, ProviderResolver } = await import("@kya-os/mcp-i-core");
493
+ const providerRegistry = new OAuthProviderRegistry(oauthConfigService);
494
+ const providerResolver = new ProviderResolver(providerRegistry, oauthConfigService);
495
+ const oauthService = new OAuthService({
496
+ configService: oauthConfigService,
497
+ fetchProvider,
498
+ agentShieldApiUrl: env.AGENTSHIELD_API_URL || "https://kya.vouched.id",
499
+ agentShieldApiKey: env.AGENTSHIELD_API_KEY,
500
+ projectId,
501
+ });
502
+ const oauthSecurityService = new OAuthSecurityService(env.DELEGATION_STORAGE, env.OAUTH_ENCRYPTION_SECRET);
503
+ // Only create IdpTokenStorage if DELEGATION_STORAGE is available
504
+ if (!env.DELEGATION_STORAGE) {
505
+ console.warn("[MCPICloudflareAgent] DELEGATION_STORAGE not configured, skipping IDP token storage");
506
+ return undefined;
507
+ }
508
+ const idpTokenStorage = new IdpTokenStorage({
509
+ storage: env.DELEGATION_STORAGE,
510
+ oauthSecurityService,
511
+ });
512
+ const tokenResolver = new IdpTokenResolver({
513
+ tokenStorage: idpTokenStorage,
514
+ oauthService,
515
+ });
516
+ const contextBuilder = new ToolContextBuilder({
517
+ tokenResolver,
518
+ configService: oauthConfigService,
519
+ providerResolver: providerResolver, // Phase 2: Pass ProviderResolver (type assertion for dist/src compatibility)
520
+ projectId,
521
+ });
522
+ // Build context
523
+ return await contextBuilder.buildContext(toolName, userDid, sessionId, delegationToken, protection);
524
+ }
525
+ catch (error) {
526
+ // Re-throw OAuthRequiredError so it can be handled by executeToolWithProof
527
+ // Use property checks instead of instanceof for Cloudflare Workers compatibility
528
+ if (error instanceof Error &&
529
+ (error.name === "OAuthRequiredError" ||
530
+ error.constructor?.name === "OAuthRequiredError" ||
531
+ ("oauthUrl" in error &&
532
+ "provider" in error &&
533
+ "requiredScopes" in error))) {
534
+ throw error;
535
+ }
536
+ console.warn("[MCPICloudflareAgent] Failed to build tool context:", error);
537
+ return undefined;
538
+ }
539
+ }
540
+ /**
541
+ * Build OAuth URL for OAuthRequiredError
542
+ *
543
+ * Creates OAuth authorization URL with PKCE challenge for secure token exchange.
544
+ *
545
+ * @private
546
+ */
547
+ async buildOAuthUrlForError(error, sessionId, env) {
548
+ const agentShieldUrl = env.AGENTSHIELD_API_URL || "https://kya.vouched.id";
549
+ const toolProtectionService = this.mcpiRuntime.config
550
+ ?.toolProtectionService;
551
+ const projectId = toolProtectionService?.getProjectId();
552
+ if (!projectId) {
553
+ throw new Error("Project ID not available for OAuth URL building");
554
+ }
555
+ // Get server URL
556
+ let serverUrl = env.MCP_SERVER_URL;
557
+ if (serverUrl &&
558
+ !serverUrl.startsWith("http://") &&
559
+ !serverUrl.startsWith("https://")) {
560
+ serverUrl = `https://${serverUrl}`;
561
+ }
562
+ if (!serverUrl) {
563
+ throw new Error("MCP_SERVER_URL not configured for OAuth callback");
564
+ }
565
+ // TypeScript: projectId is guaranteed to be defined after the check above
566
+ // Use non-null assertion since we've already validated it
567
+ const safeProjectId = projectId;
568
+ const agentDid = (await this.mcpiRuntime.getIdentity()).did;
569
+ // Generate PKCE challenge if OAuthSecurityService is available
570
+ let codeChallenge;
571
+ let stateParam;
572
+ if (env.DELEGATION_STORAGE && env.OAUTH_ENCRYPTION_SECRET) {
573
+ try {
574
+ const oauthSecurityService = new OAuthSecurityService(env.DELEGATION_STORAGE, env.OAUTH_ENCRYPTION_SECRET);
575
+ const pkceChallenge = await oauthSecurityService.generatePKCEChallenge();
576
+ codeChallenge = pkceChallenge.challenge;
577
+ // Build state data
578
+ const stateData = {
579
+ project_id: safeProjectId,
580
+ agent_did: agentDid,
581
+ session_id: sessionId || "",
582
+ scopes: error.requiredScopes,
583
+ storedAt: Date.now(),
584
+ code_verifier: pkceChallenge.verifier,
585
+ code_challenge: pkceChallenge.challenge,
586
+ redirect_uri: `${serverUrl}/oauth/callback`,
587
+ };
588
+ // Store state securely
589
+ const randomBytes = crypto.getRandomValues(new Uint8Array(32));
590
+ const stateValue = btoa(String.fromCharCode(...randomBytes))
591
+ .replace(/\+/g, "-")
592
+ .replace(/\//g, "_")
593
+ .replace(/=/g, "");
594
+ await oauthSecurityService.storeOAuthState(stateValue, stateData, 600);
595
+ stateParam = stateValue;
596
+ }
597
+ catch (err) {
598
+ console.warn("[MCPICloudflareAgent] Failed to generate PKCE challenge, using insecure state:", err);
599
+ // Fallback to insecure state encoding
600
+ const stateData = {
601
+ project_id: safeProjectId,
602
+ agent_did: agentDid,
603
+ session_id: sessionId || "",
604
+ scopes: error.requiredScopes,
605
+ };
606
+ stateParam = btoa(JSON.stringify(stateData));
607
+ }
608
+ }
609
+ else {
610
+ // Fallback to insecure state encoding
611
+ const stateData = {
612
+ project_id: safeProjectId,
613
+ agent_did: agentDid,
614
+ session_id: sessionId || "",
615
+ scopes: error.requiredScopes,
616
+ };
617
+ stateParam = btoa(JSON.stringify(stateData));
618
+ }
619
+ // Build OAuth authorization URL
620
+ const oauthUrl = new URL(`${agentShieldUrl}/bouncer/oauth/authorize`);
621
+ oauthUrl.searchParams.set("response_type", "code");
622
+ oauthUrl.searchParams.set("client_id", safeProjectId);
623
+ oauthUrl.searchParams.set("redirect_uri", `${serverUrl}/oauth/callback`);
624
+ oauthUrl.searchParams.set("scope", error.requiredScopes.join(" "));
625
+ oauthUrl.searchParams.set("state", stateParam);
626
+ if (codeChallenge) {
627
+ oauthUrl.searchParams.set("code_challenge", codeChallenge);
628
+ oauthUrl.searchParams.set("code_challenge_method", "S256");
629
+ }
630
+ return oauthUrl.toString();
631
+ }
151
632
  /**
152
633
  * Get Durable Object instance ID based on routing strategy
153
634
  */