Skip to main content

mkit_cli/commands/
mcp.rs

1//! `mkit mcp` — a local Model Context Protocol server over stdio.
2//!
3//! Exposes a conservative subset of mkit as MCP tools so LLM agents can
4//! read, search, and manipulate local mkit repositories — the mkit
5//! analog of the reference `mcp-server-git`. Design choices follow that
6//! template where it is right and diverge where mkit is stronger:
7//!
8//! * **Local + stdio.** Newline-delimited JSON-RPC 2.0 on stdin/stdout,
9//!   processed sequentially. No async runtime: the loop is plain
10//!   blocking I/O, keeping the default build tokio-free.
11//! * **Subprocess execution.** Each tool call re-invokes this same
12//!   binary (`std::env::current_exe()`) with a structured argv — never
13//!   a shell — capturing stdout/stderr and the sysexits code. The MCP
14//!   loop owns this process's stdout for the protocol, so tools must
15//!   not print here; subprocess isolation guarantees that, and the
16//!   server version always equals the CLI version.
17//! * **Conservative surface.** Like the git template: no network ops
18//!   (push/pull/fetch/clone), no history surgery (merge/rebase/
19//!   cherry-pick/revert), no worktree destruction (reset --hard /
20//!   clean / rm). The server never passes `-f`/`--force`, so mkit's
21//!   own data-loss guards remain the backstop. Unlike the template:
22//!   first-class signing + attestation tools — the reason mkit exists.
23//! * **Scoping.** `--repository <path>` confines every `repo_path`
24//!   argument (symlink-resolved) to that root. Without the flag any
25//!   path the process can reach is allowed (client-trust mode), same
26//!   as the git template.
27//! * **Injection defense.** Path/ref-like arguments are rejected if
28//!   they begin with `-` so a value can never be parsed as a flag by
29//!   the child CLI (which has no `--` separator on `add`).
30
31use std::io::{BufRead, Write};
32use std::path::{Path, PathBuf};
33
34use clap::Parser;
35use serde_json::{Value, json};
36
37use crate::clap_shim;
38use crate::exit;
39
40#[derive(Debug, Parser)]
41struct McpOpts {
42    /// Confine all tool calls to this repository path (and its
43    /// subdirectories). Strongly recommended for agent use.
44    #[arg(long, short = 'r', value_name = "PATH")]
45    repository: Option<PathBuf>,
46}
47
48/// Entry point for `mkit mcp`.
49#[must_use]
50pub fn run(args: &[String]) -> u8 {
51    let opts = match clap_shim::parse::<McpOpts>("mkit mcp", args) {
52        Ok(o) => o,
53        Err(code) => return code,
54    };
55    let allowed = match &opts.repository {
56        Some(p) => match p.canonicalize() {
57            Ok(c) => Some(c),
58            Err(e) => {
59                let mut stderr = std::io::stderr().lock();
60                let _ = writeln!(stderr, "error: --repository {}: {e}", p.display());
61                return exit::NOINPUT;
62            }
63        },
64        None => None,
65    };
66    serve(allowed.as_deref())
67}
68
69/// Blocking JSON-RPC loop: one message per line, responses flushed
70/// immediately. Returns when stdin reaches EOF (client disconnect).
71fn serve(allowed: Option<&Path>) -> u8 {
72    let stdin = std::io::stdin();
73    let mut stdout = std::io::stdout().lock();
74    // MCP lifecycle: `initialize` must precede any tool traffic.
75    let mut initialized = false;
76
77    for line in stdin.lock().lines() {
78        let Ok(line) = line else { break };
79        if line.trim().is_empty() {
80            continue;
81        }
82        let parsed: Result<Value, _> = serde_json::from_str(&line);
83        let (messages, is_batch): (Vec<Value>, bool) = match parsed {
84            // A few legacy clients batch; MCP 2025+ forbids it, but
85            // handling an array costs nothing. Per JSON-RPC 2.0, a batch
86            // request gets ONE batch (array) response — and a batch of
87            // only notifications gets no response at all.
88            Ok(Value::Array(batch)) => (batch, true),
89            Ok(v) => (vec![v], false),
90            Err(_) => {
91                write_msg(
92                    &mut stdout,
93                    &json!({
94                        "jsonrpc": "2.0",
95                        "id": null,
96                        "error": { "code": -32700, "message": "parse error" }
97                    }),
98                );
99                continue;
100            }
101        };
102        let responses: Vec<Value> = messages
103            .iter()
104            .filter_map(|msg| handle_message(msg, allowed, &mut initialized))
105            .collect();
106        if is_batch {
107            if !responses.is_empty() {
108                write_msg(&mut stdout, &Value::Array(responses));
109            }
110        } else if let Some(response) = responses.into_iter().next() {
111            write_msg(&mut stdout, &response);
112        }
113    }
114    exit::OK
115}
116
117/// Protocol revisions this server interoperates with. The wire framing
118/// and tools surface are common across these; `initialize` negotiates
119/// the requested one when supported, else falls back to the latest.
120const SUPPORTED_PROTOCOLS: &[&str] = &["2025-06-18", "2025-03-26", "2024-11-05"];
121const LATEST_PROTOCOL: &str = "2025-06-18";
122
123fn write_msg(stdout: &mut impl Write, msg: &Value) {
124    // serde_json compact form contains no raw newlines, so one
125    // message per line is structurally guaranteed.
126    if let Ok(s) = serde_json::to_string(msg) {
127        let _ = writeln!(stdout, "{s}");
128        let _ = stdout.flush();
129    }
130}
131
132/// Dispatch one JSON-RPC message. Returns `None` for notifications
133/// (nothing is written back). `initialized` tracks the MCP lifecycle:
134/// set on `initialize`, required before any tool traffic.
135fn handle_message(msg: &Value, allowed: Option<&Path>, initialized: &mut bool) -> Option<Value> {
136    let method = msg.get("method").and_then(Value::as_str)?;
137    let id = msg.get("id");
138    match (method, id) {
139        // ---- notifications (no response) --------------------------
140        (_, None | Some(Value::Null)) => None,
141        // ---- requests ----------------------------------------------
142        ("initialize", Some(id)) => {
143            *initialized = true;
144            // Negotiate the protocol: honor the client's requested
145            // revision when we support it, else return our latest so
146            // the client can decide — never claim an arbitrary version.
147            let requested = msg
148                .pointer("/params/protocolVersion")
149                .and_then(Value::as_str);
150            let version = match requested {
151                Some(v) if SUPPORTED_PROTOCOLS.contains(&v) => v,
152                _ => LATEST_PROTOCOL,
153            };
154            Some(json!({
155                "jsonrpc": "2.0",
156                "id": id,
157                "result": {
158                    "protocolVersion": version,
159                    "capabilities": { "tools": {} },
160                    "serverInfo": { "name": "mkit-repo", "version": crate::cli::CLI_VERSION },
161                    "instructions": INSTRUCTIONS,
162                }
163            }))
164        }
165        ("ping", Some(id)) => Some(json!({ "jsonrpc": "2.0", "id": id, "result": {} })),
166        // Tool traffic is rejected until the client has initialized.
167        ("tools/list" | "tools/call", Some(id)) if !*initialized => Some(json!({
168            "jsonrpc": "2.0",
169            "id": id,
170            "error": { "code": -32002, "message": "server not initialized: send `initialize` first" }
171        })),
172        ("tools/list", Some(id)) => Some(json!({
173            "jsonrpc": "2.0",
174            "id": id,
175            "result": { "tools": tool_descriptors() }
176        })),
177        ("tools/call", Some(id)) => {
178            let name = msg
179                .pointer("/params/name")
180                .and_then(Value::as_str)
181                .unwrap_or("");
182            let empty = json!({});
183            let args = msg.pointer("/params/arguments").unwrap_or(&empty);
184            match call_tool(name, args, allowed) {
185                Ok(CallOutcome { text, is_error }) => Some(json!({
186                    "jsonrpc": "2.0",
187                    "id": id,
188                    "result": {
189                        "content": [ { "type": "text", "text": text } ],
190                        "isError": is_error,
191                    }
192                })),
193                Err(protocol_err) => Some(json!({
194                    "jsonrpc": "2.0",
195                    "id": id,
196                    "error": { "code": -32602, "message": protocol_err }
197                })),
198            }
199        }
200        (_, Some(id)) => Some(json!({
201            "jsonrpc": "2.0",
202            "id": id,
203            "error": { "code": -32601, "message": format!("method not found: {method}") }
204        })),
205    }
206}
207
208const INSTRUCTIONS: &str = "Operate local mkit repositories (content-addressed VCS with \
209Ed25519-signed commits and in-toto/DSSE attestation). Every tool takes a repo_path. \
210Typical flow: mkit_init -> mkit_keygen (REQUIRED before the first commit) -> mkit_add -> \
211mkit_commit -> mkit_log/mkit_show. Differentiators: mkit_verify (check a commit/tag \
212signature), mkit_attest (attach a signed DSSE attestation), mkit_verify_attest (verify \
213attestations against trust roots), mkit_cat_object (inspect content-addressed objects). \
214This server runs no network operations (push/pull/fetch/clone), no history surgery \
215(merge/rebase/cherry-pick), and never overrides mkit's data-loss guards; a 'refuses \
216without -f' error means run that operation outside the MCP, deliberately. Path rules: an \
217attest predicate_file must resolve INSIDE the repo; a verify_attest trust_roots path must \
218resolve OUTSIDE it. For docs/specs/source of mkit itself, use the separate mkit docs MCP \
219(mcp.mkit.makechain.net).";
220
221// ---------------------------------------------------------------------------
222// Tool table
223// ---------------------------------------------------------------------------
224
225struct ToolSpec {
226    name: &'static str,
227    description: &'static str,
228    /// (`read_only`, `destructive`, `idempotent`)
229    hints: (bool, bool, bool),
230    schema: fn() -> Value,
231}
232
233fn prop(desc: &str) -> Value {
234    json!({ "type": "string", "description": desc })
235}
236
237fn schema(props: Vec<(&str, Value)>, required: &[&str]) -> Value {
238    let mut map = serde_json::Map::new();
239    for (k, v) in props {
240        map.insert(k.to_string(), v);
241    }
242    json!({ "type": "object", "properties": Value::Object(map), "required": required })
243}
244
245fn repo_prop() -> (&'static str, Value) {
246    (
247        "repo_path",
248        prop("Path to the mkit repository (the directory containing .mkit/)"),
249    )
250}
251
252const TOOLS: &[ToolSpec] = &[
253    ToolSpec {
254        name: "mkit_status",
255        description: "Show staged and working-tree changes (porcelain v2; empty means clean).",
256        hints: (true, false, true),
257        schema: || schema(vec![repo_prop()], &["repo_path"]),
258    },
259    ToolSpec {
260        name: "mkit_diff_unstaged",
261        description: "Show changes in the working directory that are not yet staged.",
262        hints: (true, false, true),
263        schema: || schema(vec![repo_prop()], &["repo_path"]),
264    },
265    ToolSpec {
266        name: "mkit_diff_staged",
267        description: "Show changes staged for the next commit.",
268        hints: (true, false, true),
269        schema: || schema(vec![repo_prop()], &["repo_path"]),
270    },
271    ToolSpec {
272        name: "mkit_diff",
273        description: "Show the diff against a target revision (branch, tag, or 64-hex BLAKE3 id).",
274        hints: (true, false, true),
275        schema: || {
276            schema(
277                vec![repo_prop(), ("target", prop("Revision to diff against"))],
278                &["repo_path", "target"],
279            )
280        },
281    },
282    ToolSpec {
283        name: "mkit_log",
284        description: "Show commit history as JSONL (hash, author identity, timestamp, message).",
285        hints: (true, false, true),
286        schema: || {
287            schema(
288                vec![
289                    repo_prop(),
290                    (
291                        "max_count",
292                        json!({ "type": "integer", "description": "Maximum commits to show (default 10)" }),
293                    ),
294                    (
295                        "rev",
296                        prop("Optional revision (or A..B range) to start the walk from"),
297                    ),
298                ],
299                &["repo_path"],
300            )
301        },
302    },
303    ToolSpec {
304        name: "mkit_show",
305        description: "Show an object: a commit with its diff, a tag, a tree listing, or blob contents.",
306        hints: (true, false, true),
307        schema: || {
308            schema(
309                vec![
310                    repo_prop(),
311                    ("revision", prop("Revision or object id to show")),
312                ],
313                &["repo_path", "revision"],
314            )
315        },
316    },
317    ToolSpec {
318        name: "mkit_branch",
319        description: "List branches as JSONL (current branch marked).",
320        hints: (true, false, true),
321        schema: || schema(vec![repo_prop()], &["repo_path"]),
322    },
323    ToolSpec {
324        name: "mkit_cat_object",
325        description: "Inspect a content-addressed object: its type, size, or pretty-printed content.",
326        hints: (true, false, true),
327        schema: || {
328            schema(
329                vec![
330                    repo_prop(),
331                    (
332                        "object",
333                        prop("Object id (64-hex BLAKE3, prefix accepted) or revision"),
334                    ),
335                    (
336                        "mode",
337                        json!({ "type": "string", "enum": ["type", "size", "pretty"], "description": "What to show (default: pretty)" }),
338                    ),
339                ],
340                &["repo_path", "object"],
341            )
342        },
343    },
344    ToolSpec {
345        name: "mkit_verify",
346        description: "Verify the Ed25519 signature on a commit, remix, or signed tag.",
347        hints: (true, false, true),
348        schema: || {
349            schema(
350                vec![
351                    repo_prop(),
352                    ("revision", prop("Revision to verify (e.g. HEAD)")),
353                ],
354                &["repo_path", "revision"],
355            )
356        },
357    },
358    ToolSpec {
359        name: "mkit_verify_attest",
360        description: "Verify every DSSE attestation attached to a commit against a trust-roots \
361                      registry. Defaults to the user-scoped trust-roots file; a trust_roots path \
362                      inside the repository is always rejected here (hostile-clone defense — \
363                      planted in-repo roots can never be selected through the MCP).",
364        hints: (true, false, true),
365        schema: || {
366            schema(
367                vec![
368                    repo_prop(),
369                    (
370                        "commit",
371                        prop("Commit hash to verify, or \"HEAD\" / omit for the current commit"),
372                    ),
373                    (
374                        "trust_roots",
375                        prop(
376                            "Path to a trust-roots TOML file OUTSIDE the repo (default: \
377                             $XDG_CONFIG_HOME/mkit/trust-roots.toml). An in-repo path is rejected.",
378                        ),
379                    ),
380                    (
381                        "algorithm",
382                        json!({ "type": "string", "enum": ["ed25519", "secp256k1", "p256"], "description": "Only report signatures of this algorithm" }),
383                    ),
384                ],
385                &["repo_path"],
386            )
387        },
388    },
389    ToolSpec {
390        name: "mkit_add",
391        description: "Stage files for the next commit. Pass explicit paths (\".\" stages everything \
392                      non-ignored under the repo root).",
393        hints: (false, false, true),
394        schema: || {
395            schema(
396                vec![
397                    repo_prop(),
398                    (
399                        "files",
400                        json!({ "type": "array", "items": { "type": "string" }, "description": "Paths to stage" }),
401                    ),
402                ],
403                &["repo_path", "files"],
404            )
405        },
406    },
407    ToolSpec {
408        name: "mkit_unstage",
409        description: "Unstage changes: with files, restores those index entries from HEAD; without, \
410                      unstages everything (mixed reset). Never touches the working tree.",
411        hints: (false, true, true),
412        schema: || {
413            schema(
414                vec![
415                    repo_prop(),
416                    (
417                        "files",
418                        json!({ "type": "array", "items": { "type": "string" }, "description": "Paths to unstage (omit to unstage all)" }),
419                    ),
420                ],
421                &["repo_path"],
422            )
423        },
424    },
425    ToolSpec {
426        name: "mkit_commit",
427        description: "Create an Ed25519-signed commit from the staging index. Requires a signing \
428                      key (mkit_keygen) — commits are always signed.",
429        hints: (false, false, false),
430        schema: || {
431            schema(
432                vec![repo_prop(), ("message", prop("Commit message"))],
433                &["repo_path", "message"],
434            )
435        },
436    },
437    ToolSpec {
438        name: "mkit_create_branch",
439        description: "Create a new branch at HEAD.",
440        hints: (false, false, false),
441        schema: || {
442            schema(
443                vec![repo_prop(), ("branch_name", prop("Name of the new branch"))],
444                &["repo_path", "branch_name"],
445            )
446        },
447    },
448    ToolSpec {
449        name: "mkit_checkout",
450        description: "Switch HEAD to a branch and restore files. Overwrites clean tracked files \
451                      and removes tracked paths absent from the target branch (dirty-worktree \
452                      changes are guarded and refuse instead).",
453        hints: (false, true, false),
454        schema: || {
455            schema(
456                vec![repo_prop(), ("branch_name", prop("Branch to switch to"))],
457                &["repo_path", "branch_name"],
458            )
459        },
460    },
461    ToolSpec {
462        name: "mkit_init",
463        description: "Create a new mkit repository (.mkit/) in repo_path. Run mkit_keygen next — \
464                      commits require a signing key.",
465        hints: (false, false, false),
466        schema: || schema(vec![repo_prop()], &["repo_path"]),
467    },
468    ToolSpec {
469        name: "mkit_keygen",
470        description: "Generate a signing key. Default (ed25519) writes the commit-signing key at \
471                      .mkit/keys/default.key; secp256k1/p256 write separate ATTESTATION signer keys \
472                      (.mkit/keys/<alg>.key) for use with mkit_attest. Refuses to overwrite.",
473        hints: (false, false, false),
474        schema: || {
475            schema(
476                vec![
477                    repo_prop(),
478                    (
479                        "algorithm",
480                        json!({ "type": "string", "enum": ["ed25519", "secp256k1", "p256"], "description": "Key algorithm (default: ed25519 = the commit key)" }),
481                    ),
482                    (
483                        "print_pubkey",
484                        json!({ "type": "boolean", "description": "Also print the public key" }),
485                    ),
486                ],
487                &["repo_path"],
488            )
489        },
490    },
491    ToolSpec {
492        name: "mkit_attest",
493        description: "Produce a signed DSSE attestation (in-toto v1 Statement) for a commit. \
494                      Prints the att-id and stores the envelope under .mkit/attestations/. \
495                      (Multi-signer envelopes and external-signer argv are intentionally NOT \
496                      exposed here — they can direct subprocess execution; use the `mkit attest` \
497                      CLI for that advanced flow.)",
498        hints: (false, false, false),
499        schema: || {
500            schema(
501                vec![
502                    repo_prop(),
503                    (
504                        "commit",
505                        prop("Commit hash to attest, or \"HEAD\" / omit for the current commit"),
506                    ),
507                    (
508                        "algorithm",
509                        json!({ "type": "string", "enum": ["ed25519", "secp256k1", "p256"], "description": "Signing algorithm (default: ed25519, always passed explicitly — user config cannot reroute the algorithm through the MCP). Non-ed25519 needs the matching mkit_keygen key." }),
510                    ),
511                    (
512                        "signer",
513                        json!({ "type": "string", "enum": ["repo-key", "keystore"], "description": "Primary signer (default: repo-key, always passed explicitly — user config cannot reroute to an external signer through the MCP)." }),
514                    ),
515                    (
516                        "predicate_type",
517                        prop("Predicate-type URI written into the Statement"),
518                    ),
519                    (
520                        "predicate_file",
521                        prop(
522                            "Path to a JSON predicate file INSIDE the repo (an outside path is rejected)",
523                        ),
524                    ),
525                ],
526                &["repo_path"],
527            )
528        },
529    },
530];
531
532fn tool_descriptors() -> Value {
533    Value::Array(
534        TOOLS
535            .iter()
536            .map(|t| {
537                let (read_only, destructive, idempotent) = t.hints;
538                json!({
539                    "name": t.name,
540                    "description": t.description,
541                    "inputSchema": (t.schema)(),
542                    "annotations": {
543                        "readOnlyHint": read_only,
544                        "destructiveHint": destructive,
545                        "idempotentHint": idempotent,
546                        "openWorldHint": false,
547                    },
548                })
549            })
550            .collect(),
551    )
552}
553
554// ---------------------------------------------------------------------------
555// Tool execution
556// ---------------------------------------------------------------------------
557
558struct CallOutcome {
559    text: String,
560    is_error: bool,
561}
562
563impl CallOutcome {
564    fn err(text: impl Into<String>) -> Self {
565        Self {
566            text: text.into(),
567            is_error: true,
568        }
569    }
570}
571
572/// `Err(_)` is a protocol-level error (unknown tool); per-call
573/// validation and execution failures come back as `Ok` with
574/// `is_error: true` so the agent sees an explanatory message.
575fn call_tool(name: &str, args: &Value, allowed: Option<&Path>) -> Result<CallOutcome, String> {
576    if !TOOLS.iter().any(|t| t.name == name) {
577        return Err(format!("unknown tool: {name}"));
578    }
579
580    let Some(repo_raw) = args.get("repo_path").and_then(Value::as_str) else {
581        return Ok(CallOutcome::err("missing required argument: repo_path"));
582    };
583    let repo = match validate_repo_path(repo_raw, allowed) {
584        Ok(p) => p,
585        Err(e) => return Ok(CallOutcome::err(e)),
586    };
587
588    // Confine path-typed arguments relative to the repo. `--repository`
589    // only constrains repo_path; predicate/trust-roots paths reach the
590    // child CLI directly, so the MCP must hold the boundary itself.
591    if let Err(e) = confine_path_args(name, args, &repo) {
592        return Ok(CallOutcome::err(e));
593    }
594
595    let command = match build_argv(name, args) {
596        Ok(a) => a,
597        Err(e) => return Ok(CallOutcome::err(e)),
598    };
599
600    Ok(run_subprocess(&repo, &command))
601}
602
603/// Enforce containment of the file-path arguments the child CLI opens
604/// itself (so `--repository` scoping can't be bypassed through them):
605///
606/// * `predicate_file` (attest) is *repo data* — it must resolve INSIDE
607///   the repo, so a prompt-injected agent can't slurp an outside file
608///   into a signed attestation.
609/// * `trust_roots` (verify-attest) is *external authority* — it must
610///   resolve OUTSIDE the repo, so a hostile clone's planted
611///   `.mkit/attest-trust-roots.toml` can never be selected via the MCP
612///   (the CLI's "explicit --trust-roots = user intent" gate assumes a
613///   user, but here the value can come from repo-controlled prompt text;
614///   see docs/THREAT-MODEL.md §"Trust-roots scope").
615fn confine_path_args(name: &str, args: &Value, repo: &Path) -> Result<(), String> {
616    match name {
617        "mkit_attest" => {
618            if let Some(f) = opt_str(args, "predicate_file") {
619                confine_path(repo, &f, Containment::Inside, "predicate_file")?;
620            }
621        }
622        "mkit_verify_attest" => {
623            if let Some(f) = opt_str(args, "trust_roots") {
624                confine_path(repo, &f, Containment::Outside, "trust_roots")?;
625            }
626        }
627        _ => {}
628    }
629    Ok(())
630}
631
632#[derive(Clone, Copy)]
633enum Containment {
634    Inside,
635    Outside,
636}
637
638/// Resolve `raw` the way the child CLI will (relative to the repo cwd,
639/// or as an absolute path) and require it to be inside / outside the
640/// repo. The target must exist (the CLI reads it), so canonicalize is
641/// the source of truth for both existence and symlink resolution.
642fn confine_path(repo: &Path, raw: &str, want: Containment, what: &str) -> Result<(), String> {
643    let candidate = if Path::new(raw).is_absolute() {
644        PathBuf::from(raw)
645    } else {
646        repo.join(raw)
647    };
648    let resolved = candidate
649        .canonicalize()
650        .map_err(|e| format!("invalid {what} '{raw}': {e}"))?;
651    let within = resolved.starts_with(repo);
652    match want {
653        Containment::Inside if !within => Err(format!(
654            "{what} '{raw}' is outside the repository; predicate files must live in the repo"
655        )),
656        Containment::Outside if within => Err(format!(
657            "{what} '{raw}' is inside the repository; trust-roots must be a user-controlled file \
658             outside the repo (hostile-clone defense — see docs/THREAT-MODEL.md)"
659        )),
660        _ => Ok(()),
661    }
662}
663
664/// Resolve and (when scoped) confine `repo_path`.
665fn validate_repo_path(raw: &str, allowed: Option<&Path>) -> Result<PathBuf, String> {
666    let resolved = PathBuf::from(raw)
667        .canonicalize()
668        .map_err(|e| format!("invalid repo_path '{raw}': {e}"))?;
669    if let Some(root) = allowed
670        && !resolved.starts_with(root)
671    {
672        return Err(format!(
673            "repo_path '{raw}' is outside the allowed repository '{}'",
674            root.display()
675        ));
676    }
677    if !resolved.is_dir() {
678        return Err(format!("repo_path '{raw}' is not a directory"));
679    }
680    Ok(resolved)
681}
682
683/// Reject values that the child CLI could parse as a flag. mkit's
684/// `add` has no `--` separator, so this is the containment line.
685fn no_dash(value: &str, what: &str) -> Result<(), String> {
686    if value.starts_with('-') {
687        return Err(format!("invalid {what} '{value}': must not start with '-'"));
688    }
689    if value.is_empty() {
690        return Err(format!("invalid {what}: must not be empty"));
691    }
692    Ok(())
693}
694
695fn req_str(args: &Value, key: &str) -> Result<String, String> {
696    args.get(key)
697        .and_then(Value::as_str)
698        .map(str::to_owned)
699        .ok_or_else(|| format!("missing required argument: {key}"))
700}
701
702fn opt_str(args: &Value, key: &str) -> Option<String> {
703    args.get(key).and_then(Value::as_str).map(str::to_owned)
704}
705
706/// Push `--commit <hash>` unless the value is "HEAD" (any case) or
707/// absent — the CLI's `--commit` parses a hex hash and rejects "HEAD",
708/// but defaults to HEAD when the flag is omitted, so map the common
709/// agent shorthand onto that default.
710fn push_commit(out: &mut Vec<String>, args: &Value) -> Result<(), String> {
711    if let Some(commit) = opt_str(args, "commit")
712        && !commit.eq_ignore_ascii_case("HEAD")
713    {
714        no_dash(&commit, "commit")?;
715        out.extend(["--commit".into(), commit]);
716    }
717    Ok(())
718}
719
720/// Validate and push `--algorithm <alg>` when present.
721fn push_algorithm(out: &mut Vec<String>, args: &Value) -> Result<(), String> {
722    if let Some(alg) = opt_str(args, "algorithm") {
723        if !matches!(alg.as_str(), "ed25519" | "secp256k1" | "p256") {
724            return Err(format!(
725                "invalid algorithm '{alg}': expected ed25519, secp256k1, or p256"
726            ));
727        }
728        out.extend(["--algorithm".into(), alg]);
729    }
730    Ok(())
731}
732
733/// Map a tool invocation to a child argv. Every push of a
734/// user-controlled value is preceded by a `no_dash` check unless the
735/// value follows a long flag that takes it as an unambiguous operand.
736/// One arm per tool — long but flat, like the dispatcher in `lib.rs`
737/// (same precedent as `serve.rs` for the line-count allowance).
738#[allow(clippy::too_many_lines)]
739fn build_argv(name: &str, args: &Value) -> Result<Vec<String>, String> {
740    let mut out: Vec<String> = Vec::new();
741    match name {
742        "mkit_status" => out.extend(["status".into(), "--porcelain=v2".into()]),
743        "mkit_diff_unstaged" => out.push("diff".into()),
744        "mkit_diff_staged" => out.extend(["diff".into(), "--staged".into()]),
745        "mkit_diff" => {
746            let target = req_str(args, "target")?;
747            no_dash(&target, "target")?;
748            out.extend(["diff".into(), target]);
749        }
750        "mkit_log" => {
751            out.extend(["log".into(), "--format=json".into(), "-n".into()]);
752            let n = args.get("max_count").and_then(Value::as_u64).unwrap_or(10);
753            out.push(n.to_string());
754            if let Some(rev) = opt_str(args, "rev") {
755                no_dash(&rev, "rev")?;
756                out.push(rev);
757            }
758        }
759        "mkit_show" => {
760            let rev = req_str(args, "revision")?;
761            no_dash(&rev, "revision")?;
762            out.extend(["show".into(), rev]);
763        }
764        "mkit_branch" => out.extend(["branch".into(), "--format=json".into()]),
765        "mkit_cat_object" => {
766            let object = req_str(args, "object")?;
767            no_dash(&object, "object")?;
768            let flag = match opt_str(args, "mode").as_deref() {
769                None | Some("pretty") => "-p",
770                Some("type") => "-t",
771                Some("size") => "-s",
772                Some(other) => {
773                    return Err(format!(
774                        "invalid mode '{other}': expected type, size, or pretty"
775                    ));
776                }
777            };
778            out.extend(["cat-file".into(), flag.into(), object]);
779        }
780        "mkit_verify" => {
781            let rev = req_str(args, "revision")?;
782            no_dash(&rev, "revision")?;
783            out.extend(["verify".into(), rev]);
784        }
785        "mkit_verify_attest" => {
786            out.push("verify-attest".into());
787            push_commit(&mut out, args)?;
788            if let Some(roots) = opt_str(args, "trust_roots") {
789                no_dash(&roots, "trust_roots")?;
790                out.extend(["--trust-roots".into(), roots]);
791            }
792            push_algorithm(&mut out, args)?;
793        }
794        "mkit_add" => {
795            out.push("add".into());
796            let files = args
797                .get("files")
798                .and_then(Value::as_array)
799                .ok_or("missing required argument: files")?;
800            if files.is_empty() {
801                return Err("files must not be empty".into());
802            }
803            for f in files {
804                let f = f.as_str().ok_or("files entries must be strings")?;
805                no_dash(f, "file path")?;
806                out.push(f.into());
807            }
808        }
809        "mkit_unstage" => {
810            match args.get("files") {
811                // Key absent = the documented "unstage everything" form:
812                // bare `reset` (mixed) — the working tree is never touched.
813                None => out.push("reset".into()),
814                // Key present: it must be a non-empty array of strings. A
815                // malformed value (string, empty array, …) must NOT silently
816                // widen a targeted unstage into a whole-index mutation.
817                Some(Value::Array(list)) if !list.is_empty() => {
818                    out.extend(["restore".into(), "--staged".into()]);
819                    for f in list {
820                        let f = f.as_str().ok_or("files entries must be strings")?;
821                        no_dash(f, "file path")?;
822                        out.push(f.into());
823                    }
824                }
825                Some(_) => {
826                    return Err(
827                        "files must be a non-empty array of paths; omit it entirely to \
828                         unstage everything"
829                            .into(),
830                    );
831                }
832            }
833        }
834        "mkit_commit" => {
835            let message = req_str(args, "message")?;
836            if message.trim().is_empty() {
837                return Err("message must not be empty".into());
838            }
839            out.extend(["commit".into(), "-m".into(), message]);
840        }
841        "mkit_create_branch" => {
842            let branch = req_str(args, "branch_name")?;
843            no_dash(&branch, "branch_name")?;
844            out.extend(["branch".into(), branch]);
845        }
846        "mkit_checkout" => {
847            let branch = req_str(args, "branch_name")?;
848            no_dash(&branch, "branch_name")?;
849            out.extend(["checkout".into(), branch]);
850        }
851        "mkit_init" => out.push("init".into()),
852        "mkit_keygen" => {
853            out.push("keygen".into());
854            push_algorithm(&mut out, args)?;
855            if args.get("print_pubkey").and_then(Value::as_bool) == Some(true) {
856                out.push("--print-pubkey".into());
857            }
858        }
859        "mkit_attest" => {
860            out.push("attest".into());
861            push_commit(&mut out, args)?;
862            // ALWAYS pass --algorithm explicitly: when absent the child CLI
863            // falls back to user-scoped `attest.default_algorithm`, which
864            // config.rs documents as a security-sensitive selector — ambient
865            // config must not steer an agent-triggered signing operation.
866            let alg = opt_str(args, "algorithm").unwrap_or_else(|| "ed25519".into());
867            if !matches!(alg.as_str(), "ed25519" | "secp256k1" | "p256") {
868                return Err(format!(
869                    "invalid algorithm '{alg}': expected ed25519, secp256k1, or p256"
870                ));
871            }
872            out.extend(["--algorithm".into(), alg]);
873            // ALWAYS pass --signer explicitly: when the flag is absent the
874            // child CLI falls back to user-scoped `attest.signer` config,
875            // which may name `external` — and the external-signer path is
876            // excluded from the MCP (it executes a configured subprocess).
877            let signer = opt_str(args, "signer").unwrap_or_else(|| "repo-key".into());
878            if !matches!(signer.as_str(), "repo-key" | "keystore") {
879                return Err(format!(
880                    "invalid signer '{signer}': expected repo-key or keystore \
881                     (external is excluded from the MCP)"
882                ));
883            }
884            out.extend(["--signer".into(), signer]);
885            if let Some(uri) = opt_str(args, "predicate_type") {
886                no_dash(&uri, "predicate_type")?;
887                out.extend(["--predicate-type".into(), uri]);
888            }
889            if let Some(file) = opt_str(args, "predicate_file") {
890                no_dash(&file, "predicate_file")?;
891                out.extend(["--predicate-file".into(), file]);
892            }
893        }
894        other => return Err(format!("unknown tool: {other}")),
895    }
896    Ok(out)
897}
898
899/// Run `mkit <argv>` in `repo`, capturing everything. The child is
900/// always this same binary, so server and CLI can never skew.
901fn run_subprocess(repo: &Path, argv: &[String]) -> CallOutcome {
902    let exe = match std::env::current_exe() {
903        Ok(p) => p,
904        Err(e) => return CallOutcome::err(format!("cannot locate mkit binary: {e}")),
905    };
906    let output = std::process::Command::new(exe)
907        .args(argv)
908        .current_dir(repo)
909        // Deterministic, capture-friendly child environment: no ANSI,
910        // and no editor fallback (commit always receives -m here, but
911        // belt-and-braces against any future interactive path).
912        .env("NO_COLOR", "1")
913        .env_remove("CLICOLOR_FORCE")
914        .env_remove("EDITOR")
915        .env_remove("VISUAL")
916        .output();
917    let output = match output {
918        Ok(o) => o,
919        Err(e) => return CallOutcome::err(format!("failed to run mkit {}: {e}", argv.join(" "))),
920    };
921
922    let stdout = String::from_utf8_lossy(&output.stdout);
923    let stderr = String::from_utf8_lossy(&output.stderr);
924    let code = output.status.code().unwrap_or(-1);
925
926    if output.status.success() {
927        let mut text = stdout.trim_end().to_string();
928        if text.is_empty() {
929            // Several mkit commands put confirmation prose on stderr
930            // and reserve stdout for machine output; surface it.
931            text = stderr.trim_end().to_string();
932        }
933        if text.is_empty() {
934            text = "(ok — no output)".into();
935        }
936        CallOutcome {
937            text,
938            is_error: false,
939        }
940    } else {
941        let mut text = format!("error: mkit exited {code} ({})", sysexits_name(code));
942        if !stderr.trim().is_empty() {
943            text.push('\n');
944            text.push_str(stderr.trim_end());
945        }
946        if !stdout.trim().is_empty() {
947            text.push('\n');
948            text.push_str(stdout.trim_end());
949        }
950        CallOutcome {
951            text,
952            is_error: true,
953        }
954    }
955}
956
957/// Human label for the BSD sysexits codes documented in docs/CLI.md.
958fn sysexits_name(code: i32) -> &'static str {
959    match code {
960        0 => "ok",
961        1 => "general error",
962        64 => "usage: wrong args or unknown subcommand",
963        65 => "dataerr: malformed input",
964        66 => "noinput: missing or unreadable input",
965        69 => "unavailable: transport could not connect",
966        73 => "cantcreat: cannot create output",
967        75 => "tempfail: transient failure, retry is safe",
968        76 => "protocol error",
969        77 => "noperm: permission denied",
970        78 => "config error",
971        _ => "unknown",
972    }
973}
974
975#[cfg(test)]
976mod tests {
977    use super::*;
978
979    #[test]
980    fn tool_table_is_complete_and_annotated() {
981        let tools = tool_descriptors();
982        let arr = tools.as_array().unwrap();
983        assert_eq!(arr.len(), 18, "tool count is part of the public surface");
984        for t in arr {
985            assert!(t.get("name").is_some());
986            assert!(t.get("description").is_some());
987            assert_eq!(t.pointer("/inputSchema/type").unwrap(), "object");
988            // Every tool is local-only.
989            assert_eq!(t.pointer("/annotations/openWorldHint").unwrap(), false);
990            // repo_path is universal.
991            assert!(t.pointer("/inputSchema/properties/repo_path").is_some());
992        }
993    }
994
995    #[test]
996    fn read_only_tools_are_marked() {
997        let tools = tool_descriptors();
998        for t in tools.as_array().unwrap() {
999            let name = t.get("name").unwrap().as_str().unwrap();
1000            let ro = t
1001                .pointer("/annotations/readOnlyHint")
1002                .unwrap()
1003                .as_bool()
1004                .unwrap();
1005            let expect_ro = matches!(
1006                name,
1007                "mkit_status"
1008                    | "mkit_diff_unstaged"
1009                    | "mkit_diff_staged"
1010                    | "mkit_diff"
1011                    | "mkit_log"
1012                    | "mkit_show"
1013                    | "mkit_branch"
1014                    | "mkit_cat_object"
1015                    | "mkit_verify"
1016                    | "mkit_verify_attest"
1017            );
1018            assert_eq!(ro, expect_ro, "readOnlyHint wrong for {name}");
1019        }
1020    }
1021
1022    #[test]
1023    fn argv_construction_basics() {
1024        let argv = build_argv("mkit_status", &json!({})).unwrap();
1025        assert_eq!(argv, ["status", "--porcelain=v2"]);
1026
1027        let argv = build_argv("mkit_commit", &json!({ "message": "hello world" })).unwrap();
1028        assert_eq!(argv, ["commit", "-m", "hello world"]);
1029
1030        let argv = build_argv("mkit_add", &json!({ "files": ["a.txt", "src/b.rs"] })).unwrap();
1031        assert_eq!(argv, ["add", "a.txt", "src/b.rs"]);
1032    }
1033
1034    #[test]
1035    fn flag_injection_is_rejected() {
1036        for (tool, args) in [
1037            ("mkit_diff", json!({ "target": "-R" })),
1038            ("mkit_show", json!({ "revision": "--help" })),
1039            ("mkit_add", json!({ "files": ["-A"] })),
1040            ("mkit_checkout", json!({ "branch_name": "-b" })),
1041            ("mkit_create_branch", json!({ "branch_name": "-D" })),
1042            ("mkit_cat_object", json!({ "object": "--batch" })),
1043            ("mkit_log", json!({ "rev": "--graph" })),
1044            ("mkit_attest", json!({ "predicate_file": "--force" })),
1045        ] {
1046            let err = build_argv(tool, &args).unwrap_err();
1047            assert!(err.contains("must not start with '-'"), "{tool}: {err}");
1048        }
1049    }
1050
1051    #[test]
1052    fn unstage_maps_to_restore_or_reset() {
1053        let argv = build_argv("mkit_unstage", &json!({})).unwrap();
1054        assert_eq!(argv, ["reset"]);
1055        let argv = build_argv("mkit_unstage", &json!({ "files": ["a.txt"] })).unwrap();
1056        assert_eq!(argv, ["restore", "--staged", "a.txt"]);
1057    }
1058
1059    #[test]
1060    fn unstage_rejects_malformed_files_instead_of_widening() {
1061        // A present-but-malformed `files` must error, never silently
1062        // broaden a targeted unstage into a whole-index reset.
1063        for bad in [
1064            json!({ "files": "a.txt" }),
1065            json!({ "files": [] }),
1066            json!({ "files": 3 }),
1067        ] {
1068            let err = build_argv("mkit_unstage", &bad).unwrap_err();
1069            assert!(err.contains("non-empty array"), "{bad}: {err}");
1070        }
1071    }
1072
1073    #[test]
1074    fn attest_always_pins_the_signer() {
1075        // Omitted signer must still emit --signer repo-key so user config
1076        // (`attest.signer = external`) can never reroute an MCP-triggered
1077        // attestation into an external-signer subprocess.
1078        let argv = build_argv("mkit_attest", &json!({})).unwrap();
1079        assert_eq!(
1080            argv,
1081            ["attest", "--algorithm", "ed25519", "--signer", "repo-key"]
1082        );
1083        let argv = build_argv("mkit_attest", &json!({ "signer": "keystore" })).unwrap();
1084        assert_eq!(
1085            argv,
1086            ["attest", "--algorithm", "ed25519", "--signer", "keystore"]
1087        );
1088        let argv = build_argv("mkit_attest", &json!({ "algorithm": "p256" })).unwrap();
1089        assert_eq!(
1090            argv,
1091            ["attest", "--algorithm", "p256", "--signer", "repo-key"]
1092        );
1093        let err = build_argv("mkit_attest", &json!({ "signer": "external" })).unwrap_err();
1094        assert!(err.contains("excluded"), "{err}");
1095    }
1096
1097    #[test]
1098    fn checkout_is_marked_destructive() {
1099        // Checkout rewrites tracked worktree files; clients use
1100        // destructiveHint to decide whether to confirm.
1101        let tools = tool_descriptors();
1102        let checkout = tools
1103            .as_array()
1104            .unwrap()
1105            .iter()
1106            .find(|t| t.get("name").unwrap() == "mkit_checkout")
1107            .unwrap();
1108        assert_eq!(
1109            checkout.pointer("/annotations/destructiveHint").unwrap(),
1110            true
1111        );
1112    }
1113
1114    #[test]
1115    fn no_force_flag_ever_emitted() {
1116        // The server must never override mkit's data-loss guards.
1117        for spec in TOOLS {
1118            let args = json!({
1119                "repo_path": "/tmp", "target": "x", "revision": "x", "object": "x",
1120                "message": "m", "branch_name": "b", "files": ["f"],
1121                "commit": "c", "predicate_type": "t", "predicate_file": "p",
1122            });
1123            if let Ok(argv) = build_argv(spec.name, &args) {
1124                assert!(
1125                    !argv.iter().any(|a| a == "-f" || a == "--force"),
1126                    "{} emits a force flag",
1127                    spec.name
1128                );
1129            }
1130        }
1131    }
1132
1133    #[test]
1134    fn scope_validation_rejects_outside_paths() {
1135        let root = tempfile::tempdir().unwrap();
1136        let outside = tempfile::tempdir().unwrap();
1137        let allowed = root.path().canonicalize().unwrap();
1138
1139        assert!(validate_repo_path(root.path().to_str().unwrap(), Some(&allowed)).is_ok());
1140        let err = validate_repo_path(outside.path().to_str().unwrap(), Some(&allowed)).unwrap_err();
1141        assert!(err.contains("outside the allowed repository"));
1142        // Unscoped mode allows anything that exists.
1143        assert!(validate_repo_path(outside.path().to_str().unwrap(), None).is_ok());
1144    }
1145
1146    #[test]
1147    fn initialize_negotiates_protocol_and_lists_tools() {
1148        let mut init_state = false;
1149
1150        // Tool traffic before initialize is rejected (-32002).
1151        let early = json!({ "jsonrpc": "2.0", "id": 0, "method": "tools/list" });
1152        let resp = handle_message(&early, None, &mut init_state).unwrap();
1153        assert_eq!(resp.pointer("/error/code").unwrap(), -32002);
1154        assert!(!init_state);
1155
1156        // A supported protocol is echoed back.
1157        let init = json!({
1158            "jsonrpc": "2.0", "id": 1, "method": "initialize",
1159            "params": { "protocolVersion": "2024-11-05", "capabilities": {} }
1160        });
1161        let resp = handle_message(&init, None, &mut init_state).unwrap();
1162        assert_eq!(
1163            resp.pointer("/result/protocolVersion").unwrap(),
1164            "2024-11-05"
1165        );
1166        assert_eq!(
1167            resp.pointer("/result/serverInfo/name").unwrap(),
1168            "mkit-repo"
1169        );
1170        assert!(resp.pointer("/result/instructions").is_some());
1171        assert!(init_state);
1172
1173        // An UNsupported protocol falls back to our latest, never echoed.
1174        let mut s2 = false;
1175        let bad = json!({
1176            "jsonrpc": "2.0", "id": 9, "method": "initialize",
1177            "params": { "protocolVersion": "1900-01-01" }
1178        });
1179        let resp = handle_message(&bad, None, &mut s2).unwrap();
1180        assert_eq!(
1181            resp.pointer("/result/protocolVersion").unwrap(),
1182            LATEST_PROTOCOL
1183        );
1184
1185        // After initialize, tools/list works.
1186        let list = json!({ "jsonrpc": "2.0", "id": 2, "method": "tools/list" });
1187        let resp = handle_message(&list, None, &mut init_state).unwrap();
1188        assert_eq!(
1189            resp.pointer("/result/tools")
1190                .unwrap()
1191                .as_array()
1192                .unwrap()
1193                .len(),
1194            18
1195        );
1196
1197        // Notifications produce no response.
1198        let note = json!({ "jsonrpc": "2.0", "method": "notifications/initialized" });
1199        assert!(handle_message(&note, None, &mut init_state).is_none());
1200
1201        // Unknown methods error.
1202        let bogus = json!({ "jsonrpc": "2.0", "id": 3, "method": "resources/list" });
1203        let resp = handle_message(&bogus, None, &mut init_state).unwrap();
1204        assert_eq!(resp.pointer("/error/code").unwrap(), -32601);
1205    }
1206}