@@ -23,6 +23,7 @@ const mcpTest = testEffect(
2323interface OAuthMcpOptions {
2424 capabilities ?: "tools" | "resources"
2525 oauthMetadata ?: boolean
26+ metadataFailure ?: "oauth" | "openid"
2627 lazyDiscovery ?: boolean
2728 protectedCalls ?: boolean
2829 backgroundChallenge ?: boolean
@@ -42,6 +43,8 @@ function serveOAuthMcp(options: OAuthMcpOptions = {}) {
4243 } )
4344 let listToolsCalls = 0
4445 let protectedCalls = 0
46+ let authorizeCalls = 0
47+ const metadataRequests : string [ ] = [ ]
4548 let releaseGet : ( ) => void = ( ) => { }
4649 const getGate = new Promise < void > ( ( resolve ) => {
4750 releaseGet = resolve
@@ -68,6 +71,14 @@ function serveOAuthMcp(options: OAuthMcpOptions = {}) {
6871 const origin = url . origin
6972 const mcpUrl = `${ origin } /mcp`
7073
74+ if ( url . pathname === "/authorize" ) authorizeCalls ++
75+ if ( url . pathname . startsWith ( "/.well-known/" ) ) metadataRequests . push ( url . pathname )
76+ if (
77+ ( options . metadataFailure === "oauth" && url . pathname === "/.well-known/oauth-authorization-server" ) ||
78+ ( options . metadataFailure === "openid" && url . pathname === "/.well-known/openid-configuration" )
79+ ) {
80+ return new Response ( "Unavailable" , { status : 503 } )
81+ }
7182 if ( options . oauthMetadata === false && url . pathname . startsWith ( "/.well-known/" ) ) {
7283 return new Response ( "Not found" , { status : 404 } )
7384 }
@@ -181,6 +192,8 @@ function serveOAuthMcp(options: OAuthMcpOptions = {}) {
181192 } ,
182193 listToolsCalls : ( ) => listToolsCalls ,
183194 protectedCalls : ( ) => protectedCalls ,
195+ authorizeCalls : ( ) => authorizeCalls ,
196+ metadataRequests : ( ) => metadataRequests ,
184197 releaseGet : ( ) => releaseGet ( ) ,
185198 close : async ( ) => {
186199 await http . stop ( true )
@@ -344,6 +357,29 @@ mcpTest.instance("authenticate() connects a public server without OAuth metadata
344357 } ) ,
345358)
346359
360+ for ( const metadataFailure of [ "oauth" , "openid" ] as const ) {
361+ mcpTest . instance ( `authenticate() connects without OAuth when ${ metadataFailure } discovery returns 503` , ( ) =>
362+ Effect . gen ( function * ( ) {
363+ yield * stopOAuthCallback
364+ const server = yield * serveOAuthMcp ( { oauthMetadata : false , metadataFailure, lazyDiscovery : true } )
365+ server . allowAnonymous ( )
366+ const mcp = yield * MCP . Service
367+ const name = `test-oauth-discovery-${ metadataFailure } -503`
368+ const added = yield * mcp . add ( name , {
369+ ...remote ( server . url ) ,
370+ ...( metadataFailure === "openid" ? { headers : { Authorization : "Bearer static-test-token" } } : { } ) ,
371+ } )
372+ expect ( ( added . status as Record < string , { status : string } > ) [ name ] ?. status ) . toBe ( "connected" )
373+ expect ( ( yield * mcp . authenticate ( name ) ) . status ) . toBe ( "connected" )
374+ expect ( ( yield * mcp . status ( ) ) [ name ] ?. status ) . toBe ( "connected" )
375+ expect ( server . metadataRequests ( ) ) . toContain (
376+ metadataFailure === "oauth" ? "/.well-known/oauth-authorization-server" : "/.well-known/openid-configuration" ,
377+ )
378+ expect ( server . authorizeCalls ( ) ) . toBe ( 0 )
379+ } ) ,
380+ )
381+ }
382+
347383mcpTest . instance ( "authenticate() connects a public resource-only server without listing tools" , ( ) =>
348384 Effect . gen ( function * ( ) {
349385 yield * stopOAuthCallback
0 commit comments