-
Notifications
You must be signed in to change notification settings - Fork 469
flowing env vars to the client app for inspector #258
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
base: main
Are you sure you want to change the base?
Changes from all commits
d290758
8c911d5
04800f7
d76a79a
3e89930
0cfd10c
b6b30d6
71fb6bc
9c2a89c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,114 @@ | ||
#!/usr/bin/env node | ||
|
||
import { join, dirname } from "path"; | ||
import { fileURLToPath } from "url"; | ||
import handler from "serve-handler"; | ||
import fs from "fs"; | ||
import http from "http"; | ||
import https from "https"; | ||
import { dirname, join } from "path"; | ||
import handler from "serve-handler"; | ||
import { fileURLToPath } from "url"; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
const distPath = join(__dirname, "../dist"); | ||
|
||
// Function to determine the MCP server URL | ||
const getMcpServerUrl = () => { | ||
if (process.env.MCP_PROXY_FULL_ADDRESS) { | ||
return process.env.MCP_PROXY_FULL_ADDRESS; | ||
} | ||
|
||
// Use current host with custom port if specified | ||
const port = process.env.SERVER_PORT || "6277"; | ||
// Default to http://localhost:port | ||
return `http://localhost:${port}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be 127.0.0.1 instead of localhost. The are generally interchangeable, however the OAuth spec recommends to use 127.0.0.1 instead of localhost:
cliffhall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
const server = http.createServer((request, response) => { | ||
return handler(request, response, { | ||
public: distPath, | ||
rewrites: [{ source: "/**", destination: "/index.html" }], | ||
}); | ||
// Handle the /config endpoint as a proxy to the MCP server | ||
if (request.url === "/config") { | ||
const mcpServerUrl = getMcpServerUrl(); | ||
const configUrl = `${mcpServerUrl}/config`; | ||
|
||
try { | ||
const clientModule = mcpServerUrl.startsWith("https:") ? https : http; | ||
const proxyReq = clientModule.request(configUrl, (proxyRes) => { | ||
// Capture the response data to modify it | ||
let data = ""; | ||
proxyRes.on("data", (chunk) => { | ||
data += chunk; | ||
}); | ||
|
||
proxyRes.on("end", () => { | ||
try { | ||
// Parse the JSON response | ||
const jsonResponse = JSON.parse(data); | ||
|
||
// Add the MCP_PROXY_FULL_ADDRESS to the response | ||
jsonResponse.config = { | ||
MCP_PROXY_FULL_ADDRESS: { value: mcpServerUrl }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this contain all the env vars that the client is interested in, i.e;
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can they all be set an environment variables? If so, I'll update here. |
||
}; | ||
|
||
// Send the modified response | ||
response.writeHead(proxyRes.statusCode, { | ||
"Content-Type": "application/json", | ||
}); | ||
response.end(JSON.stringify(jsonResponse)); | ||
} catch (e) { | ||
// If parsing fails, just forward the original response | ||
response.writeHead(proxyRes.statusCode, proxyRes.headers); | ||
response.end(data); | ||
} | ||
}); | ||
}); | ||
|
||
proxyReq.on("error", (err) => { | ||
console.error(`Error proxying request to ${configUrl}:`, err.message); | ||
response.statusCode = 500; | ||
response.end( | ||
JSON.stringify({ | ||
error: "Failed to connect to MCP server", | ||
defaultEnvironment: {}, | ||
mcpServerUrl: mcpServerUrl, | ||
}), | ||
); | ||
}); | ||
|
||
request.pipe(proxyReq); | ||
} catch (error) { | ||
console.error(`Error setting up proxy to ${configUrl}:`, error.message); | ||
response.statusCode = 500; | ||
response.end( | ||
JSON.stringify({ | ||
error: "Failed to connect to MCP server", | ||
defaultEnvironment: {}, | ||
mcpServerUrl: mcpServerUrl, | ||
}), | ||
); | ||
} | ||
} | ||
// Check if this is a request for index.html (either directly or via SPA routing) | ||
else if ( | ||
request.url === "/" || | ||
request.url === "/index.html" || | ||
!request.url.includes(".") | ||
) { | ||
const indexPath = join(distPath, "index.html"); | ||
fs.readFile(indexPath, "utf-8", (err, data) => { | ||
if (err) { | ||
response.statusCode = 500; | ||
response.end(`Error loading index.html: ${err.message}`); | ||
return; | ||
} | ||
|
||
response.setHeader("Content-Type", "text/html"); | ||
response.end(data); | ||
}); | ||
} else { | ||
// For all other assets, use serve-handler as before | ||
return handler(request, response, { | ||
public: distPath, | ||
rewrites: [{ source: "/**", destination: "/index.html" }], | ||
}); | ||
} | ||
}); | ||
|
||
const port = process.env.PORT || 6274; | ||
|
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.