@@ -5,18 +5,20 @@ import {
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'
8+ import { handleDevMode } from '@repo/mcp-common/src/dev-mode'
9+ import { getUserDetails , UserDetails } from '@repo/mcp-common/src/durable-objects/user_details.do'
910import { getEnv } from '@repo/mcp-common/src/env'
11+ import { RequiredScopes } from '@repo/mcp-common/src/scopes'
1012import { initSentryWithUser } from '@repo/mcp-common/src/sentry'
1113import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
12- import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
13- import { registerZoneTools } from '@repo/mcp-common/src/tools/zone'
14+ import { registerAccountTools } from '@repo/mcp-common/src/tools/account.tools '
15+ import { registerZoneTools } from '@repo/mcp-common/src/tools/zone.tools '
1416import { MetricsTracker } from '@repo/mcp-observability'
1517
16- import { registerGraphQLTools } from './tools/graphql'
18+ import { registerGraphQLTools } from './tools/graphql.tools '
1719
18- import type { AccountSchema , UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
19- import type { Env } from './context'
20+ import type { AuthProps } from '@repo/mcp-common/src/cloudflare-oauth-handler'
21+ import type { Env } from './graphql. context'
2022
2123export { UserDetails }
2224
@@ -29,13 +31,8 @@ const metrics = new MetricsTracker(env.MCP_METRICS, {
2931
3032// Context from the auth process, encrypted & stored in the auth token
3133// and provided to the DurableMCP as this.props
32- export type Props = {
33- accessToken : string
34- user : UserSchema [ 'result' ]
35- accounts : AccountSchema [ 'result' ]
36- }
37-
38- export type State = { activeAccountId : string | null }
34+ type Props = AuthProps
35+ type State = { activeAccountId : string | null }
3936
4037export class GraphQLMCP extends McpAgent < Env , State , Props > {
4138 _server : CloudflareMCPServer | undefined
@@ -51,10 +48,6 @@ export class GraphQLMCP extends McpAgent<Env, State, Props> {
5148 return this . _server
5249 }
5350
54- initialState : State = {
55- activeAccountId : null ,
56- }
57-
5851 constructor ( ctx : DurableObjectState , env : Env ) {
5952 super ( ctx , env )
6053 }
@@ -103,23 +96,35 @@ export class GraphQLMCP extends McpAgent<Env, State, Props> {
10396}
10497
10598const GraphQLScopes = {
99+ ...RequiredScopes ,
106100 'account:read' : 'See your account info such as account details, analytics, and memberships.' ,
107- 'user:read' : 'See your user info such as name, email address, and account memberships.' ,
108101 'zone:read' : 'See zone data such as settings, analytics, and DNS records.' ,
109- offline_access : 'Grants refresh tokens for long-lived access.' ,
110102} as const
111103
112- export default new OAuthProvider ( {
113- apiRoute : '/sse' ,
114- // @ts -ignore
115- apiHandler : GraphQLMCP . mount ( '/sse' ) ,
116- // @ts -ignore
117- defaultHandler : createAuthHandlers ( { scopes : GraphQLScopes , metrics } ) ,
118- authorizeEndpoint : '/oauth/authorize' ,
119- tokenEndpoint : '/token' ,
120- tokenExchangeCallback : ( options ) =>
121- handleTokenExchangeCallback ( options , env . CLOUDFLARE_CLIENT_ID , env . CLOUDFLARE_CLIENT_SECRET ) ,
122- // Cloudflare access token TTL
123- accessTokenTTL : 3600 ,
124- clientRegistrationEndpoint : '/register' ,
125- } )
104+ export default {
105+ fetch : async ( req : Request , env : Env , ctx : ExecutionContext ) => {
106+ if ( env . ENVIRONMENT === 'development' && env . DEV_DISABLE_OAUTH === 'true' ) {
107+ return await handleDevMode ( GraphQLMCP , req , env , ctx )
108+ }
109+
110+ return new OAuthProvider ( {
111+ apiHandlers : {
112+ '/mcp' : GraphQLMCP . serve ( '/mcp' ) ,
113+ '/sse' : GraphQLMCP . serveSSE ( '/sse' ) ,
114+ } ,
115+ // @ts -ignore
116+ defaultHandler : createAuthHandlers ( { scopes : GraphQLScopes , metrics } ) ,
117+ authorizeEndpoint : '/oauth/authorize' ,
118+ tokenEndpoint : '/token' ,
119+ tokenExchangeCallback : ( options ) =>
120+ handleTokenExchangeCallback (
121+ options ,
122+ env . CLOUDFLARE_CLIENT_ID ,
123+ env . CLOUDFLARE_CLIENT_SECRET
124+ ) ,
125+ // Cloudflare access token TTL
126+ accessTokenTTL : 3600 ,
127+ clientRegistrationEndpoint : '/register' ,
128+ } ) . fetch ( req , env , ctx )
129+ }
130+ }
0 commit comments