|
| 1 | +// @ts-check |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +// Ensures global.core is available when running outside github-script context |
| 5 | +require("./shim.cjs"); |
| 6 | + |
| 7 | +/** |
| 8 | + * convert_gateway_config_claude.cjs |
| 9 | + * |
| 10 | + * Converts the MCP gateway's standard HTTP-based configuration to the JSON |
| 11 | + * format expected by Claude. Reads the gateway output JSON, filters out |
| 12 | + * CLI-mounted servers, sets type:"http", rewrites URLs to use the correct |
| 13 | + * domain, and writes the result to ${RUNNER_TEMP}/gh-aw/mcp-config/mcp-servers.json. |
| 14 | + * |
| 15 | + * Required environment variables: |
| 16 | + * - MCP_GATEWAY_OUTPUT: Path to gateway output configuration file |
| 17 | + * - MCP_GATEWAY_DOMAIN: Domain for MCP server URLs (e.g., host.docker.internal) |
| 18 | + * - MCP_GATEWAY_PORT: Port for MCP gateway (e.g., 80) |
| 19 | + * - RUNNER_TEMP: GitHub Actions runner temp directory |
| 20 | + * |
| 21 | + * Optional: |
| 22 | + * - GH_AW_MCP_CLI_SERVERS: JSON array of server names to exclude from agent config |
| 23 | + */ |
| 24 | + |
| 25 | +const fs = require("fs"); |
| 26 | +const path = require("path"); |
| 27 | + |
| 28 | +const OUTPUT_PATH = path.join(process.env.RUNNER_TEMP || "/tmp", "gh-aw/mcp-config/mcp-servers.json"); |
| 29 | + |
| 30 | +/** |
| 31 | + * Rewrite a gateway URL to use the configured domain and port. |
| 32 | + * Replaces http://<anything>/mcp/ with http://<domain>:<port>/mcp/. |
| 33 | + * |
| 34 | + * @param {string} url - Original URL from gateway output |
| 35 | + * @param {string} urlPrefix - Target URL prefix (e.g., http://host.docker.internal:80) |
| 36 | + * @returns {string} Rewritten URL |
| 37 | + */ |
| 38 | +function rewriteUrl(url, urlPrefix) { |
| 39 | + return url.replace(/^http:\/\/[^/]+\/mcp\//, `${urlPrefix}/mcp/`); |
| 40 | +} |
| 41 | + |
| 42 | +function main() { |
| 43 | + const gatewayOutput = process.env.MCP_GATEWAY_OUTPUT; |
| 44 | + const domain = process.env.MCP_GATEWAY_DOMAIN; |
| 45 | + const port = process.env.MCP_GATEWAY_PORT; |
| 46 | + |
| 47 | + if (!gatewayOutput) { |
| 48 | + core.error("ERROR: MCP_GATEWAY_OUTPUT environment variable is required"); |
| 49 | + process.exit(1); |
| 50 | + } |
| 51 | + if (!fs.existsSync(gatewayOutput)) { |
| 52 | + core.error(`ERROR: Gateway output file not found: ${gatewayOutput}`); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | + if (!domain) { |
| 56 | + core.error("ERROR: MCP_GATEWAY_DOMAIN environment variable is required"); |
| 57 | + process.exit(1); |
| 58 | + } |
| 59 | + if (!port) { |
| 60 | + core.error("ERROR: MCP_GATEWAY_PORT environment variable is required"); |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + core.info("Converting gateway configuration to Claude format..."); |
| 65 | + core.info(`Input: ${gatewayOutput}`); |
| 66 | + core.info(`Target domain: ${domain}:${port}`); |
| 67 | + |
| 68 | + const urlPrefix = `http://${domain}:${port}`; |
| 69 | + |
| 70 | + /** @type {Set<string>} */ |
| 71 | + const cliServers = new Set(JSON.parse(process.env.GH_AW_MCP_CLI_SERVERS || "[]")); |
| 72 | + if (cliServers.size > 0) { |
| 73 | + core.info(`CLI-mounted servers to filter: ${[...cliServers].join(", ")}`); |
| 74 | + } |
| 75 | + |
| 76 | + /** @type {Record<string, unknown>} */ |
| 77 | + const config = JSON.parse(fs.readFileSync(gatewayOutput, "utf8")); |
| 78 | + const rawServers = config.mcpServers; |
| 79 | + const servers = |
| 80 | + /** @type {Record<string, Record<string, unknown>>} */ |
| 81 | + rawServers && typeof rawServers === "object" && !Array.isArray(rawServers) ? rawServers : {}; |
| 82 | + |
| 83 | + /** @type {Record<string, Record<string, unknown>>} */ |
| 84 | + const result = {}; |
| 85 | + for (const [name, value] of Object.entries(servers)) { |
| 86 | + if (cliServers.has(name)) continue; |
| 87 | + const entry = { ...value }; |
| 88 | + // Claude uses "type": "http" for HTTP-based MCP servers |
| 89 | + entry.type = "http"; |
| 90 | + // Fix the URL to use the correct domain |
| 91 | + if (typeof entry.url === "string") { |
| 92 | + entry.url = rewriteUrl(entry.url, urlPrefix); |
| 93 | + } |
| 94 | + result[name] = entry; |
| 95 | + } |
| 96 | + |
| 97 | + const output = JSON.stringify({ mcpServers: result }, null, 2); |
| 98 | + |
| 99 | + const serverCount = Object.keys(result).length; |
| 100 | + const totalCount = Object.keys(servers).length; |
| 101 | + const filteredCount = totalCount - serverCount; |
| 102 | + core.info(`Servers: ${serverCount} included, ${filteredCount} filtered (CLI-mounted)`); |
| 103 | + |
| 104 | + // Ensure output directory exists |
| 105 | + fs.mkdirSync(path.dirname(OUTPUT_PATH), { recursive: true }); |
| 106 | + |
| 107 | + // Write with owner-only permissions (0o600) to protect the gateway bearer token. |
| 108 | + // An attacker who reads mcp-servers.json could bypass --allowed-tools by issuing |
| 109 | + // raw JSON-RPC calls directly to the gateway. |
| 110 | + fs.writeFileSync(OUTPUT_PATH, output, { mode: 0o600 }); |
| 111 | + |
| 112 | + core.info(`Claude configuration written to ${OUTPUT_PATH}`); |
| 113 | + core.info(""); |
| 114 | + core.info("Converted configuration:"); |
| 115 | + core.info(output); |
| 116 | +} |
| 117 | + |
| 118 | +main(); |
| 119 | + |
| 120 | +module.exports = { rewriteUrl }; |
0 commit comments