@kya-os/mcp-i-cloudflare 1.17.0 → 1.18.1
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 +39 -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 +386 -404
- 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/amount-limit-refusal.d.ts +3 -0
- package/dist/runtime/amount-limit-refusal.d.ts.map +1 -0
- package/dist/runtime/amount-limit-refusal.js +7 -0
- package/dist/runtime/amount-limit-refusal.js.map +1 -0
- 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 +31 -34
- 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 +65 -16
- 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/proof.service.d.ts +10 -0
- package/dist/services/proof.service.d.ts.map +1 -1
- package/dist/services/proof.service.js +28 -6
- package/dist/services/proof.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/oauth-profile.d.ts +19 -0
- package/dist/utils/oauth-profile.d.ts.map +1 -1
- package/dist/utils/oauth-profile.js +54 -1
- package/dist/utils/oauth-profile.js.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 +5 -6
- 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 -393
- 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
|
@@ -12,7 +12,10 @@ import { createCloudflareRuntime } from "./index";
|
|
|
12
12
|
import { WELL_KNOWN_CORS_HEADERS, MCP_CORS_HEADERS, PREFLIGHT_CORS_HEADERS, DelegationErrorFormatter, SESSION_ID_PREFIX, } from "@kya-os/mcp-i-runtime";
|
|
13
13
|
import { findKnownClient } from "./utils/known-clients";
|
|
14
14
|
import { buildNeedsAuthorizationMeta } from "./needs-authorization-meta";
|
|
15
|
+
import { isAmountLimitError } from "./runtime/amount-limit-refusal";
|
|
16
|
+
import { amountLimitToolResult } from "@kya-os/mcp-i-runtime";
|
|
15
17
|
import { KVProofArchive } from "./storage/kv-proof-archive";
|
|
18
|
+
import { runWithRoutedHost } from "./security/routed-request";
|
|
16
19
|
import { STORAGE_KEYS } from "./constants/storage-keys";
|
|
17
20
|
import { DEFAULT_SESSION_CACHE_TTL, DEFAULT_SESSION_TTL } from "./constants";
|
|
18
21
|
import { normalizeCloudflareEnv } from "./helpers/env-mapper";
|
|
@@ -65,62 +68,6 @@ class CloudflareMCPServer {
|
|
|
65
68
|
calculateDOInstanceId(sessionId) {
|
|
66
69
|
return calculateDOInstanceId(sessionId, this.doRoutingStrategy, this.doShardCount);
|
|
67
70
|
}
|
|
68
|
-
/**
|
|
69
|
-
* Fetch delegation from Durable Object (strongly consistent storage)
|
|
70
|
-
*
|
|
71
|
-
* This provides parity with the Agent pattern by using the same DO-based
|
|
72
|
-
* storage for delegations. The consent service stores to DO first, and
|
|
73
|
-
* now the adapter can read from DO first as well.
|
|
74
|
-
*
|
|
75
|
-
* @param sessionId - Session ID to lookup
|
|
76
|
-
* @param userDid - Optional user DID for user+agent scoped lookup
|
|
77
|
-
* @param agentDid - Agent DID for the lookup
|
|
78
|
-
* @returns Delegation token if found, undefined otherwise
|
|
79
|
-
*/
|
|
80
|
-
async getDelegationFromDO(sessionId, userDid, agentDid) {
|
|
81
|
-
if (!this.doNamespace) {
|
|
82
|
-
return undefined;
|
|
83
|
-
}
|
|
84
|
-
try {
|
|
85
|
-
// Use the same routing strategy as consent service
|
|
86
|
-
const doInstanceId = this.calculateDOInstanceId(sessionId);
|
|
87
|
-
const doId = this.doNamespace.idFromName(doInstanceId);
|
|
88
|
-
const doStub = this.doNamespace.get(doId);
|
|
89
|
-
// Build request URL - use the origin or a placeholder
|
|
90
|
-
const origin = this.requestOrigin || "https://localhost";
|
|
91
|
-
const url = new URL("/_internal/delegation/get", origin);
|
|
92
|
-
const response = await doStub.fetch(url.toString(), {
|
|
93
|
-
method: "POST",
|
|
94
|
-
headers: { "Content-Type": "application/json" },
|
|
95
|
-
body: JSON.stringify({
|
|
96
|
-
sessionId,
|
|
97
|
-
userDid,
|
|
98
|
-
agentDid,
|
|
99
|
-
}),
|
|
100
|
-
});
|
|
101
|
-
if (!response.ok) {
|
|
102
|
-
console.error("[CloudflareMCPServer] DO delegation lookup failed:", {
|
|
103
|
-
status: response.status,
|
|
104
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
105
|
-
});
|
|
106
|
-
return undefined;
|
|
107
|
-
}
|
|
108
|
-
const data = (await response.json());
|
|
109
|
-
if (data.success && data.data?.delegationToken) {
|
|
110
|
-
console.error("[CloudflareMCPServer] ✅ Delegation token retrieved from DO:", {
|
|
111
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
112
|
-
userDid: userDid ? userDid.slice(0, 20) + "..." : "none",
|
|
113
|
-
tokenLength: data.data.delegationToken.length,
|
|
114
|
-
});
|
|
115
|
-
return data.data.delegationToken;
|
|
116
|
-
}
|
|
117
|
-
return undefined;
|
|
118
|
-
}
|
|
119
|
-
catch (error) {
|
|
120
|
-
console.error("[CloudflareMCPServer] DO delegation lookup error:", error);
|
|
121
|
-
return undefined;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
71
|
/**
|
|
125
72
|
* Retrieve stored client capabilities from DO storage.
|
|
126
73
|
* The agent stores these during the MCP initialize handshake.
|
|
@@ -150,10 +97,9 @@ class CloudflareMCPServer {
|
|
|
150
97
|
* Priority: 1. `Mcp-Session-Id` header (MCP protocol standard),
|
|
151
98
|
* 2. `session_id` / `sessionId` param (legacy clients).
|
|
152
99
|
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
* id
|
|
156
|
-
* 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.
|
|
157
103
|
*/
|
|
158
104
|
resolveTransportSessionId(meta, params) {
|
|
159
105
|
const headerSessionId = meta?.request?.headers?.get?.("mcp-session-id") ||
|
|
@@ -216,17 +162,14 @@ class CloudflareMCPServer {
|
|
|
216
162
|
throw new Error(`Tool not found: ${toolName}`);
|
|
217
163
|
}
|
|
218
164
|
// The transport session id (Mcp-Session-Id header, else session_id
|
|
219
|
-
// param)
|
|
220
|
-
//
|
|
221
|
-
//
|
|
222
|
-
//
|
|
223
|
-
//
|
|
224
|
-
//
|
|
225
|
-
//
|
|
226
|
-
//
|
|
227
|
-
// isolate; keying on it made the lookup miss forever and could read
|
|
228
|
-
// another user's delegation (#691). It stays the fallback only when
|
|
229
|
-
// 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.
|
|
230
173
|
const transportSession = this.resolveTransportSessionId(meta, params);
|
|
231
174
|
let session = null;
|
|
232
175
|
try {
|
|
@@ -234,8 +177,7 @@ class CloudflareMCPServer {
|
|
|
234
177
|
// `getCurrentSession()` which skips sessions the ClockProvider says
|
|
235
178
|
// have expired (`now > expiresAt`). Mirror that check here so an
|
|
236
179
|
// expired in-memory session is treated as absent and the ephemeral
|
|
237
|
-
// path below rebuilds it from the transport id
|
|
238
|
-
// reusing its stale delegationToken/userDid.
|
|
180
|
+
// path below rebuilds it from the transport id.
|
|
239
181
|
const inMemorySession = transportSession.id
|
|
240
182
|
? this.runtime.getSession?.(transportSession.id)
|
|
241
183
|
: undefined;
|
|
@@ -261,7 +203,11 @@ class CloudflareMCPServer {
|
|
|
261
203
|
console.error("[CloudflareMCPServer] Error resolving runtime session:", error);
|
|
262
204
|
// Continue with null session (will create ephemeral)
|
|
263
205
|
}
|
|
264
|
-
//
|
|
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.
|
|
265
211
|
if (!session) {
|
|
266
212
|
const providedSessionId = transportSession.id;
|
|
267
213
|
const timestamp = Date.now();
|
|
@@ -274,201 +220,6 @@ class CloudflareMCPServer {
|
|
|
274
220
|
isProvided: !!providedSessionId,
|
|
275
221
|
source: transportSession.source ?? "generated",
|
|
276
222
|
});
|
|
277
|
-
// Extract OAuth identity from request or session cache for persistent userDid lookup
|
|
278
|
-
let oauthIdentity = undefined;
|
|
279
|
-
if (meta?.request) {
|
|
280
|
-
oauthIdentity = this.extractOAuthIdentityFromRequest(meta.request);
|
|
281
|
-
if (oauthIdentity) {
|
|
282
|
-
console.error("[CloudflareMCPServer] Extracted OAuth identity from request:", {
|
|
283
|
-
provider: oauthIdentity.provider,
|
|
284
|
-
subject: oauthIdentity.subject.substring(0, 20) + "...",
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
// If OAuth identity not in request, try to get from session cache
|
|
289
|
-
if (!oauthIdentity && sessionId && this.delegationStorage) {
|
|
290
|
-
try {
|
|
291
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
292
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
293
|
-
if (sessionData?.oauthIdentity?.provider) {
|
|
294
|
-
// Note: We only have subjectHash in session cache (PII protection)
|
|
295
|
-
// We can't fully reconstruct OAuth identity, but we can use it to lookup userDid
|
|
296
|
-
console.error("[CloudflareMCPServer] Found OAuth identity in session cache:", {
|
|
297
|
-
provider: sessionData.oauthIdentity.provider,
|
|
298
|
-
hasSubjectHash: !!sessionData.oauthIdentity.subjectHash,
|
|
299
|
-
});
|
|
300
|
-
// OAuth identity from session cache is incomplete (subjectHash only), so we'll use it for userDid lookup via storage
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
catch (error) {
|
|
304
|
-
console.warn("[CloudflareMCPServer] Failed to get OAuth identity from session:", error);
|
|
305
|
-
// Non-fatal - continue without OAuth identity
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
// Check KV storage for stored delegation token if not provided in params
|
|
309
|
-
let delegationToken = params.delegationToken;
|
|
310
|
-
if (!delegationToken &&
|
|
311
|
-
(this.delegationStorage || this.doNamespace)) {
|
|
312
|
-
try {
|
|
313
|
-
// Get userDID from params first
|
|
314
|
-
let userDid = params.clientDid || params.userDid;
|
|
315
|
-
// ✅ PRIORITY 1: If OAuth identity available, use it to lookup persistent userDid
|
|
316
|
-
if (!userDid && oauthIdentity && this.delegationStorage) {
|
|
317
|
-
try {
|
|
318
|
-
const oauthKey = STORAGE_KEYS.oauthIdentity(oauthIdentity.provider, oauthIdentity.subject);
|
|
319
|
-
const mappedUserDid = await this.delegationStorage.get(oauthKey, "text");
|
|
320
|
-
if (mappedUserDid) {
|
|
321
|
-
userDid = mappedUserDid;
|
|
322
|
-
console.error("[CloudflareMCPServer] ✅ Retrieved persistent userDid from OAuth mapping:", {
|
|
323
|
-
provider: oauthIdentity.provider,
|
|
324
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
catch (error) {
|
|
329
|
-
console.warn("[CloudflareMCPServer] Failed to lookup userDid from OAuth mapping:", error);
|
|
330
|
-
// Non-fatal - continue with session cache lookup
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
// ✅ PRIORITY 2: If userDid not in params or OAuth mapping, try to retrieve from session cache
|
|
334
|
-
if (!userDid && sessionId && this.delegationStorage) {
|
|
335
|
-
try {
|
|
336
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
337
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
338
|
-
if (sessionData?.userDid) {
|
|
339
|
-
userDid = sessionData.userDid;
|
|
340
|
-
console.error("[CloudflareMCPServer] ✅ Retrieved userDid from session cache:", {
|
|
341
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
342
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
catch (error) {
|
|
347
|
-
console.warn("[CloudflareMCPServer] Failed to get userDid from session:", error);
|
|
348
|
-
// Non-fatal - continue without userDid
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
console.error("[CloudflareMCPServer] 🔍 Starting delegation token lookup:", {
|
|
352
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
353
|
-
userDid: userDid ? userDid.slice(0, 20) + "..." : "none",
|
|
354
|
-
sessionId: sessionId
|
|
355
|
-
? sessionId.slice(0, 20) + "..."
|
|
356
|
-
: "none",
|
|
357
|
-
hasDelegationStorage: !!this.delegationStorage,
|
|
358
|
-
hasDONamespace: !!this.doNamespace,
|
|
359
|
-
});
|
|
360
|
-
// ✅ PRIORITY 0: Try Durable Object (strongly consistent, parity with Agent pattern)
|
|
361
|
-
// This is the preferred approach for consistent delegation storage across all deployment types
|
|
362
|
-
if (this.doNamespace && sessionId) {
|
|
363
|
-
const doToken = await this.getDelegationFromDO(sessionId, userDid, agentDid);
|
|
364
|
-
if (doToken) {
|
|
365
|
-
delegationToken = doToken;
|
|
366
|
-
console.error("[CloudflareMCPServer] ✅ PRIORITY 0: Delegation token from DO (strongly consistent)");
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
// PRIORITY 1: Try user+agent scoped token (user-specific, most secure)
|
|
370
|
-
// This is the preferred approach for multi-user scenarios with proper user isolation
|
|
371
|
-
// Skip if we already have a token from DO (PRIORITY 0)
|
|
372
|
-
if (!delegationToken && userDid && this.delegationStorage) {
|
|
373
|
-
const userAgentKey = STORAGE_KEYS.delegation(userDid, agentDid);
|
|
374
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 1: Checking user+agent scoped key:", {
|
|
375
|
-
key: userAgentKey,
|
|
376
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
377
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
378
|
-
});
|
|
379
|
-
const userAgentToken = await this.delegationStorage.get(userAgentKey, "text");
|
|
380
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 1: User+agent scoped lookup result:", {
|
|
381
|
-
key: userAgentKey,
|
|
382
|
-
found: !!userAgentToken,
|
|
383
|
-
tokenLength: userAgentToken?.length || 0,
|
|
384
|
-
tokenPreview: userAgentToken
|
|
385
|
-
? userAgentToken.substring(0, 20) + "..."
|
|
386
|
-
: null,
|
|
387
|
-
});
|
|
388
|
-
if (userAgentToken) {
|
|
389
|
-
delegationToken = userAgentToken;
|
|
390
|
-
console.error("[CloudflareMCPServer] ✅ Delegation token retrieved from user+agent scoped key:", {
|
|
391
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
392
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
393
|
-
key: userAgentKey,
|
|
394
|
-
tokenLength: userAgentToken.length,
|
|
395
|
-
});
|
|
396
|
-
// Cache it for this session for faster future lookups
|
|
397
|
-
// Store full session data object to match consent service format
|
|
398
|
-
if (this.delegationStorage && sessionId) {
|
|
399
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
400
|
-
// Read existing session data to preserve userDid and other fields
|
|
401
|
-
const existingSession = (await this.delegationStorage.get(sessionKey, "json"));
|
|
402
|
-
await this.delegationStorage.put(sessionKey, JSON.stringify({
|
|
403
|
-
...existingSession,
|
|
404
|
-
userDid,
|
|
405
|
-
agentDid,
|
|
406
|
-
delegationToken: userAgentToken,
|
|
407
|
-
cachedAt: Date.now(),
|
|
408
|
-
}), {
|
|
409
|
-
expirationTtl: 1800, // 30 minutes
|
|
410
|
-
});
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
// PRIORITY 2: Try session-scoped token (if session ID was persisted)
|
|
415
|
-
// Note: Sessions are ephemeral in Cloudflare Workers, so this is mainly
|
|
416
|
-
// useful for caching tokens within a single request chain
|
|
417
|
-
if (!delegationToken && sessionId && this.delegationStorage) {
|
|
418
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
419
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 2: Checking session-scoped key:", {
|
|
420
|
-
key: sessionKey,
|
|
421
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
422
|
-
});
|
|
423
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
424
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 2: Session-scoped lookup result:", {
|
|
425
|
-
key: sessionKey,
|
|
426
|
-
found: !!sessionData,
|
|
427
|
-
hasDelegationToken: !!sessionData?.delegationToken,
|
|
428
|
-
tokenLength: sessionData?.delegationToken?.length || 0,
|
|
429
|
-
tokenPreview: sessionData?.delegationToken
|
|
430
|
-
? sessionData.delegationToken.substring(0, 20) + "..."
|
|
431
|
-
: null,
|
|
432
|
-
});
|
|
433
|
-
if (sessionData?.delegationToken) {
|
|
434
|
-
delegationToken = sessionData.delegationToken;
|
|
435
|
-
console.error("[CloudflareMCPServer] ✅ Delegation token retrieved from session cache:", {
|
|
436
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
437
|
-
tokenLength: delegationToken.length,
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
// ❌ REMOVED: Legacy agent-scoped fallback (PRIORITY 3)
|
|
442
|
-
// This was a security vulnerability that allowed cross-user delegation sharing.
|
|
443
|
-
// Delegations are now strictly session-scoped or user+agent scoped.
|
|
444
|
-
if (!delegationToken) {
|
|
445
|
-
const checkedKeys = [];
|
|
446
|
-
// Include user+agent scoped key if userDid is available
|
|
447
|
-
if (userDid) {
|
|
448
|
-
checkedKeys.push(STORAGE_KEYS.delegation(userDid, agentDid));
|
|
449
|
-
}
|
|
450
|
-
// Include session-scoped key if sessionId is available
|
|
451
|
-
if (sessionId) {
|
|
452
|
-
checkedKeys.push(STORAGE_KEYS.session(sessionId));
|
|
453
|
-
}
|
|
454
|
-
console.error("[CloudflareMCPServer] ⚠️ No delegation token found - user must complete consent flow:", {
|
|
455
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
456
|
-
userDid: userDid
|
|
457
|
-
? userDid.slice(0, 20) + "..."
|
|
458
|
-
: "none (consent-only)",
|
|
459
|
-
sessionId: sessionId
|
|
460
|
-
? sessionId.slice(0, 20) + "..."
|
|
461
|
-
: "none",
|
|
462
|
-
checkedKeys,
|
|
463
|
-
hint: "Delegation tokens are session-scoped or user+agent scoped. Complete consent flow for this session.",
|
|
464
|
-
});
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
catch (error) {
|
|
468
|
-
// Log but don't fail - delegation lookup is best-effort
|
|
469
|
-
console.error("[CloudflareMCPServer] Failed to retrieve delegation token from KV:", error);
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
223
|
// Retrieve stored client capabilities from DO (set during MCP initialize)
|
|
473
224
|
const clientCapabilities = await this.getClientCapabilitiesFromDO(sessionId);
|
|
474
225
|
session = {
|
|
@@ -478,9 +229,6 @@ class CloudflareMCPServer {
|
|
|
478
229
|
agentDid,
|
|
479
230
|
createdAt: timestamp,
|
|
480
231
|
expiresAt: timestamp + 30 * 60 * 1000, // 30 minutes
|
|
481
|
-
// Use token from params or KV storage
|
|
482
|
-
delegationToken,
|
|
483
|
-
consentProof: params.consentProof,
|
|
484
232
|
// Auto-detect server origin for consent URL building
|
|
485
233
|
serverOrigin: this.requestOrigin,
|
|
486
234
|
// Client capabilities for MCP Apps UI detection
|
|
@@ -488,173 +236,21 @@ class CloudflareMCPServer {
|
|
|
488
236
|
? { capabilities: clientCapabilities }
|
|
489
237
|
: undefined,
|
|
490
238
|
};
|
|
491
|
-
// ✅ CRITICAL: Log delegation token status before processToolCall
|
|
492
|
-
console.error("[CloudflareMCPServer] ✅ Session created with delegation token status:", {
|
|
493
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
494
|
-
hasDelegationToken: !!delegationToken,
|
|
495
|
-
delegationTokenLength: delegationToken?.length || 0,
|
|
496
|
-
delegationTokenSource: params.delegationToken
|
|
497
|
-
? "params"
|
|
498
|
-
: delegationToken
|
|
499
|
-
? "kv-storage"
|
|
500
|
-
: "none",
|
|
501
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
502
|
-
});
|
|
503
239
|
}
|
|
504
240
|
else {
|
|
505
|
-
//
|
|
506
|
-
//
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
sessionId: session?.id ? session.id.slice(0, 20) + "..." : "none",
|
|
512
|
-
sessionKeys: Object.keys(session || {}),
|
|
513
|
-
});
|
|
514
|
-
if (!session.delegationToken &&
|
|
515
|
-
(this.delegationStorage || this.doNamespace)) {
|
|
516
|
-
console.error("[CloudflareMCPServer] ⚠️ Session has no delegationToken, attempting KV lookup...");
|
|
517
|
-
try {
|
|
518
|
-
const agentDid = (await this.runtime.getIdentity()).did;
|
|
519
|
-
// ✅ CRITICAL: Retrieve userDid from session cache if not in session object
|
|
520
|
-
// Check session cache first, then params, then session object
|
|
521
|
-
let userDid = session.userDid ||
|
|
522
|
-
params.clientDid ||
|
|
523
|
-
params.userDid ||
|
|
524
|
-
session.clientDid;
|
|
525
|
-
const sessionId = session.id;
|
|
526
|
-
// If userDid still not found, try retrieving from session cache
|
|
527
|
-
if (!userDid && sessionId && this.delegationStorage) {
|
|
528
|
-
try {
|
|
529
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
530
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
531
|
-
if (sessionData?.userDid) {
|
|
532
|
-
userDid = sessionData.userDid;
|
|
533
|
-
// Update session object with retrieved userDid
|
|
534
|
-
session.userDid = userDid;
|
|
535
|
-
console.error("[CloudflareMCPServer] ✅ Retrieved userDid from session cache for delegation lookup:", {
|
|
536
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
537
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
538
|
-
});
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
catch (error) {
|
|
542
|
-
console.warn("[CloudflareMCPServer] Failed to get userDid from session cache:", error);
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
console.error("[CloudflareMCPServer] 🔍 Looking up delegation token for existing session:", {
|
|
546
|
-
sessionId: sessionId
|
|
547
|
-
? sessionId.slice(0, 20) + "..."
|
|
548
|
-
: "none",
|
|
549
|
-
userDid: userDid ? userDid.slice(0, 20) + "..." : "none",
|
|
550
|
-
agentDid: agentDid ? agentDid.slice(0, 20) + "..." : "none",
|
|
551
|
-
});
|
|
552
|
-
let delegationToken = undefined;
|
|
553
|
-
// PRIORITY 0: Try Durable Object (strongly consistent, parity with Agent pattern)
|
|
554
|
-
if (this.doNamespace && sessionId) {
|
|
555
|
-
const doToken = await this.getDelegationFromDO(sessionId, userDid, agentDid);
|
|
556
|
-
if (doToken) {
|
|
557
|
-
delegationToken = doToken;
|
|
558
|
-
console.error("[CloudflareMCPServer] ✅ PRIORITY 0: Delegation token from DO (existing session)");
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
// PRIORITY 1: Try user+agent scoped token (user-specific, most secure)
|
|
562
|
-
// This is the preferred approach for multi-user scenarios with proper user isolation
|
|
563
|
-
// Track which source the token was found in for accurate logging
|
|
564
|
-
let tokenSource;
|
|
565
|
-
if (delegationToken) {
|
|
566
|
-
tokenSource = "do";
|
|
567
|
-
}
|
|
568
|
-
else if (userDid && this.delegationStorage) {
|
|
569
|
-
const userAgentKey = STORAGE_KEYS.delegation(userDid, agentDid);
|
|
570
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 1: Checking user+agent scoped key (existing session):", {
|
|
571
|
-
key: userAgentKey,
|
|
572
|
-
userDid: userDid.slice(0, 20) + "...",
|
|
573
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
574
|
-
});
|
|
575
|
-
const userAgentToken = await this.delegationStorage.get(userAgentKey, "text");
|
|
576
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 1: User+agent scoped lookup result:", {
|
|
577
|
-
key: userAgentKey,
|
|
578
|
-
found: !!userAgentToken,
|
|
579
|
-
tokenLength: userAgentToken?.length || 0,
|
|
580
|
-
});
|
|
581
|
-
if (userAgentToken) {
|
|
582
|
-
delegationToken = userAgentToken;
|
|
583
|
-
tokenSource = "user+agent-key";
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
// PRIORITY 2: Try session-scoped token (if session ID was persisted)
|
|
587
|
-
if (!delegationToken && sessionId && this.delegationStorage) {
|
|
588
|
-
const sessionKey = STORAGE_KEYS.session(sessionId);
|
|
589
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 2: Checking session-scoped key (existing session):", {
|
|
590
|
-
key: sessionKey,
|
|
591
|
-
sessionId: sessionId.slice(0, 20) + "...",
|
|
592
|
-
});
|
|
593
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
594
|
-
console.error("[CloudflareMCPServer] 🔍 PRIORITY 2: Session-scoped lookup result:", {
|
|
595
|
-
key: sessionKey,
|
|
596
|
-
found: !!sessionData,
|
|
597
|
-
hasDelegationToken: !!sessionData?.delegationToken,
|
|
598
|
-
tokenLength: sessionData?.delegationToken?.length || 0,
|
|
599
|
-
});
|
|
600
|
-
if (sessionData?.delegationToken) {
|
|
601
|
-
delegationToken = sessionData.delegationToken;
|
|
602
|
-
tokenSource = "session-key";
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
// ❌ REMOVED: Legacy agent-scoped fallback (PRIORITY 3)
|
|
606
|
-
// This was a security vulnerability that allowed cross-user delegation sharing.
|
|
607
|
-
// ✅ CRITICAL: Set delegation token on session if found
|
|
608
|
-
if (delegationToken) {
|
|
609
|
-
session = {
|
|
610
|
-
...session,
|
|
611
|
-
delegationToken,
|
|
612
|
-
};
|
|
613
|
-
console.error("[CloudflareMCPServer] ✅ Delegation token set on existing session:", {
|
|
614
|
-
sessionId: sessionId
|
|
615
|
-
? sessionId.slice(0, 20) + "..."
|
|
616
|
-
: "none",
|
|
617
|
-
tokenLength: delegationToken.length,
|
|
618
|
-
sessionHasToken: !!session.delegationToken,
|
|
619
|
-
source: tokenSource,
|
|
620
|
-
});
|
|
621
|
-
}
|
|
622
|
-
else {
|
|
623
|
-
const checkedKeys = [];
|
|
624
|
-
// Include user+agent scoped key if userDid is available
|
|
625
|
-
if (userDid) {
|
|
626
|
-
checkedKeys.push(STORAGE_KEYS.delegation(userDid, agentDid));
|
|
627
|
-
}
|
|
628
|
-
// Include session-scoped key if sessionId is available
|
|
629
|
-
if (sessionId) {
|
|
630
|
-
checkedKeys.push(STORAGE_KEYS.session(sessionId));
|
|
631
|
-
}
|
|
632
|
-
console.error("[CloudflareMCPServer] ⚠️ No delegation token found - user must complete consent flow:", {
|
|
633
|
-
sessionId: sessionId
|
|
634
|
-
? sessionId.slice(0, 20) + "..."
|
|
635
|
-
: "none",
|
|
636
|
-
checkedKeys,
|
|
637
|
-
hint: "Complete consent flow for this session to get a delegation token.",
|
|
638
|
-
});
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
catch (error) {
|
|
642
|
-
// Log but don't fail - delegation lookup is best-effort
|
|
643
|
-
console.error("[CloudflareMCPServer] Failed to retrieve delegation token from KV (existing session):", error);
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
// Update delegation fields if provided in params (takes precedence)
|
|
647
|
-
if (params.delegationToken) {
|
|
648
|
-
session.delegationToken = params.delegationToken;
|
|
649
|
-
}
|
|
650
|
-
if (params.consentProof) {
|
|
651
|
-
session.consentProof = params.consentProof;
|
|
652
|
-
}
|
|
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;
|
|
653
247
|
// Update server origin if available (for consent URL building)
|
|
654
248
|
if (this.requestOrigin) {
|
|
655
249
|
session.serverOrigin = this.requestOrigin;
|
|
656
250
|
}
|
|
657
251
|
}
|
|
252
|
+
session.delegationToken = params.delegationToken;
|
|
253
|
+
session.consentProof = params.consentProof;
|
|
658
254
|
// ✅ NEW: Determine scopeId from runtime config (for tool auto-discovery in Checkpoint)
|
|
659
255
|
let scopeId;
|
|
660
256
|
const runtimeConfig = this.runtime.config;
|
|
@@ -675,47 +271,6 @@ class CloudflareMCPServer {
|
|
|
675
271
|
session.toolName = toolName;
|
|
676
272
|
session.toolParams = params.arguments || {};
|
|
677
273
|
session.scopeId = scopeId; // ✅ ADDED: Pass scopeId for tool auto-discovery
|
|
678
|
-
// ✅ CRITICAL: Verify delegation token is set on session before processToolCall
|
|
679
|
-
// Final defensive lookup: DO first, then KV fallback
|
|
680
|
-
if (!session?.delegationToken &&
|
|
681
|
-
(this.doNamespace || this.delegationStorage)) {
|
|
682
|
-
try {
|
|
683
|
-
const agentDid = (await this.runtime.getIdentity()).did;
|
|
684
|
-
const userDid = session.userDid || params.clientDid || params.userDid;
|
|
685
|
-
let finalToken = undefined;
|
|
686
|
-
// PRIORITY 0: Durable Object (strongly consistent)
|
|
687
|
-
if (this.doNamespace && session.id) {
|
|
688
|
-
const doToken = await this.getDelegationFromDO(session.id, userDid, agentDid);
|
|
689
|
-
if (doToken) {
|
|
690
|
-
finalToken = doToken;
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
// PRIORITY 1: User+agent scoped token
|
|
694
|
-
if (!finalToken && userDid && this.delegationStorage) {
|
|
695
|
-
const userAgentKey = STORAGE_KEYS.delegation(userDid, agentDid);
|
|
696
|
-
const token = await this.delegationStorage.get(userAgentKey, "text");
|
|
697
|
-
finalToken = token || undefined; // Convert null to undefined
|
|
698
|
-
}
|
|
699
|
-
// PRIORITY 2: Session-scoped token
|
|
700
|
-
if (!finalToken && session.id && this.delegationStorage) {
|
|
701
|
-
const sessionKey = STORAGE_KEYS.session(session.id);
|
|
702
|
-
const sessionData = (await this.delegationStorage.get(sessionKey, "json"));
|
|
703
|
-
finalToken = sessionData?.delegationToken;
|
|
704
|
-
}
|
|
705
|
-
if (finalToken) {
|
|
706
|
-
session.delegationToken = finalToken;
|
|
707
|
-
console.error("[CloudflareMCPServer] ✅ Final lookup succeeded, token set on session:", {
|
|
708
|
-
toolName,
|
|
709
|
-
agentDid: agentDid.slice(0, 20) + "...",
|
|
710
|
-
userDid: userDid ? userDid.slice(0, 20) + "..." : "none",
|
|
711
|
-
tokenLength: finalToken.length,
|
|
712
|
-
});
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
catch (error) {
|
|
716
|
-
console.error("[CloudflareMCPServer] Final delegation lookup failed:", error);
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
274
|
console.error("[CloudflareMCPServer] ✅ About to call processToolCall with session:", {
|
|
720
275
|
toolName,
|
|
721
276
|
sessionId: session?.id ? session.id.slice(0, 20) + "..." : "none",
|
|
@@ -979,6 +534,13 @@ class CloudflareMCPServer {
|
|
|
979
534
|
},
|
|
980
535
|
};
|
|
981
536
|
}
|
|
537
|
+
if (isAmountLimitError(error)) {
|
|
538
|
+
return {
|
|
539
|
+
jsonrpc: "2.0",
|
|
540
|
+
id,
|
|
541
|
+
result: amountLimitToolResult(error),
|
|
542
|
+
};
|
|
543
|
+
}
|
|
982
544
|
// Generic error handler
|
|
983
545
|
let errorMessage = "Internal error";
|
|
984
546
|
if (error) {
|
|
@@ -1495,8 +1057,8 @@ export function createMCPICloudflareAdapter(config) {
|
|
|
1495
1057
|
// Extract origin from request for consent URL building
|
|
1496
1058
|
const requestUrl = new URL(request.url);
|
|
1497
1059
|
server.setRequestOrigin(requestUrl.origin);
|
|
1498
|
-
// Handle through MCP server
|
|
1499
|
-
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));
|
|
1500
1062
|
// Extract session ID from response for MCP-Session-Id header
|
|
1501
1063
|
// Priority: proof.meta.sessionId > result.sessionId
|
|
1502
1064
|
const sessionId = response?.result?.proof?.meta?.sessionId ||
|
|
@@ -1768,8 +1330,8 @@ async function handleSSEPost(server, runtime, request) {
|
|
|
1768
1330
|
// Extract origin from request for consent URL building (if not already set)
|
|
1769
1331
|
const requestUrl = new URL(request.url);
|
|
1770
1332
|
server.setRequestOrigin(requestUrl.origin);
|
|
1771
|
-
// Handle through MCP server
|
|
1772
|
-
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));
|
|
1773
1335
|
// Format response as SSE event data
|
|
1774
1336
|
const eventData = `data: ${JSON.stringify(response)}\n\n`;
|
|
1775
1337
|
// Extract session ID from response for Mcp-Session-Id header
|