55 createAuthHandlers ,
66 handleTokenExchangeCallback ,
77} from '@repo/mcp-common/src/cloudflare-oauth-handler'
8+ import { getUserDetails , UserDetails } from '@repo/mcp-common/src/durable-objects/user_details'
89import { getEnv } from '@repo/mcp-common/src/env'
910import { RequiredScopes } from '@repo/mcp-common/src/scopes'
1011import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
@@ -18,6 +19,8 @@ import { MetricsTracker } from '@repo/mcp-observability'
1819import type { AccountSchema , UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
1920import type { Env } from './context'
2021
22+ export { UserDetails }
23+
2124const env = getEnv < Env > ( )
2225
2326const metrics = new MetricsTracker ( env . MCP_METRICS , {
@@ -73,24 +76,25 @@ export class WorkersBindingsMCP extends McpAgent<Env, WorkersBindingsMCPState, P
7376 registerR2BucketTools ( this )
7477 registerD1Tools ( this )
7578 }
76- getActiveAccountId ( ) {
77- // TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch
79+
80+ async getActiveAccountId ( ) {
7881 try {
79- return this . state . activeAccountId ?? null
82+ // Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it
83+ // we do this so we can persist activeAccountId across sessions
84+ const userDetails = getUserDetails ( env , this . props . user . id )
85+ return await userDetails . getActiveAccountId ( )
8086 } catch ( e ) {
87+ this . server . recordError ( e )
8188 return null
8289 }
8390 }
8491
85- setActiveAccountId ( accountId : string ) {
86- // TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch
92+ async setActiveAccountId ( accountId : string ) {
8793 try {
88- this . setState ( {
89- ...this . state ,
90- activeAccountId : accountId ,
91- } )
94+ const userDetails = getUserDetails ( env , this . props . user . id )
95+ await userDetails . setActiveAccountId ( accountId )
9296 } catch ( e ) {
93- return null
97+ this . server . recordError ( e )
9498 }
9599 }
96100}
0 commit comments