@kya-os/mcp-i-cloudflare 1.18.0 → 1.18.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +273 -54
- package/dist/adapter.d.ts +3 -17
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +30 -477
- package/dist/adapter.js.map +1 -1
- package/dist/agent.d.ts +59 -35
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +383 -406
- package/dist/agent.js.map +1 -1
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +150 -94
- package/dist/app.js.map +1 -1
- package/dist/audit/deployment-binding.d.ts +16 -16
- package/dist/authority-outcome.d.ts +110 -0
- package/dist/authority-outcome.d.ts.map +1 -0
- package/dist/authority-outcome.js +154 -0
- package/dist/authority-outcome.js.map +1 -0
- package/dist/config.d.ts +8 -77
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +0 -83
- package/dist/config.js.map +1 -1
- package/dist/delegation-http/bearer-retry.d.ts +84 -32
- package/dist/delegation-http/bearer-retry.d.ts.map +1 -1
- package/dist/delegation-http/bearer-retry.js +124 -139
- package/dist/delegation-http/bearer-retry.js.map +1 -1
- package/dist/delegation-http/connect-gate.d.ts +47 -0
- package/dist/delegation-http/connect-gate.d.ts.map +1 -0
- package/dist/delegation-http/connect-gate.js +104 -0
- package/dist/delegation-http/connect-gate.js.map +1 -0
- package/dist/delegation-http/constants.d.ts +4 -49
- package/dist/delegation-http/constants.d.ts.map +1 -1
- package/dist/delegation-http/constants.js +4 -58
- package/dist/delegation-http/constants.js.map +1 -1
- package/dist/delegation-http/issuer-binding.d.ts +26 -0
- package/dist/delegation-http/issuer-binding.d.ts.map +1 -0
- package/dist/delegation-http/issuer-binding.js +51 -0
- package/dist/delegation-http/issuer-binding.js.map +1 -0
- package/dist/delegation-http/lazy-auth-gate.d.ts +88 -0
- package/dist/delegation-http/lazy-auth-gate.d.ts.map +1 -0
- package/dist/delegation-http/lazy-auth-gate.js +203 -0
- package/dist/delegation-http/lazy-auth-gate.js.map +1 -0
- package/dist/delegation-http/native-challenge.d.ts +52 -0
- package/dist/delegation-http/native-challenge.d.ts.map +1 -0
- package/dist/delegation-http/native-challenge.js +124 -0
- package/dist/delegation-http/native-challenge.js.map +1 -0
- package/dist/delegation-http/native-consent-redirect.d.ts +21 -0
- package/dist/delegation-http/native-consent-redirect.d.ts.map +1 -0
- package/dist/delegation-http/native-consent-redirect.js +43 -0
- package/dist/delegation-http/native-consent-redirect.js.map +1 -0
- package/dist/delegation-http/native-http.d.ts +1 -86
- package/dist/delegation-http/native-http.d.ts.map +1 -1
- package/dist/delegation-http/native-http.js +2 -130
- package/dist/delegation-http/native-http.js.map +1 -1
- package/dist/delegation-http/native-oauth-state-do.d.ts +13 -35
- package/dist/delegation-http/native-oauth-state-do.d.ts.map +1 -1
- package/dist/delegation-http/native-oauth-state-do.js +10 -165
- package/dist/delegation-http/native-oauth-state-do.js.map +1 -1
- package/dist/delegation-http/protected-resource.d.ts +37 -0
- package/dist/delegation-http/protected-resource.d.ts.map +1 -0
- package/dist/delegation-http/protected-resource.js +43 -0
- package/dist/delegation-http/protected-resource.js.map +1 -0
- package/dist/delegation-http/request-proof.d.ts +20 -0
- package/dist/delegation-http/request-proof.d.ts.map +1 -0
- package/dist/delegation-http/request-proof.js +29 -0
- package/dist/delegation-http/request-proof.js.map +1 -0
- package/dist/delegation-http/sid.d.ts +4 -12
- package/dist/delegation-http/sid.d.ts.map +1 -1
- package/dist/delegation-http/sid.js +4 -19
- package/dist/delegation-http/sid.js.map +1 -1
- package/dist/delegation-http/well-known-capability.d.ts +8 -12
- package/dist/delegation-http/well-known-capability.d.ts.map +1 -1
- package/dist/delegation-http/well-known-capability.js +9 -18
- package/dist/delegation-http/well-known-capability.js.map +1 -1
- package/dist/helpers/env-mapper.d.ts.map +1 -1
- package/dist/helpers/env-mapper.js +7 -29
- package/dist/helpers/env-mapper.js.map +1 -1
- package/dist/index.d.ts +2 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -8
- package/dist/index.js.map +1 -1
- package/dist/proof-generator.d.ts +24 -17
- package/dist/proof-generator.d.ts.map +1 -1
- package/dist/proof-generator.js +42 -56
- package/dist/proof-generator.js.map +1 -1
- package/dist/runtime/oauth-handler.d.ts +1 -1
- package/dist/runtime/oauth-handler.d.ts.map +1 -1
- package/dist/runtime/oauth-handler.js +15 -30
- package/dist/runtime/oauth-handler.js.map +1 -1
- package/dist/runtime.d.ts +34 -10
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +54 -12
- package/dist/runtime.js.map +1 -1
- package/dist/security/admission-refusal.d.ts +3 -3
- package/dist/security/admission-refusal.d.ts.map +1 -1
- package/dist/security/admission-refusal.js +14 -6
- package/dist/security/admission-refusal.js.map +1 -1
- package/dist/security/holder-binding.d.ts +34 -0
- package/dist/security/holder-binding.d.ts.map +1 -0
- package/dist/security/holder-binding.js +75 -0
- package/dist/security/holder-binding.js.map +1 -0
- package/dist/security/mcp-admission.d.ts +29 -1
- package/dist/security/mcp-admission.d.ts.map +1 -1
- package/dist/security/mcp-admission.js +78 -12
- package/dist/security/mcp-admission.js.map +1 -1
- package/dist/security/native-holder-admission.d.ts +39 -0
- package/dist/security/native-holder-admission.d.ts.map +1 -0
- package/dist/security/native-holder-admission.js +65 -0
- package/dist/security/native-holder-admission.js.map +1 -0
- package/dist/security/outcome-proof.d.ts +39 -0
- package/dist/security/outcome-proof.d.ts.map +1 -0
- package/dist/security/outcome-proof.js +54 -0
- package/dist/security/outcome-proof.js.map +1 -0
- package/dist/security/request-nonce-store.d.ts +43 -0
- package/dist/security/request-nonce-store.d.ts.map +1 -0
- package/dist/security/request-nonce-store.js +129 -0
- package/dist/security/request-nonce-store.js.map +1 -0
- package/dist/security/routed-request.d.ts +41 -0
- package/dist/security/routed-request.d.ts.map +1 -0
- package/dist/security/routed-request.js +86 -0
- package/dist/security/routed-request.js.map +1 -0
- package/dist/security/sign-in-amr.d.ts +23 -0
- package/dist/security/sign-in-amr.d.ts.map +1 -0
- package/dist/security/sign-in-amr.js +44 -0
- package/dist/security/sign-in-amr.js.map +1 -0
- package/dist/services/approval-route.d.ts +3 -2
- package/dist/services/approval-route.d.ts.map +1 -1
- package/dist/services/approval-route.js.map +1 -1
- package/dist/services/checkpoint-decision.d.ts +56 -0
- package/dist/services/checkpoint-decision.d.ts.map +1 -0
- package/dist/services/checkpoint-decision.js +109 -0
- package/dist/services/checkpoint-decision.js.map +1 -0
- package/dist/services/consent-page-renderer.d.ts.map +1 -1
- package/dist/services/consent-page-renderer.js +2 -3
- package/dist/services/consent-page-renderer.js.map +1 -1
- package/dist/services/consent.service.d.ts +11 -49
- package/dist/services/consent.service.d.ts.map +1 -1
- package/dist/services/consent.service.js +81 -307
- package/dist/services/consent.service.js.map +1 -1
- package/dist/services/credential-auth.handler.d.ts.map +1 -1
- package/dist/services/credential-auth.handler.js +2 -8
- package/dist/services/credential-auth.handler.js.map +1 -1
- package/dist/services/delegation-authority.d.ts +53 -0
- package/dist/services/delegation-authority.d.ts.map +1 -0
- package/dist/services/delegation-authority.js +130 -0
- package/dist/services/delegation-authority.js.map +1 -0
- package/dist/services/delegation-trust.d.ts +35 -64
- package/dist/services/delegation-trust.d.ts.map +1 -1
- package/dist/services/delegation-trust.js +98 -279
- package/dist/services/delegation-trust.js.map +1 -1
- package/dist/services/delegation.service.d.ts +83 -105
- package/dist/services/delegation.service.d.ts.map +1 -1
- package/dist/services/delegation.service.js +166 -518
- package/dist/services/delegation.service.js.map +1 -1
- package/dist/services/idp-token-storage.d.ts +32 -12
- package/dist/services/idp-token-storage.d.ts.map +1 -1
- package/dist/services/idp-token-storage.js +79 -36
- package/dist/services/idp-token-storage.js.map +1 -1
- package/dist/services/issuer-keys.d.ts +55 -0
- package/dist/services/issuer-keys.d.ts.map +1 -0
- package/dist/services/issuer-keys.js +184 -0
- package/dist/services/issuer-keys.js.map +1 -0
- package/dist/services/oauth-security.service.d.ts +17 -1
- package/dist/services/oauth-security.service.d.ts.map +1 -1
- package/dist/services/oauth-security.service.js +29 -10
- package/dist/services/oauth-security.service.js.map +1 -1
- package/dist/services/resource-audience.d.ts +12 -9
- package/dist/services/resource-audience.d.ts.map +1 -1
- package/dist/services/resource-audience.js +12 -9
- package/dist/services/resource-audience.js.map +1 -1
- package/dist/services/unenforceable-tool-protection.d.ts +32 -0
- package/dist/services/unenforceable-tool-protection.d.ts.map +1 -0
- package/dist/services/unenforceable-tool-protection.js +90 -0
- package/dist/services/unenforceable-tool-protection.js.map +1 -0
- package/dist/services/upstream-fetch.d.ts +14 -0
- package/dist/services/upstream-fetch.d.ts.map +1 -0
- package/dist/services/upstream-fetch.js +56 -0
- package/dist/services/upstream-fetch.js.map +1 -0
- package/dist/types.d.ts +5 -18
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/tool-security-schemes.d.ts +65 -0
- package/dist/utils/tool-security-schemes.d.ts.map +1 -0
- package/dist/utils/tool-security-schemes.js +168 -0
- package/dist/utils/tool-security-schemes.js.map +1 -0
- package/package.json +4 -5
- package/dist/delegation-http/anchor-do.d.ts +0 -84
- package/dist/delegation-http/anchor-do.d.ts.map +0 -1
- package/dist/delegation-http/anchor-do.js +0 -412
- package/dist/delegation-http/anchor-do.js.map +0 -1
- package/dist/delegation-http/anchor-registration-validate.d.ts +0 -61
- package/dist/delegation-http/anchor-registration-validate.d.ts.map +0 -1
- package/dist/delegation-http/anchor-registration-validate.js +0 -191
- package/dist/delegation-http/anchor-registration-validate.js.map +0 -1
- package/dist/delegation-http/anchor-registration.d.ts +0 -62
- package/dist/delegation-http/anchor-registration.d.ts.map +0 -1
- package/dist/delegation-http/anchor-registration.js +0 -337
- package/dist/delegation-http/anchor-registration.js.map +0 -1
- package/dist/delegation-http/anchor-routing.d.ts +0 -20
- package/dist/delegation-http/anchor-routing.d.ts.map +0 -1
- package/dist/delegation-http/anchor-routing.js +0 -27
- package/dist/delegation-http/anchor-routing.js.map +0 -1
- package/dist/delegation-http/challenge-middleware.d.ts +0 -28
- package/dist/delegation-http/challenge-middleware.d.ts.map +0 -1
- package/dist/delegation-http/challenge-middleware.js +0 -190
- package/dist/delegation-http/challenge-middleware.js.map +0 -1
- package/dist/delegation-http/challenge.d.ts +0 -102
- package/dist/delegation-http/challenge.d.ts.map +0 -1
- package/dist/delegation-http/challenge.js +0 -223
- package/dist/delegation-http/challenge.js.map +0 -1
- package/dist/delegation-http/consent-anchor-bridge.d.ts +0 -70
- package/dist/delegation-http/consent-anchor-bridge.d.ts.map +0 -1
- package/dist/delegation-http/consent-anchor-bridge.js +0 -1130
- package/dist/delegation-http/consent-anchor-bridge.js.map +0 -1
- package/dist/delegation-http/consent-preparation-page.d.ts +0 -18
- package/dist/delegation-http/consent-preparation-page.d.ts.map +0 -1
- package/dist/delegation-http/consent-preparation-page.js +0 -172
- package/dist/delegation-http/consent-preparation-page.js.map +0 -1
- package/dist/delegation-http/credential-consent.d.ts +0 -149
- package/dist/delegation-http/credential-consent.d.ts.map +0 -1
- package/dist/delegation-http/credential-consent.js +0 -338
- package/dist/delegation-http/credential-consent.js.map +0 -1
- package/dist/delegation-http/native-capabilities.d.ts +0 -4
- package/dist/delegation-http/native-capabilities.d.ts.map +0 -1
- package/dist/delegation-http/native-capabilities.js +0 -53
- package/dist/delegation-http/native-capabilities.js.map +0 -1
- package/dist/delegation-http/native-oauth-state.d.ts +0 -28
- package/dist/delegation-http/native-oauth-state.d.ts.map +0 -1
- package/dist/delegation-http/native-oauth-state.js +0 -146
- package/dist/delegation-http/native-oauth-state.js.map +0 -1
- package/dist/delegation-http/native-oauth.d.ts +0 -26
- package/dist/delegation-http/native-oauth.d.ts.map +0 -1
- package/dist/delegation-http/native-oauth.js +0 -401
- package/dist/delegation-http/native-oauth.js.map +0 -1
- package/dist/delegation-http/native-pickup.d.ts +0 -20
- package/dist/delegation-http/native-pickup.d.ts.map +0 -1
- package/dist/delegation-http/native-pickup.js +0 -227
- package/dist/delegation-http/native-pickup.js.map +0 -1
- package/dist/delegation-http/pickup-routes.d.ts +0 -43
- package/dist/delegation-http/pickup-routes.d.ts.map +0 -1
- package/dist/delegation-http/pickup-routes.js +0 -166
- package/dist/delegation-http/pickup-routes.js.map +0 -1
- package/dist/delegation-http/register.d.ts +0 -26
- package/dist/delegation-http/register.d.ts.map +0 -1
- package/dist/delegation-http/register.js +0 -95
- package/dist/delegation-http/register.js.map +0 -1
- package/dist/delegation-http/registrar-auth.d.ts +0 -24
- package/dist/delegation-http/registrar-auth.d.ts.map +0 -1
- package/dist/delegation-http/registrar-auth.js +0 -61
- package/dist/delegation-http/registrar-auth.js.map +0 -1
- package/dist/delegation-http/remote-issuer.d.ts +0 -52
- package/dist/delegation-http/remote-issuer.d.ts.map +0 -1
- package/dist/delegation-http/remote-issuer.js +0 -324
- package/dist/delegation-http/remote-issuer.js.map +0 -1
- package/dist/delegation-http/types.d.ts +0 -116
- package/dist/delegation-http/types.d.ts.map +0 -1
- package/dist/delegation-http/types.js +0 -11
- package/dist/delegation-http/types.js.map +0 -1
package/dist/adapter.js
CHANGED
|
@@ -15,6 +15,7 @@ import { buildNeedsAuthorizationMeta } from "./needs-authorization-meta";
|
|
|
15
15
|
import { isAmountLimitError } from "./runtime/amount-limit-refusal";
|
|
16
16
|
import { amountLimitToolResult } from "@kya-os/mcp-i-runtime";
|
|
17
17
|
import { KVProofArchive } from "./storage/kv-proof-archive";
|
|
18
|
+
import { runWithRoutedHost } from "./security/routed-request";
|
|
18
19
|
import { STORAGE_KEYS } from "./constants/storage-keys";
|
|
19
20
|
import { DEFAULT_SESSION_CACHE_TTL, DEFAULT_SESSION_TTL } from "./constants";
|
|
20
21
|
import { normalizeCloudflareEnv } from "./helpers/env-mapper";
|
|
@@ -67,62 +68,6 @@ class CloudflareMCPServer {
|
|
|
67
68
|
calculateDOInstanceId(sessionId) {
|
|
68
69
|
return calculateDOInstanceId(sessionId, this.doRoutingStrategy, this.doShardCount);
|
|
69
70
|
}
|
|
70
|
-
/**
|
|
71
|
-
* Fetch delegation from Durable Object (strongly consistent storage)
|
|
72
|
-
*
|
|
73
|
-
* This provides parity with the Agent pattern by using the same DO-based
|
|
74
|
-
* storage for delegations. The consent service stores to DO first, and
|
|
75
|
-
* now the adapter can read from DO first as well.
|
|
76
|
-
*
|
|
77
|
-
* @param sessionId - Session ID to lookup
|
|
78
|
-
* @param userDid - Optional user DID for user+agent scoped lookup
|
|
79
|
-
* @param agentDid - Agent DID for the lookup
|
|
80
|
-
* @returns Delegation token if found, undefined otherwise
|
|
81
|
-
*/
|
|
82
|
-
async getDelegationFromDO(sessionId, userDid, agentDid) {
|
|
83
|
-
if (!this.doNamespace) {
|
|
84
|
-
return undefined;
|
|
85
|
-
}
|
|
86
|
-
try {
|
|
87
|
-
// Use the same routing strategy as consent service
|
|
88
|
-
const doInstanceId = this.calculateDOInstanceId(sessionId);
|
|
89
|
-
const doId = this.doNamespace.idFromName(doInstanceId);
|
|
90
|
-
const doStub = this.doNamespace.get(doId);
|
|
91
|
-
// Build request URL - use the origin or a placeholder
|
|
92
|
-
const origin = this.requestOrigin || "https://localhost";
|
|
93
|
-
const url = new URL("/_internal/delegation/get", origin);
|
|
94
|
-
const response = await doStub.fetch(url.toString(), {
|
|
95
|
-
method: "POST",
|
|
96
|
-
headers: { "Content-Type": "application/json" },
|
|
97
|
-
body: JSON.stringify({
|
|
98
|
-
sessionId,
|
|
99
|
-
userDid,
|
|
100
|
-
agentDid,
|
|
101
|
-
}),
|
|
102
|
-
});
|
|
103
|
-
if (!response.ok) {
|
|
104
|
-
console.error("[CloudflareMCPServer] DO delegation lookup failed:", {
|
|
105
|
-
status: response.status,
|
|
106
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
107
|
-
});
|
|
108
|
-
return undefined;
|
|
109
|
-
}
|
|
110
|
-
const data = (await response.json());
|
|
111
|
-
if (data.success && data.data?.delegationToken) {
|
|
112
|
-
console.error("[CloudflareMCPServer] ✅ Delegation token retrieved from DO:", {
|
|
113
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
114
|
-
userDid: userDid ? userDid.slice(0, 20) + "..." : "none",
|
|
115
|
-
tokenLength: data.data.delegationToken.length,
|
|
116
|
-
});
|
|
117
|
-
return data.data.delegationToken;
|
|
118
|
-
}
|
|
119
|
-
return undefined;
|
|
120
|
-
}
|
|
121
|
-
catch (error) {
|
|
122
|
-
console.error("[CloudflareMCPServer] DO delegation lookup error:", error);
|
|
123
|
-
return undefined;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
71
|
/**
|
|
127
72
|
* Retrieve stored client capabilities from DO storage.
|
|
128
73
|
* The agent stores these during the MCP initialize handshake.
|
|
@@ -152,10 +97,9 @@ class CloudflareMCPServer {
|
|
|
152
97
|
* Priority: 1. `Mcp-Session-Id` header (MCP protocol standard),
|
|
153
98
|
* 2. `session_id` / `sessionId` param (legacy clients).
|
|
154
99
|
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
* id
|
|
158
|
-
* runtime. Returns `{ id: undefined }` when the request names no session.
|
|
100
|
+
* The session handed to the runtime carries this id, so proofs and the
|
|
101
|
+
* consent URL are attributed to it. It never selects a delegation. Returns
|
|
102
|
+
* `{ id: undefined }` when the request names no session.
|
|
159
103
|
*/
|
|
160
104
|
resolveTransportSessionId(meta, params) {
|
|
161
105
|
const headerSessionId = meta?.request?.headers?.get?.("mcp-session-id") ||
|
|
@@ -218,17 +162,14 @@ class CloudflareMCPServer {
|
|
|
218
162
|
throw new Error(`Tool not found: ${toolName}`);
|
|
219
163
|
}
|
|
220
164
|
// The transport session id (Mcp-Session-Id header, else session_id
|
|
221
|
-
// param)
|
|
222
|
-
//
|
|
223
|
-
//
|
|
224
|
-
//
|
|
225
|
-
//
|
|
226
|
-
//
|
|
227
|
-
//
|
|
228
|
-
//
|
|
229
|
-
// isolate; keying on it made the lookup miss forever and could read
|
|
230
|
-
// another user's delegation (#691). It stays the fallback only when
|
|
231
|
-
// the request carries no transport session id at all.
|
|
165
|
+
// param) names the session the runtime attributes this call to, and
|
|
166
|
+
// the consent URL it builds carries it. When the request names a
|
|
167
|
+
// transport session, only a runtime session with the SAME id may be
|
|
168
|
+
// reused. `getCurrentSession()` returns the first non-expired session
|
|
169
|
+
// in the runtime's in-memory map, i.e. a random handshake id from
|
|
170
|
+
// whichever client handshook first on this isolate (#691). It stays
|
|
171
|
+
// the fallback only when the request carries no transport session id
|
|
172
|
+
// at all. Neither ever supplies a delegation: see below.
|
|
232
173
|
const transportSession = this.resolveTransportSessionId(meta, params);
|
|
233
174
|
let session = null;
|
|
234
175
|
try {
|
|
@@ -236,8 +177,7 @@ class CloudflareMCPServer {
|
|
|
236
177
|
// `getCurrentSession()` which skips sessions the ClockProvider says
|
|
237
178
|
// have expired (`now > expiresAt`). Mirror that check here so an
|
|
238
179
|
// expired in-memory session is treated as absent and the ephemeral
|
|
239
|
-
// path below rebuilds it from the transport id
|
|
240
|
-
// reusing its stale delegationToken/userDid.
|
|
180
|
+
// path below rebuilds it from the transport id.
|
|
241
181
|
const inMemorySession = transportSession.id
|
|
242
182
|
? this.runtime.getSession?.(transportSession.id)
|
|
243
183
|
: undefined;
|
|
@@ -263,7 +203,11 @@ class CloudflareMCPServer {
|
|
|
263
203
|
console.error("[CloudflareMCPServer] Error resolving runtime session:", error);
|
|
264
204
|
// Continue with null session (will create ephemeral)
|
|
265
205
|
}
|
|
266
|
-
//
|
|
206
|
+
// The delegation is whatever THIS call presents: params.delegationToken,
|
|
207
|
+
// which the bearer gate fills from `Authorization: Bearer` or the
|
|
208
|
+
// KYA-OS header. Nothing is looked up by session id, user DID or agent
|
|
209
|
+
// DID, and a token one call presented is never kept for the next: the
|
|
210
|
+
// session handed to the runtime is a per-call copy.
|
|
267
211
|
if (!session) {
|
|
268
212
|
const providedSessionId = transportSession.id;
|
|
269
213
|
const timestamp = Date.now();
|
|
@@ -276,201 +220,6 @@ class CloudflareMCPServer {
|
|
|
276
220
|
isProvided: !!providedSessionId,
|
|
277
221
|
source: transportSession.source ?? "generated",
|
|
278
222
|
});
|
|
279
|
-
// Extract OAuth identity from request or session cache for persistent userDid lookup
|
|
280
|
-
let oauthIdentity = undefined;
|
|
281
|
-
if (meta?.request) {
|
|
282
|
-
oauthIdentity = this.extractOAuthIdentityFromRequest(meta.request);
|
|
283
|
-
if (oauthIdentity) {
|
|
284
|
-
console.error("[CloudflareMCPServer] Extracted OAuth identity from request:", {
|
|
285
|
-
provider: oauthIdentity.provider,
|
|
286
|
-
subject: oauthIdentity.subject.substring(0, 20) + "...",
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
// If OAuth identity not in request, try to get from session cache
|
|
291
|
-
if (!oauthIdentity && sessionId && this.delegationStorage) {
|
|
292
|
-
try {
|
|
293
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
294
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
295
|
-
if (sessionData?.oauthIdentity?.provider) {
|
|
296
|
-
// Note: We only have subjectHash in session cache (PII protection)
|
|
297
|
-
// We can't fully reconstruct OAuth identity, but we can use it to lookup userDid
|
|
298
|
-
console.error("[CloudflareMCPServer] Found OAuth identity in session cache:", {
|
|
299
|
-
provider: sessionData.oauthIdentity.provider,
|
|
300
|
-
hasSubjectHash: !!sessionData.oauthIdentity.subjectHash,
|
|
301
|
-
});
|
|
302
|
-
// OAuth identity from session cache is incomplete (subjectHash only), so we'll use it for userDid lookup via storage
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
catch (error) {
|
|
306
|
-
console.warn("[CloudflareMCPServer] Failed to get OAuth identity from session:", error);
|
|
307
|
-
// Non-fatal - continue without OAuth identity
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
// Check KV storage for stored delegation token if not provided in params
|
|
311
|
-
let delegationToken = params.delegationToken;
|
|
312
|
-
if (!delegationToken &&
|
|
313
|
-
(this.delegationStorage || this.doNamespace)) {
|
|
314
|
-
try {
|
|
315
|
-
// Get userDID from params first
|
|
316
|
-
let userDid = params.clientDid || params.userDid;
|
|
317
|
-
// ✅ PRIORITY 1: If OAuth identity available, use it to lookup persistent userDid
|
|
318
|
-
if (!userDid && oauthIdentity && this.delegationStorage) {
|
|
319
|
-
try {
|
|
320
|
-
const oauthKey = STORAGE_KEYS.oauthIdentity(oauthIdentity.provider, oauthIdentity.subject);
|
|
321
|
-
const mappedUserDid = await this.delegationStorage.get(oauthKey, "text");
|
|
322
|
-
if (mappedUserDid) {
|
|
323
|
-
userDid = mappedUserDid;
|
|
324
|
-
console.error("[CloudflareMCPServer] ✅ Retrieved persistent userDid from OAuth mapping:", {
|
|
325
|
-
provider: oauthIdentity.provider,
|
|
326
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
catch (error) {
|
|
331
|
-
console.warn("[CloudflareMCPServer] Failed to lookup userDid from OAuth mapping:", error);
|
|
332
|
-
// Non-fatal - continue with session cache lookup
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
// ✅ PRIORITY 2: If userDid not in params or OAuth mapping, try to retrieve from session cache
|
|
336
|
-
if (!userDid && sessionId && this.delegationStorage) {
|
|
337
|
-
try {
|
|
338
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
339
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
340
|
-
if (sessionData?.userDid) {
|
|
341
|
-
userDid = sessionData.userDid;
|
|
342
|
-
console.error("[CloudflareMCPServer] ✅ Retrieved userDid from session cache:", {
|
|
343
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
344
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
catch (error) {
|
|
349
|
-
console.warn("[CloudflareMCPServer] Failed to get userDid from session:", error);
|
|
350
|
-
// Non-fatal - continue without userDid
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
console.error("[CloudflareMCPServer] 🔍 Starting delegation token lookup:", {
|
|
354
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
355
|
-
userDid: userDid ? userDid.slice(0, 20) + "..." : "none",
|
|
356
|
-
sessionId: sessionId
|
|
357
|
-
? sessionId.slice(0, 20) + "..."
|
|
358
|
-
: "none",
|
|
359
|
-
hasDelegationStorage: !!this.delegationStorage,
|
|
360
|
-
hasDONamespace: !!this.doNamespace,
|
|
361
|
-
});
|
|
362
|
-
// ✅ PRIORITY 0: Try Durable Object (strongly consistent, parity with Agent pattern)
|
|
363
|
-
// This is the preferred approach for consistent delegation storage across all deployment types
|
|
364
|
-
if (this.doNamespace && sessionId) {
|
|
365
|
-
const doToken = await this.getDelegationFromDO(sessionId, userDid, agentDid);
|
|
366
|
-
if (doToken) {
|
|
367
|
-
delegationToken = doToken;
|
|
368
|
-
console.error("[CloudflareMCPServer] ✅ PRIORITY 0: Delegation token from DO (strongly consistent)");
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
// PRIORITY 1: Try user+agent scoped token (user-specific, most secure)
|
|
372
|
-
// This is the preferred approach for multi-user scenarios with proper user isolation
|
|
373
|
-
// Skip if we already have a token from DO (PRIORITY 0)
|
|
374
|
-
if (!delegationToken && userDid && this.delegationStorage) {
|
|
375
|
-
const userAgentKey = STORAGE_KEYS.delegation(userDid, agentDid);
|
|
376
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 1: Checking user+agent scoped key:", {
|
|
377
|
-
key: userAgentKey,
|
|
378
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
379
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
380
|
-
});
|
|
381
|
-
const userAgentToken = await this.delegationStorage.get(userAgentKey, "text");
|
|
382
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 1: User+agent scoped lookup result:", {
|
|
383
|
-
key: userAgentKey,
|
|
384
|
-
found: !!userAgentToken,
|
|
385
|
-
tokenLength: userAgentToken?.length || 0,
|
|
386
|
-
tokenPreview: userAgentToken
|
|
387
|
-
? userAgentToken.substring(0, 20) + "..."
|
|
388
|
-
: null,
|
|
389
|
-
});
|
|
390
|
-
if (userAgentToken) {
|
|
391
|
-
delegationToken = userAgentToken;
|
|
392
|
-
console.error("[CloudflareMCPServer] ✅ Delegation token retrieved from user+agent scoped key:", {
|
|
393
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
394
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
395
|
-
key: userAgentKey,
|
|
396
|
-
tokenLength: userAgentToken.length,
|
|
397
|
-
});
|
|
398
|
-
// Cache it for this session for faster future lookups
|
|
399
|
-
// Store full session data object to match consent service format
|
|
400
|
-
if (this.delegationStorage && sessionId) {
|
|
401
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
402
|
-
// Read existing session data to preserve userDid and other fields
|
|
403
|
-
const existingSession = (await this.delegationStorage.get(sessionKey, "json"));
|
|
404
|
-
await this.delegationStorage.put(sessionKey, JSON.stringify({
|
|
405
|
-
...existingSession,
|
|
406
|
-
userDid,
|
|
407
|
-
agentDid,
|
|
408
|
-
delegationToken: userAgentToken,
|
|
409
|
-
cachedAt: Date.now(),
|
|
410
|
-
}), {
|
|
411
|
-
expirationTtl: 1800, // 30 minutes
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
// PRIORITY 2: Try session-scoped token (if session ID was persisted)
|
|
417
|
-
// Note: Sessions are ephemeral in Cloudflare Workers, so this is mainly
|
|
418
|
-
// useful for caching tokens within a single request chain
|
|
419
|
-
if (!delegationToken && sessionId && this.delegationStorage) {
|
|
420
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
421
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 2: Checking session-scoped key:", {
|
|
422
|
-
key: sessionKey,
|
|
423
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
424
|
-
});
|
|
425
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
426
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 2: Session-scoped lookup result:", {
|
|
427
|
-
key: sessionKey,
|
|
428
|
-
found: !!sessionData,
|
|
429
|
-
hasDelegationToken: !!sessionData?.delegationToken,
|
|
430
|
-
tokenLength: sessionData?.delegationToken?.length || 0,
|
|
431
|
-
tokenPreview: sessionData?.delegationToken
|
|
432
|
-
? sessionData.delegationToken.substring(0, 20) + "..."
|
|
433
|
-
: null,
|
|
434
|
-
});
|
|
435
|
-
if (sessionData?.delegationToken) {
|
|
436
|
-
delegationToken = sessionData.delegationToken;
|
|
437
|
-
console.error("[CloudflareMCPServer] ✅ Delegation token retrieved from session cache:", {
|
|
438
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
439
|
-
tokenLength: delegationToken.length,
|
|
440
|
-
});
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
// ❌ REMOVED: Legacy agent-scoped fallback (PRIORITY 3)
|
|
444
|
-
// This was a security vulnerability that allowed cross-user delegation sharing.
|
|
445
|
-
// Delegations are now strictly session-scoped or user+agent scoped.
|
|
446
|
-
if (!delegationToken) {
|
|
447
|
-
const checkedKeys = [];
|
|
448
|
-
// Include user+agent scoped key if userDid is available
|
|
449
|
-
if (userDid) {
|
|
450
|
-
checkedKeys.push(STORAGE_KEYS.delegation(userDid, agentDid));
|
|
451
|
-
}
|
|
452
|
-
// Include session-scoped key if sessionId is available
|
|
453
|
-
if (sessionId) {
|
|
454
|
-
checkedKeys.push(STORAGE_KEYS.session(sessionId));
|
|
455
|
-
}
|
|
456
|
-
console.error("[CloudflareMCPServer] ⚠️ No delegation token found - user must complete consent flow:", {
|
|
457
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
458
|
-
userDid: userDid
|
|
459
|
-
? userDid.slice(0, 20) + "..."
|
|
460
|
-
: "none (consent-only)",
|
|
461
|
-
sessionId: sessionId
|
|
462
|
-
? sessionId.slice(0, 20) + "..."
|
|
463
|
-
: "none",
|
|
464
|
-
checkedKeys,
|
|
465
|
-
hint: "Delegation tokens are session-scoped or user+agent scoped. Complete consent flow for this session.",
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
catch (error) {
|
|
470
|
-
// Log but don't fail - delegation lookup is best-effort
|
|
471
|
-
console.error("[CloudflareMCPServer] Failed to retrieve delegation token from KV:", error);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
223
|
// Retrieve stored client capabilities from DO (set during MCP initialize)
|
|
475
224
|
const clientCapabilities = await this.getClientCapabilitiesFromDO(sessionId);
|
|
476
225
|
session = {
|
|
@@ -480,9 +229,6 @@ class CloudflareMCPServer {
|
|
|
480
229
|
agentDid,
|
|
481
230
|
createdAt: timestamp,
|
|
482
231
|
expiresAt: timestamp + 30 * 60 * 1000, // 30 minutes
|
|
483
|
-
// Use token from params or KV storage
|
|
484
|
-
delegationToken,
|
|
485
|
-
consentProof: params.consentProof,
|
|
486
232
|
// Auto-detect server origin for consent URL building
|
|
487
233
|
serverOrigin: this.requestOrigin,
|
|
488
234
|
// Client capabilities for MCP Apps UI detection
|
|
@@ -490,173 +236,21 @@ class CloudflareMCPServer {
|
|
|
490
236
|
? { capabilities: clientCapabilities }
|
|
491
237
|
: undefined,
|
|
492
238
|
};
|
|
493
|
-
// ✅ CRITICAL: Log delegation token status before processToolCall
|
|
494
|
-
console.error("[CloudflareMCPServer] ✅ Session created with delegation token status:", {
|
|
495
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
496
|
-
hasDelegationToken: !!delegationToken,
|
|
497
|
-
delegationTokenLength: delegationToken?.length || 0,
|
|
498
|
-
delegationTokenSource: params.delegationToken
|
|
499
|
-
? "params"
|
|
500
|
-
: delegationToken
|
|
501
|
-
? "kv-storage"
|
|
502
|
-
: "none",
|
|
503
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
504
|
-
});
|
|
505
239
|
}
|
|
506
240
|
else {
|
|
507
|
-
//
|
|
508
|
-
//
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
sessionId: session?.id ? session.id.slice(0, 20) + "..." : "none",
|
|
514
|
-
sessionKeys: Object.keys(session || {}),
|
|
515
|
-
});
|
|
516
|
-
if (!session.delegationToken &&
|
|
517
|
-
(this.delegationStorage || this.doNamespace)) {
|
|
518
|
-
console.error("[CloudflareMCPServer] ⚠️ Session has no delegationToken, attempting KV lookup...");
|
|
519
|
-
try {
|
|
520
|
-
const agentDid = (await this.runtime.getIdentity()).did;
|
|
521
|
-
// ✅ CRITICAL: Retrieve userDid from session cache if not in session object
|
|
522
|
-
// Check session cache first, then params, then session object
|
|
523
|
-
let userDid = session.userDid ||
|
|
524
|
-
params.clientDid ||
|
|
525
|
-
params.userDid ||
|
|
526
|
-
session.clientDid;
|
|
527
|
-
const sessionId = session.id;
|
|
528
|
-
// If userDid still not found, try retrieving from session cache
|
|
529
|
-
if (!userDid && sessionId && this.delegationStorage) {
|
|
530
|
-
try {
|
|
531
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
532
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
533
|
-
if (sessionData?.userDid) {
|
|
534
|
-
userDid = sessionData.userDid;
|
|
535
|
-
// Update session object with retrieved userDid
|
|
536
|
-
session.userDid = userDid;
|
|
537
|
-
console.error("[CloudflareMCPServer] ✅ Retrieved userDid from session cache for delegation lookup:", {
|
|
538
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
539
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
catch (error) {
|
|
544
|
-
console.warn("[CloudflareMCPServer] Failed to get userDid from session cache:", error);
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
console.error("[CloudflareMCPServer] 🔍 Looking up delegation token for existing session:", {
|
|
548
|
-
sessionId: sessionId
|
|
549
|
-
? sessionId.slice(0, 20) + "..."
|
|
550
|
-
: "none",
|
|
551
|
-
userDid: userDid ? userDid.slice(0, 20) + "..." : "none",
|
|
552
|
-
agentDid: agentDid ? agentDid.slice(0, 20) + "..." : "none",
|
|
553
|
-
});
|
|
554
|
-
let delegationToken = undefined;
|
|
555
|
-
// PRIORITY 0: Try Durable Object (strongly consistent, parity with Agent pattern)
|
|
556
|
-
if (this.doNamespace && sessionId) {
|
|
557
|
-
const doToken = await this.getDelegationFromDO(sessionId, userDid, agentDid);
|
|
558
|
-
if (doToken) {
|
|
559
|
-
delegationToken = doToken;
|
|
560
|
-
console.error("[CloudflareMCPServer] ✅ PRIORITY 0: Delegation token from DO (existing session)");
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
// PRIORITY 1: Try user+agent scoped token (user-specific, most secure)
|
|
564
|
-
// This is the preferred approach for multi-user scenarios with proper user isolation
|
|
565
|
-
// Track which source the token was found in for accurate logging
|
|
566
|
-
let tokenSource;
|
|
567
|
-
if (delegationToken) {
|
|
568
|
-
tokenSource = "do";
|
|
569
|
-
}
|
|
570
|
-
else if (userDid && this.delegationStorage) {
|
|
571
|
-
const userAgentKey = STORAGE_KEYS.delegation(userDid, agentDid);
|
|
572
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 1: Checking user+agent scoped key (existing session):", {
|
|
573
|
-
key: userAgentKey,
|
|
574
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
575
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
576
|
-
});
|
|
577
|
-
const userAgentToken = await this.delegationStorage.get(userAgentKey, "text");
|
|
578
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 1: User+agent scoped lookup result:", {
|
|
579
|
-
key: userAgentKey,
|
|
580
|
-
found: !!userAgentToken,
|
|
581
|
-
tokenLength: userAgentToken?.length || 0,
|
|
582
|
-
});
|
|
583
|
-
if (userAgentToken) {
|
|
584
|
-
delegationToken = userAgentToken;
|
|
585
|
-
tokenSource = "user+agent-key";
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
// PRIORITY 2: Try session-scoped token (if session ID was persisted)
|
|
589
|
-
if (!delegationToken && sessionId && this.delegationStorage) {
|
|
590
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
591
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 2: Checking session-scoped key (existing session):", {
|
|
592
|
-
key: sessionKey,
|
|
593
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
594
|
-
});
|
|
595
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
596
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 2: Session-scoped lookup result:", {
|
|
597
|
-
key: sessionKey,
|
|
598
|
-
found: !!sessionData,
|
|
599
|
-
hasDelegationToken: !!sessionData?.delegationToken,
|
|
600
|
-
tokenLength: sessionData?.delegationToken?.length || 0,
|
|
601
|
-
});
|
|
602
|
-
if (sessionData?.delegationToken) {
|
|
603
|
-
delegationToken = sessionData.delegationToken;
|
|
604
|
-
tokenSource = "session-key";
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
// ❌ REMOVED: Legacy agent-scoped fallback (PRIORITY 3)
|
|
608
|
-
// This was a security vulnerability that allowed cross-user delegation sharing.
|
|
609
|
-
// ✅ CRITICAL: Set delegation token on session if found
|
|
610
|
-
if (delegationToken) {
|
|
611
|
-
session = {
|
|
612
|
-
...session,
|
|
613
|
-
delegationToken,
|
|
614
|
-
};
|
|
615
|
-
console.error("[CloudflareMCPServer] ✅ Delegation token set on existing session:", {
|
|
616
|
-
sessionId: sessionId
|
|
617
|
-
? sessionId.slice(0, 20) + "..."
|
|
618
|
-
: "none",
|
|
619
|
-
tokenLength: delegationToken.length,
|
|
620
|
-
sessionHasToken: !!session.delegationToken,
|
|
621
|
-
source: tokenSource,
|
|
622
|
-
});
|
|
623
|
-
}
|
|
624
|
-
else {
|
|
625
|
-
const checkedKeys = [];
|
|
626
|
-
// Include user+agent scoped key if userDid is available
|
|
627
|
-
if (userDid) {
|
|
628
|
-
checkedKeys.push(STORAGE_KEYS.delegation(userDid, agentDid));
|
|
629
|
-
}
|
|
630
|
-
// Include session-scoped key if sessionId is available
|
|
631
|
-
if (sessionId) {
|
|
632
|
-
checkedKeys.push(STORAGE_KEYS.session(sessionId));
|
|
633
|
-
}
|
|
634
|
-
console.error("[CloudflareMCPServer] ⚠️ No delegation token found - user must complete consent flow:", {
|
|
635
|
-
sessionId: sessionId
|
|
636
|
-
? sessionId.slice(0, 20) + "..."
|
|
637
|
-
: "none",
|
|
638
|
-
checkedKeys,
|
|
639
|
-
hint: "Complete consent flow for this session to get a delegation token.",
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
catch (error) {
|
|
644
|
-
// Log but don't fail - delegation lookup is best-effort
|
|
645
|
-
console.error("[CloudflareMCPServer] Failed to retrieve delegation token from KV (existing session):", error);
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
// Update delegation fields if provided in params (takes precedence)
|
|
649
|
-
if (params.delegationToken) {
|
|
650
|
-
session.delegationToken = params.delegationToken;
|
|
651
|
-
}
|
|
652
|
-
if (params.consentProof) {
|
|
653
|
-
session.consentProof = params.consentProof;
|
|
654
|
-
}
|
|
241
|
+
// Reuse the session for attribution only. Grant facts an earlier
|
|
242
|
+
// call left on it (its account, grant id, sign-in method) are
|
|
243
|
+
// dropped: they come only from the credential this call presents,
|
|
244
|
+
// which the runtime verifies.
|
|
245
|
+
const { userDid: _userDid, delegationId: _delegationId, authorizationType: _authorizationType, ...attribution } = session;
|
|
246
|
+
session = attribution;
|
|
655
247
|
// Update server origin if available (for consent URL building)
|
|
656
248
|
if (this.requestOrigin) {
|
|
657
249
|
session.serverOrigin = this.requestOrigin;
|
|
658
250
|
}
|
|
659
251
|
}
|
|
252
|
+
session.delegationToken = params.delegationToken;
|
|
253
|
+
session.consentProof = params.consentProof;
|
|
660
254
|
// ✅ NEW: Determine scopeId from runtime config (for tool auto-discovery in Checkpoint)
|
|
661
255
|
let scopeId;
|
|
662
256
|
const runtimeConfig = this.runtime.config;
|
|
@@ -677,47 +271,6 @@ class CloudflareMCPServer {
|
|
|
677
271
|
session.toolName = toolName;
|
|
678
272
|
session.toolParams = params.arguments || {};
|
|
679
273
|
session.scopeId = scopeId; // ✅ ADDED: Pass scopeId for tool auto-discovery
|
|
680
|
-
// ✅ CRITICAL: Verify delegation token is set on session before processToolCall
|
|
681
|
-
// Final defensive lookup: DO first, then KV fallback
|
|
682
|
-
if (!session?.delegationToken &&
|
|
683
|
-
(this.doNamespace || this.delegationStorage)) {
|
|
684
|
-
try {
|
|
685
|
-
const agentDid = (await this.runtime.getIdentity()).did;
|
|
686
|
-
const userDid = session.userDid || params.clientDid || params.userDid;
|
|
687
|
-
let finalToken = undefined;
|
|
688
|
-
// PRIORITY 0: Durable Object (strongly consistent)
|
|
689
|
-
if (this.doNamespace && session.id) {
|
|
690
|
-
const doToken = await this.getDelegationFromDO(session.id, userDid, agentDid);
|
|
691
|
-
if (doToken) {
|
|
692
|
-
finalToken = doToken;
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
// PRIORITY 1: User+agent scoped token
|
|
696
|
-
if (!finalToken && userDid && this.delegationStorage) {
|
|
697
|
-
const userAgentKey = STORAGE_KEYS.delegation(userDid, agentDid);
|
|
698
|
-
const token = await this.delegationStorage.get(userAgentKey, "text");
|
|
699
|
-
finalToken = token || undefined; // Convert null to undefined
|
|
700
|
-
}
|
|
701
|
-
// PRIORITY 2: Session-scoped token
|
|
702
|
-
if (!finalToken && session.id && this.delegationStorage) {
|
|
703
|
-
const sessionKey = STORAGE_KEYS.session(session.id);
|
|
704
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
705
|
-
finalToken = sessionData?.delegationToken;
|
|
706
|
-
}
|
|
707
|
-
if (finalToken) {
|
|
708
|
-
session.delegationToken = finalToken;
|
|
709
|
-
console.error("[CloudflareMCPServer] ✅ Final lookup succeeded, token set on session:", {
|
|
710
|
-
toolName,
|
|
711
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
712
|
-
userDid: userDid ? userDid.slice(0, 20) + "..." : "none",
|
|
713
|
-
tokenLength: finalToken.length,
|
|
714
|
-
});
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
catch (error) {
|
|
718
|
-
console.error("[CloudflareMCPServer] Final delegation lookup failed:", error);
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
274
|
console.error("[CloudflareMCPServer] ✅ About to call processToolCall with session:", {
|
|
722
275
|
toolName,
|
|
723
276
|
sessionId: session?.id ? session.id.slice(0, 20) + "..." : "none",
|
|
@@ -1504,8 +1057,8 @@ export function createMCPICloudflareAdapter(config) {
|
|
|
1504
1057
|
// Extract origin from request for consent URL building
|
|
1505
1058
|
const requestUrl = new URL(request.url);
|
|
1506
1059
|
server.setRequestOrigin(requestUrl.origin);
|
|
1507
|
-
// Handle through MCP server
|
|
1508
|
-
const response = await server.handleRequest(parsed);
|
|
1060
|
+
// Handle through MCP server, for the host this request was routed for
|
|
1061
|
+
const response = await runWithRoutedHost(request, () => server.handleRequest(parsed));
|
|
1509
1062
|
// Extract session ID from response for MCP-Session-Id header
|
|
1510
1063
|
// Priority: proof.meta.sessionId > result.sessionId
|
|
1511
1064
|
const sessionId = response?.result?.proof?.meta?.sessionId ||
|
|
@@ -1777,8 +1330,8 @@ async function handleSSEPost(server, runtime, request) {
|
|
|
1777
1330
|
// Extract origin from request for consent URL building (if not already set)
|
|
1778
1331
|
const requestUrl = new URL(request.url);
|
|
1779
1332
|
server.setRequestOrigin(requestUrl.origin);
|
|
1780
|
-
// Handle through MCP server
|
|
1781
|
-
const response = await server.handleRequest(parsed);
|
|
1333
|
+
// Handle through MCP server, for the host this request was routed for
|
|
1334
|
+
const response = await runWithRoutedHost(request, () => server.handleRequest(parsed));
|
|
1782
1335
|
// Format response as SSE event data
|
|
1783
1336
|
const eventData = `data: ${JSON.stringify(response)}\n\n`;
|
|
1784
1337
|
// Extract session ID from response for Mcp-Session-Id header
|