@@ -6,9 +6,11 @@ import {
66 handleTokenExchangeCallback ,
77} from '@repo/mcp-common/src/cloudflare-oauth-handler'
88import { handleDevMode } from '@repo/mcp-common/src/dev-mode'
9+ import { getUserDetails , UserDetails } from '@repo/mcp-common/src/durable-objects/user_details'
910import { getEnv } from '@repo/mcp-common/src/env'
1011import { RequiredScopes } from '@repo/mcp-common/src/scopes'
1112import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
13+ import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
1214import { MetricsTracker } from '@repo/mcp-observability'
1315
1416import { registerRadarTools } from './tools/radar'
@@ -19,6 +21,8 @@ import type { Env } from './context'
1921
2022const env = getEnv < Env > ( )
2123
24+ export { UserDetails }
25+
2226const metrics = new MetricsTracker ( env . MCP_METRICS , {
2327 name : env . MCP_SERVER_NAME ,
2428 version : env . MCP_SERVER_VERSION ,
@@ -27,15 +31,13 @@ const metrics = new MetricsTracker(env.MCP_METRICS, {
2731// Context from the auth process, encrypted & stored in the auth token
2832// and provided to the DurableMCP as this.props
2933type Props = AuthProps
30-
31- type State = never
34+ type State = { activeAccountId : string | null }
3235
3336export class RadarMCP extends McpAgent < Env , State , Props > {
3437 _server : CloudflareMCPServer | undefined
3538 set server ( server : CloudflareMCPServer ) {
3639 this . _server = server
3740 }
38-
3941 get server ( ) : CloudflareMCPServer {
4042 if ( ! this . _server ) {
4143 throw new Error ( 'Tried to access server before it was initialized' )
@@ -44,10 +46,7 @@ export class RadarMCP extends McpAgent<Env, State, Props> {
4446 return this . _server
4547 }
4648
47- constructor (
48- public ctx : DurableObjectState ,
49- public env : Env
50- ) {
49+ constructor ( ctx : DurableObjectState , env : Env ) {
5150 super ( ctx , env )
5251 }
5352
@@ -61,15 +60,38 @@ export class RadarMCP extends McpAgent<Env, State, Props> {
6160 } ,
6261 } )
6362
63+ registerAccountTools ( this )
6464 registerRadarTools ( this )
6565 registerUrlScannerTools ( this )
6666 }
67+
68+ async getActiveAccountId ( ) {
69+ try {
70+ // Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it
71+ // we do this so we can persist activeAccountId across sessions
72+ const userDetails = getUserDetails ( env , this . props . user . id )
73+ return await userDetails . getActiveAccountId ( )
74+ } catch ( e ) {
75+ this . server . recordError ( e )
76+ return null
77+ }
78+ }
79+
80+ async setActiveAccountId ( accountId : string ) {
81+ try {
82+ const userDetails = getUserDetails ( env , this . props . user . id )
83+ await userDetails . setActiveAccountId ( accountId )
84+ } catch ( e ) {
85+ this . server . recordError ( e )
86+ }
87+ }
6788}
6889
6990const RadarScopes = {
7091 ...RequiredScopes ,
71- // TODO 'radar:read': 'Grants access to read Cloudflare Radar data.',
72- // TODO 'url_scanner:write': 'Grants write level access to URL Scanner', // Remove URL_SCANNER_API_TOKEN env var
92+ 'account:read' : 'See your account info such as account details, analytics, and memberships.' ,
93+ 'radar:read' : 'Grants access to read Cloudflare Radar data.' ,
94+ 'url_scanner:write' : 'Grants write level access to URL Scanner' ,
7395} as const
7496
7597export default {
0 commit comments