@@ -3,6 +3,7 @@ import { McpAgent } from 'agents/mcp'
33
44import {
55 createAuthHandlers ,
6+ getUserAndAccounts ,
67 handleTokenExchangeCallback ,
78} from '@repo/mcp-common/src/cloudflare-oauth-handler'
89import { getEnv } from '@repo/mcp-common/src/env'
@@ -103,17 +104,42 @@ const BindingsScopes = {
103104 'd1:write' : 'Create, read, and write to D1 databases' ,
104105} as const
105106
106- // Export the OAuth handler as the default
107- export default new OAuthProvider ( {
108- apiRoute : '/sse' ,
109- apiHandler : WorkersBindingsMCP . mount ( '/sse' ) ,
110- // @ts -ignore
111- defaultHandler : createAuthHandlers ( { scopes : BindingsScopes , metrics } ) ,
112- authorizeEndpoint : '/oauth/authorize' ,
113- tokenEndpoint : '/token' ,
114- tokenExchangeCallback : ( options ) =>
115- handleTokenExchangeCallback ( options , env . CLOUDFLARE_CLIENT_ID , env . CLOUDFLARE_CLIENT_SECRET ) ,
116- // Cloudflare access token TTL
117- accessTokenTTL : 3600 ,
118- clientRegistrationEndpoint : '/register' ,
119- } )
107+ // TODO: Move this in to wci-common
108+ async function handleDevMode ( req : Request , env : Env , ctx : ExecutionContext ) {
109+ const { user, accounts } = await getUserAndAccounts ( env . DEV_CLOUDFLARE_API_TOKEN , {
110+ 'X-Auth-Email' : env . DEV_CLOUDFLARE_EMAIL ,
111+ 'X-Auth-Key' : env . DEV_CLOUDFLARE_API_TOKEN ,
112+ } )
113+ ctx . props = {
114+ accessToken : env . DEV_CLOUDFLARE_API_TOKEN ,
115+ user,
116+ accounts,
117+ } as Props
118+ return WorkersBindingsMCP . mount ( '/sse' ) . fetch ( req , env , ctx )
119+ }
120+
121+ export default {
122+ fetch : async ( req : Request , env : Env , ctx : ExecutionContext ) => {
123+ if ( env . ENVIRONMENT === 'development' && env . DEV_DISABLE_OAUTH === 'true' ) {
124+ return await handleDevMode ( req , env , ctx )
125+ }
126+
127+ return new OAuthProvider ( {
128+ apiRoute : '/sse' ,
129+ apiHandler : WorkersBindingsMCP . mount ( '/sse' ) ,
130+ // @ts -ignore
131+ defaultHandler : createAuthHandlers ( { scopes : BindingsScopes , metrics } ) ,
132+ authorizeEndpoint : '/oauth/authorize' ,
133+ tokenEndpoint : '/token' ,
134+ tokenExchangeCallback : ( options ) =>
135+ handleTokenExchangeCallback (
136+ options ,
137+ env . CLOUDFLARE_CLIENT_ID ,
138+ env . CLOUDFLARE_CLIENT_SECRET
139+ ) ,
140+ // Cloudflare access token TTL
141+ accessTokenTTL : 3600 ,
142+ clientRegistrationEndpoint : '/register' ,
143+ } ) . fetch ( req , env , ctx )
144+ } ,
145+ }
0 commit comments