77 lines
2.6 KiB
JavaScript
77 lines
2.6 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* HTTP MCP Client Bridge
|
|
* Acts as a bridge between:
|
|
* - HTTP Server (StreamableHTTPServerTransport at /mcp endpoint)
|
|
* - Claude Code (via stdio)
|
|
*
|
|
* Usage: node http-mcp-client.js <url> [--header name:value] ...
|
|
* Example: node http-mcp-client.js http://localhost:3000/mcp --header X-Acai-Token:abc123
|
|
*/
|
|
|
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/http.js";
|
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
|
|
async function main() {
|
|
const args = process.argv.slice(2);
|
|
|
|
if (args.length === 0) {
|
|
console.error("Usage: node http-mcp-client.js <url> [--header name:value] ...");
|
|
console.error("Example: node http-mcp-client.js http://localhost:3000/mcp --header X-Acai-Token:abc123");
|
|
process.exit(1);
|
|
}
|
|
|
|
const url = args[0];
|
|
const headers = {};
|
|
|
|
// Parse headers from command line
|
|
for (let i = 1; i < args.length; i += 2) {
|
|
if (args[i] === "--header" && i + 1 < args.length) {
|
|
const [name, value] = args[i + 1].split(":");
|
|
if (name && value) {
|
|
headers[name.trim()] = value.trim();
|
|
}
|
|
}
|
|
}
|
|
|
|
console.error(`[HTTP Bridge] Connecting to ${url}`);
|
|
console.error(`[HTTP Bridge] Headers: ${Object.keys(headers).join(", ")}`);
|
|
|
|
try {
|
|
// Create StreamableHTTP client transport to connect to our HTTP server
|
|
// This transport supports the modern HTTP/EventStream protocol (2025-03-26)
|
|
const httpTransport = new StreamableHTTPClientTransport(new URL(url), {
|
|
headers
|
|
});
|
|
|
|
// Create stdio transport for Claude Code communication
|
|
const stdioTransport = new StdioClientTransport();
|
|
|
|
// Create MCP client connected to HTTP server
|
|
const client = new Client({
|
|
name: "acai-http-bridge",
|
|
version: "1.0.0"
|
|
});
|
|
|
|
console.error("[HTTP Bridge] Connecting to HTTP server...");
|
|
await client.connect(httpTransport);
|
|
console.error("[HTTP Bridge] Connected to HTTP server successfully");
|
|
|
|
// Now bridge the client's tools to Claude Code via stdio
|
|
console.error("[HTTP Bridge] Forwarding tools to Claude Code via stdio...");
|
|
await stdioTransport.start(client);
|
|
console.error("[HTTP Bridge] Bridge active and ready");
|
|
|
|
} catch (error) {
|
|
console.error("[HTTP Bridge] ERROR:", error.message);
|
|
if (error.stack) {
|
|
console.error(error.stack);
|
|
}
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|