Add a stdio MCP server as clisok mcp, built from the existing Rust code behind an
optional mcp Cargo feature. No separate wrapper process and no duplicated scan
logic.
client <-- JSON-RPC/stdio --> clisok mcp --> run_check()/run_fix()/run_list()/rules()
The server lives in the binary crate as src/mcp.rs, next to check, list and
rule. src/lib.rs stays what it is today: pure parsing boundaries
(pub mod parsing, used by benches/parsing.rs). No library split.
Default cargo build stays dependency-free and byte-for-byte the same CLI.
cargo build --features mcp adds the server.
output::check_json()(src/output.rs:193) andoutput::statistics_json()(src/output.rs:254) already return the JSON a tool should emit.Report,ListReport,Ruleare serde-serializable (src/report.rs:227,src/report.rs:487,src/rule.rs:61).Report::exit_code()preserves CLI semantics.- Probes cannot read or write the transport: child stdin is
NUL//dev/nulland stdout is captured (src/platform/windows/spawn.rs:383-396,src/platform/unix/spawn.rs:187). The only stdout writers are the CLI adapters viaprint_stdout. - The progress renderer already disables itself when stderr is not a terminal
(
progress.rs,Terminal::new), which is the normal case for an MCP server. - The real work is removing print/exit/global-state from the core path.
Option structs, so the MCP layer never builds clap types:
CheckOptions,FixOptions,ListOptionswithpub(crate)fields.registrystays#[cfg(windows)], as inCheckArgs. Probe vs. static is the existingstatic_only(--static,src/check.rs:62).CheckArgs::to_options(),ListArgs::to_options().
Pure functions extracted from check::check (src/check.rs:572):
run_check(&CheckOptions) -> Result<Report, String>: privilege policy,platform::init(), PATH findings, discovery, prepare/run, timeout retry,platform::cleanup(). Noprint_stdout, noExitCode, noctrlc.run_fix(&FixOptions) -> Result<FixOutcome, String>. Needs no tool scan:--diffalready returns before discovery, and after--fixonly the saved-settings findings are recomputed (src/check.rs:621-635).- dry run: the diff text (
print_difflogic, returned instead of printed). - apply:
applied: Vec<Applied>,errors: Vec<String>(todayeprintln!ed atsrc/check.rs:537,550), and the remaining PATH / App Paths findings.
- dry run: the diff text (
run_list(&ListOptions) -> Result<ListReport, String>.rules(Option<Code>) -> Vec<Rule>;Rulebecomespub(crate).check()/list()/rule()stay as thin CLI adapters: installctrlc(CLI only), format, print, map exit codes. Error texts and exit codes unchanged.
Global state that breaks a second run in one process:
- Work directory (
src/platform/mod.rs:28-55):WORK_DIRis aOnceLockbutcleanup()deletes the directory, so run #2 probes from a dead cwd. Change toMutex<Option<TempDir>>:work_dir()returnsPathBufand creates a fresh uniquely named dir when empty;cleanup()takes and drops it. Each run gets its own dir; no shutdown hook needed. Callers:src/platform/windows/spawn.rs:364,src/platform/unix/spawn.rs:186, test atsrc/platform/unix/spawn.rs:376. - Python probe cache (
src/inspect/python.rs:69):PROBE_CACHElives for the process, so a long-lived server reports stale module results after apip install. Clear it at the start of eachrun_check. - Ctrl+C (
src/check.rs:585-590): movectrlc::set_handlerandprocess::exit(130)into the CLI adapter. - Interrupt flags: reset
progress::INTERRUPTEDat the start of each run. UnixSTOPPING(src/platform/unix/spawn.rs:33) is only set bykill_children(), which v1 never calls in the server; leave it.
Tests: two consecutive run_check calls (static, then probe) in one process both
succeed; the work dir exists during a run and is gone after it.
Dependencies, behind the feature:
[features]
default = []
mcp = ["dep:rmcp", "dep:tokio"]
[dependencies]
rmcp = { version = "=3.8.0", default-features = false, features = ["server", "macros", "transport-io"], optional = true }
tokio = { version = "1", features = ["rt", "sync"], optional = true }rmcp 3.8.0declares rust-version 1.88 (checked withcargo info rmcp), the repo MSRV. Pin it exactly, likecriterion, so a later 3.x cannot raise it.- rmcp's
serverfeature already pulls in schemars; deriveJsonSchemathrough thermcp::schemarsre-export, not a direct dependency. - Build a current-thread tokio runtime by hand in
mcp::serve().
CLI surface: Command::Mcp(McpArgs) behind #[cfg(feature = "mcp")], with
server-side safety flags that the model cannot flip:
--allow-fix: thefixtool may apply changes.--allow-unsafe-fixes(requires--allow-fix):unsafe_fixes=trueis honored.--allow-root: probes may run as root / elevated.
ClisokMcp uses #[tool_router] + #[tool] + Parameters<T>, served with
ServiceExt::serve(stdio()). Every run_* call runs in spawn_blocking under
one std::sync::Mutex, because platform and progress state are global;
concurrent calls queue.
Tool surface:
| Tool | Params | Returns | Annotation |
|---|---|---|---|
check |
mode: "static"|"probe" (default static), system, registry (Windows), skip[], ignore[], timeout_secs (default 10), jobs, detail: "summary"|"findings"|"full" (default summary) |
structuredContent = detail payload + exit_code; one-line text summary |
readOnlyHint |
list |
system |
ListReport JSON |
readOnlyHint |
rules |
code?; omitted = full catalog |
rule object or array | readOnlyHint |
fix |
apply (default false), unsafe_fixes (default false) |
diff, or applied + errors + remaining findings |
destructiveHint |
detail maps onto existing functions, so there is no separate verbose param:
summary -> statistics_json, findings -> check_json(_, false),
full -> check_json(_, true).
Rules:
- Findings are a successful call, not
isError. Runtime failures (PATH empty, privilege refusal, I/O) areCallToolResult::error; an unknown code or enum value isMcpError::invalid_params. fixwithapply=truewithout--allow-fix, orunsafe_fixes=truewithout--allow-unsafe-fixes, is a tool error naming the missing server flag.probeon an elevated server without--allow-rootis refused, as in the CLI.- Backups stay under
%LOCALAPPDATA%\clisok\backups\. get_info()advertises nameclisok, the crate version, and instructions:probeexecutes tools from PATH; the server checks the PATH it was launched with, which may differ from the user's shell (registryon Windows also covers the saved PATH).
tests/mcp.rs(#![cfg(feature = "mcp")]): spawnCARGO_BIN_EXE_clisok mcpand speak JSON-RPC:initialize->tools/list->check {mode:"static"}twice (work-dir regression) ->list->rules.rules {code:"CLI999"}-> invalid params.fix {apply:true}without--allow-fix-> gate error.- Every stdout line parses as JSON-RPC (stdout purity).
- CI (
ci.yml): addcargo clippy --all-targets --features mcp -- -D warningsandcargo test --locked --features mcpto the native matrix, including the existing 1.88.0 rows (MSRV check for rmcp/tokio). - Release: build published binaries with
--features mcp; checktests/release/test_artifacts.pyandreproducible.ymlstill hold. - README: "Use as an MCP server" with a client config snippet, the three server flags, and the PATH caveat.
- CHANGELOG entry; no
SCHEMA_VERSIONbump (payload unchanged).
| Phase | Work | Estimate |
|---|---|---|
| 1 | Options, run_* extraction, globals, regression tests |
1-1.5 days |
| 2 | rmcp server, 4 tools, flags, feature plumbing | 1-1.5 days |
| 3 | MCP integration tests, CI, release, docs | 0.5-1 day |
| Total | 2.5-4 days |
- Long scans: a probe run can take minutes and clients time out. Mitigated by
the
staticdefault and a 10 s MCPtimeout_secsdefault (CLI stays 30 s). - No cancellation in v1: an in-flight check cannot be aborted. Later: map
notifications/cancelledtoplatform::kill_children(), then resetprogress::INTERRUPTEDand UnixSTOPPING. - Killed mid-run: a server killed during a probe can leave one work dir behind, same as the CLI under SIGKILL today.
- PATH mismatch: the server sees its launch PATH, not the user's shell.
Documented in
get_infoand the README. fixmutates the host: dry-run by default; applying needs--allow-fixin the client config, unsafe fixes need--allow-unsafe-fixes.- Payload size: default
detail:"summary";fullcan be large.
- Route: native
clisok mcpin the binary crate. - Default check mode:
static. fixtool: included, dry-run by default, apply gated by server flags.- Release binaries: built with
--features mcp; plaincargo buildstays lean. - Transport: stdio only.