@hivefloor/cli 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ resolveSession
4
+ } from "./chunk-BAB4DPCN.js";
5
+ import {
6
+ detectRepo
7
+ } from "./chunk-OQ6QVFSH.js";
8
+ import {
9
+ toolFromEnv
10
+ } from "./chunk-2IMJECYD.js";
11
+ import {
12
+ flagStr
13
+ } from "./chunk-TKMIV7BG.js";
14
+ import {
15
+ loadToken
16
+ } from "./chunk-4A5WOW2D.js";
17
+ import "./chunk-WNDUGQDX.js";
18
+ import {
19
+ cliNpxCommand,
20
+ isSecureUrl,
21
+ normalizeHost
22
+ } from "./chunk-RE2EQZWL.js";
23
+ import "./chunk-K3NQKI34.js";
24
+
25
+ // src/mcp.ts
26
+ import os from "os";
27
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
28
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
29
+ async function contextHeaders(ctx, token, toolName) {
30
+ const h = { Authorization: `Bearer ${token}`, "X-HF-Tool": ctx.env.HIVEFLOOR_TOOL || toolFromEnv(ctx.env) || toolName, "X-HF-Machine": os.hostname().split(".")[0] ?? "machine" };
31
+ const session = ctx.env.HIVEFLOOR_SESSION || ctx.env.CLAUDE_SESSION_ID || ctx.env.GROK_SESSION_ID || ctx.env.CODEX_SESSION_ID;
32
+ if (session) h["X-HF-Session"] = session;
33
+ const model = ctx.env.HIVEFLOOR_MODEL || ctx.env.ANTHROPIC_MODEL;
34
+ if (model) h["X-HF-Model"] = model;
35
+ const repo = await detectRepo(ctx.cwd);
36
+ if (repo) {
37
+ h["X-HF-Repo"] = repo.remote;
38
+ if (repo.branch) h["X-HF-Branch"] = repo.branch;
39
+ if (repo.path) h["X-HF-Path"] = repo.path;
40
+ }
41
+ return h;
42
+ }
43
+ var isRequest = (m) => "method" in m && "id" in m;
44
+ function mcpTargetUrl(flagUrl, host) {
45
+ if (!flagUrl) return new URL(`${host}/mcp`);
46
+ let u;
47
+ try {
48
+ u = new URL(flagUrl);
49
+ } catch {
50
+ throw new Error("--url is not a URL");
51
+ }
52
+ if (!isSecureUrl(u)) throw new Error(`--url must be https (http is only allowed on localhost): ${u.origin}`);
53
+ return u;
54
+ }
55
+ async function runMcpProxy(ctx, p, io = { stdin: process.stdin, stdout: process.stdout }) {
56
+ const s = await resolveSession(ctx, p);
57
+ if (!s) {
58
+ ctx.err(`hivefloor: not connected. Run: ${cliNpxCommand("connect")}
59
+ `);
60
+ return 1;
61
+ }
62
+ let url;
63
+ try {
64
+ url = mcpTargetUrl(flagStr(p, "url"), s.host);
65
+ } catch (e) {
66
+ ctx.err(`hivefloor mcp: ${e.message}
67
+ `);
68
+ return 1;
69
+ }
70
+ let token = s.token;
71
+ let tool = s.tool;
72
+ if (url.origin !== new URL(s.host).origin) {
73
+ const other = await loadToken(normalizeHost(url.origin));
74
+ if (!other) {
75
+ ctx.err(`hivefloor mcp: refusing to send the token of ${s.host} to ${url.origin}; connect to that host first
76
+ `);
77
+ return 1;
78
+ }
79
+ token = other.token;
80
+ tool = other.tool;
81
+ }
82
+ const headers = await contextHeaders(ctx, token, tool);
83
+ const upstream = new StreamableHTTPClientTransport(url, { requestInit: { headers } });
84
+ const downstream = new StdioServerTransport(io.stdin, io.stdout);
85
+ return await new Promise((resolve) => {
86
+ let closing = false;
87
+ const finish = (code) => {
88
+ if (closing) return;
89
+ closing = true;
90
+ void Promise.allSettled([upstream.close(), downstream.close()]).then(() => resolve(code));
91
+ };
92
+ downstream.onmessage = (msg) => {
93
+ upstream.send(msg).catch((e) => {
94
+ if (isRequest(msg)) void downstream.send({ jsonrpc: "2.0", id: msg.id, error: { code: -32e3, message: `Hivefloor is not reachable: ${e.message}` } });
95
+ });
96
+ };
97
+ upstream.onmessage = (msg) => {
98
+ const result = msg.result;
99
+ if (result && typeof result.protocolVersion === "string") upstream.setProtocolVersion?.(result.protocolVersion);
100
+ void downstream.send(msg);
101
+ };
102
+ upstream.onerror = (e) => ctx.err(`hivefloor mcp: ${e.message}
103
+ `);
104
+ downstream.onerror = (e) => ctx.err(`hivefloor mcp: ${e.message}
105
+ `);
106
+ downstream.onclose = () => finish(0);
107
+ io.stdin.once("end", () => finish(0));
108
+ io.stdin.once("close", () => finish(0));
109
+ upstream.onclose = () => finish(0);
110
+ upstream.start().then(() => downstream.start()).catch((e) => {
111
+ ctx.err(`hivefloor mcp: ${e.message}
112
+ `);
113
+ finish(1);
114
+ });
115
+ });
116
+ }
117
+ export {
118
+ contextHeaders,
119
+ mcpTargetUrl,
120
+ runMcpProxy
121
+ };