Skip to content

Commit 0fc9bd0

Browse files
authored
Merge pull request #687 from smithery-ai/patch-1
Fix `/.well-known/oauth-authorization-server` dropping path
2 parents cb4743d + 1ff08e4 commit 0fc9bd0

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/client/auth.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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;

src/client/auth.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff 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, {

0 commit comments

Comments
 (0)