File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -207,6 +207,24 @@ describe("OAuth Authorization", () => {
207207 } ) ;
208208 } ) ;
209209
210+ it ( "returns metadata when discovery succeeds with path" , async ( ) => {
211+ mockFetch . mockResolvedValueOnce ( {
212+ ok : true ,
213+ status : 200 ,
214+ json : async ( ) => validMetadata ,
215+ } ) ;
216+
217+ const metadata = await discoverOAuthMetadata ( "https://auth.example.com/path/name" ) ;
218+ expect ( metadata ) . toEqual ( validMetadata ) ;
219+ const calls = mockFetch . mock . calls ;
220+ expect ( calls . length ) . toBe ( 1 ) ;
221+ const [ url , options ] = calls [ 0 ] ;
222+ expect ( url . toString ( ) ) . toBe ( "https://auth.example.com/.well-known/oauth-authorization-server/path/name" ) ;
223+ expect ( options . headers ) . toEqual ( {
224+ "MCP-Protocol-Version" : LATEST_PROTOCOL_VERSION
225+ } ) ;
226+ } ) ;
227+
210228 it ( "returns metadata when first fetch fails but second without MCP header succeeds" , async ( ) => {
211229 // Set up a counter to control behavior
212230 let callCount = 0 ;
Original file line number Diff line number Diff line change @@ -297,7 +297,15 @@ export async function discoverOAuthMetadata(
297297 authorizationServerUrl : string | URL ,
298298 opts ?: { protocolVersion ?: string } ,
299299) : Promise < OAuthMetadata | undefined > {
300- const url = new URL ( "/.well-known/oauth-authorization-server" , authorizationServerUrl ) ;
300+ const issuer = new URL ( authorizationServerUrl ) ;
301+
302+ let wellKnownPath = `/.well-known/oauth-authorization-server${ issuer . pathname } ` ;
303+ if ( issuer . pathname . endsWith ( '/' ) ) {
304+ // Strip trailing slash from pathname
305+ wellKnownPath = wellKnownPath . slice ( 0 , - 1 ) ;
306+ }
307+ const url = new URL ( wellKnownPath , issuer ) ;
308+
301309 let response : Response ;
302310 try {
303311 response = await fetch ( url , {
You can’t perform that action at this time.
0 commit comments