@maind-dev/mcp-bridge 0.30.0 → 0.33.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.
- package/build/index.js +825 -43
- package/build/meta.json +1 -1
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -202,7 +202,7 @@ var BRIDGE_VERSION;
|
|
|
202
202
|
var init_version = __esm({
|
|
203
203
|
"src/version.ts"() {
|
|
204
204
|
"use strict";
|
|
205
|
-
BRIDGE_VERSION = "0.
|
|
205
|
+
BRIDGE_VERSION = "0.33.0";
|
|
206
206
|
}
|
|
207
207
|
});
|
|
208
208
|
|
|
@@ -841,6 +841,12 @@ function readBridgeConfig() {
|
|
|
841
841
|
if (typeof o.guard_stop_gate === "boolean") {
|
|
842
842
|
config.guard_stop_gate = o.guard_stop_gate;
|
|
843
843
|
}
|
|
844
|
+
if (o.gateway_mode === "off" || o.gateway_mode === "warn" || o.gateway_mode === "enforce") {
|
|
845
|
+
config.gateway_mode = o.gateway_mode;
|
|
846
|
+
}
|
|
847
|
+
if (typeof o.gateway_attest === "boolean") {
|
|
848
|
+
config.gateway_attest = o.gateway_attest;
|
|
849
|
+
}
|
|
844
850
|
return config;
|
|
845
851
|
} catch {
|
|
846
852
|
return null;
|
|
@@ -5147,6 +5153,9 @@ var init_cli = __esm({
|
|
|
5147
5153
|
|
|
5148
5154
|
// src/guard/mcp-client.ts
|
|
5149
5155
|
async function fetchSessionBriefing(opts) {
|
|
5156
|
+
return fetchToolText("get_session_briefing", { tools: ["claude-code"] }, opts);
|
|
5157
|
+
}
|
|
5158
|
+
async function fetchToolText(toolName, toolArgs, opts) {
|
|
5150
5159
|
const mcpUrl = opts.mcpUrl ?? process.env.MAIND_MCP_URL ?? DEFAULT_MCP_URL3;
|
|
5151
5160
|
const controller = new AbortController();
|
|
5152
5161
|
const timer = setTimeout(() => controller.abort(), opts.timeoutMs ?? 3e3);
|
|
@@ -5155,8 +5164,8 @@ async function fetchSessionBriefing(opts) {
|
|
|
5155
5164
|
"content-type": "application/json",
|
|
5156
5165
|
accept: "application/json, text/event-stream",
|
|
5157
5166
|
authorization: `Bearer ${opts.apiKey}`,
|
|
5158
|
-
//
|
|
5159
|
-
"x-maind-client-family": "claude-code",
|
|
5167
|
+
// Attribution: default claude-code (guard), überschreibbar (gateway).
|
|
5168
|
+
"x-maind-client-family": opts.clientFamily ?? "claude-code",
|
|
5160
5169
|
"x-maind-client-version": opts.bridgeVersion,
|
|
5161
5170
|
"x-maind-bridge-version": opts.bridgeVersion
|
|
5162
5171
|
};
|
|
@@ -5196,8 +5205,8 @@ async function fetchSessionBriefing(opts) {
|
|
|
5196
5205
|
id: "guard-briefing",
|
|
5197
5206
|
method: "tools/call",
|
|
5198
5207
|
params: {
|
|
5199
|
-
name:
|
|
5200
|
-
arguments:
|
|
5208
|
+
name: toolName,
|
|
5209
|
+
arguments: toolArgs
|
|
5201
5210
|
}
|
|
5202
5211
|
})
|
|
5203
5212
|
});
|
|
@@ -5588,7 +5597,7 @@ function parseFrame(line) {
|
|
|
5588
5597
|
}
|
|
5589
5598
|
return { kind: "opaque" };
|
|
5590
5599
|
}
|
|
5591
|
-
function denyResult(id, toolName) {
|
|
5600
|
+
function denyResult(id, toolName, reason) {
|
|
5592
5601
|
return JSON.stringify({
|
|
5593
5602
|
jsonrpc: "2.0",
|
|
5594
5603
|
id,
|
|
@@ -5596,7 +5605,7 @@ function denyResult(id, toolName) {
|
|
|
5596
5605
|
content: [
|
|
5597
5606
|
{
|
|
5598
5607
|
type: "text",
|
|
5599
|
-
text: `maind gateway: tool '${toolName}' is denied by local gateway policy. The call was not forwarded to the server. Remove the --deny-tool flag (or adjust the gateway policy) to allow it.`
|
|
5608
|
+
text: reason ?? `maind gateway: tool '${toolName}' is denied by local gateway policy. The call was not forwarded to the server. Remove the --deny-tool flag (or adjust the gateway policy) to allow it.`
|
|
5600
5609
|
}
|
|
5601
5610
|
],
|
|
5602
5611
|
isError: true
|
|
@@ -5606,12 +5615,686 @@ function denyResult(id, toolName) {
|
|
|
5606
5615
|
function toolEvent(event, server, tool, at) {
|
|
5607
5616
|
return { at, event, server, tool };
|
|
5608
5617
|
}
|
|
5618
|
+
function policyEvent(event, server, tool, at, conventionId, conventionVersion) {
|
|
5619
|
+
return {
|
|
5620
|
+
at,
|
|
5621
|
+
event,
|
|
5622
|
+
server,
|
|
5623
|
+
tool,
|
|
5624
|
+
convention_id: conventionId,
|
|
5625
|
+
convention_version: conventionVersion
|
|
5626
|
+
};
|
|
5627
|
+
}
|
|
5609
5628
|
var init_frames = __esm({
|
|
5610
5629
|
"src/gateway/frames.ts"() {
|
|
5611
5630
|
"use strict";
|
|
5612
5631
|
}
|
|
5613
5632
|
});
|
|
5614
5633
|
|
|
5634
|
+
// src/gateway/attest.ts
|
|
5635
|
+
import { appendFile as appendFile2, mkdir as mkdir6, readFile as readFile5, rename as rename5, writeFile as writeFile6 } from "node:fs/promises";
|
|
5636
|
+
import { homedir as homedir8 } from "node:os";
|
|
5637
|
+
import { join as join21 } from "node:path";
|
|
5638
|
+
function aggregateWindow(events, mode, serverLabel, projectKey) {
|
|
5639
|
+
if (mode === "off" || events.length === 0) return null;
|
|
5640
|
+
const counts = {};
|
|
5641
|
+
const citations = /* @__PURE__ */ new Map();
|
|
5642
|
+
let min = Infinity;
|
|
5643
|
+
let max = -Infinity;
|
|
5644
|
+
for (const e of events) {
|
|
5645
|
+
if (e.at < min) min = e.at;
|
|
5646
|
+
if (e.at > max) max = e.at;
|
|
5647
|
+
const c = counts[e.tool] ??= { calls: 0, warns: 0, denies: 0 };
|
|
5648
|
+
if (e.event === "tool_call") c.calls += 1;
|
|
5649
|
+
else if (e.event === "policy_warn") c.warns += 1;
|
|
5650
|
+
else c.denies += 1;
|
|
5651
|
+
if ((e.event === "policy_warn" || e.event === "policy_deny") && e.convention_id !== void 0 && e.convention_version !== void 0) {
|
|
5652
|
+
const prev = citations.get(e.convention_id);
|
|
5653
|
+
if (prev === void 0 || e.convention_version > prev) {
|
|
5654
|
+
citations.set(e.convention_id, e.convention_version);
|
|
5655
|
+
}
|
|
5656
|
+
}
|
|
5657
|
+
}
|
|
5658
|
+
const toolEntries = Object.entries(counts).sort((a, b) => b[1].calls + b[1].warns + b[1].denies - (a[1].calls + a[1].warns + a[1].denies)).slice(0, 64);
|
|
5659
|
+
return {
|
|
5660
|
+
window_start: new Date(min).toISOString(),
|
|
5661
|
+
window_end: new Date(max).toISOString(),
|
|
5662
|
+
server_label: serverLabel,
|
|
5663
|
+
gateway_mode: mode,
|
|
5664
|
+
counts: Object.fromEntries(toolEntries),
|
|
5665
|
+
event_count: Math.min(events.length, 1e5),
|
|
5666
|
+
citations: [...citations.entries()].slice(0, 32).map(([convention_id, convention_version]) => ({ convention_id, convention_version })),
|
|
5667
|
+
...projectKey ? { project_key: projectKey } : {}
|
|
5668
|
+
};
|
|
5669
|
+
}
|
|
5670
|
+
function attestSpoolPath() {
|
|
5671
|
+
return join21(homedir8(), ".maind", "gateway", "attest-spool.jsonl");
|
|
5672
|
+
}
|
|
5673
|
+
async function postAttestation(payload) {
|
|
5674
|
+
const apiKey = readApiKey();
|
|
5675
|
+
if (apiKey === null) return false;
|
|
5676
|
+
const text = await fetchToolText("gateway_attest", { ...payload }, {
|
|
5677
|
+
apiKey,
|
|
5678
|
+
deviceId: ensureDeviceId(),
|
|
5679
|
+
projectKey: payload.project_key ?? null,
|
|
5680
|
+
bridgeVersion: BRIDGE_VERSION,
|
|
5681
|
+
clientFamily: "gateway",
|
|
5682
|
+
timeoutMs: 6e3
|
|
5683
|
+
});
|
|
5684
|
+
if (text === null) return false;
|
|
5685
|
+
try {
|
|
5686
|
+
const parsed = JSON.parse(text);
|
|
5687
|
+
return parsed.ok === true;
|
|
5688
|
+
} catch {
|
|
5689
|
+
return false;
|
|
5690
|
+
}
|
|
5691
|
+
}
|
|
5692
|
+
async function spoolAttestation(payload) {
|
|
5693
|
+
try {
|
|
5694
|
+
await mkdir6(join21(homedir8(), ".maind", "gateway"), { recursive: true });
|
|
5695
|
+
await appendFile2(attestSpoolPath(), JSON.stringify(payload) + "\n", { mode: 384 });
|
|
5696
|
+
await capSpool();
|
|
5697
|
+
} catch {
|
|
5698
|
+
}
|
|
5699
|
+
}
|
|
5700
|
+
async function capSpool() {
|
|
5701
|
+
try {
|
|
5702
|
+
const raw = await readFile5(attestSpoolPath(), "utf8");
|
|
5703
|
+
const lines2 = raw.split("\n").filter((l) => l.trim());
|
|
5704
|
+
if (lines2.length <= SPOOL_CAP) return;
|
|
5705
|
+
const tmp = `${attestSpoolPath()}.${process.pid}.tmp`;
|
|
5706
|
+
await writeFile6(tmp, lines2.slice(-SPOOL_CAP).join("\n") + "\n", { mode: 384 });
|
|
5707
|
+
await rename5(tmp, attestSpoolPath());
|
|
5708
|
+
} catch {
|
|
5709
|
+
}
|
|
5710
|
+
}
|
|
5711
|
+
async function flushAttestSpool() {
|
|
5712
|
+
let lines2;
|
|
5713
|
+
try {
|
|
5714
|
+
lines2 = (await readFile5(attestSpoolPath(), "utf8")).split("\n").filter((l) => l.trim());
|
|
5715
|
+
} catch {
|
|
5716
|
+
return;
|
|
5717
|
+
}
|
|
5718
|
+
if (lines2.length === 0) return;
|
|
5719
|
+
const remaining = [];
|
|
5720
|
+
let failed3 = false;
|
|
5721
|
+
for (const line of lines2) {
|
|
5722
|
+
if (failed3) {
|
|
5723
|
+
remaining.push(line);
|
|
5724
|
+
continue;
|
|
5725
|
+
}
|
|
5726
|
+
let payload = null;
|
|
5727
|
+
try {
|
|
5728
|
+
payload = JSON.parse(line);
|
|
5729
|
+
} catch {
|
|
5730
|
+
continue;
|
|
5731
|
+
}
|
|
5732
|
+
if (await postAttestation(payload)) continue;
|
|
5733
|
+
failed3 = true;
|
|
5734
|
+
remaining.push(line);
|
|
5735
|
+
}
|
|
5736
|
+
try {
|
|
5737
|
+
const tmp = `${attestSpoolPath()}.${process.pid}.tmp`;
|
|
5738
|
+
await writeFile6(tmp, remaining.length > 0 ? remaining.join("\n") + "\n" : "", { mode: 384 });
|
|
5739
|
+
await rename5(tmp, attestSpoolPath());
|
|
5740
|
+
} catch {
|
|
5741
|
+
}
|
|
5742
|
+
}
|
|
5743
|
+
var SPOOL_CAP;
|
|
5744
|
+
var init_attest = __esm({
|
|
5745
|
+
"src/gateway/attest.ts"() {
|
|
5746
|
+
"use strict";
|
|
5747
|
+
init_device_id();
|
|
5748
|
+
init_mcp_client();
|
|
5749
|
+
init_version();
|
|
5750
|
+
SPOOL_CAP = 256;
|
|
5751
|
+
}
|
|
5752
|
+
});
|
|
5753
|
+
|
|
5754
|
+
// src/gateway/config.ts
|
|
5755
|
+
function resolveGatewayMode(env = process.env, cfg = readBridgeConfig()) {
|
|
5756
|
+
const fromEnv = env.MAIND_GATEWAY;
|
|
5757
|
+
if (fromEnv === "off" || fromEnv === "warn" || fromEnv === "enforce") return fromEnv;
|
|
5758
|
+
return cfg?.gateway_mode ?? "warn";
|
|
5759
|
+
}
|
|
5760
|
+
function resolveGatewayAttest(env = process.env, cfg = readBridgeConfig()) {
|
|
5761
|
+
const fromEnv = env.MAIND_GATEWAY_ATTEST;
|
|
5762
|
+
if (fromEnv === "1") return true;
|
|
5763
|
+
if (fromEnv === "0") return false;
|
|
5764
|
+
return cfg?.gateway_attest === true;
|
|
5765
|
+
}
|
|
5766
|
+
async function writeGatewayConfigPatch(patch, configPath = bridgeConfigPath()) {
|
|
5767
|
+
let existing = {};
|
|
5768
|
+
try {
|
|
5769
|
+
const raw = await readFileSafe(configPath);
|
|
5770
|
+
if (raw !== null && raw.trim().length > 0) {
|
|
5771
|
+
const parsed = JSON.parse(raw);
|
|
5772
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
5773
|
+
existing = parsed;
|
|
5774
|
+
}
|
|
5775
|
+
}
|
|
5776
|
+
} catch {
|
|
5777
|
+
}
|
|
5778
|
+
const next = { ...existing, ...patch };
|
|
5779
|
+
await atomicWriteFile(configPath, JSON.stringify(next, null, 2) + "\n", 384);
|
|
5780
|
+
}
|
|
5781
|
+
async function writeGatewayMode(mode, configPath = bridgeConfigPath()) {
|
|
5782
|
+
await writeGatewayConfigPatch({ gateway_mode: mode }, configPath);
|
|
5783
|
+
}
|
|
5784
|
+
async function writeGatewayAttest(enabled, configPath = bridgeConfigPath()) {
|
|
5785
|
+
await writeGatewayConfigPatch({ gateway_attest: enabled }, configPath);
|
|
5786
|
+
}
|
|
5787
|
+
var init_config2 = __esm({
|
|
5788
|
+
"src/gateway/config.ts"() {
|
|
5789
|
+
"use strict";
|
|
5790
|
+
init_bridge_config();
|
|
5791
|
+
init_atomic_write();
|
|
5792
|
+
}
|
|
5793
|
+
});
|
|
5794
|
+
|
|
5795
|
+
// src/gateway/policy.ts
|
|
5796
|
+
function extractGatewayRules(payload) {
|
|
5797
|
+
if (typeof payload !== "object" || payload === null) return [];
|
|
5798
|
+
const list = payload.conventions;
|
|
5799
|
+
if (!Array.isArray(list)) return [];
|
|
5800
|
+
const rules = [];
|
|
5801
|
+
for (const row of list) {
|
|
5802
|
+
if (typeof row !== "object" || row === null) continue;
|
|
5803
|
+
const c = row;
|
|
5804
|
+
const gate = c.gateway_gate;
|
|
5805
|
+
if (typeof gate !== "object" || gate === null) continue;
|
|
5806
|
+
const g = gate;
|
|
5807
|
+
if (g.enabled !== true) continue;
|
|
5808
|
+
const id = typeof c.id === "string" && c.id.length > 0 && c.id.length <= 64 ? c.id : null;
|
|
5809
|
+
const version = typeof c.version === "number" && Number.isInteger(c.version) && c.version >= 1 ? c.version : null;
|
|
5810
|
+
if (id === null || version === null) continue;
|
|
5811
|
+
const severity = typeof c.severity === "string" && SEVERITIES.has(c.severity) ? c.severity : "recommended";
|
|
5812
|
+
const summary = typeof c.summary === "string" ? c.summary : "";
|
|
5813
|
+
const servers = Array.isArray(g.servers) ? g.servers.filter((s) => typeof s === "string" && s.length > 0) : null;
|
|
5814
|
+
if (servers !== null && servers.length === 0) continue;
|
|
5815
|
+
const tools = Array.isArray(g.deny_tools) ? g.deny_tools.filter((t) => typeof t === "string" && t.length > 0) : [];
|
|
5816
|
+
for (const tool of tools) {
|
|
5817
|
+
if (rules.length >= MAX_RULES) return rules;
|
|
5818
|
+
rules.push({ tool, servers, conventionId: id, conventionVersion: version, severity, summary });
|
|
5819
|
+
}
|
|
5820
|
+
}
|
|
5821
|
+
return rules;
|
|
5822
|
+
}
|
|
5823
|
+
function ruleMatches(rule, tool, serverLabel) {
|
|
5824
|
+
if (rule.tool !== tool) return false;
|
|
5825
|
+
if (rule.servers === null) return true;
|
|
5826
|
+
return rule.servers.includes("*") || rule.servers.includes(serverLabel);
|
|
5827
|
+
}
|
|
5828
|
+
function decideToolCall(tool, serverLabel, rules, mode) {
|
|
5829
|
+
if (mode === "off") return { kind: "allow" };
|
|
5830
|
+
let best = null;
|
|
5831
|
+
for (const r of rules) {
|
|
5832
|
+
if (!ruleMatches(r, tool, serverLabel)) continue;
|
|
5833
|
+
if (best === null || SEVERITY_RANK[r.severity] > SEVERITY_RANK[best.severity]) best = r;
|
|
5834
|
+
}
|
|
5835
|
+
if (best === null) return { kind: "allow" };
|
|
5836
|
+
if (best.severity === "required" && mode === "enforce") return { kind: "deny", rule: best };
|
|
5837
|
+
return { kind: "warn", rule: best };
|
|
5838
|
+
}
|
|
5839
|
+
function policyDenyText(tool, rule) {
|
|
5840
|
+
const cite = `${rule.conventionId} v${rule.conventionVersion}`;
|
|
5841
|
+
const summary = rule.summary ? ` ("${rule.summary}")` : "";
|
|
5842
|
+
return `maind gateway: tool '${tool}' is denied by convention ${cite}${summary}. The call was not forwarded to the server. Exits: ask the user to run 'maind-mcp-bridge gateway mode warn' (observe instead of enforce), or to disable the convention for themselves (set_convention_pref) if it is not mandatory.`;
|
|
5843
|
+
}
|
|
5844
|
+
var MAX_RULES, SEVERITIES, SEVERITY_RANK;
|
|
5845
|
+
var init_policy = __esm({
|
|
5846
|
+
"src/gateway/policy.ts"() {
|
|
5847
|
+
"use strict";
|
|
5848
|
+
MAX_RULES = 256;
|
|
5849
|
+
SEVERITIES = /* @__PURE__ */ new Set(["required", "recommended", "info"]);
|
|
5850
|
+
SEVERITY_RANK = {
|
|
5851
|
+
required: 2,
|
|
5852
|
+
recommended: 1,
|
|
5853
|
+
info: 0
|
|
5854
|
+
};
|
|
5855
|
+
}
|
|
5856
|
+
});
|
|
5857
|
+
|
|
5858
|
+
// src/gateway/policy-cache.ts
|
|
5859
|
+
import { mkdirSync as mkdirSync5, readFileSync as readFileSync10, renameSync as renameSync3, writeFileSync as writeFileSync4 } from "node:fs";
|
|
5860
|
+
import { homedir as homedir9 } from "node:os";
|
|
5861
|
+
import { dirname as dirname7, join as join22 } from "node:path";
|
|
5862
|
+
function policyCachePath() {
|
|
5863
|
+
return join22(homedir9(), ".maind", "gateway", "policy-cache.json");
|
|
5864
|
+
}
|
|
5865
|
+
function writePolicyCache(identity, rules, nowIso = (/* @__PURE__ */ new Date()).toISOString()) {
|
|
5866
|
+
try {
|
|
5867
|
+
const payload = {
|
|
5868
|
+
version: CACHE_VERSION2,
|
|
5869
|
+
cached_at: nowIso,
|
|
5870
|
+
mcp_url: identity.mcpUrl,
|
|
5871
|
+
key_fp: identity.keyFp,
|
|
5872
|
+
rules
|
|
5873
|
+
};
|
|
5874
|
+
const target = policyCachePath();
|
|
5875
|
+
mkdirSync5(dirname7(target), { recursive: true });
|
|
5876
|
+
const tmp = `${target}.${process.pid}.tmp`;
|
|
5877
|
+
writeFileSync4(tmp, JSON.stringify(payload, null, 2), { encoding: "utf8", mode: 384 });
|
|
5878
|
+
renameSync3(tmp, target);
|
|
5879
|
+
} catch {
|
|
5880
|
+
}
|
|
5881
|
+
}
|
|
5882
|
+
function readPolicyCache(identity) {
|
|
5883
|
+
try {
|
|
5884
|
+
const raw = readFileSync10(policyCachePath(), "utf8");
|
|
5885
|
+
const parsed = JSON.parse(raw);
|
|
5886
|
+
if (parsed.version !== CACHE_VERSION2 || parsed.mcp_url !== identity.mcpUrl || parsed.key_fp !== identity.keyFp || typeof parsed.cached_at !== "string" || !Array.isArray(parsed.rules)) {
|
|
5887
|
+
return null;
|
|
5888
|
+
}
|
|
5889
|
+
return {
|
|
5890
|
+
version: CACHE_VERSION2,
|
|
5891
|
+
cached_at: parsed.cached_at,
|
|
5892
|
+
mcp_url: parsed.mcp_url,
|
|
5893
|
+
key_fp: parsed.key_fp,
|
|
5894
|
+
rules: parsed.rules
|
|
5895
|
+
};
|
|
5896
|
+
} catch {
|
|
5897
|
+
return null;
|
|
5898
|
+
}
|
|
5899
|
+
}
|
|
5900
|
+
var CACHE_VERSION2;
|
|
5901
|
+
var init_policy_cache = __esm({
|
|
5902
|
+
"src/gateway/policy-cache.ts"() {
|
|
5903
|
+
"use strict";
|
|
5904
|
+
CACHE_VERSION2 = 1;
|
|
5905
|
+
}
|
|
5906
|
+
});
|
|
5907
|
+
|
|
5908
|
+
// src/gateway/policy-fetch.ts
|
|
5909
|
+
function policyIdentity(env = process.env) {
|
|
5910
|
+
const mcpUrl = env.MAIND_MCP_URL ?? DEFAULT_MCP_URL3;
|
|
5911
|
+
const apiKey = readApiKey();
|
|
5912
|
+
return { mcpUrl, keyFp: apiKey ? keyFingerprint(apiKey) : "anon" };
|
|
5913
|
+
}
|
|
5914
|
+
async function fetchGatewayRules(timeoutMs = 6e3) {
|
|
5915
|
+
const apiKey = readApiKey();
|
|
5916
|
+
if (apiKey === null) return null;
|
|
5917
|
+
let projectKey = null;
|
|
5918
|
+
try {
|
|
5919
|
+
projectKey = computeProjectKey();
|
|
5920
|
+
} catch {
|
|
5921
|
+
projectKey = null;
|
|
5922
|
+
}
|
|
5923
|
+
const text = await fetchToolText(
|
|
5924
|
+
"get_conventions",
|
|
5925
|
+
{},
|
|
5926
|
+
{
|
|
5927
|
+
apiKey,
|
|
5928
|
+
deviceId: ensureDeviceId(),
|
|
5929
|
+
projectKey,
|
|
5930
|
+
bridgeVersion: BRIDGE_VERSION,
|
|
5931
|
+
clientFamily: "gateway",
|
|
5932
|
+
timeoutMs
|
|
5933
|
+
}
|
|
5934
|
+
);
|
|
5935
|
+
if (text === null) return null;
|
|
5936
|
+
let payload;
|
|
5937
|
+
try {
|
|
5938
|
+
payload = JSON.parse(text);
|
|
5939
|
+
} catch {
|
|
5940
|
+
return null;
|
|
5941
|
+
}
|
|
5942
|
+
return extractGatewayRules(payload);
|
|
5943
|
+
}
|
|
5944
|
+
var init_policy_fetch = __esm({
|
|
5945
|
+
"src/gateway/policy-fetch.ts"() {
|
|
5946
|
+
"use strict";
|
|
5947
|
+
init_conventions_cache();
|
|
5948
|
+
init_device_id();
|
|
5949
|
+
init_mcp_client();
|
|
5950
|
+
init_project_key();
|
|
5951
|
+
init_version();
|
|
5952
|
+
init_policy();
|
|
5953
|
+
}
|
|
5954
|
+
});
|
|
5955
|
+
|
|
5956
|
+
// src/gateway/wrap.ts
|
|
5957
|
+
function isGatewayWrapped(entry) {
|
|
5958
|
+
const args = Array.isArray(entry.args) ? entry.args.map(String) : [];
|
|
5959
|
+
const k = args.indexOf("gateway");
|
|
5960
|
+
if (k === -1) return false;
|
|
5961
|
+
if (args.indexOf("--", k + 1) === -1) return false;
|
|
5962
|
+
if (k === 0) return true;
|
|
5963
|
+
const prefix = [String(entry.command ?? ""), ...args.slice(0, k)].join(" ");
|
|
5964
|
+
return /maind-mcp-bridge|@maind-dev\/mcp-bridge|\.js\s*$/.test(prefix);
|
|
5965
|
+
}
|
|
5966
|
+
function isMaindProxyEntry(entry) {
|
|
5967
|
+
if (isGatewayWrapped(entry)) return false;
|
|
5968
|
+
const cmd = typeof entry.command === "string" ? entry.command : "";
|
|
5969
|
+
const args = Array.isArray(entry.args) ? entry.args : [];
|
|
5970
|
+
const all = [cmd, ...args.map(String)].join(" ");
|
|
5971
|
+
return all.includes("maind-mcp-bridge") || /@maind-dev\/mcp-bridge/.test(all);
|
|
5972
|
+
}
|
|
5973
|
+
function classifyServer(entry) {
|
|
5974
|
+
if (typeof entry.command !== "string" || entry.command.length === 0) {
|
|
5975
|
+
return { action: "skip", reason: "not-stdio" };
|
|
5976
|
+
}
|
|
5977
|
+
if (typeof entry.url === "string") {
|
|
5978
|
+
return { action: "skip", reason: "not-stdio" };
|
|
5979
|
+
}
|
|
5980
|
+
if (isGatewayWrapped(entry)) {
|
|
5981
|
+
return { action: "skip", reason: "already-wrapped" };
|
|
5982
|
+
}
|
|
5983
|
+
if (isMaindProxyEntry(entry)) {
|
|
5984
|
+
return { action: "skip", reason: "maind-proxy" };
|
|
5985
|
+
}
|
|
5986
|
+
return { action: "wrap" };
|
|
5987
|
+
}
|
|
5988
|
+
function wrapServerEntry(entry, launch, label) {
|
|
5989
|
+
const origCommand = String(entry.command);
|
|
5990
|
+
const origArgs = Array.isArray(entry.args) ? entry.args.map(String) : [];
|
|
5991
|
+
const origEnv = typeof entry.env === "object" && entry.env !== null ? entry.env : void 0;
|
|
5992
|
+
const mergedEnv = origEnv || launch.env ? { ...origEnv ?? {}, ...launch.env ?? {} } : void 0;
|
|
5993
|
+
const out2 = {
|
|
5994
|
+
...entry,
|
|
5995
|
+
command: launch.command,
|
|
5996
|
+
args: [...launch.args, "gateway", "--label", label, "--", origCommand, ...origArgs]
|
|
5997
|
+
};
|
|
5998
|
+
if (mergedEnv) out2.env = mergedEnv;
|
|
5999
|
+
return out2;
|
|
6000
|
+
}
|
|
6001
|
+
function unwrapServerEntry(entry) {
|
|
6002
|
+
if (!isGatewayWrapped(entry)) return null;
|
|
6003
|
+
const args = entry.args.map(String);
|
|
6004
|
+
const sep2 = args.indexOf("--", args.indexOf("gateway") + 1);
|
|
6005
|
+
if (sep2 === -1 || sep2 + 1 >= args.length) return null;
|
|
6006
|
+
const out2 = {
|
|
6007
|
+
...entry,
|
|
6008
|
+
command: args[sep2 + 1],
|
|
6009
|
+
args: args.slice(sep2 + 2)
|
|
6010
|
+
};
|
|
6011
|
+
return out2;
|
|
6012
|
+
}
|
|
6013
|
+
function planRewrites(servers) {
|
|
6014
|
+
return Object.entries(servers).map(([key, entry]) => {
|
|
6015
|
+
const c = classifyServer(entry);
|
|
6016
|
+
return c.action === "wrap" ? { key, action: "wrap" } : { key, action: "skip", reason: c.reason };
|
|
6017
|
+
});
|
|
6018
|
+
}
|
|
6019
|
+
var init_wrap = __esm({
|
|
6020
|
+
"src/gateway/wrap.ts"() {
|
|
6021
|
+
"use strict";
|
|
6022
|
+
}
|
|
6023
|
+
});
|
|
6024
|
+
|
|
6025
|
+
// src/gateway/install.ts
|
|
6026
|
+
var install_exports = {};
|
|
6027
|
+
__export(install_exports, {
|
|
6028
|
+
runGatewayInstall: () => runGatewayInstall,
|
|
6029
|
+
runGatewayUninstall: () => runGatewayUninstall
|
|
6030
|
+
});
|
|
6031
|
+
import { existsSync as existsSync15, readFileSync as readFileSync11 } from "node:fs";
|
|
6032
|
+
import { homedir as homedir10 } from "node:os";
|
|
6033
|
+
import { join as join23 } from "node:path";
|
|
6034
|
+
import { createInterface as createInterface8 } from "node:readline";
|
|
6035
|
+
function parseFlags(argv) {
|
|
6036
|
+
const f = { yes: false, dryRun: false };
|
|
6037
|
+
for (const a of argv) {
|
|
6038
|
+
if (a === "--yes") f.yes = true;
|
|
6039
|
+
else if (a === "--dry-run") f.dryRun = true;
|
|
6040
|
+
else return null;
|
|
6041
|
+
}
|
|
6042
|
+
return f;
|
|
6043
|
+
}
|
|
6044
|
+
function targets() {
|
|
6045
|
+
return [
|
|
6046
|
+
{ label: "Claude Code (project .mcp.json)", path: join23(process.cwd(), ".mcp.json") },
|
|
6047
|
+
{ label: "Claude Code (user ~/.claude.json)", path: join23(homedir10(), ".claude.json") }
|
|
6048
|
+
];
|
|
6049
|
+
}
|
|
6050
|
+
async function confirm(question) {
|
|
6051
|
+
const rl = createInterface8({ input: process.stdin, output: process.stderr });
|
|
6052
|
+
try {
|
|
6053
|
+
const answer = await new Promise(
|
|
6054
|
+
(resolve3) => rl.question(`${question} [y/N] `, resolve3)
|
|
6055
|
+
);
|
|
6056
|
+
return answer.trim().toLowerCase() === "y" || answer.trim().toLowerCase() === "yes";
|
|
6057
|
+
} finally {
|
|
6058
|
+
rl.close();
|
|
6059
|
+
}
|
|
6060
|
+
}
|
|
6061
|
+
function loadConfig(path8) {
|
|
6062
|
+
const text = readFileSafeSync(path8);
|
|
6063
|
+
if (text === null) return null;
|
|
6064
|
+
try {
|
|
6065
|
+
const raw = JSON.parse(text);
|
|
6066
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return null;
|
|
6067
|
+
const obj = raw;
|
|
6068
|
+
const servers = obj.mcpServers;
|
|
6069
|
+
if (typeof servers !== "object" || servers === null || Array.isArray(servers)) return null;
|
|
6070
|
+
return { raw: obj, servers };
|
|
6071
|
+
} catch {
|
|
6072
|
+
return null;
|
|
6073
|
+
}
|
|
6074
|
+
}
|
|
6075
|
+
function readFileSafeSync(path8) {
|
|
6076
|
+
try {
|
|
6077
|
+
if (!existsSync15(path8)) return null;
|
|
6078
|
+
return readFileSync11(path8, "utf8");
|
|
6079
|
+
} catch {
|
|
6080
|
+
return null;
|
|
6081
|
+
}
|
|
6082
|
+
}
|
|
6083
|
+
async function runGatewayInstall(argv) {
|
|
6084
|
+
const flags = parseFlags(argv);
|
|
6085
|
+
if (flags === null) {
|
|
6086
|
+
process.stderr.write("usage: maind-mcp-bridge gateway install [--yes] [--dry-run]\n");
|
|
6087
|
+
return 2;
|
|
6088
|
+
}
|
|
6089
|
+
const launch = resolveBridgeLaunch({
|
|
6090
|
+
entryPath: currentBridgeEntry(),
|
|
6091
|
+
nodePath: process.execPath,
|
|
6092
|
+
isElectron: process.versions.electron !== void 0
|
|
6093
|
+
});
|
|
6094
|
+
let wrapped = 0;
|
|
6095
|
+
for (const t of targets()) {
|
|
6096
|
+
const cfg = loadConfig(t.path);
|
|
6097
|
+
if (cfg === null) {
|
|
6098
|
+
process.stderr.write(` skipped ${t.label} \u2014 no mcpServers object
|
|
6099
|
+
`);
|
|
6100
|
+
continue;
|
|
6101
|
+
}
|
|
6102
|
+
const plan = planRewrites(cfg.servers);
|
|
6103
|
+
for (const row of plan) {
|
|
6104
|
+
if (row.action === "skip") {
|
|
6105
|
+
process.stderr.write(` skipped ${t.label} \xB7 ${row.key} \u2014 ${row.reason}
|
|
6106
|
+
`);
|
|
6107
|
+
continue;
|
|
6108
|
+
}
|
|
6109
|
+
const ok = flags.yes || await confirm(
|
|
6110
|
+
`Wrap MCP server '${row.key}' in ${t.label} with the maind gateway?
|
|
6111
|
+
Tool-call NAMES (never arguments) will be logged to ~/.maind/gateway/events.jsonl.
|
|
6112
|
+
The original launch command stays inside the entry and 'gateway uninstall' restores it.`
|
|
6113
|
+
);
|
|
6114
|
+
if (!ok) {
|
|
6115
|
+
process.stderr.write(` declined ${t.label} \xB7 ${row.key}
|
|
6116
|
+
`);
|
|
6117
|
+
continue;
|
|
6118
|
+
}
|
|
6119
|
+
cfg.servers[row.key] = wrapServerEntry(cfg.servers[row.key], launch, row.key);
|
|
6120
|
+
wrapped += 1;
|
|
6121
|
+
process.stderr.write(
|
|
6122
|
+
` ${flags.dryRun ? "would wrap" : "wrapped"} ${t.label} \xB7 ${row.key}
|
|
6123
|
+
`
|
|
6124
|
+
);
|
|
6125
|
+
}
|
|
6126
|
+
if (!flags.dryRun && plan.some((r) => r.action === "wrap")) {
|
|
6127
|
+
await atomicWriteFile(t.path, JSON.stringify(cfg.raw, null, 2) + "\n", 384);
|
|
6128
|
+
}
|
|
6129
|
+
}
|
|
6130
|
+
process.stderr.write(
|
|
6131
|
+
wrapped > 0 ? `
|
|
6132
|
+
${flags.dryRun ? "Dry-run \u2014 nothing written. " : ""}Takes effect for NEW sessions (MCP servers spawn at session start).
|
|
6133
|
+
` : "\nNothing to wrap.\n"
|
|
6134
|
+
);
|
|
6135
|
+
return 0;
|
|
6136
|
+
}
|
|
6137
|
+
async function runGatewayUninstall(argv) {
|
|
6138
|
+
const flags = parseFlags(argv);
|
|
6139
|
+
if (flags === null) {
|
|
6140
|
+
process.stderr.write("usage: maind-mcp-bridge gateway uninstall [--yes] [--dry-run]\n");
|
|
6141
|
+
return 2;
|
|
6142
|
+
}
|
|
6143
|
+
let unwrapped = 0;
|
|
6144
|
+
for (const t of targets()) {
|
|
6145
|
+
const cfg = loadConfig(t.path);
|
|
6146
|
+
if (cfg === null) continue;
|
|
6147
|
+
let changed = false;
|
|
6148
|
+
for (const [key, entry] of Object.entries(cfg.servers)) {
|
|
6149
|
+
if (!isGatewayWrapped(entry)) continue;
|
|
6150
|
+
const ok = flags.yes || await confirm(`Unwrap MCP server '${key}' in ${t.label}?`);
|
|
6151
|
+
if (!ok) continue;
|
|
6152
|
+
const restored = unwrapServerEntry(entry);
|
|
6153
|
+
if (restored === null) {
|
|
6154
|
+
process.stderr.write(` BROKEN ${t.label} \xB7 ${key} \u2014 wrapped shape unrecognized, left untouched
|
|
6155
|
+
`);
|
|
6156
|
+
continue;
|
|
6157
|
+
}
|
|
6158
|
+
cfg.servers[key] = restored;
|
|
6159
|
+
changed = true;
|
|
6160
|
+
unwrapped += 1;
|
|
6161
|
+
process.stderr.write(` ${flags.dryRun ? "would unwrap" : "unwrapped"} ${t.label} \xB7 ${key}
|
|
6162
|
+
`);
|
|
6163
|
+
}
|
|
6164
|
+
if (!flags.dryRun && changed) {
|
|
6165
|
+
await atomicWriteFile(t.path, JSON.stringify(cfg.raw, null, 2) + "\n", 384);
|
|
6166
|
+
}
|
|
6167
|
+
}
|
|
6168
|
+
process.stderr.write(
|
|
6169
|
+
unwrapped > 0 ? `
|
|
6170
|
+
${flags.dryRun ? "Dry-run \u2014 nothing written. " : ""}Takes effect for NEW sessions.
|
|
6171
|
+
` : "\nNothing wrapped \u2014 nothing to restore.\n"
|
|
6172
|
+
);
|
|
6173
|
+
return 0;
|
|
6174
|
+
}
|
|
6175
|
+
var init_install2 = __esm({
|
|
6176
|
+
"src/gateway/install.ts"() {
|
|
6177
|
+
"use strict";
|
|
6178
|
+
init_atomic_write();
|
|
6179
|
+
init_bridge_entry();
|
|
6180
|
+
init_wrap();
|
|
6181
|
+
}
|
|
6182
|
+
});
|
|
6183
|
+
|
|
6184
|
+
// src/gateway/policy-cli.ts
|
|
6185
|
+
var policy_cli_exports = {};
|
|
6186
|
+
__export(policy_cli_exports, {
|
|
6187
|
+
runGatewayAttest: () => runGatewayAttest,
|
|
6188
|
+
runGatewayMode: () => runGatewayMode,
|
|
6189
|
+
runGatewayPolicy: () => runGatewayPolicy
|
|
6190
|
+
});
|
|
6191
|
+
async function runGatewayMode(argv) {
|
|
6192
|
+
const arg = argv[0];
|
|
6193
|
+
if (arg === void 0) {
|
|
6194
|
+
const mode = resolveGatewayMode();
|
|
6195
|
+
const fromEnv = process.env.MAIND_GATEWAY !== void 0;
|
|
6196
|
+
process.stdout.write(
|
|
6197
|
+
`maind: gateway mode is '${mode}'${fromEnv ? " (from MAIND_GATEWAY env)" : ""}
|
|
6198
|
+
`
|
|
6199
|
+
);
|
|
6200
|
+
return 0;
|
|
6201
|
+
}
|
|
6202
|
+
if (arg !== "off" && arg !== "warn" && arg !== "enforce") {
|
|
6203
|
+
process.stderr.write("usage: maind-mcp-bridge gateway mode [off|warn|enforce]\n");
|
|
6204
|
+
return 2;
|
|
6205
|
+
}
|
|
6206
|
+
await writeGatewayMode(arg);
|
|
6207
|
+
process.stdout.write(
|
|
6208
|
+
`maind: gateway mode set to '${arg}'. ` + (arg === "enforce" ? "Conventions with gateway_gate + severity=required now DENY matching tools.\n" : arg === "warn" ? "Convention matches are logged (policy_warn) and forwarded \u2014 nothing is denied.\n" : "Convention policy is off; only explicit --deny-tool flags apply.\n")
|
|
6209
|
+
);
|
|
6210
|
+
return 0;
|
|
6211
|
+
}
|
|
6212
|
+
async function runGatewayAttest(argv) {
|
|
6213
|
+
const arg = argv[0];
|
|
6214
|
+
if (arg === void 0) {
|
|
6215
|
+
const on = resolveGatewayAttest();
|
|
6216
|
+
const fromEnv = process.env.MAIND_GATEWAY_ATTEST !== void 0;
|
|
6217
|
+
process.stdout.write(
|
|
6218
|
+
`maind: gateway attestation reports are ${on ? "ON" : "OFF"}${fromEnv ? " (from MAIND_GATEWAY_ATTEST env)" : ""}.
|
|
6219
|
+
` + (on ? "" : "maind: enable with 'gateway attest on' \u2014 reports observation windows (per-tool call/warn/deny COUNTERS, tool NAMES, convention citations, server label) to your maind account. Never tool arguments, results, paths, or prompts.\n")
|
|
6220
|
+
);
|
|
6221
|
+
return 0;
|
|
6222
|
+
}
|
|
6223
|
+
if (arg !== "on" && arg !== "off") {
|
|
6224
|
+
process.stderr.write("usage: maind-mcp-bridge gateway attest [on|off]\n");
|
|
6225
|
+
return 2;
|
|
6226
|
+
}
|
|
6227
|
+
await writeGatewayAttest(arg === "on");
|
|
6228
|
+
process.stdout.write(
|
|
6229
|
+
arg === "on" ? "maind: attestation reports ON. Each gateway now reports observation windows (tool-name counters + convention citations \u2014 never arguments) to your maind account, at gateway end and every 30 min.\n" : "maind: attestation reports OFF. Events stay local (~/.maind/gateway/events.jsonl).\n"
|
|
6230
|
+
);
|
|
6231
|
+
return 0;
|
|
6232
|
+
}
|
|
6233
|
+
function renderRules(rules) {
|
|
6234
|
+
if (rules.length === 0) return " (no rules \u2014 no visible convention carries a gateway_gate)\n";
|
|
6235
|
+
let out2 = "";
|
|
6236
|
+
for (const r of rules) {
|
|
6237
|
+
const scope = r.servers === null ? "*" : r.servers.join(",");
|
|
6238
|
+
const grade = r.severity === "required" ? "deny-capable" : "warn-only";
|
|
6239
|
+
out2 += ` ${r.tool} [servers: ${scope}] ${r.conventionId} v${r.conventionVersion} (${r.severity} \u2192 ${grade})
|
|
6240
|
+
`;
|
|
6241
|
+
}
|
|
6242
|
+
return out2;
|
|
6243
|
+
}
|
|
6244
|
+
function cacheAge(cache, now = Date.now()) {
|
|
6245
|
+
const then = Date.parse(cache.cached_at);
|
|
6246
|
+
if (Number.isNaN(then)) return "unknown age";
|
|
6247
|
+
const mins = Math.floor((now - then) / 6e4);
|
|
6248
|
+
if (mins < 1) return "just now";
|
|
6249
|
+
if (mins < 60) return `${mins} min ago`;
|
|
6250
|
+
const hours = Math.floor(mins / 60);
|
|
6251
|
+
if (hours < 24) return `${hours} h ago`;
|
|
6252
|
+
return `${Math.floor(hours / 24)} d ago`;
|
|
6253
|
+
}
|
|
6254
|
+
async function runGatewayPolicy(argv) {
|
|
6255
|
+
const refresh = argv.includes("--refresh");
|
|
6256
|
+
const mode = resolveGatewayMode();
|
|
6257
|
+
const identity = policyIdentity();
|
|
6258
|
+
if (refresh) {
|
|
6259
|
+
const rules = await fetchGatewayRules();
|
|
6260
|
+
if (rules === null) {
|
|
6261
|
+
process.stderr.write(
|
|
6262
|
+
"maind: policy refresh failed (not paired, offline, or server error) \u2014 showing last cached state.\n"
|
|
6263
|
+
);
|
|
6264
|
+
} else {
|
|
6265
|
+
writePolicyCache(identity, rules);
|
|
6266
|
+
process.stdout.write(`maind: policy refreshed (${rules.length} rule(s)).
|
|
6267
|
+
`);
|
|
6268
|
+
}
|
|
6269
|
+
}
|
|
6270
|
+
const cache = readPolicyCache(identity);
|
|
6271
|
+
process.stdout.write(`maind: gateway mode '${mode}'
|
|
6272
|
+
`);
|
|
6273
|
+
if (cache === null) {
|
|
6274
|
+
process.stdout.write(
|
|
6275
|
+
"maind: no cached policy for this identity. Run 'gateway policy --refresh' while paired.\n"
|
|
6276
|
+
);
|
|
6277
|
+
return 0;
|
|
6278
|
+
}
|
|
6279
|
+
process.stdout.write(`maind: policy from cache (${cacheAge(cache)}, ${identity.mcpUrl}):
|
|
6280
|
+
`);
|
|
6281
|
+
process.stdout.write(renderRules(cache.rules));
|
|
6282
|
+
if (mode !== "enforce") {
|
|
6283
|
+
process.stdout.write(
|
|
6284
|
+
"maind: note \u2014 deny-capable rules only deny in mode 'enforce'; current mode logs them as policy_warn.\n"
|
|
6285
|
+
);
|
|
6286
|
+
}
|
|
6287
|
+
return 0;
|
|
6288
|
+
}
|
|
6289
|
+
var init_policy_cli = __esm({
|
|
6290
|
+
"src/gateway/policy-cli.ts"() {
|
|
6291
|
+
"use strict";
|
|
6292
|
+
init_config2();
|
|
6293
|
+
init_policy_cache();
|
|
6294
|
+
init_policy_fetch();
|
|
6295
|
+
}
|
|
6296
|
+
});
|
|
6297
|
+
|
|
5615
6298
|
// src/gateway/run.ts
|
|
5616
6299
|
var run_exports = {};
|
|
5617
6300
|
__export(run_exports, {
|
|
@@ -5620,23 +6303,24 @@ __export(run_exports, {
|
|
|
5620
6303
|
runGateway: () => runGateway
|
|
5621
6304
|
});
|
|
5622
6305
|
import { spawn as spawn2 } from "node:child_process";
|
|
5623
|
-
import { appendFile as
|
|
5624
|
-
import { homedir as
|
|
5625
|
-
import { basename as basename2, join as
|
|
5626
|
-
import { createInterface as
|
|
6306
|
+
import { appendFile as appendFile3, mkdir as mkdir7 } from "node:fs/promises";
|
|
6307
|
+
import { homedir as homedir11 } from "node:os";
|
|
6308
|
+
import { basename as basename2, join as join24 } from "node:path";
|
|
6309
|
+
import { createInterface as createInterface9 } from "node:readline";
|
|
5627
6310
|
function gatewayEventsFile() {
|
|
5628
|
-
return
|
|
6311
|
+
return join24(homedir11(), ".maind", "gateway", "events.jsonl");
|
|
5629
6312
|
}
|
|
5630
6313
|
async function logEvent(e) {
|
|
5631
6314
|
try {
|
|
5632
6315
|
const file = gatewayEventsFile();
|
|
5633
|
-
await
|
|
5634
|
-
await
|
|
6316
|
+
await mkdir7(join24(homedir11(), ".maind", "gateway"), { recursive: true });
|
|
6317
|
+
await appendFile3(file, JSON.stringify(e) + "\n", { mode: 384 });
|
|
5635
6318
|
} catch {
|
|
5636
6319
|
}
|
|
5637
6320
|
}
|
|
5638
6321
|
function parseGatewayArgs(argv) {
|
|
5639
6322
|
const denyTools = /* @__PURE__ */ new Set();
|
|
6323
|
+
let label = null;
|
|
5640
6324
|
let i = 0;
|
|
5641
6325
|
while (i < argv.length) {
|
|
5642
6326
|
const a = argv[i];
|
|
@@ -5647,25 +6331,84 @@ function parseGatewayArgs(argv) {
|
|
|
5647
6331
|
i += 2;
|
|
5648
6332
|
continue;
|
|
5649
6333
|
}
|
|
6334
|
+
if (a === "--label") {
|
|
6335
|
+
const v = argv[i + 1];
|
|
6336
|
+
if (!v) return null;
|
|
6337
|
+
label = v;
|
|
6338
|
+
i += 2;
|
|
6339
|
+
continue;
|
|
6340
|
+
}
|
|
5650
6341
|
if (a === "--") {
|
|
5651
6342
|
const command = argv[i + 1];
|
|
5652
6343
|
if (!command) return null;
|
|
5653
|
-
return { denyTools, command, args: argv.slice(i + 2) };
|
|
6344
|
+
return { denyTools, label, command, args: argv.slice(i + 2) };
|
|
5654
6345
|
}
|
|
5655
6346
|
return null;
|
|
5656
6347
|
}
|
|
5657
6348
|
return null;
|
|
5658
6349
|
}
|
|
5659
6350
|
async function runGateway(argv) {
|
|
6351
|
+
if (argv[0] === "install" || argv[0] === "uninstall") {
|
|
6352
|
+
const { runGatewayInstall: runGatewayInstall2, runGatewayUninstall: runGatewayUninstall2 } = await Promise.resolve().then(() => (init_install2(), install_exports));
|
|
6353
|
+
return argv[0] === "install" ? runGatewayInstall2(argv.slice(1)) : runGatewayUninstall2(argv.slice(1));
|
|
6354
|
+
}
|
|
6355
|
+
if (argv[0] === "mode" || argv[0] === "policy" || argv[0] === "attest") {
|
|
6356
|
+
const { runGatewayAttest: runGatewayAttest2, runGatewayMode: runGatewayMode2, runGatewayPolicy: runGatewayPolicy2 } = await Promise.resolve().then(() => (init_policy_cli(), policy_cli_exports));
|
|
6357
|
+
if (argv[0] === "mode") return runGatewayMode2(argv.slice(1));
|
|
6358
|
+
if (argv[0] === "attest") return runGatewayAttest2(argv.slice(1));
|
|
6359
|
+
return runGatewayPolicy2(argv.slice(1));
|
|
6360
|
+
}
|
|
5660
6361
|
const parsed = parseGatewayArgs(argv);
|
|
5661
6362
|
if (parsed === null) {
|
|
5662
6363
|
process.stderr.write(
|
|
5663
|
-
"usage: maind-mcp-bridge gateway [--deny-tool <name>]... -- <command> [args...]\nEXPERIMENTAL (gateway spike): wraps a stdio MCP server; logs tool-call\nNAMES to ~/.maind/gateway/events.jsonl and answers denied tools locally.\n"
|
|
6364
|
+
"usage: maind-mcp-bridge gateway [--deny-tool <name>]... [--label <name>] -- <command> [args...]\n maind-mcp-bridge gateway install [--yes] [--dry-run]\n maind-mcp-bridge gateway uninstall [--yes] [--dry-run]\n maind-mcp-bridge gateway mode [off|warn|enforce]\n maind-mcp-bridge gateway policy [--refresh]\n maind-mcp-bridge gateway attest [on|off]\nEXPERIMENTAL (gateway spike): wraps a stdio MCP server; logs tool-call\nNAMES to ~/.maind/gateway/events.jsonl and answers denied tools locally.\ninstall/uninstall rewrite Claude Code MCP configs (consent per server).\nmode/policy: convention-bound policy (Stufe 2b) \u2014 deny only in enforce.\n"
|
|
5664
6365
|
);
|
|
5665
6366
|
return 2;
|
|
5666
6367
|
}
|
|
5667
|
-
const { denyTools, command, args } = parsed;
|
|
5668
|
-
const serverLabel = basename2(command);
|
|
6368
|
+
const { denyTools, label, command, args } = parsed;
|
|
6369
|
+
const serverLabel = label ?? basename2(command);
|
|
6370
|
+
const mode = resolveGatewayMode();
|
|
6371
|
+
let policyRules = mode === "off" ? [] : readPolicyCache(policyIdentity())?.rules ?? [];
|
|
6372
|
+
if (mode !== "off") {
|
|
6373
|
+
void fetchGatewayRules().then((rules) => {
|
|
6374
|
+
if (rules !== null) {
|
|
6375
|
+
policyRules = rules;
|
|
6376
|
+
writePolicyCache(policyIdentity(), rules);
|
|
6377
|
+
}
|
|
6378
|
+
}).catch(() => {
|
|
6379
|
+
});
|
|
6380
|
+
}
|
|
6381
|
+
const windowEvents = [];
|
|
6382
|
+
const attested = resolveGatewayAttest() && readApiKey() !== null && mode !== "off";
|
|
6383
|
+
let observedProjectKey = null;
|
|
6384
|
+
try {
|
|
6385
|
+
observedProjectKey = computeProjectKey();
|
|
6386
|
+
} catch {
|
|
6387
|
+
observedProjectKey = null;
|
|
6388
|
+
}
|
|
6389
|
+
const record = (e) => {
|
|
6390
|
+
void logEvent(e);
|
|
6391
|
+
if (attested) windowEvents.push(e);
|
|
6392
|
+
};
|
|
6393
|
+
const attestAndReset = async () => {
|
|
6394
|
+
if (!attested) return;
|
|
6395
|
+
const payload = aggregateWindow(
|
|
6396
|
+
windowEvents.splice(0),
|
|
6397
|
+
mode,
|
|
6398
|
+
serverLabel,
|
|
6399
|
+
observedProjectKey
|
|
6400
|
+
);
|
|
6401
|
+
if (payload === null) return;
|
|
6402
|
+
if (!await postAttestation(payload)) await spoolAttestation(payload);
|
|
6403
|
+
};
|
|
6404
|
+
if (attested) {
|
|
6405
|
+
void flushAttestSpool().catch(() => {
|
|
6406
|
+
});
|
|
6407
|
+
}
|
|
6408
|
+
const attestTimer = setInterval(() => {
|
|
6409
|
+
void attestAndReset();
|
|
6410
|
+
}, 30 * 6e4);
|
|
6411
|
+
attestTimer.unref();
|
|
5669
6412
|
const child = spawn2(command, args, {
|
|
5670
6413
|
stdio: ["pipe", "pipe", "inherit"]
|
|
5671
6414
|
});
|
|
@@ -5675,32 +6418,71 @@ async function runGateway(argv) {
|
|
|
5675
6418
|
`);
|
|
5676
6419
|
process.exitCode = 1;
|
|
5677
6420
|
});
|
|
5678
|
-
const rl =
|
|
6421
|
+
const rl = createInterface9({ input: process.stdin, terminal: false });
|
|
5679
6422
|
rl.on("line", (line) => {
|
|
5680
6423
|
const frame = parseFrame(line);
|
|
5681
6424
|
if (frame.kind === "request" && frame.method === "tools/call" && frame.toolName) {
|
|
5682
6425
|
if (denyTools.has(frame.toolName) && frame.id !== void 0) {
|
|
5683
6426
|
process.stdout.write(denyResult(frame.id, frame.toolName) + "\n");
|
|
5684
|
-
|
|
6427
|
+
record(toolEvent("deny", serverLabel, frame.toolName, Date.now()));
|
|
6428
|
+
return;
|
|
6429
|
+
}
|
|
6430
|
+
const decision = decideToolCall(frame.toolName, serverLabel, policyRules, mode);
|
|
6431
|
+
if (decision.kind === "deny" && frame.id !== void 0) {
|
|
6432
|
+
process.stdout.write(
|
|
6433
|
+
denyResult(frame.id, frame.toolName, policyDenyText(frame.toolName, decision.rule)) + "\n"
|
|
6434
|
+
);
|
|
6435
|
+
record(
|
|
6436
|
+
policyEvent(
|
|
6437
|
+
"policy_deny",
|
|
6438
|
+
serverLabel,
|
|
6439
|
+
frame.toolName,
|
|
6440
|
+
Date.now(),
|
|
6441
|
+
decision.rule.conventionId,
|
|
6442
|
+
decision.rule.conventionVersion
|
|
6443
|
+
)
|
|
6444
|
+
);
|
|
5685
6445
|
return;
|
|
5686
6446
|
}
|
|
5687
|
-
|
|
6447
|
+
if (decision.kind === "warn" || decision.kind === "deny") {
|
|
6448
|
+
record(
|
|
6449
|
+
policyEvent(
|
|
6450
|
+
"policy_warn",
|
|
6451
|
+
serverLabel,
|
|
6452
|
+
frame.toolName,
|
|
6453
|
+
Date.now(),
|
|
6454
|
+
decision.rule.conventionId,
|
|
6455
|
+
decision.rule.conventionVersion
|
|
6456
|
+
)
|
|
6457
|
+
);
|
|
6458
|
+
}
|
|
6459
|
+
record(toolEvent("tool_call", serverLabel, frame.toolName, Date.now()));
|
|
5688
6460
|
}
|
|
5689
6461
|
child.stdin.write(line + "\n");
|
|
5690
6462
|
});
|
|
5691
6463
|
rl.on("close", () => {
|
|
5692
6464
|
child.stdin.end();
|
|
5693
6465
|
});
|
|
5694
|
-
|
|
6466
|
+
const exitCode = await new Promise((resolve3) => {
|
|
5695
6467
|
child.on("exit", (code, signal) => {
|
|
5696
6468
|
resolve3(code ?? (signal ? 1 : 0));
|
|
5697
6469
|
});
|
|
5698
6470
|
});
|
|
6471
|
+
clearInterval(attestTimer);
|
|
6472
|
+
await attestAndReset();
|
|
6473
|
+
return exitCode;
|
|
5699
6474
|
}
|
|
5700
6475
|
var init_run = __esm({
|
|
5701
6476
|
"src/gateway/run.ts"() {
|
|
5702
6477
|
"use strict";
|
|
5703
6478
|
init_frames();
|
|
6479
|
+
init_attest();
|
|
6480
|
+
init_config2();
|
|
6481
|
+
init_policy();
|
|
6482
|
+
init_policy_cache();
|
|
6483
|
+
init_policy_fetch();
|
|
6484
|
+
init_device_id();
|
|
6485
|
+
init_project_key();
|
|
5704
6486
|
}
|
|
5705
6487
|
});
|
|
5706
6488
|
|
|
@@ -5717,11 +6499,11 @@ __export(cross_session_exports, {
|
|
|
5717
6499
|
runGuardCrossObserve: () => runGuardCrossObserve,
|
|
5718
6500
|
staleReason: () => staleReason
|
|
5719
6501
|
});
|
|
5720
|
-
import { mkdir as
|
|
5721
|
-
import { homedir as
|
|
5722
|
-
import { join as
|
|
6502
|
+
import { mkdir as mkdir8, readFile as readFile6, readdir as readdir2, stat as stat3, unlink as unlink3, writeFile as writeFile7 } from "node:fs/promises";
|
|
6503
|
+
import { homedir as homedir12 } from "node:os";
|
|
6504
|
+
import { join as join25 } from "node:path";
|
|
5723
6505
|
function crossSessionDir() {
|
|
5724
|
-
return
|
|
6506
|
+
return join25(homedir12(), ".maind", "cross-session");
|
|
5725
6507
|
}
|
|
5726
6508
|
function parseSessionDb(raw) {
|
|
5727
6509
|
const empty = { files: {}, warned: {} };
|
|
@@ -5762,12 +6544,12 @@ function staleReason(path8, v) {
|
|
|
5762
6544
|
}
|
|
5763
6545
|
async function dbPath(sessionId) {
|
|
5764
6546
|
const dir = crossSessionDir();
|
|
5765
|
-
await
|
|
5766
|
-
return
|
|
6547
|
+
await mkdir8(dir, { recursive: true });
|
|
6548
|
+
return join25(dir, `${sessionId}.json`);
|
|
5767
6549
|
}
|
|
5768
6550
|
async function loadDb(sessionId, now) {
|
|
5769
6551
|
try {
|
|
5770
|
-
const raw = await
|
|
6552
|
+
const raw = await readFile6(await dbPath(sessionId), "utf8");
|
|
5771
6553
|
return pruneDb(parseSessionDb(JSON.parse(raw)), now);
|
|
5772
6554
|
} catch {
|
|
5773
6555
|
return { files: {}, warned: {} };
|
|
@@ -5775,7 +6557,7 @@ async function loadDb(sessionId, now) {
|
|
|
5775
6557
|
}
|
|
5776
6558
|
async function saveDb(sessionId, db) {
|
|
5777
6559
|
try {
|
|
5778
|
-
await
|
|
6560
|
+
await writeFile7(await dbPath(sessionId), JSON.stringify(db), { mode: 384 });
|
|
5779
6561
|
} catch {
|
|
5780
6562
|
}
|
|
5781
6563
|
}
|
|
@@ -5784,7 +6566,7 @@ async function sweepStaleSessionFiles(now) {
|
|
|
5784
6566
|
const dir = crossSessionDir();
|
|
5785
6567
|
for (const name of await readdir2(dir)) {
|
|
5786
6568
|
if (!name.endsWith(".json")) continue;
|
|
5787
|
-
const p =
|
|
6569
|
+
const p = join25(dir, name);
|
|
5788
6570
|
const s = await stat3(p).catch(() => null);
|
|
5789
6571
|
if (s && now - s.mtimeMs > TTL_MS) await unlink3(p).catch(() => {
|
|
5790
6572
|
});
|
|
@@ -6975,7 +7757,7 @@ function claudeDesktopConfigPath(ctx) {
|
|
|
6975
7757
|
}
|
|
6976
7758
|
}
|
|
6977
7759
|
function getClientTargets(ctx) {
|
|
6978
|
-
const
|
|
7760
|
+
const targets2 = [
|
|
6979
7761
|
{
|
|
6980
7762
|
key: "claude-code-project",
|
|
6981
7763
|
label: "Claude Code (project-scoped .mcp.json)",
|
|
@@ -7025,11 +7807,11 @@ function getClientTargets(ctx) {
|
|
|
7025
7807
|
kind: "toml"
|
|
7026
7808
|
}
|
|
7027
7809
|
];
|
|
7028
|
-
return
|
|
7810
|
+
return targets2;
|
|
7029
7811
|
}
|
|
7030
|
-
function filterTargetsByFamily(
|
|
7031
|
-
if (family === null) return
|
|
7032
|
-
return
|
|
7812
|
+
function filterTargetsByFamily(targets2, family) {
|
|
7813
|
+
if (family === null) return targets2;
|
|
7814
|
+
return targets2.filter((t) => t.clientFamilyEnv === family);
|
|
7033
7815
|
}
|
|
7034
7816
|
function makeDefaultContext(args) {
|
|
7035
7817
|
return {
|
|
@@ -8045,9 +8827,9 @@ function buildPreambleTargets(ctx) {
|
|
|
8045
8827
|
];
|
|
8046
8828
|
}
|
|
8047
8829
|
async function writePreambleFiles(ctx, dryRun = false) {
|
|
8048
|
-
const
|
|
8830
|
+
const targets2 = buildPreambleTargets(ctx);
|
|
8049
8831
|
const results = [];
|
|
8050
|
-
for (const t of
|
|
8832
|
+
for (const t of targets2) {
|
|
8051
8833
|
results.push(await writeOnePreambleFile(t, dryRun));
|
|
8052
8834
|
}
|
|
8053
8835
|
return results;
|
|
@@ -9372,12 +10154,12 @@ Skipping remaining steps.
|
|
|
9372
10154
|
platform: process.platform,
|
|
9373
10155
|
env: process.env
|
|
9374
10156
|
});
|
|
9375
|
-
let
|
|
9376
|
-
|
|
10157
|
+
let targets2 = getClientTargets(ctx);
|
|
10158
|
+
targets2 = filterTargetsByFamily(targets2, flags.client);
|
|
9377
10159
|
if (flags.skipCodex) {
|
|
9378
|
-
|
|
10160
|
+
targets2 = targets2.filter((t) => t.key !== "codex-cli");
|
|
9379
10161
|
}
|
|
9380
|
-
if (
|
|
10162
|
+
if (targets2.length === 0) {
|
|
9381
10163
|
process.stderr.write(
|
|
9382
10164
|
flags.client !== null ? `error: --client "${flags.client}" matched no known target. Run --help for the list.
|
|
9383
10165
|
` : "error: no client targets selected (did you skip all of them?)\n"
|
|
@@ -9385,7 +10167,7 @@ Skipping remaining steps.
|
|
|
9385
10167
|
report.print(process.stdout);
|
|
9386
10168
|
return 2;
|
|
9387
10169
|
}
|
|
9388
|
-
for (const target of
|
|
10170
|
+
for (const target of targets2) {
|
|
9389
10171
|
const serverEntry = buildMaindServerEntry({
|
|
9390
10172
|
mcpUrl,
|
|
9391
10173
|
clientFamily: target.clientFamilyEnv
|