11import OAuthProvider from '@cloudflare/workers-oauth-provider'
22import { McpAgent } from 'agents/mcp'
33
4+ import { createApiHandler } from '@repo/mcp-common/src/api-handler'
45import {
56 createAuthHandlers ,
67 handleTokenExchangeCallback ,
78} from '@repo/mcp-common/src/cloudflare-oauth-handler'
9+ import { handleDevMode } from '@repo/mcp-common/src/dev-mode'
810import { getUserDetails , UserDetails } from '@repo/mcp-common/src/durable-objects/user_details'
911import { getEnv } from '@repo/mcp-common/src/env'
1012import { RequiredScopes } from '@repo/mcp-common/src/scopes'
@@ -14,7 +16,7 @@ import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
1416import { MetricsTracker } from '../../../packages/mcp-observability/src'
1517import { registerIntegrationsTools } from './tools/integrations'
1618
17- import type { AccountSchema , UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
19+ import type { AuthProps } from '@repo/mcp-common/src/cloudflare-oauth-handler'
1820import type { Env } from './context'
1921
2022export { UserDetails }
@@ -26,13 +28,11 @@ const metrics = new MetricsTracker(env.MCP_METRICS, {
2628 version : env . MCP_SERVER_VERSION ,
2729} )
2830
29- export type Props = {
30- accessToken : string
31- user : UserSchema [ 'result' ]
32- accounts : AccountSchema [ 'result' ]
33- }
31+ // Context from the auth process, encrypted & stored in the auth token
32+ // and provided to the DurableMCP as this.props
33+ type Props = AuthProps
3434
35- export type State = { activeAccountId : string | null }
35+ type State = { activeAccountId : string | null }
3636export class CASBMCP extends McpAgent < Env , State , Props > {
3737 _server : CloudflareMCPServer | undefined
3838 set server ( server : CloudflareMCPServer ) {
@@ -92,17 +92,29 @@ const CloudflareOneCasbScopes = {
9292 'teams:read' : 'See Cloudflare One Resources' ,
9393} as const
9494
95- export default new OAuthProvider ( {
96- apiRoute : '/sse' ,
97- // @ts -ignore
98- apiHandler : CASBMCP . mount ( '/sse' ) ,
99- // @ts -ignore
100- defaultHandler : createAuthHandlers ( { scopes : CloudflareOneCasbScopes , metrics } ) ,
101- authorizeEndpoint : '/oauth/authorize' ,
102- tokenEndpoint : '/token' ,
103- tokenExchangeCallback : ( options ) =>
104- handleTokenExchangeCallback ( options , env . CLOUDFLARE_CLIENT_ID , env . CLOUDFLARE_CLIENT_SECRET ) ,
105- // Cloudflare access token TTL
106- accessTokenTTL : 3600 ,
107- clientRegistrationEndpoint : '/register' ,
108- } )
95+ export default {
96+ fetch : async ( req : Request , env : Env , ctx : ExecutionContext ) => {
97+ if ( env . ENVIRONMENT === 'development' && env . DEV_DISABLE_OAUTH === 'true' ) {
98+ return await handleDevMode ( CASBMCP , req , env , ctx )
99+ }
100+
101+ return new OAuthProvider ( {
102+ apiRoute : [ '/mcp' , '/sse' ] ,
103+ // @ts -ignore
104+ apiHandler : createApiHandler ( CASBMCP ) ,
105+ // @ts -ignore
106+ defaultHandler : createAuthHandlers ( { scopes : CloudflareOneCasbScopes , metrics } ) ,
107+ authorizeEndpoint : '/oauth/authorize' ,
108+ tokenEndpoint : '/token' ,
109+ tokenExchangeCallback : ( options ) =>
110+ handleTokenExchangeCallback (
111+ options ,
112+ env . CLOUDFLARE_CLIENT_ID ,
113+ env . CLOUDFLARE_CLIENT_SECRET
114+ ) ,
115+ // Cloudflare access token TTL
116+ accessTokenTTL : 3600 ,
117+ clientRegistrationEndpoint : '/register' ,
118+ } ) . fetch ( req , env , ctx )
119+ } ,
120+ }
0 commit comments