11import OAuthProvider from '@cloudflare/workers-oauth-provider'
2+ import { McpAgent } from 'agents/mcp'
23import { env } from 'cloudflare:workers'
34
45import {
@@ -16,7 +17,7 @@ export { ContainerManager, ContainerMcpAgent }
1617export type Env = {
1718 CONTAINER_MCP_AGENT : DurableObjectNamespace < ContainerMcpAgent >
1819 CONTAINER_MANAGER : DurableObjectNamespace < ContainerManager >
19- ENVIRONMENT : 'dev' | 'prod'
20+ ENVIRONMENT : 'dev' | 'prod' | 'test'
2021 CLOUDFLARE_CLIENT_ID : string
2122 CLOUDFLARE_CLIENT_SECRET : string
2223}
@@ -38,17 +39,36 @@ const ContainerScopes = {
3839 offline_access : 'Grants refresh tokens for long-lived access.' ,
3940} as const
4041
41- export default new OAuthProvider ( {
42- apiRoute : '/sse' ,
43- // @ts -ignore
44- apiHandler : ContainerMcpAgent . mount ( '/sse' , { binding : 'CONTAINER_MCP_AGENT' } ) ,
45- // @ts -ignore
46- defaultHandler : createAuthHandlers ( { scopes : ContainerScopes } ) ,
47- authorizeEndpoint : '/oauth/authorize' ,
48- tokenEndpoint : '/token' ,
49- tokenExchangeCallback : ( options ) =>
50- handleTokenExchangeCallback ( options , env . CLOUDFLARE_CLIENT_ID , env . CLOUDFLARE_CLIENT_SECRET ) ,
51- // Cloudflare access token TTL
52- accessTokenTTL : 3600 ,
53- clientRegistrationEndpoint : '/register' ,
54- } )
42+ export default {
43+ fetch : ( req : Request , env : Env , ctx : ExecutionContext ) => {
44+ if ( env . ENVIRONMENT === 'test' ) {
45+ ctx . props = { }
46+ return ContainerMcpAgent . mount ( '/sse' , { binding : 'CONTAINER_MCP_AGENT' } ) . fetch (
47+ req ,
48+ env as Record < string , DurableObjectNamespace < McpAgent > | any > ,
49+ ctx
50+ )
51+ }
52+
53+ return new OAuthProvider ( {
54+ apiRoute : '/sse' ,
55+ // @ts -ignore
56+ apiHandler : ContainerMcpAgent . mount ( '/sse' , { binding : 'CONTAINER_MCP_AGENT' } ) ,
57+ // @ts -ignore
58+ defaultHandler : createAuthHandlers ( { scopes : ContainerScopes } ) ,
59+ authorizeEndpoint : '/oauth/authorize' ,
60+ tokenEndpoint : '/token' ,
61+ tokenExchangeCallback : ( options ) =>
62+ handleTokenExchangeCallback (
63+ options ,
64+ env . CLOUDFLARE_CLIENT_ID ,
65+ env . CLOUDFLARE_CLIENT_SECRET
66+ ) ,
67+ // Cloudflare access token TTL
68+ accessTokenTTL : 3600 ,
69+ clientRegistrationEndpoint : '/register' ,
70+ } ) . fetch ( req , env , ctx )
71+ } ,
72+ } /*
73+
74+ */
0 commit comments