|
1 | 1 | import OAuthProvider from '@cloudflare/workers-oauth-provider' |
2 | 2 | import { McpAgent } from 'agents/mcp' |
3 | 3 |
|
| 4 | +import { createApiHandler } from '@repo/mcp-common/src/api-handler' |
4 | 5 | import { |
5 | 6 | createAuthHandlers, |
6 | 7 | handleTokenExchangeCallback, |
7 | 8 | } from '@repo/mcp-common/src/cloudflare-oauth-handler' |
| 9 | +import { handleDevMode } from '@repo/mcp-common/src/dev-mode' |
8 | 10 | import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details' |
9 | 11 | import { getEnv } from '@repo/mcp-common/src/env' |
10 | 12 | import { RequiredScopes } from '@repo/mcp-common/src/scopes' |
@@ -99,18 +101,28 @@ const AuditlogScopes = { |
99 | 101 | 'auditlogs:read': 'See your resource configuration changes.', |
100 | 102 | } as const |
101 | 103 |
|
102 | | -// Export the OAuth handler as the default |
103 | | -export default new OAuthProvider({ |
104 | | - apiRoute: '/sse', |
105 | | - // @ts-ignore |
106 | | - apiHandler: AuditlogMCP.mount('/sse'), |
107 | | - // @ts-ignore |
108 | | - defaultHandler: createAuthHandlers({ scopes: AuditlogScopes, metrics }), |
109 | | - authorizeEndpoint: '/oauth/authorize', |
110 | | - tokenEndpoint: '/token', |
111 | | - tokenExchangeCallback: (options) => |
112 | | - handleTokenExchangeCallback(options, env.CLOUDFLARE_CLIENT_ID, env.CLOUDFLARE_CLIENT_SECRET), |
113 | | - // Cloudflare access token TTL |
114 | | - accessTokenTTL: 3600, |
115 | | - clientRegistrationEndpoint: '/register', |
116 | | -}) |
| 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(AuditlogMCP, req, env, ctx) |
| 108 | + } |
| 109 | + |
| 110 | + return new OAuthProvider({ |
| 111 | + apiRoute: ['/mcp', '/sse'], |
| 112 | + apiHandler: createApiHandler(AuditlogMCP), |
| 113 | + // @ts-ignore |
| 114 | + defaultHandler: createAuthHandlers({ scopes: AuditlogScopes, metrics }), |
| 115 | + authorizeEndpoint: '/oauth/authorize', |
| 116 | + tokenEndpoint: '/token', |
| 117 | + tokenExchangeCallback: (options) => |
| 118 | + handleTokenExchangeCallback( |
| 119 | + options, |
| 120 | + env.CLOUDFLARE_CLIENT_ID, |
| 121 | + env.CLOUDFLARE_CLIENT_SECRET |
| 122 | + ), |
| 123 | + // Cloudflare access token TTL |
| 124 | + accessTokenTTL: 3600, |
| 125 | + clientRegistrationEndpoint: '/register', |
| 126 | + }).fetch(req, env, ctx) |
| 127 | + }, |
| 128 | +} |
0 commit comments