Skip to content

Fix connection issue for servers with auth #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 21, 2025
Merged
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
1 change: 0 additions & 1 deletion client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ const App = () => {
const onOAuthConnect = useCallback(
(serverUrl: string) => {
setSseUrl(serverUrl);
setTransportType("sse");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is transport type no longer set here?

setIsAuthDebuggerVisible(false);
void connectMcpServer();
},
Expand Down
18 changes: 12 additions & 6 deletions client/src/lib/hooks/useConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,16 @@ export function useConnection({
}
};

const is401Error = (error: unknown): boolean => {
return (
(error instanceof SseError && error.code === 401) ||
(error instanceof Error && error.message.includes("401")) ||
(error instanceof Error && error.message.includes("Unauthorized"))
);
};

const handleAuthError = async (error: unknown) => {
if (error instanceof SseError && error.code === 401) {
// Create a new auth provider with the current server URL
if (is401Error(error)) {
const serverAuthProvider = new InspectorOAuthClientProvider(sseUrl);

const result = await auth(serverAuthProvider, { serverUrl: sseUrl });
Expand Down Expand Up @@ -330,7 +337,6 @@ export function useConnection({
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/sse`);
mcpProxyServerUrl.searchParams.append("url", sseUrl);
transportOptions = {
authProvider: serverAuthProvider,
eventSourceInit: {
fetch: (
url: string | URL | globalThis.Request,
Expand All @@ -347,7 +353,6 @@ export function useConnection({
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/mcp`);
mcpProxyServerUrl.searchParams.append("url", sseUrl);
transportOptions = {
authProvider: serverAuthProvider,
eventSourceInit: {
fetch: (
url: string | URL | globalThis.Request,
Expand Down Expand Up @@ -425,13 +430,14 @@ export function useConnection({
`Failed to connect to MCP Server via the MCP Inspector Proxy: ${mcpProxyServerUrl}:`,
error,
);

const shouldRetry = await handleAuthError(error);
if (shouldRetry) {
return connect(undefined, retryCount + 1);
}

if (error instanceof SseError && error.code === 401) {
if (is401Error(error)) {
// Don't set error state if we're about to redirect for auth

return;
}
throw error;
Expand Down
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion server/src/mcpProxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import { isJSONRPCRequest } from "@modelcontextprotocol/sdk/types.js";

function onClientError(error: Error) {
console.error("Error from inspector client:", error);
Expand All @@ -19,7 +20,21 @@ export default function mcpProxy({
let transportToServerClosed = false;

transportToClient.onmessage = (message) => {
transportToServer.send(message).catch(onServerError);
transportToServer.send(message).catch((error) => {
// Send error response back to client if it was a request (has id) and connection is still open
if (isJSONRPCRequest(message) && !transportToClientClosed) {
const errorResponse = {
jsonrpc: "2.0" as const,
id: message.id,
error: {
code: -32001,
message: error.message,
data: error,
},
};
transportToClient.send(errorResponse).catch(onClientError);
}
});
};

transportToServer.onmessage = (message) => {
Expand Down