Skip to content

Commit 8b66a9e

Browse files
refactor: deduplicate URL generation and standardize on localhost
- Extract duplicated URL generation code into getClientUrl() helper function in start.js - Replace all 127.0.0.1 references with localhost for consistency across codebase - Update server to respect HOST environment variable for URL generation - Remove 127.0.0.1 from default allowed origins in CORS configuration - Update documentation to use localhost instead of 127.0.0.1 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 58661be commit 8b66a9e

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Thanks for your interest in contributing! This guide explains how to get involve
77
1. Fork the repository and clone it locally
88
2. Install dependencies with `npm install`
99
3. Run `npm run dev` to start both client and server in development mode
10-
4. Use the web UI at http://127.0.0.1:6274 to interact with the inspector
10+
4. Use the web UI at http://localhost:6274 to interact with the inspector
1111

1212
## Development Process & Pull Requests
1313

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ DANGEROUSLY_OMIT_AUTH=true npm start
168168

169169
#### Local-only Binding
170170

171-
By default, both the MCP Inspector proxy server and client bind only to `127.0.0.1` (localhost) to prevent network access. This ensures they are not accessible from other devices on the network. If you need to bind to all interfaces for development purposes, you can override this with the `HOST` environment variable:
171+
By default, both the MCP Inspector proxy server and client bind only to `localhost` to prevent network access. This ensures they are not accessible from other devices on the network. If you need to bind to all interfaces for development purposes, you can override this with the `HOST` environment variable:
172172

173173
```bash
174174
HOST=0.0.0.0 npm start
@@ -181,7 +181,7 @@ HOST=0.0.0.0 npm start
181181
To prevent DNS rebinding attacks, the MCP Inspector validates the `Origin` header on incoming requests. By default, only requests from the client origin are allowed (respects `CLIENT_PORT` if set, defaulting to port 6274). You can configure additional allowed origins by setting the `ALLOWED_ORIGINS` environment variable (comma-separated list):
182182

183183
```bash
184-
ALLOWED_ORIGINS=http://localhost:6274,http://127.0.0.1:6274,http://localhost:8000 npm start
184+
ALLOWED_ORIGINS=http://localhost:6274,http://localhost:8000 npm start
185185
```
186186

187187
### Configuration

client/bin/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const server = http.createServer((request, response) => {
4040
});
4141

4242
const port = process.env.PORT || 6274;
43-
const host = process.env.HOST || "127.0.0.1";
43+
const host = process.env.HOST || "localhost";
4444
server.on("listening", () => {
4545
console.log(
4646
`🔍 MCP Inspector is up and running at http://${host}:${port} 🚀`,

client/bin/start.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ function delay(ms) {
1212
return new Promise((resolve) => setTimeout(resolve, ms, true));
1313
}
1414

15+
function getClientUrl(port, authDisabled, sessionToken) {
16+
const host = process.env.HOST || "localhost";
17+
const baseUrl = `http://${host}:${port}`;
18+
return authDisabled
19+
? baseUrl
20+
: `${baseUrl}/?MCP_PROXY_AUTH_TOKEN=${sessionToken}`;
21+
}
22+
1523
async function startDevServer(serverOptions) {
1624
const { SERVER_PORT, CLIENT_PORT, sessionToken, envVars, abort } =
1725
serverOptions;
@@ -102,7 +110,7 @@ async function startDevClient(clientOptions) {
102110
const { CLIENT_PORT, authDisabled, sessionToken, abort, cancelled } =
103111
clientOptions;
104112
const clientCommand = "npx";
105-
const host = process.env.HOST || "127.0.0.1";
113+
const host = process.env.HOST || "localhost";
106114
const clientArgs = ["vite", "--port", CLIENT_PORT, "--host", host];
107115

108116
const client = spawn(clientCommand, clientArgs, {
@@ -114,10 +122,7 @@ async function startDevClient(clientOptions) {
114122

115123
// Auto-open browser after vite starts
116124
if (process.env.MCP_AUTO_OPEN_ENABLED !== "false") {
117-
const clientHost = process.env.HOST || "127.0.0.1";
118-
const url = authDisabled
119-
? `http://${clientHost}:${CLIENT_PORT}`
120-
: `http://${clientHost}:${CLIENT_PORT}/?MCP_PROXY_AUTH_TOKEN=${sessionToken}`;
125+
const url = getClientUrl(CLIENT_PORT, authDisabled, sessionToken);
121126

122127
// Give vite time to start before opening browser
123128
setTimeout(() => {
@@ -153,10 +158,7 @@ async function startProdClient(clientOptions) {
153158

154159
// Only auto-open browser if not cancelled
155160
if (process.env.MCP_AUTO_OPEN_ENABLED !== "false" && !cancelled) {
156-
const clientHost = process.env.HOST || "127.0.0.1";
157-
const url = authDisabled
158-
? `http://${clientHost}:${CLIENT_PORT}`
159-
: `http://${clientHost}:${CLIENT_PORT}/?MCP_PROXY_AUTH_TOKEN=${sessionToken}`;
161+
const url = getClientUrl(CLIENT_PORT, authDisabled, sessionToken);
160162
open(url);
161163
}
162164

server/src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,10 @@ const originValidationMiddleware = (
104104

105105
// Default origins based on CLIENT_PORT or use environment variable
106106
const clientPort = process.env.CLIENT_PORT || "6274";
107-
const defaultOrigins = [
108-
`http://localhost:${clientPort}`,
109-
`http://127.0.0.1:${clientPort}`,
107+
const defaultOrigins = `http://localhost:${clientPort}`;
108+
const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(",") || [
109+
defaultOrigins,
110110
];
111-
const allowedOrigins =
112-
process.env.ALLOWED_ORIGINS?.split(",") || defaultOrigins;
113111

114112
if (origin && !allowedOrigins.includes(origin)) {
115113
console.error(`Invalid origin: ${origin}`);
@@ -531,7 +529,7 @@ app.get("/config", originValidationMiddleware, authMiddleware, (req, res) => {
531529
});
532530

533531
const PORT = parseInt(process.env.PORT || "6277", 10);
534-
const HOST = process.env.HOST || "127.0.0.1";
532+
const HOST = process.env.HOST || "localhost";
535533

536534
const server = app.listen(PORT, HOST);
537535
server.on("listening", () => {
@@ -544,7 +542,8 @@ server.on("listening", () => {
544542

545543
// Display clickable URL with pre-filled token
546544
const clientPort = process.env.CLIENT_PORT || "6274";
547-
const clientUrl = `http://localhost:${clientPort}/?MCP_PROXY_AUTH_TOKEN=${sessionToken}`;
545+
const clientHost = process.env.HOST || "localhost";
546+
const clientUrl = `http://${clientHost}:${clientPort}/?MCP_PROXY_AUTH_TOKEN=${sessionToken}`;
548547
console.log(
549548
`\n🔗 Open inspector with token pre-filled:\n ${clientUrl}\n`,
550549
);

0 commit comments

Comments
 (0)