1 const hostname = process.env.REPLIT_CONNECTORS_HOSTNAME;
2 const token = process.env.REPL_IDENTITY ? "repl " + process.env.REPL_IDENTITY
3 : process.env.WEB_REPL_RENEWAL ? "depl " + process.env.WEB_REPL_RENEWAL : null;
4
5 async function mcpCall(method, params = {}) {
6 const resp = await fetch(`https://${hostname}/api/v2/proxy/mcp`, {
7 method: "POST",
8 headers: {
9 "Content-Type": "application/json",
10 Accept: "application/json, text/event-stream",
11 "X-Replit-Token": token,
12 "Connector-Name": "whop",
13 },
14 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
15 });
16 const text = await resp.text();
17 const lines = text.split('\n').filter(l => l.startsWith('data:'));
18 const envelope = JSON.parse(lines.at(-1).replace(/^data:\s*/, ''));
19 const result = envelope.result;
20 const textContent = result?.content?.[0]?.text;
21 return textContent ? JSON.parse(textContent) : result;
22 }
23
24 const [cmd, argsJson] = process.argv.slice(2);
25 if (cmd === "--list-tools") {
26 const { tools } = await mcpCall("tools/list");
27 tools.forEach(t => console.log(t.name + " — " + t.description));
28 } else if (cmd === "--schema") {
29 const { tools } = await mcpCall("tools/list");
30 const tool = tools.find(t => t.name === argsJson);
31 console.log(tool ? JSON.stringify(tool.inputSchema, null, 2) : `Tool "${argsJson}" not found`);
32 } else if (cmd) {
33 const result = await mcpCall("tools/call", {
34 name: cmd,
35 arguments: JSON.parse(argsJson || "{}"),
36 });
37 console.log(JSON.stringify(result, null, 2));
38 } else {
39 console.log("Usage: node whop-mcp.mjs --list-tools | --schema <tool> | <tool> '{...}'");
40 }