Skip to content

Commit f61e097

Browse files
committed
Add dev handler, and new createApiHandler for dealing with SSE + Streamable HTTP
1 parent 713459a commit f61e097

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

apps/auditlogs/src/context.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ export interface Env {
1212
MCP_OBJECT: DurableObjectNamespace<AuditlogMCP>
1313
USER_DETAILS: DurableObjectNamespace<UserDetails>
1414
MCP_METRICS: AnalyticsEngineDataset
15+
DEV_DISABLE_OAUTH: string
16+
DEV_CLOUDFLARE_API_TOKEN: string
17+
DEV_CLOUDFLARE_EMAIL: string
1518
}

apps/auditlogs/src/index.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import OAuthProvider from '@cloudflare/workers-oauth-provider'
22
import { McpAgent } from 'agents/mcp'
33

4+
import { createApiHandler } from '@repo/mcp-common/src/api-handler'
45
import {
56
createAuthHandlers,
67
handleTokenExchangeCallback,
78
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
9+
import { handleDevMode } from '@repo/mcp-common/src/dev-mode'
810
import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details'
911
import { getEnv } from '@repo/mcp-common/src/env'
1012
import { RequiredScopes } from '@repo/mcp-common/src/scopes'
@@ -99,18 +101,28 @@ const AuditlogScopes = {
99101
'auditlogs:read': 'See your resource configuration changes.',
100102
} as const
101103

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

Comments
 (0)