@kya-os/mcp-i-cloudflare 1.2.3-canary.0 → 1.2.3-canary.10

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.
@@ -0,0 +1,185 @@
1
+ /**
2
+ * CloudflareRuntime - Extended runtime with CloudflareProofGenerator
3
+ *
4
+ * Extends MCPIRuntimeBase with Cloudflare-specific proof generation
5
+ * using Web Crypto API instead of Node.js crypto.
6
+ *
7
+ * This runtime automatically uses CloudflareProofGenerator for all proof generation,
8
+ * producing full JWS compact format (header.payload.signature) compatible with
9
+ * AgentShield and the MCP-I proof specification.
10
+ */
11
+ import { MCPIRuntimeBase, ToolProtectionService } from '@kya-os/mcp-i-core';
12
+ import { CloudflareProofGenerator } from './proof-generator';
13
+ import { KVToolProtectionCache } from './cache/kv-tool-protection-cache';
14
+ export class CloudflareRuntime extends MCPIRuntimeBase {
15
+ proofGenerator;
16
+ lastDetachedProof;
17
+ lastToolCallContext;
18
+ constructor(config) {
19
+ super(config);
20
+ }
21
+ /**
22
+ * Initialize runtime and proof generator
23
+ */
24
+ async initialize() {
25
+ await super.initialize();
26
+ // Initialize CloudflareProofGenerator with identity
27
+ const identity = await this.getIdentity();
28
+ this.proofGenerator = new CloudflareProofGenerator(identity);
29
+ if (this.config.audit?.enabled) {
30
+ console.log('[MCP-I] CloudflareRuntime initialized with CloudflareProofGenerator');
31
+ console.log('[MCP-I] DID:', identity.did);
32
+ }
33
+ }
34
+ /**
35
+ * Override createProof to use CloudflareProofGenerator
36
+ *
37
+ * This returns a DetachedProof with full JWS format:
38
+ * - jws: Full compact JWS (header.payload.signature)
39
+ * - meta: ProofMeta with all required fields
40
+ *
41
+ * The proof is compatible with AgentShield and follows MCP-I specification.
42
+ */
43
+ async createProof(data, session) {
44
+ if (!this.proofGenerator) {
45
+ throw new Error('CloudflareProofGenerator not initialized. Call initialize() first.');
46
+ }
47
+ if (!session) {
48
+ throw new Error('Session required for proof generation');
49
+ }
50
+ // Ensure we have a nonce (generate one if not provided)
51
+ let nonce = session.nonce;
52
+ if (!nonce) {
53
+ nonce = await this.issueNonce(session.id);
54
+ // Store nonce back to session for consistency
55
+ session.nonce = nonce;
56
+ }
57
+ // Build ToolRequest from session data
58
+ // The processToolCall method should pass toolName in the session
59
+ const request = {
60
+ method: session.toolName || 'unknown',
61
+ params: session.toolParams || session.args || {},
62
+ };
63
+ // Build ToolResponse
64
+ const response = {
65
+ data,
66
+ };
67
+ // Build SessionContext for CloudflareProofGenerator
68
+ const sessionContext = {
69
+ nonce,
70
+ audience: session.audience || 'mcp-client',
71
+ sessionId: session.id,
72
+ };
73
+ // ✅ NEW: Determine scopeId from session or runtime config
74
+ let scopeId;
75
+ // Option 1: Get from session (if adapter passes it)
76
+ if (session?.scopeId) {
77
+ scopeId = session.scopeId;
78
+ }
79
+ // Option 2: Generate fallback from toolName
80
+ else if (session?.toolName) {
81
+ scopeId = `${session.toolName}:execute`;
82
+ }
83
+ if (this.config.audit?.enabled && scopeId) {
84
+ console.log(`[MCP-I] Proof scopeId for tool "${session.toolName}": ${scopeId}`);
85
+ }
86
+ // ✅ Generate full JWS proof WITH scopeId using CloudflareProofGenerator
87
+ const detachedProof = await this.proofGenerator.generateProof(request, response, sessionContext, { scopeId } // ← ADDED: Pass scopeId for tool auto-discovery
88
+ );
89
+ // Store the proof for getLastProof() retrieval
90
+ this.lastDetachedProof = detachedProof;
91
+ // ✅ Store tool call context for AgentShield dashboard
92
+ this.lastToolCallContext = {
93
+ tool: session.toolName || 'unknown',
94
+ args: session.toolParams || session.args || {},
95
+ result: data,
96
+ scopeId: scopeId || 'unknown',
97
+ userId: session.userId, // Optional user identifier
98
+ };
99
+ if (this.config.audit?.enabled) {
100
+ console.log('[MCP-I] Proof generated:', {
101
+ did: detachedProof.meta.did,
102
+ sessionId: detachedProof.meta.sessionId,
103
+ requestHash: detachedProof.meta.requestHash.substring(0, 20) + '...',
104
+ responseHash: detachedProof.meta.responseHash.substring(0, 20) + '...',
105
+ scopeId: detachedProof.meta.scopeId, // ← ADDED: Log scopeId
106
+ jwsFormat: detachedProof.jws.split('.').length === 3 ? 'valid (3 parts)' : 'invalid',
107
+ });
108
+ }
109
+ return detachedProof;
110
+ }
111
+ /**
112
+ * Override processToolCall to pass tool metadata through session
113
+ *
114
+ * This ensures that CloudflareProofGenerator has access to the tool name
115
+ * and parameters for generating accurate request hashes.
116
+ */
117
+ async processToolCall(toolName, args, handler, session) {
118
+ // Enhance session with tool metadata for proof generation
119
+ const enhancedSession = session ? {
120
+ ...session,
121
+ toolName,
122
+ toolParams: args,
123
+ } : undefined;
124
+ // Call parent implementation with enhanced session
125
+ return await super.processToolCall(toolName, args, handler, enhancedSession);
126
+ }
127
+ /**
128
+ * Get the CloudflareProofGenerator instance (for advanced usage)
129
+ */
130
+ getProofGenerator() {
131
+ return this.proofGenerator;
132
+ }
133
+ /**
134
+ * Override getLastProof to return DetachedProof format
135
+ *
136
+ * This ensures compatibility with applications expecting the full JWS proof format.
137
+ */
138
+ getLastProof() {
139
+ return this.lastDetachedProof;
140
+ }
141
+ /**
142
+ * Get the last tool call context
143
+ *
144
+ * Returns plaintext tool execution data for AgentShield dashboard integration.
145
+ * This context can be submitted alongside proofs for enhanced UX.
146
+ */
147
+ getLastToolCallContext() {
148
+ return this.lastToolCallContext;
149
+ }
150
+ /**
151
+ * Create a ToolProtectionService with CloudFlare KV cache
152
+ *
153
+ * The service fetches tool protection config from AgentShield by agent DID.
154
+ * Config is cached in KV for 5 minutes to minimize API calls.
155
+ *
156
+ * Usage in CloudFlare Worker:
157
+ * ```typescript
158
+ * const toolProtectionService = CloudflareRuntime.createToolProtectionService(
159
+ * env.TOOL_PROTECTION_KV, // KV namespace from wrangler.toml
160
+ * {
161
+ * apiUrl: env.AGENTSHIELD_API_URL || 'https://kya.vouched.id',
162
+ * apiKey: env.AGENTSHIELD_API_KEY,
163
+ * cacheTtl: 300000, // 5 minutes (default)
164
+ * debug: env.MCPI_ENV === 'development',
165
+ * fallbackConfig: {
166
+ * toolProtections: {
167
+ * greet: { requiresDelegation: false, requiredScopes: ['greet:execute'] }
168
+ * }
169
+ * }
170
+ * }
171
+ * );
172
+ *
173
+ * // Pass to runtime config
174
+ * const runtime = new CloudflareRuntime({
175
+ * ...providers,
176
+ * toolProtectionService,
177
+ * });
178
+ * ```
179
+ */
180
+ static createToolProtectionService(kv, config) {
181
+ const cache = new KVToolProtectionCache(kv);
182
+ return new ToolProtectionService(config, cache);
183
+ }
184
+ }
185
+ //# sourceMappingURL=runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EACL,eAAe,EAEf,qBAAqB,EAEtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAE7D,OAAO,EAAE,qBAAqB,EAAoB,MAAM,kCAAkC,CAAC;AAgB3F,MAAM,OAAO,iBAAkB,SAAQ,eAAe;IAC5C,cAAc,CAA4B;IAC1C,iBAAiB,CAAiB;IAClC,mBAAmB,CAAmB;IAE9C,YAAY,MAAyB;QACnC,KAAK,CAAC,MAAM,CAAC,CAAC;IAChB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU;QACd,MAAM,KAAK,CAAC,UAAU,EAAE,CAAC;QAEzB,oDAAoD;QACpD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;QAC1C,IAAI,CAAC,cAAc,GAAG,IAAI,wBAAwB,CAAC,QAAQ,CAAC,CAAC;QAE7D,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC;YAC/B,OAAO,CAAC,GAAG,CAAC,qEAAqE,CAAC,CAAC;YACnF,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED;;;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AAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,SAAS;aACrF,CAAC,CAAC;QACL,CAAC;QAED,OAAO,aAAa,CAAC;IACvB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,eAAe,CACnB,QAAgB,EAChB,IAAS,EACT,OAAoC,EACpC,OAAa;QAEb,0DAA0D;QAC1D,MAAM,eAAe,GAAG,OAAO,CAAC,CAAC,CAAC;YAChC,GAAG,OAAO;YACV,QAAQ;YACR,UAAU,EAAE,IAAI;SACjB,CAAC,CAAC,CAAC,SAAS,CAAC;QAEd,mDAAmD;QACnD,OAAO,MAAM,KAAK,CAAC,eAAe,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,eAAe,CAAC,CAAC;IAC/E,CAAC;IAED;;OAEG;IACH,iBAAiB;QACf,OAAO,IAAI,CAAC,cAAc,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACH,YAAY;QACV,OAAO,IAAI,CAAC,iBAAiB,CAAC;IAChC,CAAC;IAED;;;;;OAKG;IACH,sBAAsB;QACpB,OAAO,IAAI,CAAC,mBAAmB,CAAC;IAClC,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACH,MAAM,CAAC,2BAA2B,CAChC,EAAe,EACf,MAAmC;QAEnC,MAAM,KAAK,GAAG,IAAI,qBAAqB,CAAC,EAAE,CAAC,CAAC;QAC5C,OAAO,IAAI,qBAAqB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kya-os/mcp-i-cloudflare",
3
- "version": "1.2.3-canary.0",
3
+ "version": "1.2.3-canary.10",
4
4
  "description": "Cloudflare Workers implementation of MCP-I framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -33,9 +33,10 @@
33
33
  "edge"
34
34
  ],
35
35
  "dependencies": {
36
- "@kya-os/mcp-i-core": "^1.1.0",
37
36
  "@kya-os/contracts": "^1.3.0",
38
- "@modelcontextprotocol/sdk": "^1.11.4"
37
+ "@kya-os/mcp-i-core": "^1.1.1-canary.0",
38
+ "@modelcontextprotocol/sdk": "^1.11.4",
39
+ "base-x": "^5.0.1"
39
40
  },
40
41
  "devDependencies": {
41
42
  "@cloudflare/workers-types": "^4.0.0",