Skip to content

Commit c5a31dd

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 c5a31dd

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

cli/src/cli.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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));
1112

@@ -72,6 +73,10 @@ async function runWebClient(args: Args): Promise<void> {
7273

7374
console.log("Starting MCP inspector...");
7475

76+
// Generate session token for authentication
77+
const sessionToken = randomBytes(32).toString("hex");
78+
const authDisabled = !!process.env.DANGEROUSLY_OMIT_AUTH;
79+
7580
const abort = new AbortController();
7681
let cancelled: boolean = false;
7782
process.on("SIGINT", () => {
@@ -93,7 +98,9 @@ async function runWebClient(args: Args): Promise<void> {
9398
{
9499
env: {
95100
...process.env,
96-
PORT: SERVER_PORT,
101+
SERVER_PORT,
102+
CLIENT_PORT,
103+
MCP_PROXY_TOKEN: sessionToken,
97104
MCP_ENV_VARS: JSON.stringify(args.envArgs),
98105
},
99106
signal: abort.signal,
@@ -107,8 +114,26 @@ async function runWebClient(args: Args): Promise<void> {
107114

108115
if (serverOk) {
109116
try {
117+
// Build the client URL with authentication parameters
118+
const host = process.env.HOST || "localhost";
119+
const baseUrl = `http://${host}:${CLIENT_PORT}`;
120+
const params = new URLSearchParams();
121+
122+
if (SERVER_PORT !== "6277") {
123+
params.set("MCP_PROXY_PORT", SERVER_PORT);
124+
}
125+
if (!authDisabled) {
126+
params.set("MCP_PROXY_AUTH_TOKEN", sessionToken);
127+
}
128+
129+
const url = params.size > 0 ? `${baseUrl}/?${params.toString()}` : baseUrl;
130+
110131
await spawnPromise("node", [inspectorClientPath], {
111-
env: { ...process.env, PORT: CLIENT_PORT },
132+
env: {
133+
...process.env,
134+
CLIENT_PORT,
135+
INSPECTOR_URL: url
136+
},
112137
signal: abort.signal,
113138
echoOutput: true,
114139
});

0 commit comments

Comments
 (0)