@muhaven/mcp 0.1.0 → 0.1.3
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/CHANGELOG.md +136 -2
- package/README.md +125 -125
- package/bin/muhaven-broker.cjs +11 -11
- package/bin/muhaven-mcp.cjs +11 -11
- package/dist/broker.cjs +138 -22
- package/dist/broker.d.cts +21 -1
- package/dist/broker.d.ts +21 -1
- package/dist/broker.js +138 -23
- package/dist/index.cjs +124 -20
- package/dist/index.d.cts +143 -13
- package/dist/index.d.ts +143 -13
- package/dist/index.js +120 -21
- package/manifest.json +98 -98
- package/package.json +104 -104
package/dist/broker.cjs
CHANGED
|
@@ -73,23 +73,26 @@ function loadMcpConfig(env = process.env) {
|
|
|
73
73
|
}
|
|
74
74
|
var PRIVKEY_HEX_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
75
75
|
function loadBrokerConfig(env = process.env) {
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
throw new Error("MUHAVEN_BROKER_SESSION_KEY must be a 0x-prefixed 32-byte hex string");
|
|
76
|
+
const sessionKeyHexRaw = env.MUHAVEN_BROKER_SESSION_KEY;
|
|
77
|
+
let sessionKeyHex;
|
|
78
|
+
if (sessionKeyHexRaw && sessionKeyHexRaw.length > 0) {
|
|
79
|
+
if (!PRIVKEY_HEX_RE.test(sessionKeyHexRaw)) {
|
|
80
|
+
throw new Error("MUHAVEN_BROKER_SESSION_KEY must be a 0x-prefixed 32-byte hex string");
|
|
81
|
+
}
|
|
82
|
+
sessionKeyHex = sessionKeyHexRaw;
|
|
84
83
|
}
|
|
85
84
|
const endpoint = env.MUHAVEN_BROKER_ENDPOINT ?? defaultBrokerEndpoint();
|
|
86
85
|
const maxRequestBytes = readEnvInt("MUHAVEN_BROKER_MAX_BYTES", DEFAULT_BROKER_MAX_BYTES, env);
|
|
87
86
|
const requestTimeoutMs = readEnvInt("MUHAVEN_BROKER_TIMEOUT_MS", DEFAULT_BROKER_TIMEOUT_MS, env);
|
|
87
|
+
const backendBaseUrl = trimTrailingSlash(env.MUHAVEN_BACKEND_URL ?? DEFAULT_BACKEND_URL);
|
|
88
|
+
const dashboardBaseUrl = trimTrailingSlash(env.MUHAVEN_DASHBOARD_URL ?? DEFAULT_DASHBOARD_URL);
|
|
88
89
|
return {
|
|
89
90
|
endpoint,
|
|
90
91
|
sessionKeyHex,
|
|
91
92
|
maxRequestBytes,
|
|
92
|
-
requestTimeoutMs
|
|
93
|
+
requestTimeoutMs,
|
|
94
|
+
backendBaseUrl,
|
|
95
|
+
dashboardBaseUrl
|
|
93
96
|
};
|
|
94
97
|
}
|
|
95
98
|
var BrokerClientError = class extends Error {
|
|
@@ -522,7 +525,7 @@ async function openKeystore(options = {}) {
|
|
|
522
525
|
}
|
|
523
526
|
|
|
524
527
|
// src/broker/protocol.ts
|
|
525
|
-
var BROKER_PROTOCOL_VERSION = "0.
|
|
528
|
+
var BROKER_PROTOCOL_VERSION = "0.3.0";
|
|
526
529
|
var HASH_HEX_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
527
530
|
var JWT_RE = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
|
|
528
531
|
function isHashHex(value) {
|
|
@@ -608,6 +611,21 @@ function parseBrokerRequest(line) {
|
|
|
608
611
|
function serializeResponse(res) {
|
|
609
612
|
return JSON.stringify(res) + "\n";
|
|
610
613
|
}
|
|
614
|
+
var MissingSessionKeyError = class extends Error {
|
|
615
|
+
constructor() {
|
|
616
|
+
super(
|
|
617
|
+
"session_key_unavailable: daemon booted in read-only posture (no MUHAVEN_BROKER_SESSION_KEY at env-load time). Mint a session key via the dashboard /agent/policy/transition flow, set MUHAVEN_BROKER_SESSION_KEY, and restart the daemon. (Note: `muhaven-broker login` mints a JWT, NOT a session key \u2014 do not loop on that command for this error.)"
|
|
618
|
+
);
|
|
619
|
+
this.name = "MissingSessionKeyError";
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
623
|
+
var NullSigner = class {
|
|
624
|
+
address = ZERO_ADDRESS;
|
|
625
|
+
async signHash(_hash) {
|
|
626
|
+
throw new MissingSessionKeyError();
|
|
627
|
+
}
|
|
628
|
+
};
|
|
611
629
|
var ViemSigner = class {
|
|
612
630
|
account;
|
|
613
631
|
constructor(privateKey) {
|
|
@@ -624,7 +642,7 @@ var ViemSigner = class {
|
|
|
624
642
|
// src/broker/daemon.ts
|
|
625
643
|
var noopLogger = (_e) => {
|
|
626
644
|
};
|
|
627
|
-
async function handleBrokerRequest(req, signer, keystore, nowSec = () => Math.floor(Date.now() / 1e3)) {
|
|
645
|
+
async function handleBrokerRequest(req, signer, keystore, nowSec = () => Math.floor(Date.now() / 1e3), options = {}) {
|
|
628
646
|
switch (req.type) {
|
|
629
647
|
case "hello": {
|
|
630
648
|
let hasJwt = false;
|
|
@@ -634,16 +652,26 @@ async function handleBrokerRequest(req, signer, keystore, nowSec = () => Math.fl
|
|
|
634
652
|
} catch {
|
|
635
653
|
hasJwt = false;
|
|
636
654
|
}
|
|
655
|
+
const hasSessionKey = options.hasSessionKey ?? true;
|
|
637
656
|
return {
|
|
638
657
|
type: "hello",
|
|
639
658
|
version: BROKER_PROTOCOL_VERSION,
|
|
640
659
|
sessionKeyAddress: signer.address,
|
|
641
|
-
hasJwt
|
|
660
|
+
hasJwt,
|
|
661
|
+
hasSessionKey,
|
|
662
|
+
...options.effectiveConfig ? { effectiveConfig: options.effectiveConfig } : {}
|
|
642
663
|
};
|
|
643
664
|
}
|
|
644
665
|
case "sign_hash": {
|
|
645
|
-
|
|
646
|
-
|
|
666
|
+
try {
|
|
667
|
+
const signature = await signer.signHash(req.hash);
|
|
668
|
+
return { type: "sign_hash", signature, signerAddress: signer.address };
|
|
669
|
+
} catch (err) {
|
|
670
|
+
if (err instanceof MissingSessionKeyError) {
|
|
671
|
+
return errorResponse("session_key_unavailable", err.message);
|
|
672
|
+
}
|
|
673
|
+
throw err;
|
|
674
|
+
}
|
|
647
675
|
}
|
|
648
676
|
case "store_jwt": {
|
|
649
677
|
try {
|
|
@@ -715,9 +743,24 @@ var BrokerDaemon = class {
|
|
|
715
743
|
log;
|
|
716
744
|
config;
|
|
717
745
|
keystore;
|
|
746
|
+
/**
|
|
747
|
+
* Whether a session-key private half is actually loaded. `false` =
|
|
748
|
+
* daemon booted in read-only posture (no `MUHAVEN_BROKER_SESSION_KEY`
|
|
749
|
+
* at env-load time) and uses a `NullSigner` whose `signHash` throws.
|
|
750
|
+
*/
|
|
751
|
+
hasSessionKey;
|
|
718
752
|
constructor(options) {
|
|
719
753
|
this.config = options.config;
|
|
720
|
-
|
|
754
|
+
if (options.signer) {
|
|
755
|
+
this.signer = options.signer;
|
|
756
|
+
this.hasSessionKey = true;
|
|
757
|
+
} else if (options.config.sessionKeyHex) {
|
|
758
|
+
this.signer = new ViemSigner(options.config.sessionKeyHex);
|
|
759
|
+
this.hasSessionKey = true;
|
|
760
|
+
} else {
|
|
761
|
+
this.signer = new NullSigner();
|
|
762
|
+
this.hasSessionKey = false;
|
|
763
|
+
}
|
|
721
764
|
this.keystore = options.keystore ?? null;
|
|
722
765
|
this.log = options.logger ?? noopLogger;
|
|
723
766
|
this.server = net.createServer((socket) => this.onConnection(socket));
|
|
@@ -755,6 +798,7 @@ var BrokerDaemon = class {
|
|
|
755
798
|
meta: {
|
|
756
799
|
endpoint: this.config.endpoint,
|
|
757
800
|
signer: this.signer.address,
|
|
801
|
+
hasSessionKey: this.hasSessionKey,
|
|
758
802
|
keystore: this.keystore.backend,
|
|
759
803
|
version: BROKER_PROTOCOL_VERSION
|
|
760
804
|
}
|
|
@@ -836,7 +880,19 @@ var BrokerDaemon = class {
|
|
|
836
880
|
return;
|
|
837
881
|
}
|
|
838
882
|
try {
|
|
839
|
-
const res = await handleBrokerRequest(
|
|
883
|
+
const res = await handleBrokerRequest(
|
|
884
|
+
parsed,
|
|
885
|
+
this.signer,
|
|
886
|
+
this.keystore,
|
|
887
|
+
void 0,
|
|
888
|
+
{
|
|
889
|
+
hasSessionKey: this.hasSessionKey,
|
|
890
|
+
effectiveConfig: {
|
|
891
|
+
backendBaseUrl: this.config.backendBaseUrl,
|
|
892
|
+
dashboardBaseUrl: this.config.dashboardBaseUrl
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
);
|
|
840
896
|
socket.end(serializeResponse(res));
|
|
841
897
|
} catch (err) {
|
|
842
898
|
this.log({
|
|
@@ -852,6 +908,15 @@ var BrokerDaemon = class {
|
|
|
852
908
|
};
|
|
853
909
|
async function runBrokerDaemonCli() {
|
|
854
910
|
const config = loadBrokerConfig();
|
|
911
|
+
if (!config.sessionKeyHex) {
|
|
912
|
+
process.stderr.write(
|
|
913
|
+
JSON.stringify({
|
|
914
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
915
|
+
level: "info",
|
|
916
|
+
msg: "broker booting in read-only posture (no MUHAVEN_BROKER_SESSION_KEY)"
|
|
917
|
+
}) + "\n"
|
|
918
|
+
);
|
|
919
|
+
}
|
|
855
920
|
const daemon = new BrokerDaemon({
|
|
856
921
|
config,
|
|
857
922
|
logger: (e) => {
|
|
@@ -904,9 +969,11 @@ function parseLoginFlags(argv) {
|
|
|
904
969
|
let brokerEndpoint;
|
|
905
970
|
let backendBaseUrl;
|
|
906
971
|
let dashboardBaseUrl;
|
|
972
|
+
let fromDaemon = false;
|
|
907
973
|
for (let i = 0; i < argv.length; i++) {
|
|
908
974
|
const a = argv[i];
|
|
909
975
|
if (a === "--no-launch-browser") noLaunchBrowser = true;
|
|
976
|
+
else if (a === "--from-daemon") fromDaemon = true;
|
|
910
977
|
else if (a === "--broker-endpoint" && i + 1 < argv.length) {
|
|
911
978
|
brokerEndpoint = argv[++i];
|
|
912
979
|
} else if (a === "--backend-base-url" && i + 1 < argv.length) {
|
|
@@ -917,7 +984,12 @@ function parseLoginFlags(argv) {
|
|
|
917
984
|
throw new Error(`unknown flag: ${a}`);
|
|
918
985
|
}
|
|
919
986
|
}
|
|
920
|
-
|
|
987
|
+
if (fromDaemon && (backendBaseUrl || dashboardBaseUrl)) {
|
|
988
|
+
throw new Error(
|
|
989
|
+
"--from-daemon is mutually exclusive with --backend-base-url / --dashboard-base-url"
|
|
990
|
+
);
|
|
991
|
+
}
|
|
992
|
+
return { noLaunchBrowser, brokerEndpoint, backendBaseUrl, dashboardBaseUrl, fromDaemon };
|
|
921
993
|
}
|
|
922
994
|
async function tryLaunchBrowser(url) {
|
|
923
995
|
return new Promise((resolve) => {
|
|
@@ -931,7 +1003,9 @@ async function runLogin(argv) {
|
|
|
931
1003
|
flags = parseLoginFlags(argv);
|
|
932
1004
|
} catch (err) {
|
|
933
1005
|
printErr(`error: ${err.message}`);
|
|
934
|
-
printErr(
|
|
1006
|
+
printErr(
|
|
1007
|
+
"usage: muhaven-broker login [--no-launch-browser] [--broker-endpoint PATH] [--from-daemon | (--backend-base-url URL --dashboard-base-url URL)]"
|
|
1008
|
+
);
|
|
935
1009
|
return 2;
|
|
936
1010
|
}
|
|
937
1011
|
const env = process.env;
|
|
@@ -945,8 +1019,9 @@ async function runLogin(argv) {
|
|
|
945
1019
|
endpoint: config.brokerEndpoint,
|
|
946
1020
|
timeoutMs: config.brokerTimeoutMs
|
|
947
1021
|
});
|
|
1022
|
+
let helloResult;
|
|
948
1023
|
try {
|
|
949
|
-
await broker.hello();
|
|
1024
|
+
helloResult = await broker.hello();
|
|
950
1025
|
} catch (err) {
|
|
951
1026
|
printErr(
|
|
952
1027
|
`cannot reach muhaven-broker daemon at ${config.brokerEndpoint}: ${err.message}`
|
|
@@ -954,9 +1029,42 @@ async function runLogin(argv) {
|
|
|
954
1029
|
printErr("hint: start the daemon first (`muhaven-broker` with no subcommand).");
|
|
955
1030
|
return 1;
|
|
956
1031
|
}
|
|
1032
|
+
let backendBaseUrl = config.backendBaseUrl;
|
|
1033
|
+
let dashboardBaseUrl = config.dashboardBaseUrl;
|
|
1034
|
+
if (flags.fromDaemon) {
|
|
1035
|
+
if (!helloResult.effectiveConfig) {
|
|
1036
|
+
printErr(
|
|
1037
|
+
"--from-daemon requested but broker did not return effectiveConfig (daemon is older than protocol 0.3.0). Upgrade the daemon (`@muhaven/mcp@0.1.3+`) or drop the flag."
|
|
1038
|
+
);
|
|
1039
|
+
return 1;
|
|
1040
|
+
}
|
|
1041
|
+
const daemonBackend = helloResult.effectiveConfig.backendBaseUrl;
|
|
1042
|
+
const daemonDashboard = helloResult.effectiveConfig.dashboardBaseUrl;
|
|
1043
|
+
if (!daemonBackend || !daemonDashboard) {
|
|
1044
|
+
printErr(
|
|
1045
|
+
"--from-daemon: daemon returned an empty backend/dashboard URL \u2014 refusing to proceed."
|
|
1046
|
+
);
|
|
1047
|
+
return 1;
|
|
1048
|
+
}
|
|
1049
|
+
if (daemonBackend !== config.backendBaseUrl) {
|
|
1050
|
+
print(
|
|
1051
|
+
`\u26A0 daemon backend (${daemonBackend}) differs from CLI env (${config.backendBaseUrl}). Using daemon's value per --from-daemon.`
|
|
1052
|
+
);
|
|
1053
|
+
}
|
|
1054
|
+
if (daemonDashboard !== config.dashboardBaseUrl) {
|
|
1055
|
+
print(
|
|
1056
|
+
`\u26A0 daemon dashboard (${daemonDashboard}) differs from CLI env (${config.dashboardBaseUrl}). Using daemon's value per --from-daemon.`
|
|
1057
|
+
);
|
|
1058
|
+
}
|
|
1059
|
+
backendBaseUrl = daemonBackend;
|
|
1060
|
+
dashboardBaseUrl = daemonDashboard;
|
|
1061
|
+
print(`Using daemon's effective config:`);
|
|
1062
|
+
print(` backend: ${backendBaseUrl}`);
|
|
1063
|
+
print(` dashboard: ${dashboardBaseUrl}`);
|
|
1064
|
+
}
|
|
957
1065
|
const flow = new DeviceFlowClient({
|
|
958
|
-
backendBaseUrl
|
|
959
|
-
dashboardBaseUrl
|
|
1066
|
+
backendBaseUrl,
|
|
1067
|
+
dashboardBaseUrl,
|
|
960
1068
|
requesterMetadata: {
|
|
961
1069
|
processName: detectMcpHost(),
|
|
962
1070
|
hostname: os.hostname(),
|
|
@@ -1071,7 +1179,13 @@ async function runDoctor() {
|
|
|
1071
1179
|
});
|
|
1072
1180
|
try {
|
|
1073
1181
|
const h = await broker.hello();
|
|
1074
|
-
|
|
1182
|
+
const hasKey = h.hasSessionKey ?? true;
|
|
1183
|
+
const keyTag = hasKey ? `signer ${h.sessionKeyAddress}` : "NO SESSION KEY (read-only posture)";
|
|
1184
|
+
print(`Broker daemon : reachable (proto v${h.version}, ${keyTag}, hasJwt=${h.hasJwt})`);
|
|
1185
|
+
if (h.effectiveConfig) {
|
|
1186
|
+
print(`Daemon backend URL: ${h.effectiveConfig.backendBaseUrl}`);
|
|
1187
|
+
print(`Daemon dashboard : ${h.effectiveConfig.dashboardBaseUrl}`);
|
|
1188
|
+
}
|
|
1075
1189
|
return 0;
|
|
1076
1190
|
} catch (err) {
|
|
1077
1191
|
print(`Broker daemon : NOT reachable (${err.message})`);
|
|
@@ -1084,6 +1198,7 @@ function printUsage() {
|
|
|
1084
1198
|
print("");
|
|
1085
1199
|
print(" (no subcommand) Run the daemon (production mode)");
|
|
1086
1200
|
print(" login Acquire a JWT via the device-code flow + store in keystore");
|
|
1201
|
+
print(" [--from-daemon] resolves backend/dashboard URLs from the running daemon");
|
|
1087
1202
|
print(" logout Clear the JWT from the keystore");
|
|
1088
1203
|
print(" doctor Print environment + keystore + reachability report");
|
|
1089
1204
|
print(" -h, --help Show this help");
|
|
@@ -1111,6 +1226,7 @@ async function runCli(argv) {
|
|
|
1111
1226
|
}
|
|
1112
1227
|
}
|
|
1113
1228
|
|
|
1229
|
+
exports.parseLoginFlags = parseLoginFlags;
|
|
1114
1230
|
exports.runCli = runCli;
|
|
1115
1231
|
exports.runDoctor = runDoctor;
|
|
1116
1232
|
exports.runLogin = runLogin;
|
package/dist/broker.d.cts
CHANGED
|
@@ -8,9 +8,29 @@
|
|
|
8
8
|
* doctor → environment + keystore capability report
|
|
9
9
|
* --help, -h → usage
|
|
10
10
|
*/
|
|
11
|
+
interface LoginFlags {
|
|
12
|
+
noLaunchBrowser: boolean;
|
|
13
|
+
brokerEndpoint?: string;
|
|
14
|
+
backendBaseUrl?: string;
|
|
15
|
+
dashboardBaseUrl?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Resolve `backendBaseUrl` + `dashboardBaseUrl` from the running
|
|
18
|
+
* daemon's view (returned in `hello.effectiveConfig`) rather than the
|
|
19
|
+
* CLI's env. Solves the daemon-vs-CLI env-divergence problem when the
|
|
20
|
+
* CLI is launched from a different shell (ssh, IDE-spawned terminal)
|
|
21
|
+
* than the systemd / launchd-launched daemon. Closes §3e⁶
|
|
22
|
+
* F-broker-env-divergence.
|
|
23
|
+
*
|
|
24
|
+
* Mutually exclusive with explicit `--backend-base-url` /
|
|
25
|
+
* `--dashboard-base-url`; the CLI rejects the combination so the
|
|
26
|
+
* operator picks one source of truth.
|
|
27
|
+
*/
|
|
28
|
+
fromDaemon: boolean;
|
|
29
|
+
}
|
|
30
|
+
declare function parseLoginFlags(argv: readonly string[]): LoginFlags;
|
|
11
31
|
declare function runLogin(argv: readonly string[]): Promise<number>;
|
|
12
32
|
declare function runLogout(): Promise<number>;
|
|
13
33
|
declare function runDoctor(): Promise<number>;
|
|
14
34
|
declare function runCli(argv: readonly string[]): Promise<number>;
|
|
15
35
|
|
|
16
|
-
export { runCli, runDoctor, runLogin, runLogout };
|
|
36
|
+
export { parseLoginFlags, runCli, runDoctor, runLogin, runLogout };
|
package/dist/broker.d.ts
CHANGED
|
@@ -8,9 +8,29 @@
|
|
|
8
8
|
* doctor → environment + keystore capability report
|
|
9
9
|
* --help, -h → usage
|
|
10
10
|
*/
|
|
11
|
+
interface LoginFlags {
|
|
12
|
+
noLaunchBrowser: boolean;
|
|
13
|
+
brokerEndpoint?: string;
|
|
14
|
+
backendBaseUrl?: string;
|
|
15
|
+
dashboardBaseUrl?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Resolve `backendBaseUrl` + `dashboardBaseUrl` from the running
|
|
18
|
+
* daemon's view (returned in `hello.effectiveConfig`) rather than the
|
|
19
|
+
* CLI's env. Solves the daemon-vs-CLI env-divergence problem when the
|
|
20
|
+
* CLI is launched from a different shell (ssh, IDE-spawned terminal)
|
|
21
|
+
* than the systemd / launchd-launched daemon. Closes §3e⁶
|
|
22
|
+
* F-broker-env-divergence.
|
|
23
|
+
*
|
|
24
|
+
* Mutually exclusive with explicit `--backend-base-url` /
|
|
25
|
+
* `--dashboard-base-url`; the CLI rejects the combination so the
|
|
26
|
+
* operator picks one source of truth.
|
|
27
|
+
*/
|
|
28
|
+
fromDaemon: boolean;
|
|
29
|
+
}
|
|
30
|
+
declare function parseLoginFlags(argv: readonly string[]): LoginFlags;
|
|
11
31
|
declare function runLogin(argv: readonly string[]): Promise<number>;
|
|
12
32
|
declare function runLogout(): Promise<number>;
|
|
13
33
|
declare function runDoctor(): Promise<number>;
|
|
14
34
|
declare function runCli(argv: readonly string[]): Promise<number>;
|
|
15
35
|
|
|
16
|
-
export { runCli, runDoctor, runLogin, runLogout };
|
|
36
|
+
export { parseLoginFlags, runCli, runDoctor, runLogin, runLogout };
|
package/dist/broker.js
CHANGED
|
@@ -71,23 +71,26 @@ function loadMcpConfig(env = process.env) {
|
|
|
71
71
|
}
|
|
72
72
|
var PRIVKEY_HEX_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
73
73
|
function loadBrokerConfig(env = process.env) {
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
throw new Error("MUHAVEN_BROKER_SESSION_KEY must be a 0x-prefixed 32-byte hex string");
|
|
74
|
+
const sessionKeyHexRaw = env.MUHAVEN_BROKER_SESSION_KEY;
|
|
75
|
+
let sessionKeyHex;
|
|
76
|
+
if (sessionKeyHexRaw && sessionKeyHexRaw.length > 0) {
|
|
77
|
+
if (!PRIVKEY_HEX_RE.test(sessionKeyHexRaw)) {
|
|
78
|
+
throw new Error("MUHAVEN_BROKER_SESSION_KEY must be a 0x-prefixed 32-byte hex string");
|
|
79
|
+
}
|
|
80
|
+
sessionKeyHex = sessionKeyHexRaw;
|
|
82
81
|
}
|
|
83
82
|
const endpoint = env.MUHAVEN_BROKER_ENDPOINT ?? defaultBrokerEndpoint();
|
|
84
83
|
const maxRequestBytes = readEnvInt("MUHAVEN_BROKER_MAX_BYTES", DEFAULT_BROKER_MAX_BYTES, env);
|
|
85
84
|
const requestTimeoutMs = readEnvInt("MUHAVEN_BROKER_TIMEOUT_MS", DEFAULT_BROKER_TIMEOUT_MS, env);
|
|
85
|
+
const backendBaseUrl = trimTrailingSlash(env.MUHAVEN_BACKEND_URL ?? DEFAULT_BACKEND_URL);
|
|
86
|
+
const dashboardBaseUrl = trimTrailingSlash(env.MUHAVEN_DASHBOARD_URL ?? DEFAULT_DASHBOARD_URL);
|
|
86
87
|
return {
|
|
87
88
|
endpoint,
|
|
88
89
|
sessionKeyHex,
|
|
89
90
|
maxRequestBytes,
|
|
90
|
-
requestTimeoutMs
|
|
91
|
+
requestTimeoutMs,
|
|
92
|
+
backendBaseUrl,
|
|
93
|
+
dashboardBaseUrl
|
|
91
94
|
};
|
|
92
95
|
}
|
|
93
96
|
var BrokerClientError = class extends Error {
|
|
@@ -520,7 +523,7 @@ async function openKeystore(options = {}) {
|
|
|
520
523
|
}
|
|
521
524
|
|
|
522
525
|
// src/broker/protocol.ts
|
|
523
|
-
var BROKER_PROTOCOL_VERSION = "0.
|
|
526
|
+
var BROKER_PROTOCOL_VERSION = "0.3.0";
|
|
524
527
|
var HASH_HEX_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
525
528
|
var JWT_RE = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
|
|
526
529
|
function isHashHex(value) {
|
|
@@ -606,6 +609,21 @@ function parseBrokerRequest(line) {
|
|
|
606
609
|
function serializeResponse(res) {
|
|
607
610
|
return JSON.stringify(res) + "\n";
|
|
608
611
|
}
|
|
612
|
+
var MissingSessionKeyError = class extends Error {
|
|
613
|
+
constructor() {
|
|
614
|
+
super(
|
|
615
|
+
"session_key_unavailable: daemon booted in read-only posture (no MUHAVEN_BROKER_SESSION_KEY at env-load time). Mint a session key via the dashboard /agent/policy/transition flow, set MUHAVEN_BROKER_SESSION_KEY, and restart the daemon. (Note: `muhaven-broker login` mints a JWT, NOT a session key \u2014 do not loop on that command for this error.)"
|
|
616
|
+
);
|
|
617
|
+
this.name = "MissingSessionKeyError";
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
621
|
+
var NullSigner = class {
|
|
622
|
+
address = ZERO_ADDRESS;
|
|
623
|
+
async signHash(_hash) {
|
|
624
|
+
throw new MissingSessionKeyError();
|
|
625
|
+
}
|
|
626
|
+
};
|
|
609
627
|
var ViemSigner = class {
|
|
610
628
|
account;
|
|
611
629
|
constructor(privateKey) {
|
|
@@ -622,7 +640,7 @@ var ViemSigner = class {
|
|
|
622
640
|
// src/broker/daemon.ts
|
|
623
641
|
var noopLogger = (_e) => {
|
|
624
642
|
};
|
|
625
|
-
async function handleBrokerRequest(req, signer, keystore, nowSec = () => Math.floor(Date.now() / 1e3)) {
|
|
643
|
+
async function handleBrokerRequest(req, signer, keystore, nowSec = () => Math.floor(Date.now() / 1e3), options = {}) {
|
|
626
644
|
switch (req.type) {
|
|
627
645
|
case "hello": {
|
|
628
646
|
let hasJwt = false;
|
|
@@ -632,16 +650,26 @@ async function handleBrokerRequest(req, signer, keystore, nowSec = () => Math.fl
|
|
|
632
650
|
} catch {
|
|
633
651
|
hasJwt = false;
|
|
634
652
|
}
|
|
653
|
+
const hasSessionKey = options.hasSessionKey ?? true;
|
|
635
654
|
return {
|
|
636
655
|
type: "hello",
|
|
637
656
|
version: BROKER_PROTOCOL_VERSION,
|
|
638
657
|
sessionKeyAddress: signer.address,
|
|
639
|
-
hasJwt
|
|
658
|
+
hasJwt,
|
|
659
|
+
hasSessionKey,
|
|
660
|
+
...options.effectiveConfig ? { effectiveConfig: options.effectiveConfig } : {}
|
|
640
661
|
};
|
|
641
662
|
}
|
|
642
663
|
case "sign_hash": {
|
|
643
|
-
|
|
644
|
-
|
|
664
|
+
try {
|
|
665
|
+
const signature = await signer.signHash(req.hash);
|
|
666
|
+
return { type: "sign_hash", signature, signerAddress: signer.address };
|
|
667
|
+
} catch (err) {
|
|
668
|
+
if (err instanceof MissingSessionKeyError) {
|
|
669
|
+
return errorResponse("session_key_unavailable", err.message);
|
|
670
|
+
}
|
|
671
|
+
throw err;
|
|
672
|
+
}
|
|
645
673
|
}
|
|
646
674
|
case "store_jwt": {
|
|
647
675
|
try {
|
|
@@ -713,9 +741,24 @@ var BrokerDaemon = class {
|
|
|
713
741
|
log;
|
|
714
742
|
config;
|
|
715
743
|
keystore;
|
|
744
|
+
/**
|
|
745
|
+
* Whether a session-key private half is actually loaded. `false` =
|
|
746
|
+
* daemon booted in read-only posture (no `MUHAVEN_BROKER_SESSION_KEY`
|
|
747
|
+
* at env-load time) and uses a `NullSigner` whose `signHash` throws.
|
|
748
|
+
*/
|
|
749
|
+
hasSessionKey;
|
|
716
750
|
constructor(options) {
|
|
717
751
|
this.config = options.config;
|
|
718
|
-
|
|
752
|
+
if (options.signer) {
|
|
753
|
+
this.signer = options.signer;
|
|
754
|
+
this.hasSessionKey = true;
|
|
755
|
+
} else if (options.config.sessionKeyHex) {
|
|
756
|
+
this.signer = new ViemSigner(options.config.sessionKeyHex);
|
|
757
|
+
this.hasSessionKey = true;
|
|
758
|
+
} else {
|
|
759
|
+
this.signer = new NullSigner();
|
|
760
|
+
this.hasSessionKey = false;
|
|
761
|
+
}
|
|
719
762
|
this.keystore = options.keystore ?? null;
|
|
720
763
|
this.log = options.logger ?? noopLogger;
|
|
721
764
|
this.server = createServer((socket) => this.onConnection(socket));
|
|
@@ -753,6 +796,7 @@ var BrokerDaemon = class {
|
|
|
753
796
|
meta: {
|
|
754
797
|
endpoint: this.config.endpoint,
|
|
755
798
|
signer: this.signer.address,
|
|
799
|
+
hasSessionKey: this.hasSessionKey,
|
|
756
800
|
keystore: this.keystore.backend,
|
|
757
801
|
version: BROKER_PROTOCOL_VERSION
|
|
758
802
|
}
|
|
@@ -834,7 +878,19 @@ var BrokerDaemon = class {
|
|
|
834
878
|
return;
|
|
835
879
|
}
|
|
836
880
|
try {
|
|
837
|
-
const res = await handleBrokerRequest(
|
|
881
|
+
const res = await handleBrokerRequest(
|
|
882
|
+
parsed,
|
|
883
|
+
this.signer,
|
|
884
|
+
this.keystore,
|
|
885
|
+
void 0,
|
|
886
|
+
{
|
|
887
|
+
hasSessionKey: this.hasSessionKey,
|
|
888
|
+
effectiveConfig: {
|
|
889
|
+
backendBaseUrl: this.config.backendBaseUrl,
|
|
890
|
+
dashboardBaseUrl: this.config.dashboardBaseUrl
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
);
|
|
838
894
|
socket.end(serializeResponse(res));
|
|
839
895
|
} catch (err) {
|
|
840
896
|
this.log({
|
|
@@ -850,6 +906,15 @@ var BrokerDaemon = class {
|
|
|
850
906
|
};
|
|
851
907
|
async function runBrokerDaemonCli() {
|
|
852
908
|
const config = loadBrokerConfig();
|
|
909
|
+
if (!config.sessionKeyHex) {
|
|
910
|
+
process.stderr.write(
|
|
911
|
+
JSON.stringify({
|
|
912
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
913
|
+
level: "info",
|
|
914
|
+
msg: "broker booting in read-only posture (no MUHAVEN_BROKER_SESSION_KEY)"
|
|
915
|
+
}) + "\n"
|
|
916
|
+
);
|
|
917
|
+
}
|
|
853
918
|
const daemon = new BrokerDaemon({
|
|
854
919
|
config,
|
|
855
920
|
logger: (e) => {
|
|
@@ -902,9 +967,11 @@ function parseLoginFlags(argv) {
|
|
|
902
967
|
let brokerEndpoint;
|
|
903
968
|
let backendBaseUrl;
|
|
904
969
|
let dashboardBaseUrl;
|
|
970
|
+
let fromDaemon = false;
|
|
905
971
|
for (let i = 0; i < argv.length; i++) {
|
|
906
972
|
const a = argv[i];
|
|
907
973
|
if (a === "--no-launch-browser") noLaunchBrowser = true;
|
|
974
|
+
else if (a === "--from-daemon") fromDaemon = true;
|
|
908
975
|
else if (a === "--broker-endpoint" && i + 1 < argv.length) {
|
|
909
976
|
brokerEndpoint = argv[++i];
|
|
910
977
|
} else if (a === "--backend-base-url" && i + 1 < argv.length) {
|
|
@@ -915,7 +982,12 @@ function parseLoginFlags(argv) {
|
|
|
915
982
|
throw new Error(`unknown flag: ${a}`);
|
|
916
983
|
}
|
|
917
984
|
}
|
|
918
|
-
|
|
985
|
+
if (fromDaemon && (backendBaseUrl || dashboardBaseUrl)) {
|
|
986
|
+
throw new Error(
|
|
987
|
+
"--from-daemon is mutually exclusive with --backend-base-url / --dashboard-base-url"
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
return { noLaunchBrowser, brokerEndpoint, backendBaseUrl, dashboardBaseUrl, fromDaemon };
|
|
919
991
|
}
|
|
920
992
|
async function tryLaunchBrowser(url) {
|
|
921
993
|
return new Promise((resolve) => {
|
|
@@ -929,7 +1001,9 @@ async function runLogin(argv) {
|
|
|
929
1001
|
flags = parseLoginFlags(argv);
|
|
930
1002
|
} catch (err) {
|
|
931
1003
|
printErr(`error: ${err.message}`);
|
|
932
|
-
printErr(
|
|
1004
|
+
printErr(
|
|
1005
|
+
"usage: muhaven-broker login [--no-launch-browser] [--broker-endpoint PATH] [--from-daemon | (--backend-base-url URL --dashboard-base-url URL)]"
|
|
1006
|
+
);
|
|
933
1007
|
return 2;
|
|
934
1008
|
}
|
|
935
1009
|
const env = process.env;
|
|
@@ -943,8 +1017,9 @@ async function runLogin(argv) {
|
|
|
943
1017
|
endpoint: config.brokerEndpoint,
|
|
944
1018
|
timeoutMs: config.brokerTimeoutMs
|
|
945
1019
|
});
|
|
1020
|
+
let helloResult;
|
|
946
1021
|
try {
|
|
947
|
-
await broker.hello();
|
|
1022
|
+
helloResult = await broker.hello();
|
|
948
1023
|
} catch (err) {
|
|
949
1024
|
printErr(
|
|
950
1025
|
`cannot reach muhaven-broker daemon at ${config.brokerEndpoint}: ${err.message}`
|
|
@@ -952,9 +1027,42 @@ async function runLogin(argv) {
|
|
|
952
1027
|
printErr("hint: start the daemon first (`muhaven-broker` with no subcommand).");
|
|
953
1028
|
return 1;
|
|
954
1029
|
}
|
|
1030
|
+
let backendBaseUrl = config.backendBaseUrl;
|
|
1031
|
+
let dashboardBaseUrl = config.dashboardBaseUrl;
|
|
1032
|
+
if (flags.fromDaemon) {
|
|
1033
|
+
if (!helloResult.effectiveConfig) {
|
|
1034
|
+
printErr(
|
|
1035
|
+
"--from-daemon requested but broker did not return effectiveConfig (daemon is older than protocol 0.3.0). Upgrade the daemon (`@muhaven/mcp@0.1.3+`) or drop the flag."
|
|
1036
|
+
);
|
|
1037
|
+
return 1;
|
|
1038
|
+
}
|
|
1039
|
+
const daemonBackend = helloResult.effectiveConfig.backendBaseUrl;
|
|
1040
|
+
const daemonDashboard = helloResult.effectiveConfig.dashboardBaseUrl;
|
|
1041
|
+
if (!daemonBackend || !daemonDashboard) {
|
|
1042
|
+
printErr(
|
|
1043
|
+
"--from-daemon: daemon returned an empty backend/dashboard URL \u2014 refusing to proceed."
|
|
1044
|
+
);
|
|
1045
|
+
return 1;
|
|
1046
|
+
}
|
|
1047
|
+
if (daemonBackend !== config.backendBaseUrl) {
|
|
1048
|
+
print(
|
|
1049
|
+
`\u26A0 daemon backend (${daemonBackend}) differs from CLI env (${config.backendBaseUrl}). Using daemon's value per --from-daemon.`
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
if (daemonDashboard !== config.dashboardBaseUrl) {
|
|
1053
|
+
print(
|
|
1054
|
+
`\u26A0 daemon dashboard (${daemonDashboard}) differs from CLI env (${config.dashboardBaseUrl}). Using daemon's value per --from-daemon.`
|
|
1055
|
+
);
|
|
1056
|
+
}
|
|
1057
|
+
backendBaseUrl = daemonBackend;
|
|
1058
|
+
dashboardBaseUrl = daemonDashboard;
|
|
1059
|
+
print(`Using daemon's effective config:`);
|
|
1060
|
+
print(` backend: ${backendBaseUrl}`);
|
|
1061
|
+
print(` dashboard: ${dashboardBaseUrl}`);
|
|
1062
|
+
}
|
|
955
1063
|
const flow = new DeviceFlowClient({
|
|
956
|
-
backendBaseUrl
|
|
957
|
-
dashboardBaseUrl
|
|
1064
|
+
backendBaseUrl,
|
|
1065
|
+
dashboardBaseUrl,
|
|
958
1066
|
requesterMetadata: {
|
|
959
1067
|
processName: detectMcpHost(),
|
|
960
1068
|
hostname: hostname(),
|
|
@@ -1069,7 +1177,13 @@ async function runDoctor() {
|
|
|
1069
1177
|
});
|
|
1070
1178
|
try {
|
|
1071
1179
|
const h = await broker.hello();
|
|
1072
|
-
|
|
1180
|
+
const hasKey = h.hasSessionKey ?? true;
|
|
1181
|
+
const keyTag = hasKey ? `signer ${h.sessionKeyAddress}` : "NO SESSION KEY (read-only posture)";
|
|
1182
|
+
print(`Broker daemon : reachable (proto v${h.version}, ${keyTag}, hasJwt=${h.hasJwt})`);
|
|
1183
|
+
if (h.effectiveConfig) {
|
|
1184
|
+
print(`Daemon backend URL: ${h.effectiveConfig.backendBaseUrl}`);
|
|
1185
|
+
print(`Daemon dashboard : ${h.effectiveConfig.dashboardBaseUrl}`);
|
|
1186
|
+
}
|
|
1073
1187
|
return 0;
|
|
1074
1188
|
} catch (err) {
|
|
1075
1189
|
print(`Broker daemon : NOT reachable (${err.message})`);
|
|
@@ -1082,6 +1196,7 @@ function printUsage() {
|
|
|
1082
1196
|
print("");
|
|
1083
1197
|
print(" (no subcommand) Run the daemon (production mode)");
|
|
1084
1198
|
print(" login Acquire a JWT via the device-code flow + store in keystore");
|
|
1199
|
+
print(" [--from-daemon] resolves backend/dashboard URLs from the running daemon");
|
|
1085
1200
|
print(" logout Clear the JWT from the keystore");
|
|
1086
1201
|
print(" doctor Print environment + keystore + reachability report");
|
|
1087
1202
|
print(" -h, --help Show this help");
|
|
@@ -1109,4 +1224,4 @@ async function runCli(argv) {
|
|
|
1109
1224
|
}
|
|
1110
1225
|
}
|
|
1111
1226
|
|
|
1112
|
-
export { runCli, runDoctor, runLogin, runLogout };
|
|
1227
|
+
export { parseLoginFlags, runCli, runDoctor, runLogin, runLogout };
|