Skip to content

Commit e552377

Browse files
Fix proxy token authentication in npx command
The npx entry point (cli/src/cli.ts) was not generating or passing authentication tokens, causing proxy authentication to fail when using `npx @modelcontextprotocol/inspector@latest`. This change aligns the npx behavior with the local development behavior: - Generate session token using crypto.randomBytes(32) - Pass MCP_PROXY_TOKEN to server via environment - Use correct SERVER_PORT instead of PORT - Build client URL with authentication parameters - Pass URL to client via INSPECTOR_URL environment variable Now both `npm run start` and `npx` commands handle authentication consistently.
1 parent 38bead3 commit e552377

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

cli/src/cli.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import path from "node:path";
66
import { dirname, resolve } from "path";
77
import { spawnPromise } from "spawn-rx";
88
import { fileURLToPath } from "url";
9+
import { randomBytes } from "crypto";
910

1011
const __dirname = dirname(fileURLToPath(import.meta.url));
12+
const DEFAULT_MCP_PROXY_LISTEN_PORT = "6277";
1113

1214
type Args = {
1315
command: string;
@@ -68,10 +70,15 @@ async function runWebClient(args: Args): Promise<void> {
6870
);
6971

7072
const CLIENT_PORT: string = process.env.CLIENT_PORT ?? "6274";
71-
const SERVER_PORT: string = process.env.SERVER_PORT ?? "6277";
73+
const SERVER_PORT: string =
74+
process.env.SERVER_PORT ?? DEFAULT_MCP_PROXY_LISTEN_PORT;
7275

7376
console.log("Starting MCP inspector...");
7477

78+
// Generate session token for authentication
79+
const sessionToken = randomBytes(32).toString("hex");
80+
const authDisabled = !!process.env.DANGEROUSLY_OMIT_AUTH;
81+
7582
const abort = new AbortController();
7683
let cancelled: boolean = false;
7784
process.on("SIGINT", () => {
@@ -93,7 +100,9 @@ async function runWebClient(args: Args): Promise<void> {
93100
{
94101
env: {
95102
...process.env,
96-
PORT: SERVER_PORT,
103+
SERVER_PORT,
104+
CLIENT_PORT,
105+
MCP_PROXY_TOKEN: sessionToken,
97106
MCP_ENV_VARS: JSON.stringify(args.envArgs),
98107
},
99108
signal: abort.signal,
@@ -107,8 +116,27 @@ async function runWebClient(args: Args): Promise<void> {
107116

108117
if (serverOk) {
109118
try {
119+
// Build the client URL with authentication parameters
120+
const host = process.env.HOST || "localhost";
121+
const baseUrl = `http://${host}:${CLIENT_PORT}`;
122+
const params = new URLSearchParams();
123+
124+
if (SERVER_PORT !== DEFAULT_MCP_PROXY_LISTEN_PORT) {
125+
params.set("MCP_PROXY_PORT", SERVER_PORT);
126+
}
127+
if (!authDisabled) {
128+
params.set("MCP_PROXY_AUTH_TOKEN", sessionToken);
129+
}
130+
131+
const url =
132+
params.size > 0 ? `${baseUrl}/?${params.toString()}` : baseUrl;
133+
110134
await spawnPromise("node", [inspectorClientPath], {
111-
env: { ...process.env, PORT: CLIENT_PORT },
135+
env: {
136+
...process.env,
137+
CLIENT_PORT,
138+
INSPECTOR_URL: url,
139+
},
112140
signal: abort.signal,
113141
echoOutput: true,
114142
});

0 commit comments

Comments
 (0)