Skip to content

Commit 908d0ee

Browse files
feat: add client support for proxy authentication
- Add MCP_PROXY_AUTH_TOKEN configuration field to store session token - Update useConnection hook to include proxy auth token in all requests - Add separate proxy auth headers to avoid conflicts with MCP server auth - Add specific error handling for proxy authentication failures - Show helpful toast message directing users to Configuration settings - Update README with instructions for configuring token in UI The client now properly authenticates with the proxy server using the session token displayed in the proxy console.
1 parent 111e282 commit 908d0ee

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

client/src/lib/configurationTypes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ export type InspectorConfig = {
3333
* The full address of the MCP Proxy Server, in case it is running on a non-default address. Example: http://10.1.1.22:5577
3434
*/
3535
MCP_PROXY_FULL_ADDRESS: ConfigItem;
36+
37+
/**
38+
* Session token for authenticating with the MCP Proxy Server. This token is displayed in the proxy server console on startup.
39+
*/
40+
MCP_PROXY_AUTH_TOKEN: ConfigItem;
3641
};

client/src/lib/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ export const DEFAULT_INSPECTOR_CONFIG: InspectorConfig = {
5454
"Set this if you are running the MCP Inspector Proxy on a non-default address. Example: http://10.1.1.22:5577",
5555
value: "",
5656
},
57+
MCP_PROXY_AUTH_TOKEN: {
58+
label: "Proxy Session Token",
59+
description:
60+
"Session token for authenticating with the MCP Proxy Server (displayed in proxy console on startup)",
61+
value: "",
62+
},
5763
} as const;

client/src/lib/hooks/useConnection.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
getMCPProxyAddress,
4343
getMCPServerRequestMaxTotalTimeout,
4444
resetRequestTimeoutOnProgress,
45+
getMCPProxyAuthToken,
4546
} from "@/utils/configUtils";
4647
import { getMCPServerRequestTimeout } from "@/utils/configUtils";
4748
import { InspectorConfig } from "../configurationTypes";
@@ -242,7 +243,12 @@ export function useConnection({
242243
const checkProxyHealth = async () => {
243244
try {
244245
const proxyHealthUrl = new URL(`${getMCPProxyAddress(config)}/health`);
245-
const proxyHealthResponse = await fetch(proxyHealthUrl);
246+
const proxyAuthToken = getMCPProxyAuthToken(config);
247+
const headers: HeadersInit = {};
248+
if (proxyAuthToken) {
249+
headers['Authorization'] = `Bearer ${proxyAuthToken}`;
250+
}
251+
const proxyHealthResponse = await fetch(proxyHealthUrl, { headers });
246252
const proxyHealth = await proxyHealthResponse.json();
247253
if (proxyHealth?.status !== "ok") {
248254
throw new Error("MCP Proxy Server is not healthy");
@@ -261,6 +267,13 @@ export function useConnection({
261267
);
262268
};
263269

270+
const isProxyAuthError = (error: unknown): boolean => {
271+
return (
272+
error instanceof Error &&
273+
error.message.includes("Authentication required. Use the session token")
274+
);
275+
};
276+
264277
const handleAuthError = async (error: unknown) => {
265278
if (is401Error(error)) {
266279
const serverAuthProvider = new InspectorOAuthClientProvider(sseUrl);
@@ -318,6 +331,13 @@ export function useConnection({
318331
}
319332
}
320333

334+
// Add proxy authentication
335+
const proxyAuthToken = getMCPProxyAuthToken(config);
336+
const proxyHeaders: HeadersInit = {};
337+
if (proxyAuthToken) {
338+
proxyHeaders['Authorization'] = `Bearer ${proxyAuthToken}`;
339+
}
340+
321341
// Create appropriate transport
322342
let transportOptions:
323343
| StreamableHTTPClientTransportOptions
@@ -336,10 +356,10 @@ export function useConnection({
336356
fetch: (
337357
url: string | URL | globalThis.Request,
338358
init: RequestInit | undefined,
339-
) => fetch(url, { ...init, headers }),
359+
) => fetch(url, { ...init, headers: { ...headers, ...proxyHeaders } }),
340360
},
341361
requestInit: {
342-
headers,
362+
headers: { ...headers, ...proxyHeaders },
343363
},
344364
};
345365
break;
@@ -352,10 +372,10 @@ export function useConnection({
352372
fetch: (
353373
url: string | URL | globalThis.Request,
354374
init: RequestInit | undefined,
355-
) => fetch(url, { ...init, headers }),
375+
) => fetch(url, { ...init, headers: { ...headers, ...proxyHeaders } }),
356376
},
357377
requestInit: {
358-
headers,
378+
headers: { ...headers, ...proxyHeaders },
359379
},
360380
};
361381
break;
@@ -368,10 +388,10 @@ export function useConnection({
368388
fetch: (
369389
url: string | URL | globalThis.Request,
370390
init: RequestInit | undefined,
371-
) => fetch(url, { ...init, headers }),
391+
) => fetch(url, { ...init, headers: { ...headers, ...proxyHeaders } }),
372392
},
373393
requestInit: {
374-
headers,
394+
headers: { ...headers, ...proxyHeaders },
375395
},
376396
// TODO these should be configurable...
377397
reconnectionOptions: {
@@ -447,6 +467,17 @@ export function useConnection({
447467
error,
448468
);
449469

470+
// Check if it's a proxy auth error
471+
if (isProxyAuthError(error)) {
472+
toast({
473+
title: "Proxy Authentication Required",
474+
description: "Please enter the session token from the proxy server console in the Configuration settings.",
475+
variant: "destructive",
476+
});
477+
setConnectionStatus("error");
478+
return;
479+
}
480+
450481
const shouldRetry = await handleAuthError(error);
451482
if (shouldRetry) {
452483
return connect(undefined, retryCount + 1);

client/src/utils/configUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export const getMCPServerRequestMaxTotalTimeout = (
2828
return config.MCP_REQUEST_MAX_TOTAL_TIMEOUT.value as number;
2929
};
3030

31+
export const getMCPProxyAuthToken = (config: InspectorConfig): string => {
32+
return config.MCP_PROXY_AUTH_TOKEN.value as string;
33+
};
34+
3135
const getSearchParam = (key: string): string | null => {
3236
try {
3337
const url = new URL(window.location.href);

0 commit comments

Comments
 (0)