Skip to content

add support for proxy base path routing #733

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 10 additions & 2 deletions client/bin/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ function delay(ms) {
}

function getClientUrl(port, authDisabled, sessionToken, serverPort) {
const host = process.env.HOST || "localhost";
const baseUrl = `http://${host}:${port}`;
// Check for custom base URL from environment
const customBaseUrl = process.env.MCP_INSPECTOR_BASE_URL;

let baseUrl;
if (customBaseUrl) {
baseUrl = `${customBaseUrl}/proxy/${port}`;
} else {
const host = process.env.HOST || "localhost";
baseUrl = `http://${host}:${port}`;
}

const params = new URLSearchParams();
if (serverPort && serverPort !== DEFAULT_MCP_PROXY_LISTEN_PORT) {
Expand Down
18 changes: 14 additions & 4 deletions client/src/lib/hooks/useConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ export function useConnection({

const checkProxyHealth = async () => {
try {
const proxyHealthUrl = new URL(`${getMCPProxyAddress(config)}/health`);
const proxyAddress = getMCPProxyAddress(config);
const proxyHealthUrl = proxyAddress.startsWith('http')
? new URL(`${proxyAddress}/health`)
: new URL(`${proxyAddress}/health`, window.location.origin);
const { token: proxyAuthToken, header: proxyAuthTokenHeader } =
getMCPProxyAuthToken(config);
const headers: HeadersInit = {};
Expand Down Expand Up @@ -408,10 +411,13 @@ export function useConnection({
| StreamableHTTPClientTransportOptions
| SSEClientTransportOptions;

const proxyAddress = getMCPProxyAddress(config);
let mcpProxyServerUrl;
switch (transportType) {
case "stdio":
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/stdio`);
mcpProxyServerUrl = proxyAddress.startsWith('http')
? new URL(`${proxyAddress}/stdio`)
: new URL(`${proxyAddress}/stdio`, window.location.origin);
mcpProxyServerUrl.searchParams.append("command", command);
mcpProxyServerUrl.searchParams.append("args", args);
mcpProxyServerUrl.searchParams.append("env", JSON.stringify(env));
Expand All @@ -434,7 +440,9 @@ export function useConnection({
break;

case "sse":
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/sse`);
mcpProxyServerUrl = proxyAddress.startsWith('http')
? new URL(`${proxyAddress}/sse`)
: new URL(`${proxyAddress}/sse`, window.location.origin);
mcpProxyServerUrl.searchParams.append("url", sseUrl);
transportOptions = {
eventSourceInit: {
Expand All @@ -454,7 +462,9 @@ export function useConnection({
break;

case "streamable-http":
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/mcp`);
mcpProxyServerUrl = proxyAddress.startsWith('http')
? new URL(`${proxyAddress}/mcp`)
: new URL(`${proxyAddress}/mcp`, window.location.origin);
mcpProxyServerUrl.searchParams.append("url", sseUrl);
transportOptions = {
eventSourceInit: {
Expand Down
8 changes: 8 additions & 0 deletions client/src/utils/configUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export const getMCPProxyAddress = (config: InspectorConfig): string => {
const proxyPort =
getSearchParam("MCP_PROXY_PORT") || DEFAULT_MCP_PROXY_LISTEN_PORT;

// If we're being served through a proxy path (like /proxy/6274/),
// construct relative URL to the proxy server port
const currentPath = window.location.pathname;
if (currentPath.includes('/proxy/')) {
const basePath = currentPath.replace(/\/proxy\/\d+\/.*$/, '');
return `${basePath}/proxy/${proxyPort}`;
}

return `${window.location.protocol}//${window.location.hostname}:${proxyPort}`;
};

Expand Down
1 change: 1 addition & 0 deletions client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { defineConfig } from "vite";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
base: "./",
server: {
host: true,
},
Expand Down