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 { registerLogsTools } from './tools/logs'
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
2022const env = getEnv < Env > ( )
@@ -28,14 +30,8 @@ const metrics = new MetricsTracker(env.MCP_METRICS, {
2830
2931// Context from the auth process, encrypted & stored in the auth token
3032// and provided to the DurableMCP as this.props
31- export type Props = {
32- accessToken : string
33- apiToken : string
34- user : UserSchema [ 'result' ]
35- accounts : AccountSchema [ 'result' ]
36- }
37-
38- export type State = { activeAccountId : string | null }
33+ type Props = AuthProps
34+ type State = { activeAccountId : string | null }
3935
4036export class LogsMCP extends McpAgent < Env , State , Props > {
4137 _server : CloudflareMCPServer | undefined
@@ -67,7 +63,7 @@ export class LogsMCP extends McpAgent<Env, State, Props> {
6763 registerAccountTools ( this )
6864
6965 // Register Cloudflare Log Push tools
70- registerLogsTools ( this , env . CLOUDFLARE_ACCESS_TOKEN )
66+ registerLogsTools ( this )
7167 }
7268
7369 async getActiveAccountId ( ) {
@@ -99,16 +95,28 @@ const LogPushScopes = {
9995 'Grants read and write access to Logpull and Logpush, and read access to Instant Logs. Note that all Logpush API operations require Logs: Write permission because Logpush jobs contain sensitive information.' ,
10096} as const
10197
102- export default new OAuthProvider ( {
103- apiRoute : '/sse' ,
104- apiHandler : LogsMCP . mount ( '/sse' ) ,
105- // @ts -ignore
106- defaultHandler : createAuthHandlers ( { scopes : LogPushScopes , metrics } ) ,
107- authorizeEndpoint : '/oauth/authorize' ,
108- tokenEndpoint : '/token' ,
109- tokenExchangeCallback : ( options ) =>
110- handleTokenExchangeCallback ( options , env . CLOUDFLARE_CLIENT_ID , env . CLOUDFLARE_CLIENT_SECRET ) ,
111- // Cloudflare access token TTL
112- accessTokenTTL : 3600 ,
113- clientRegistrationEndpoint : '/register' ,
114- } )
98+ export default {
99+ fetch : async ( req : Request , env : Env , ctx : ExecutionContext ) => {
100+ if ( env . ENVIRONMENT === 'development' && env . DEV_DISABLE_OAUTH === 'true' ) {
101+ return await handleDevMode ( LogsMCP , req , env , ctx )
102+ }
103+
104+ return new OAuthProvider ( {
105+ apiRoute : [ '/mcp' , '/sse' ] ,
106+ apiHandler : createApiHandler ( LogsMCP ) ,
107+ // @ts -ignore
108+ defaultHandler : createAuthHandlers ( { scopes : LogPushScopes , metrics } ) ,
109+ authorizeEndpoint : '/oauth/authorize' ,
110+ tokenEndpoint : '/token' ,
111+ tokenExchangeCallback : ( options ) =>
112+ handleTokenExchangeCallback (
113+ options ,
114+ env . CLOUDFLARE_CLIENT_ID ,
115+ env . CLOUDFLARE_CLIENT_SECRET
116+ ) ,
117+ // Cloudflare access token TTL
118+ accessTokenTTL : 3600 ,
119+ clientRegistrationEndpoint : '/register' ,
120+ } ) . fetch ( req , env , ctx )
121+ } ,
122+ }
0 commit comments