- 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>
167 lines
5 KiB
Rust
167 lines
5 KiB
Rust
//! `lutra mcp` end to end: the binary on stdio against a test backend
|
|
//! that claims a temporary rendezvous directory.
|
|
|
|
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
|
|
|
|
use std::io::{BufRead, BufReader, Write};
|
|
use std::path::Path;
|
|
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
|
|
use std::sync::Arc;
|
|
|
|
use lutra_control::rendezvous::{self, Instance};
|
|
use lutra_testkit::EngineBackend;
|
|
use serde_json::{Value, json};
|
|
|
|
fn claim(dir: &Path) -> Arc<EngineBackend> {
|
|
let backend = Arc::new(EngineBackend::new());
|
|
let token = rendezvous::new_token().unwrap();
|
|
let server = lutra_control::start_with_token(0, backend.clone(), &token).unwrap();
|
|
rendezvous::write(
|
|
dir,
|
|
&Instance {
|
|
port: server.local_addr().port(),
|
|
token,
|
|
pid: std::process::id(),
|
|
version: "test".into(),
|
|
},
|
|
)
|
|
.unwrap();
|
|
backend
|
|
}
|
|
|
|
struct Mcp {
|
|
child: Child,
|
|
stdin: ChildStdin,
|
|
stdout: BufReader<ChildStdout>,
|
|
}
|
|
|
|
impl Mcp {
|
|
fn start(dir: &Path, args: &[&str]) -> Self {
|
|
let mut child = Command::new(env!("CARGO_BIN_EXE_lutra"))
|
|
.arg("mcp")
|
|
.args(args)
|
|
.env(rendezvous::DIR_ENV, dir)
|
|
.env("LUTRA_DESKTOP", dir.join("no-such-desktop"))
|
|
.stdin(Stdio::piped())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::null())
|
|
.spawn()
|
|
.unwrap();
|
|
let stdin = child.stdin.take().unwrap();
|
|
let stdout = BufReader::new(child.stdout.take().unwrap());
|
|
Self {
|
|
child,
|
|
stdin,
|
|
stdout,
|
|
}
|
|
}
|
|
|
|
fn send(&mut self, message: Value) {
|
|
writeln!(self.stdin, "{message}").unwrap();
|
|
self.stdin.flush().unwrap();
|
|
}
|
|
|
|
fn receive(&mut self) -> Value {
|
|
let mut line = String::new();
|
|
self.stdout.read_line(&mut line).unwrap();
|
|
serde_json::from_str(&line).unwrap()
|
|
}
|
|
|
|
fn ask(&mut self, id: u64, method: &str, params: Value) -> Value {
|
|
self.send(json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}));
|
|
let response = self.receive();
|
|
assert_eq!(response["id"], id);
|
|
response
|
|
}
|
|
|
|
fn finish(self) -> std::process::ExitStatus {
|
|
let Self {
|
|
mut child, stdin, ..
|
|
} = self;
|
|
drop(stdin);
|
|
child.wait().unwrap()
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn drives_lutra_over_stdio() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let project = dir.path().join("project");
|
|
std::fs::create_dir_all(&project).unwrap();
|
|
std::fs::write(project.join("a.php"), "<?php\n").unwrap();
|
|
let backend = claim(dir.path());
|
|
let mut mcp = Mcp::start(dir.path(), &[]);
|
|
|
|
let init = mcp.ask(
|
|
1,
|
|
"initialize",
|
|
json!({"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "e2e", "version": "1"}}),
|
|
);
|
|
assert_eq!(init["result"]["protocolVersion"], "2025-06-18");
|
|
mcp.send(json!({"jsonrpc": "2.0", "method": "notifications/initialized"}));
|
|
let tools = mcp.ask(2, "tools/list", json!({}));
|
|
assert_eq!(tools["result"]["tools"].as_array().unwrap().len(), 5);
|
|
|
|
let call = |mcp: &mut Mcp, id: u64, name: &str, args: Value| {
|
|
let r = mcp.ask(id, "tools/call", json!({"name": name, "arguments": args}));
|
|
assert_eq!(r["result"]["isError"], false, "{r}");
|
|
r["result"].clone()
|
|
};
|
|
call(
|
|
&mut mcp,
|
|
3,
|
|
"execute_command",
|
|
json!({"command": "project.open", "params": {"path": project}}),
|
|
);
|
|
call(
|
|
&mut mcp,
|
|
4,
|
|
"execute_command",
|
|
json!({"command": "file.open", "params": {"path": "a.php"}}),
|
|
);
|
|
let read = call(&mut mcp, 5, "read_buffer", json!({"path": "a.php"}));
|
|
assert_eq!(read["content"][1]["text"], "<?php\n");
|
|
let resources = mcp.ask(6, "resources/list", json!({}));
|
|
assert_eq!(
|
|
resources["result"]["resources"][0]["uri"],
|
|
"lutra://buffer/a.php"
|
|
);
|
|
assert_eq!(
|
|
backend.engine.lock().unwrap().state().buffers.ids().len(),
|
|
1
|
|
);
|
|
|
|
assert!(mcp.finish().success(), "stdin closed: clean exit");
|
|
}
|
|
|
|
#[test]
|
|
fn reports_a_missing_lutra_as_a_tool_error() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let mut mcp = Mcp::start(dir.path(), &["--no-start"]);
|
|
let r = mcp.ask(
|
|
1,
|
|
"tools/call",
|
|
json!({"name": "list_commands", "arguments": {}}),
|
|
);
|
|
assert_eq!(r["result"]["isError"], true);
|
|
assert_eq!(
|
|
r["result"]["structuredContent"]["error"]["code"],
|
|
"unavailable"
|
|
);
|
|
assert!(mcp.finish().success());
|
|
}
|
|
|
|
#[test]
|
|
fn usage_errors_exit_with_two() {
|
|
let out = Command::new(env!("CARGO_BIN_EXE_lutra"))
|
|
.args(["mcp", "--bogus"])
|
|
.output()
|
|
.unwrap();
|
|
assert_eq!(out.status.code(), Some(2));
|
|
let out = Command::new(env!("CARGO_BIN_EXE_lutra"))
|
|
.args(["mcp", "--help"])
|
|
.output()
|
|
.unwrap();
|
|
assert_eq!(out.status.code(), Some(0));
|
|
assert!(String::from_utf8_lossy(&out.stdout).contains("claude mcp add"));
|
|
}
|