@mcpbox/mcpbox-claude 0.1.2 → 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/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # @mcpbox/mcpbox-claude
2
2
 
3
- MCPBox agent harness for **Claude Code**, **Codex**, and **Cursor**. Wires the
4
- single canonical MCP server (`https://mcpbox.ru/v1/mcp` — the daruma tracker
5
- tools plus the platform pipeline `torii→satori→enma→yatagarasu→fujin→daruma`)
3
+ MCPBox agent harness for **Claude Code**, **Codex**, **Cursor**, and **Kimi Code**.
4
+ Wires the single canonical MCP server (`https://mcpbox.ru/v1/mcp` — the daruma
5
+ tracker tools plus the platform pipeline `torii→satori→enma→yatagarasu→fujin→daruma`)
6
6
  and drops a managed policy into each agent's native config file.
7
7
 
8
8
  Normally installed for you by `npx @mcpbox/mcpbox setup` (or
@@ -12,11 +12,22 @@ just the policy.
12
12
 
13
13
  ## What it manages
14
14
 
15
- | Agent | Policy surface | MCP server |
16
- |--------|----------------------------------------|-------------------------------------|
17
- | Claude | `CLAUDE.md` block + plugin (hooks, `/mcpbox:*`) | `claude mcp add` (`.mcp.json` / `~/.claude.json`) |
18
- | Codex | `AGENTS.md` block (`mcpbox:policy`) | `[mcp_servers.mcpbox]` in `~/.codex/config.toml` |
19
- | Cursor | `.cursor/rules/mcpbox-policy.mdc` (`alwaysApply`) | `mcpbox` entry in `.cursor/mcp.json` |
15
+ | Agent | Policy surface | MCP server | Hooks |
16
+ |--------|----------------------------------------|-------------------------------------|-------|
17
+ | Claude | `CLAUDE.md` block + plugin (hooks, `/mcpbox:*`) | `claude mcp add` (`.mcp.json` / `~/.claude.json`) | plugin `hooks.json` |
18
+ | Codex | `AGENTS.md` block (`mcpbox:policy`) | `[mcp_servers.mcpbox]` in `~/.codex/config.toml` | — |
19
+ | Cursor | `.cursor/rules/mcpbox-policy.mdc` (`alwaysApply`) | `mcpbox` entry in `.cursor/mcp.json` | — |
20
+ | Kimi | `AGENTS.md` block (`mcpbox:policy`) | `mcpbox` entry in `~/.kimi-code/mcp.json` | `[[hooks]]` block in `~/.kimi-code/config.toml` |
21
+
22
+ Kimi specifics:
23
+
24
+ - MCP and hooks are **user-level** (Kimi reads hooks only from
25
+ `~/.kimi-code/config.toml`; `$KIMI_CODE_HOME` is honored). One
26
+ `/mcp-config login mcpbox` OAuth then covers every project.
27
+ - The hook scripts (`SessionStart` task summary, `UserPromptSubmit` routing
28
+ hints, `Stop` auto-record nudge) are the same ones the Claude plugin ships;
29
+ `kimi-init` copies them into `~/.kimi-code/mcpbox/` so the config never
30
+ references an ephemeral npx cache path. `kimi-uninit` removes that directory.
20
31
 
21
32
  ## CLI
22
33
 
@@ -27,6 +38,9 @@ mcpbox-claude codex-init [--dir DIR] AGENTS.md policy + ~/.codex/config.toml
27
38
  mcpbox-claude codex-uninit [--dir DIR]
28
39
  mcpbox-claude cursor-init [--dir DIR] .cursor/rules policy + .cursor/mcp.json server.
29
40
  mcpbox-claude cursor-uninit [--dir DIR]
41
+ mcpbox-claude kimi-init [--dir DIR] AGENTS.md policy + ~/.kimi-code/mcp.json server
42
+ + ~/.kimi-code/config.toml [[hooks]].
43
+ mcpbox-claude kimi-uninit [--dir DIR]
30
44
  mcpbox-claude doctor [--json] Check the mcpbox MCP server + policy are wired.
31
45
  ```
32
46
 
@@ -20,11 +20,12 @@ import { spawnSync } from "node:child_process";
20
20
  import { installPolicy, removePolicy, BEGIN } from "../lib/policy.mjs";
21
21
  import { installCodex, removeCodex, installCodexPolicy } from "../lib/codex.mjs";
22
22
  import { installCursor, removeCursor, installCursorRule } from "../lib/cursor.mjs";
23
+ import { installKimi, removeKimi } from "../lib/kimi.mjs";
23
24
 
24
25
  const __dirname = dirname(fileURLToPath(import.meta.url));
25
26
  const pkg = JSON.parse(readFileSync(join(__dirname, "..", "package.json"), "utf8"));
26
27
 
27
- const HELP = `mcpbox-claude v${pkg.version} — MCPBox agent harness (Claude / Codex / Cursor)
28
+ const HELP = `mcpbox-claude v${pkg.version} — MCPBox agent harness (Claude / Codex / Cursor / Kimi)
28
29
 
29
30
  Usage:
30
31
  mcpbox-claude init [--dir DIR] Write the managed CLAUDE.md policy block
@@ -36,6 +37,10 @@ Usage:
36
37
  mcpbox-claude cursor-init [--dir DIR] Write .cursor/rules/mcpbox-policy.mdc +
37
38
  .cursor/mcp.json mcpbox entry. Idempotent.
38
39
  mcpbox-claude cursor-uninit [--dir DIR]
40
+ mcpbox-claude kimi-init [--dir DIR] Write AGENTS.md policy + ~/.kimi-code/mcp.json
41
+ mcpbox entry + [[hooks]] in ~/.kimi-code/config.toml.
42
+ Idempotent.
43
+ mcpbox-claude kimi-uninit [--dir DIR]
39
44
  mcpbox-claude doctor [--json] Check the mcpbox MCP server + policy are wired.
40
45
  mcpbox-claude --version | -v
41
46
  mcpbox-claude --help | -h
@@ -110,6 +115,22 @@ async function cmdCursorUninit(rest) {
110
115
  report(mcp);
111
116
  }
112
117
 
118
+ async function cmdKimiInit(rest) {
119
+ const { projectDir } = parseDirFlag(rest);
120
+ const { policy, mcp, hooks } = await installKimi({ projectDir });
121
+ report(policy);
122
+ report(mcp);
123
+ report(hooks);
124
+ }
125
+
126
+ async function cmdKimiUninit(rest) {
127
+ const { projectDir } = parseDirFlag(rest);
128
+ const { policy, mcp, hooks } = await removeKimi({ projectDir });
129
+ report(policy);
130
+ report(mcp);
131
+ report(hooks);
132
+ }
133
+
113
134
  // Policy-only variants — for callers (mcpbox-cli setup) that already own the
114
135
  // MCP-server wiring and just need the managed policy file dropped.
115
136
  async function cmdCodexPolicy(rest) {
@@ -186,6 +207,10 @@ async function main(argv) {
186
207
  return cmdCursorInit(rest);
187
208
  case "cursor-uninit":
188
209
  return cmdCursorUninit(rest);
210
+ case "kimi-init":
211
+ return cmdKimiInit(rest);
212
+ case "kimi-uninit":
213
+ return cmdKimiUninit(rest);
189
214
  case "codex-policy":
190
215
  return cmdCodexPolicy(rest);
191
216
  case "cursor-policy":
@@ -1,12 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
  // UserPromptSubmit hook: detect capture / sync / status / close / pipeline
3
- // intent in the user's prompt and prepend a short routing hint so Claude routes
4
- // to the right slash command rather than improvising.
3
+ // intent in the user's prompt and prepend a short routing hint so the agent
4
+ // routes to the right slash command rather than improvising.
5
5
  //
6
- // Output goes to stdout — Claude Code injects it as a <system-reminder>.
7
- // Exit 0 always; this hook must never block a prompt from being submitted.
6
+ // Output goes to stdout — Claude Code injects it as a <system-reminder>; Kimi
7
+ // Code appends it to the context on exit 0. Exit 0 always; this hook must
8
+ // never block a prompt from being submitted.
8
9
  //
9
- // Reads the prompt text from CLAUDE_USER_PROMPT env var (set by Claude Code).
10
+ // Prompt source: CLAUDE_USER_PROMPT env var (Claude Code); otherwise the hook
11
+ // event JSON on stdin (Kimi Code passes the payload via stdin, and so does
12
+ // Claude Code — this also covers Claude setups without the env var).
10
13
 
11
14
  import { pathToFileURL } from "node:url";
12
15
 
@@ -55,8 +58,48 @@ export function promptSubmitHint(promptText = "") {
55
58
  return "";
56
59
  }
57
60
 
58
- function main() {
59
- const hint = promptSubmitHint(process.env.CLAUDE_USER_PROMPT ?? "");
61
+ // Reads stdin to EOF with a timeout so a hook caller that keeps stdin open
62
+ // can never hang the prompt submit path.
63
+ function readStdin(timeoutMs = 3000) {
64
+ return new Promise((resolve) => {
65
+ if (process.stdin.isTTY) return resolve("");
66
+ let raw = "";
67
+ const done = () => {
68
+ clearTimeout(timer);
69
+ resolve(raw);
70
+ };
71
+ const timer = setTimeout(() => {
72
+ process.stdin.destroy();
73
+ resolve(raw);
74
+ }, timeoutMs);
75
+ process.stdin.setEncoding("utf8");
76
+ process.stdin.on("data", (chunk) => { raw += chunk; });
77
+ process.stdin.on("end", done);
78
+ process.stdin.on("error", done);
79
+ });
80
+ }
81
+
82
+ // Kimi's UserPromptSubmit payload carries the prompt text; the exact field
83
+ // name is not documented, so accept the common shapes.
84
+ export function promptFromHookPayload(payload) {
85
+ if (!payload || typeof payload !== "object") return "";
86
+ for (const key of ["prompt", "user_prompt", "userPrompt", "message", "text"]) {
87
+ if (typeof payload[key] === "string" && payload[key].trim()) return payload[key];
88
+ }
89
+ return "";
90
+ }
91
+
92
+ async function main() {
93
+ let prompt = process.env.CLAUDE_USER_PROMPT ?? "";
94
+ if (!prompt) {
95
+ const raw = await readStdin();
96
+ if (raw.trim()) {
97
+ try {
98
+ prompt = promptFromHookPayload(JSON.parse(raw));
99
+ } catch { /* not JSON — no prompt to route */ }
100
+ }
101
+ }
102
+ const hint = promptSubmitHint(prompt);
60
103
  if (hint) process.stdout.write(hint + "\n");
61
104
  }
62
105
 
package/lib/kimi.mjs ADDED
@@ -0,0 +1,191 @@
1
+ // Kimi Code CLI surface for the mcpbox harness — parity with the Claude
2
+ // CLAUDE.md plugin and the Codex AGENTS.md surface. Three idempotent writes:
3
+ // 1. AGENTS.md policy block (Kimi reads AGENTS.md every session in the
4
+ // workspace) — same managed block as the Codex surface.
5
+ // 2. `$KIMI_CODE_HOME/mcp.json` (default `~/.kimi-code/mcp.json`) — upsert
6
+ // the `mcpbox` HTTP server entry, preserving other servers. A bare `url`
7
+ // entry means HTTP transport in Kimi. User-level on purpose: one server
8
+ // entry (and one `/mcp-config login mcpbox` OAuth) covers every project.
9
+ // 3. `$KIMI_CODE_HOME/config.toml` — managed `[[hooks]]` block pointing at
10
+ // the hook scripts copied into `$KIMI_CODE_HOME/mcpbox/`. Kimi reads
11
+ // hooks only from the user-level config.toml (no project-level hooks),
12
+ // so — like the Codex MCP entry — the block is fenced with comment
13
+ // markers and replaced in place; surrounding hand-written TOML is
14
+ // preserved.
15
+ //
16
+ // The policy body is BLOCK_BODY from lib/policy.mjs — one source of truth
17
+ // shared across CLAUDE.md / AGENTS.md / .cursor/rules.
18
+ //
19
+ // Hook semantics in Kimi (https://www.kimi.com/code/docs/en/kimi-code-cli/customization/hooks.html):
20
+ // stdin carries the event JSON; exit 0 + stdout appends text to the context;
21
+ // SessionStart/UserPromptSubmit/Stop all exist. The hook scripts are the same
22
+ // ones the Claude plugin ships (hooks/), copied into the Kimi home so the
23
+ // config never references an ephemeral npx cache path.
24
+
25
+ import { promises as fs } from "node:fs";
26
+ import { homedir } from "node:os";
27
+ import { dirname, join, resolve } from "node:path";
28
+ import { fileURLToPath } from "node:url";
29
+ import {
30
+ BLOCK_BODY,
31
+ MCP_URL,
32
+ installManagedBlock,
33
+ removeManagedBlock,
34
+ } from "./policy.mjs";
35
+
36
+ const BEGIN = "<!-- mcpbox:policy:begin -->";
37
+ const END = "<!-- mcpbox:policy:end -->";
38
+
39
+ const TOML_BEGIN = "# mcpbox:hooks:begin";
40
+ const TOML_END = "# mcpbox:hooks:end";
41
+
42
+ // Hook assets copied into the Kimi home: the three hook scripts plus the
43
+ // stdio MCP client session-start.mjs imports (../lib/mcp-client.mjs).
44
+ const HOOK_ASSETS = [
45
+ ["hooks", "session-start.mjs"],
46
+ ["hooks", "user-prompt-submit.mjs"],
47
+ ["hooks", "stop.mjs"],
48
+ ["lib", "mcp-client.mjs"],
49
+ ];
50
+
51
+ const MCP_ENTRY = { url: MCP_URL };
52
+
53
+ function packageRoot() {
54
+ return resolve(dirname(fileURLToPath(import.meta.url)), "..");
55
+ }
56
+
57
+ // Kimi data dir: explicit override (tests) > $KIMI_CODE_HOME > ~/.kimi-code.
58
+ export function kimiHomeDir(home) {
59
+ if (home) return resolve(home);
60
+ const env = process.env.KIMI_CODE_HOME?.trim();
61
+ if (env) return env;
62
+ return join(homedir(), ".kimi-code");
63
+ }
64
+
65
+ // --- 1. AGENTS.md policy block in the project directory ---
66
+
67
+ export function installKimiPolicy({ projectDir } = {}) {
68
+ const dir = projectDir ? resolve(projectDir) : process.cwd();
69
+ return installManagedBlock({ target: join(dir, "AGENTS.md"), begin: BEGIN, end: END, block: BLOCK_BODY });
70
+ }
71
+
72
+ export function removeKimiPolicy({ projectDir } = {}) {
73
+ const dir = projectDir ? resolve(projectDir) : process.cwd();
74
+ return removeManagedBlock({ target: join(dir, "AGENTS.md"), begin: BEGIN, end: END });
75
+ }
76
+
77
+ // --- 2. `mcpbox` entry in the user-level mcp.json ---
78
+
79
+ async function readMcp(target) {
80
+ try {
81
+ const doc = JSON.parse(await fs.readFile(target, "utf8"));
82
+ if (!doc || typeof doc !== "object" || Array.isArray(doc)) {
83
+ throw new Error(`${target} is not a JSON object`);
84
+ }
85
+ if (!doc.mcpServers || typeof doc.mcpServers !== "object") doc.mcpServers = {};
86
+ return doc;
87
+ } catch (err) {
88
+ if (err.code === "ENOENT") return { mcpServers: {} };
89
+ throw err;
90
+ }
91
+ }
92
+
93
+ export async function installKimiMcp({ mcpPath, kimiHome } = {}) {
94
+ const target = mcpPath ? resolve(mcpPath) : join(kimiHomeDir(kimiHome), "mcp.json");
95
+ const doc = await readMcp(target);
96
+ const before = doc.mcpServers.mcpbox;
97
+ if (before && JSON.stringify(before) === JSON.stringify(MCP_ENTRY)) {
98
+ return { action: "unchanged", path: target };
99
+ }
100
+ doc.mcpServers.mcpbox = MCP_ENTRY;
101
+ await fs.mkdir(dirname(target), { recursive: true });
102
+ await fs.writeFile(target, JSON.stringify(doc, null, 2) + "\n");
103
+ return { action: before ? "updated" : "installed", path: target };
104
+ }
105
+
106
+ export async function removeKimiMcp({ mcpPath, kimiHome } = {}) {
107
+ const target = mcpPath ? resolve(mcpPath) : join(kimiHomeDir(kimiHome), "mcp.json");
108
+ let doc;
109
+ try {
110
+ doc = await readMcp(target);
111
+ } catch (err) {
112
+ if (err.code === "ENOENT") return { action: "missing", path: target };
113
+ throw err;
114
+ }
115
+ if (!doc.mcpServers.mcpbox) return { action: "missing", path: target };
116
+ delete doc.mcpServers.mcpbox;
117
+ await fs.writeFile(target, JSON.stringify(doc, null, 2) + "\n");
118
+ return { action: "removed-block", path: target };
119
+ }
120
+
121
+ // --- 3. Hooks: copied scripts + managed [[hooks]] block in config.toml ---
122
+
123
+ // TOML literal strings (single quotes) keep absolute paths with spaces or
124
+ // backslashes intact without escaping.
125
+ function hooksTomlBlock(assetsRoot) {
126
+ const command = (file) => `command = 'node "${join(assetsRoot, "hooks", file)}"'`;
127
+ return [
128
+ "[[hooks]]",
129
+ 'event = "SessionStart"',
130
+ command("session-start.mjs"),
131
+ "timeout = 15",
132
+ "",
133
+ "[[hooks]]",
134
+ 'event = "UserPromptSubmit"',
135
+ command("user-prompt-submit.mjs"),
136
+ "",
137
+ "[[hooks]]",
138
+ 'event = "Stop"',
139
+ command("stop.mjs"),
140
+ "",
141
+ ].join("\n");
142
+ }
143
+
144
+ async function copyHookAssets(assetsRoot) {
145
+ for (const [subdir, file] of HOOK_ASSETS) {
146
+ const target = join(assetsRoot, subdir, file);
147
+ await fs.mkdir(dirname(target), { recursive: true });
148
+ await fs.copyFile(join(packageRoot(), subdir, file), target);
149
+ }
150
+ }
151
+
152
+ export async function installKimiHooks({ configPath, kimiHome } = {}) {
153
+ const home = kimiHomeDir(kimiHome);
154
+ const assetsRoot = join(home, "mcpbox");
155
+ await copyHookAssets(assetsRoot);
156
+ const target = configPath ? resolve(configPath) : join(home, "config.toml");
157
+ return installManagedBlock({
158
+ target,
159
+ begin: TOML_BEGIN,
160
+ end: TOML_END,
161
+ block: hooksTomlBlock(assetsRoot),
162
+ });
163
+ }
164
+
165
+ export async function removeKimiHooks({ configPath, kimiHome } = {}) {
166
+ const home = kimiHomeDir(kimiHome);
167
+ const target = configPath ? resolve(configPath) : join(home, "config.toml");
168
+ const block = await removeManagedBlock({ target, begin: TOML_BEGIN, end: TOML_END });
169
+ // The assets dir is ours (namespaced `mcpbox` under the Kimi home) — drop it.
170
+ const assetsRoot = join(home, "mcpbox");
171
+ await fs.rm(assetsRoot, { recursive: true, force: true });
172
+ return block;
173
+ }
174
+
175
+ // --- All three surfaces at once (what `mcpbox-claude kimi-init` calls) ---
176
+
177
+ export async function installKimi({ projectDir, kimiHome } = {}) {
178
+ const policy = await installKimiPolicy({ projectDir });
179
+ const mcp = await installKimiMcp({ kimiHome });
180
+ const hooks = await installKimiHooks({ kimiHome });
181
+ return { policy, mcp, hooks };
182
+ }
183
+
184
+ export async function removeKimi({ projectDir, kimiHome } = {}) {
185
+ const policy = await removeKimiPolicy({ projectDir });
186
+ const mcp = await removeKimiMcp({ kimiHome });
187
+ const hooks = await removeKimiHooks({ kimiHome });
188
+ return { policy, mcp, hooks };
189
+ }
190
+
191
+ export { BEGIN, END, TOML_BEGIN, TOML_END };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcpbox/mcpbox-claude",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "MCPBox agent harness for Claude Code: managed policy (daruma tracker + full pipeline), hooks, and slash commands over the single mcpbox MCP server.",
5
5
  "type": "module",
6
6
  "publishConfig": {