M3.8: MCP server lutra mcp

- crates/mcp (L5): JSON-RPC 2.0 over stdio, dual era: MCP 2026-07-28
  (per-request _meta, server/discover, -32022) and legacy initialize
  (2025-11-25, 2025-06-18, 2025-03-26, 2024-11-05). Tools list_commands,
  execute_command (app.quit only with confirm: true), screenshot (image
  content), read_buffer, get_events (ring of 1000 events via
  events.subscribe); resources lutra://buffer/<path> for open buffers.
  Engine errors become tool errors with the engine code. Lines capped at
  16 MiB, guard around every request, parser and tool args fuzzed.
- lutra mcp [--port PORT] [--no-start] in apps/lutra-cli: connects through
  the rendezvous file and token, or starts Lutra.
- Engine: read-only buffer.list command.
- Tests: protocol units + proptest fuzz, integration over the token
  listener, stdio end to end. Docs: architecture (MCP), G7, ROADMAP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-19 18:20:57 +02:00
parent 51eba0b3cb
commit 0e56c22b66
24 changed files with 3055 additions and 18 deletions

View file

@ -1,4 +1,5 @@
//! Buffer commands: `buffer.get`, `buffer.edit`, `buffer.close`.
//! Buffer commands: `buffer.get`, `buffer.edit`, `buffer.close`,
//! `buffer.list`.
//!
//! Edits use UTF-16 code unit offsets, as CodeMirror and LSP do, and follow
//! CodeMirror's change-set semantics: every `{from, to}` of a batch refers
@ -222,3 +223,37 @@ impl Command for Close {
Ok(Outcome::value(Value::Null))
}
}
/// `buffer.list`: the open buffers without their text, for agents (MCP
/// resources) that need to know which files are open.
pub struct List;
impl Command for List {
fn spec(&self) -> &CommandSpec {
&CommandSpec {
id: "buffer.list",
label: "List Buffers",
menu_path: None,
default_shortcut: None,
params: "none; returns [{ id, path, version, dirty }] ordered by id, empty without a project",
}
}
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
let list: Vec<Value> = state
.buffers
.ids()
.into_iter()
.filter_map(|id| {
let open = state.buffers.get(id)?;
Some(json!({
"id": id,
"path": path_string(&open.path),
"version": open.version,
"dirty": open.dirty(),
}))
})
.collect();
Ok(Outcome::value(Value::Array(list)))
}
}

View file

@ -52,5 +52,6 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
registry.register(Box::new(syntax::OutlineCommand))?;
registry.register(Box::new(toolchain::Status))?;
registry.register(Box::new(toolchain::DownloadPhpactor))?;
registry.register(Box::new(buffer::List))?;
Ok(())
}

View file

@ -388,6 +388,7 @@ mod tests {
"buffer.close",
"buffer.edit",
"buffer.get",
"buffer.list",
"file.open",
"file.save",
"file.saveAll",

View file

@ -529,3 +529,29 @@ fn a_panicking_command_leaves_open_buffers_intact() {
assert_eq!(got["dirty"], true);
run(&mut engine, "file.save", json!({"id": id}));
}
#[test]
fn buffer_list_names_open_buffers_without_text() {
let mut engine = Engine::new().unwrap();
assert_eq!(
run(&mut engine, "buffer.list", Value::Null).value,
json!([])
);
let (_dir, mut engine, _) = open_project(&[("a.php", b"a"), ("src/b.php", b"b")]);
let (a, _) = open_file(&mut engine, "a.php");
let (b, _) = open_file(&mut engine, "src/b.php");
run(
&mut engine,
"buffer.edit",
json!({"id": b, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": "!"}]}),
);
let out = run(&mut engine, "buffer.list", json!({}));
assert_eq!(
out.value,
json!([
{"id": a, "path": "a.php", "version": 1, "dirty": false},
{"id": b, "path": "src/b.php", "version": 2, "dirty": true},
])
);
assert!(out.events.is_empty());
}