Skip to content

Commit 13eb077

Browse files
committed
fix auth conect
1 parent c6fad3f commit 13eb077

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

client/src/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ const App = () => {
234234
const onOAuthConnect = useCallback(
235235
(serverUrl: string) => {
236236
setSseUrl(serverUrl);
237-
setTransportType("sse");
238237
setIsAuthDebuggerVisible(false);
239238
void connectMcpServer();
240239
},

client/src/lib/hooks/useConnection.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,12 @@ export function useConnection({
250250
};
251251

252252
const handleAuthError = async (error: unknown) => {
253-
if (error instanceof SseError && error.code === 401) {
254-
// Create a new auth provider with the current server URL
253+
const is401Error =
254+
(error instanceof SseError && error.code === 401) ||
255+
(error instanceof Error && error.message.includes('401')) ||
256+
(error instanceof Error && error.message.includes('Unauthorized'));
257+
258+
if (is401Error) {
255259
const serverAuthProvider = new InspectorOAuthClientProvider(sseUrl);
256260

257261
const result = await auth(serverAuthProvider, { serverUrl: sseUrl });
@@ -330,7 +334,6 @@ export function useConnection({
330334
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/sse`);
331335
mcpProxyServerUrl.searchParams.append("url", sseUrl);
332336
transportOptions = {
333-
authProvider: serverAuthProvider,
334337
eventSourceInit: {
335338
fetch: (
336339
url: string | URL | globalThis.Request,
@@ -347,7 +350,6 @@ export function useConnection({
347350
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/mcp`);
348351
mcpProxyServerUrl.searchParams.append("url", sseUrl);
349352
transportOptions = {
350-
authProvider: serverAuthProvider,
351353
eventSourceInit: {
352354
fetch: (
353355
url: string | URL | globalThis.Request,
@@ -375,9 +377,9 @@ export function useConnection({
375377
const clientTransport =
376378
transportType === "streamable-http"
377379
? new StreamableHTTPClientTransport(mcpProxyServerUrl as URL, {
378-
sessionId: undefined,
379-
...transportOptions,
380-
})
380+
sessionId: undefined,
381+
...transportOptions,
382+
})
381383
: new SSEClientTransport(mcpProxyServerUrl as URL, transportOptions);
382384

383385
if (onNotification) {
@@ -425,12 +427,19 @@ export function useConnection({
425427
`Failed to connect to MCP Server via the MCP Inspector Proxy: ${mcpProxyServerUrl}:`,
426428
error,
427429
);
428-
const shouldRetry = await handleAuthError(error);
429-
if (shouldRetry) {
430-
return connect(undefined, retryCount + 1);
431-
}
432430

433-
if (error instanceof SseError && error.code === 401) {
431+
// Check for auth-related errors
432+
const is401Error =
433+
(error instanceof SseError && error.code === 401) ||
434+
(error instanceof Error && error.message.includes('401')) ||
435+
(error instanceof Error && error.message.includes('Unauthorized'));
436+
437+
if (is401Error) {
438+
console.log("Detected 401 error, attempting OAuth flow");
439+
const shouldRetry = await handleAuthError(error);
440+
if (shouldRetry) {
441+
return connect(undefined, retryCount + 1);
442+
}
434443
// Don't set error state if we're about to redirect for auth
435444
return;
436445
}

package-lock.json

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/src/mcpProxy.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
2+
import { isJSONRPCRequest } from "@modelcontextprotocol/sdk/types.js";
23

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

2122
transportToClient.onmessage = (message) => {
22-
transportToServer.send(message).catch(onServerError);
23+
transportToServer.send(message).catch((error) => {
24+
// Send error response back to client if it was a request (has id) and connection is still open
25+
if (isJSONRPCRequest(message) && !transportToClientClosed) {
26+
const errorResponse = {
27+
jsonrpc: "2.0" as const,
28+
id: message.id,
29+
error: {
30+
code: -32001,
31+
message: error.message,
32+
data: error
33+
}
34+
};
35+
transportToClient.send(errorResponse).catch(onClientError);
36+
}
37+
});
2338
};
2439

2540
transportToServer.onmessage = (message) => {

0 commit comments

Comments
 (0)