fix: pass proxy port via URL params when non-default

- Update client to read MCP_PROXY_PORT from URL parameters
- Update server to include port in displayed URL when non-default
- Update start scripts to pass SERVER_PORT to client
- Use DEFAULT_MCP_PROXY_LISTEN_PORT constant instead of magic numbers

This fixes the issue where the client couldn't connect to the proxy
when SERVER_PORT was set to a non-default value.
This commit is contained in:
Felix Weinberger 2025-06-24 11:42:45 +01:00
commit 9fcfd89326
No known key found for this signature in database
3 changed files with 77 additions and 23 deletions

View file

@ -7,17 +7,29 @@ import { fileURLToPath } from "url";
import { randomBytes } from "crypto"; import { randomBytes } from "crypto";
const __dirname = dirname(fileURLToPath(import.meta.url)); const __dirname = dirname(fileURLToPath(import.meta.url));
const DEFAULT_MCP_PROXY_LISTEN_PORT = "6277";
function delay(ms) { function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms, true)); return new Promise((resolve) => setTimeout(resolve, ms, true));
} }
function getClientUrl(port, authDisabled, sessionToken) { function getClientUrl(port, authDisabled, sessionToken, serverPort) {
const host = process.env.HOST || "localhost"; const host = process.env.HOST || "localhost";
const baseUrl = `http://${host}:${port}`; const baseUrl = `http://${host}:${port}`;
return authDisabled
? baseUrl if (authDisabled) {
: `${baseUrl}/?MCP_PROXY_AUTH_TOKEN=${sessionToken}`; return baseUrl;
}
const params = new URLSearchParams();
params.set("MCP_PROXY_AUTH_TOKEN", sessionToken);
// Add server port if it's not the default
if (serverPort && serverPort !== DEFAULT_MCP_PROXY_LISTEN_PORT) {
params.set("MCP_PROXY_PORT", serverPort);
}
return `${baseUrl}/?${params.toString()}`;
} }
async function startDevServer(serverOptions) { async function startDevServer(serverOptions) {
@ -107,8 +119,14 @@ async function startProdServer(serverOptions) {
} }
async function startDevClient(clientOptions) { async function startDevClient(clientOptions) {
const { CLIENT_PORT, authDisabled, sessionToken, abort, cancelled } = const {
clientOptions; CLIENT_PORT,
SERVER_PORT,
authDisabled,
sessionToken,
abort,
cancelled,
} = clientOptions;
const clientCommand = "npx"; const clientCommand = "npx";
const host = process.env.HOST || "localhost"; const host = process.env.HOST || "localhost";
const clientArgs = ["vite", "--port", CLIENT_PORT, "--host", host]; const clientArgs = ["vite", "--port", CLIENT_PORT, "--host", host];
@ -122,7 +140,12 @@ async function startDevClient(clientOptions) {
// Auto-open browser after vite starts // Auto-open browser after vite starts
if (process.env.MCP_AUTO_OPEN_ENABLED !== "false") { if (process.env.MCP_AUTO_OPEN_ENABLED !== "false") {
const url = getClientUrl(CLIENT_PORT, authDisabled, sessionToken); const url = getClientUrl(
CLIENT_PORT,
authDisabled,
sessionToken,
SERVER_PORT,
);
// Give vite time to start before opening browser // Give vite time to start before opening browser
setTimeout(() => { setTimeout(() => {
@ -146,8 +169,14 @@ async function startDevClient(clientOptions) {
} }
async function startProdClient(clientOptions) { async function startProdClient(clientOptions) {
const { CLIENT_PORT, authDisabled, sessionToken, abort, cancelled } = const {
clientOptions; CLIENT_PORT,
SERVER_PORT,
authDisabled,
sessionToken,
abort,
cancelled,
} = clientOptions;
const inspectorClientPath = resolve( const inspectorClientPath = resolve(
__dirname, __dirname,
"../..", "../..",
@ -158,7 +187,12 @@ async function startProdClient(clientOptions) {
// Only auto-open browser if not cancelled // Only auto-open browser if not cancelled
if (process.env.MCP_AUTO_OPEN_ENABLED !== "false" && !cancelled) { if (process.env.MCP_AUTO_OPEN_ENABLED !== "false" && !cancelled) {
const url = getClientUrl(CLIENT_PORT, authDisabled, sessionToken); const url = getClientUrl(
CLIENT_PORT,
authDisabled,
sessionToken,
SERVER_PORT,
);
open(url); open(url);
} }
@ -210,7 +244,7 @@ async function main() {
} }
const CLIENT_PORT = process.env.CLIENT_PORT ?? "6274"; const CLIENT_PORT = process.env.CLIENT_PORT ?? "6274";
const SERVER_PORT = process.env.SERVER_PORT ?? "6277"; const SERVER_PORT = process.env.SERVER_PORT ?? DEFAULT_MCP_PROXY_LISTEN_PORT;
console.log( console.log(
isDev isDev
@ -255,6 +289,7 @@ async function main() {
try { try {
const clientOptions = { const clientOptions = {
CLIENT_PORT, CLIENT_PORT,
SERVER_PORT,
authDisabled, authDisabled,
sessionToken, sessionToken,
abort, abort,

View file

@ -4,12 +4,26 @@ import {
DEFAULT_INSPECTOR_CONFIG, DEFAULT_INSPECTOR_CONFIG,
} from "@/lib/constants"; } from "@/lib/constants";
const getSearchParam = (key: string): string | null => {
try {
const url = new URL(window.location.href);
return url.searchParams.get(key);
} catch {
return null;
}
};
export const getMCPProxyAddress = (config: InspectorConfig): string => { export const getMCPProxyAddress = (config: InspectorConfig): string => {
const proxyFullAddress = config.MCP_PROXY_FULL_ADDRESS.value as string; const proxyFullAddress = config.MCP_PROXY_FULL_ADDRESS.value as string;
if (proxyFullAddress) { if (proxyFullAddress) {
return proxyFullAddress; return proxyFullAddress;
} }
return `${window.location.protocol}//${window.location.hostname}:${DEFAULT_MCP_PROXY_LISTEN_PORT}`;
// Check for proxy port from query params, fallback to default
const proxyPort =
getSearchParam("MCP_PROXY_PORT") || DEFAULT_MCP_PROXY_LISTEN_PORT;
return `${window.location.protocol}//${window.location.hostname}:${proxyPort}`;
}; };
export const getMCPServerRequestTimeout = (config: InspectorConfig): number => { export const getMCPServerRequestTimeout = (config: InspectorConfig): number => {
@ -40,15 +54,6 @@ export const getMCPProxyAuthToken = (
}; };
}; };
const getSearchParam = (key: string): string | null => {
try {
const url = new URL(window.location.href);
return url.searchParams.get(key);
} catch {
return null;
}
};
export const getInitialTransportType = (): export const getInitialTransportType = ():
| "stdio" | "stdio"
| "sse" | "sse"

View file

@ -21,6 +21,7 @@ import { findActualExecutable } from "spawn-rx";
import mcpProxy from "./mcpProxy.js"; import mcpProxy from "./mcpProxy.js";
import { randomUUID, randomBytes, timingSafeEqual } from "node:crypto"; import { randomUUID, randomBytes, timingSafeEqual } from "node:crypto";
const DEFAULT_MCP_PROXY_LISTEN_PORT = "6277";
const SSE_HEADERS_PASSTHROUGH = ["authorization"]; const SSE_HEADERS_PASSTHROUGH = ["authorization"];
const STREAMABLE_HTTP_HEADERS_PASSTHROUGH = [ const STREAMABLE_HTTP_HEADERS_PASSTHROUGH = [
"authorization", "authorization",
@ -528,7 +529,10 @@ app.get("/config", originValidationMiddleware, authMiddleware, (req, res) => {
} }
}); });
const PORT = parseInt(process.env.SERVER_PORT || "6277", 10); const PORT = parseInt(
process.env.SERVER_PORT || DEFAULT_MCP_PROXY_LISTEN_PORT,
10,
);
const HOST = process.env.HOST || "localhost"; const HOST = process.env.HOST || "localhost";
const server = app.listen(PORT, HOST); const server = app.listen(PORT, HOST);
@ -543,7 +547,17 @@ server.on("listening", () => {
// Display clickable URL with pre-filled token // Display clickable URL with pre-filled token
const clientPort = process.env.CLIENT_PORT || "6274"; const clientPort = process.env.CLIENT_PORT || "6274";
const clientHost = process.env.HOST || "localhost"; const clientHost = process.env.HOST || "localhost";
const clientUrl = `http://${clientHost}:${clientPort}/?MCP_PROXY_AUTH_TOKEN=${sessionToken}`;
// Build URL with query parameters
const params = new URLSearchParams();
params.set("MCP_PROXY_AUTH_TOKEN", sessionToken);
// Add server port if it's not the default
if (PORT !== parseInt(DEFAULT_MCP_PROXY_LISTEN_PORT, 10)) {
params.set("MCP_PROXY_PORT", PORT.toString());
}
const clientUrl = `http://${clientHost}:${clientPort}/?${params.toString()}`;
console.log( console.log(
`\n🔗 Open inspector with token pre-filled:\n ${clientUrl}\n`, `\n🔗 Open inspector with token pre-filled:\n ${clientUrl}\n`,
); );