Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion client/src/lib/hooks/__tests__/useConnection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
ElicitResult,
ElicitRequest,
} from "@modelcontextprotocol/sdk/types.js";
import { auth } from "@modelcontextprotocol/sdk/client/auth.js";
import {
auth,
discoverOAuthProtectedResourceMetadata,
} from "@modelcontextprotocol/sdk/client/auth.js";
import { discoverScopes } from "../../auth";
import { CustomHeaders } from "../../types/customHeaders";

Expand Down Expand Up @@ -129,6 +132,9 @@ jest.mock("@modelcontextprotocol/sdk/client/auth.js", () => {
return {
UnauthorizedError,
auth: jest.fn().mockResolvedValue("AUTHORIZED"),
discoverOAuthProtectedResourceMetadata: jest
.fn()
.mockResolvedValue(undefined),
};
});

Expand All @@ -154,6 +160,10 @@ jest.mock("../../auth", () => ({
}));

const mockAuth = auth as jest.MockedFunction<typeof auth>;
const mockDiscoverOAuthProtectedResourceMetadata =
discoverOAuthProtectedResourceMetadata as jest.MockedFunction<
typeof discoverOAuthProtectedResourceMetadata
>;
const mockDiscoverScopes = discoverScopes as jest.MockedFunction<
typeof discoverScopes
>;
Expand Down Expand Up @@ -1603,6 +1613,44 @@ describe("useConnection", () => {
expect.not.objectContaining({ fetchFn: expect.anything() }),
);
});

it("preserves path-based MCP endpoints when discovering protected resource metadata", async () => {
const previewUrl =
"https://mcp.stg.services.atlassian.com/v1/mcp/preview";
const resourceMetadata = {
resource: previewUrl,
authorization_servers: ["https://auth.stg.atlassian.com/example"],
bearer_methods_supported: ["header"],
scopes_supported: ["search:rovo:agent-interface"],
};

mockDiscoverOAuthProtectedResourceMetadata.mockResolvedValueOnce(
resourceMetadata,
);
mockDiscoverScopes.mockResolvedValue("search:rovo:agent-interface");
setup401Error();

await attemptConnection({
...defaultProps,
sseUrl: previewUrl,
});

expect(
mockDiscoverOAuthProtectedResourceMetadata.mock.calls[0][0].href,
).toBe(previewUrl);
expect(mockDiscoverScopes).toHaveBeenCalledWith(
previewUrl,
resourceMetadata,
expect.any(Function),
);
expect(mockAuth).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
serverUrl: previewUrl,
scope: "search:rovo:agent-interface",
}),
);
});
});

describe("Inspector proxy McpError auth recovery", () => {
Expand Down
2 changes: 1 addition & 1 deletion client/src/lib/hooks/useConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export function useConnection({
let resourceMetadata;
try {
resourceMetadata = await discoverOAuthProtectedResourceMetadata(
new URL("/", sseUrl),
new URL(sseUrl),
{},
fetchFn,
);
Expand Down