From c875bc6f8ad77f3e589044bd7d4b32bf11202ff4 Mon Sep 17 00:00:00 2001 From: Li <2188718831@qq.com> Date: Thu, 26 Nov 2026 12:31:38 +0000 Subject: [PATCH 1/2] test(ide-host): restore stdio MCP discovery coverage for both protocols Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- CONTRIBUTING.md | 8 +- rust/Cargo.lock | 33 +++++ rust/contracts/ide-api/v1.md | 9 +- rust/crates/ide-host/Cargo.toml | 10 ++ rust/crates/ide-host/tests/mcp_protocol.rs | 160 +++++++++++++++++++++ rust/fixtures/ide-api/mcp-protocol.json | 1 - 6 files changed, 213 insertions(+), 8 deletions(-) create mode 100644 rust/crates/ide-host/tests/mcp_protocol.rs diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1760c4f2..83a768df 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -16,7 +16,7 @@ 2. **结果取决于真实操作系统的行为**(进程树、job object、权限位、batch 启动、可执行文件查找等):放进 [`rust/crates/platform-tests/tests/platform/`](rust/crates/platform-tests/tests/platform) 里对应主题的模块。需要被启动的程序时,给 `lithe-platform-fixture` 增加一种模式。不要把这类测试写在被测 crate 里,也不要在其他 crate 里加 `#[cfg(windows)]`、`#[cfg(target_os = "macos")]` 之类的测试。 3. **只借用 Unix 才有的夹具**(符号链接、权限位)来验证本 crate 自己的逻辑,并且 Linux 上会运行的 `#[cfg(unix)]` 测试:可以留在本 crate。如果 Windows 上应有另一种结果,那个结果按第 2 条写进 `lithe-platform-tests`。 4. **测试里的 `cfg!`** 只能把夹具改成本机格式,例如 Windows 绝对路径要带盘符;不能用它为不同系统选择不同的预期结果。预期随系统变化的断言属于第 2 条。 -5. **例外**:crate 的集成测试必须启动本包的可执行文件时,因为 `CARGO_BIN_EXE_*` 只对同一个包可见,测试无法搬走。这种 crate 在自己的 `Cargo.toml` 声明主机。目前只有 [`lithe-remote`](rust/crates/remote):它的服务端运行在 Linux 和 macOS 主机上。新增例外要在 PR 里说明为什么不能放进 `lithe-platform-tests`。 +5. **例外**:crate 的集成测试必须启动本包的可执行文件时,因为 `CARGO_BIN_EXE_*` 只对同一个包可见,测试无法搬走。这种 crate 在自己的 `Cargo.toml` 声明主机。例如 [`lithe-remote`](rust/crates/remote) 启动远端服务端,[`lithe-ide-host`](rust/crates/ide-host) 启动 `lithe-mcp` 验证 stdio 协议。新增例外要在 PR 里说明为什么不能放进 `lithe-platform-tests`。 [`scripts/rust-core/verify-platform-tests.mjs`](scripts/rust-core/verify-platform-tests.mjs) 检查的是:有没有测试在所属 crate 的每台 CI 主机上都被 `cfg` 编译掉,或者被 `cfg_attr(…, ignore)` 忽略。这样的测试在 CI 里永远不会运行,检查会失败。CI 的 `Select crates` 作业每次都运行这项检查。加 `--list` 会列出所有只在部分主机上运行的测试。第 3、4 条要看测试的意图,脚本判断不了,由评审把关。 @@ -28,7 +28,7 @@ hosts = ["macos", "windows"] # 除 Linux 外,还要在这些主机上运行 test-features = ["server"] # 测试需要的 feature,在每台主机上都启用 ``` -`hosts` 只接受 `macos` 和 `windows`;每个 crate 都会在 Linux 上测试,不必声明。目前声明主机的 crate 有 `lithe-platform`、`lithe-platform-tests` 和 `lithe-remote`。声明主机后,每个影响到该 crate 的 PR 都会多占用 macOS 或 Windows runner,所以只能用于上一节允许的情况。 +`hosts` 只接受 `macos` 和 `windows`;每个 crate 都会在 Linux 上测试,不必声明。声明主机后,每个影响到该 crate 的 PR 都会多占用 macOS 或 Windows runner,所以只能用于上一节允许的情况。 ## CI 怎样只运行受影响的 crate @@ -85,7 +85,7 @@ Take the first rule that applies: 2. **A result that depends on the real operating system** (process trees, job objects, permission bits, batch launches, executable lookup and the like): add it to the module for that subject under [`rust/crates/platform-tests/tests/platform/`](rust/crates/platform-tests/tests/platform). When a test needs a program to start, add a mode to `lithe-platform-fixture`. Do not write such tests in the crate under test, and do not add `#[cfg(windows)]`, `#[cfg(target_os = "macos")]` or similar tests to any other crate. 3. **A `#[cfg(unix)]` test that only borrows a Unix fixture** (a symbolic link, permission bits) to check the crate's own logic, and that runs on Linux: it may stay in its crate. If Windows should behave differently, that result goes into `lithe-platform-tests` under rule 2. 4. **`cfg!` in a test** may only convert a fixture to the host's form, for example give a Windows absolute path its drive. It must not choose a different expected result per system; such assertions fall under rule 2. -5. **Exception**: when a crate's integration tests must start the crate's own executable, they cannot move, because `CARGO_BIN_EXE_*` is visible only within the package. Such a crate declares its hosts in its own `Cargo.toml`. Today only [`lithe-remote`](rust/crates/remote) does, because its server runs on Linux and macOS hosts. A new exception explains in its pull request why the tests cannot go into `lithe-platform-tests`. +5. **Exception**: when a crate's integration tests must start the crate's own executable, they cannot move, because `CARGO_BIN_EXE_*` is visible only within the package. Such a crate declares its hosts in its own `Cargo.toml`. For example, [`lithe-remote`](rust/crates/remote) starts the remote server and [`lithe-ide-host`](rust/crates/ide-host) starts `lithe-mcp` to exercise its stdio protocol. A new exception explains in its pull request why the tests cannot go into `lithe-platform-tests`. [`scripts/rust-core/verify-platform-tests.mjs`](scripts/rust-core/verify-platform-tests.mjs) fails when a test is compiled out by `cfg`, or ignored by `cfg_attr(…, ignore)`, on every CI host of its crate, because CI would never run it. The `Select crates` job runs it on every change. `--list` also prints every test that runs on only some hosts. Rules 3 and 4 are about a test's intent, which the script cannot judge; review enforces them. @@ -97,7 +97,7 @@ hosts = ["macos", "windows"] # hosts that also run the crate's tests, besides test-features = ["server"] # features the tests need, enabled on every host ``` -`hosts` accepts `macos` and `windows`; every crate is tested on Linux without declaring it. Today `lithe-platform`, `lithe-platform-tests` and `lithe-remote` declare hosts. A declaration makes every pull request that affects the crate use a macOS or Windows runner, so it is only for the cases the previous section allows. +`hosts` accepts `macos` and `windows`; every crate is tested on Linux without declaring it. A declaration makes every pull request that affects the crate use a macOS or Windows runner, so it is only for the cases the previous section allows. ## How CI runs only the affected crates diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 5cbb6fd8..ec318926 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -923,6 +923,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + [[package]] name = "litemap" version = "0.8.2" @@ -1185,6 +1191,7 @@ dependencies = [ "serde", "serde_json", "sha2", + "tempfile", "tokio", "uuid", ] @@ -2070,6 +2077,19 @@ version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" +[[package]] +name = "rustix" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "rustversion" version = "1.0.23" @@ -2371,6 +2391,19 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys", +] + [[package]] name = "tendril" version = "0.5.1" diff --git a/rust/contracts/ide-api/v1.md b/rust/contracts/ide-api/v1.md index 20d1d99d..f8843a30 100644 --- a/rust/contracts/ide-api/v1.md +++ b/rust/contracts/ide-api/v1.md @@ -87,8 +87,11 @@ the project dispatcher answered a tool call. MCP `tools/list` includes `ttlMs: 0` and `cacheScope: "private"`, including for `2026-07-28` discovery. Protocol negotiation and envelopes remain SDK-owned; -legacy and modern clients are verified in separate stdio sessions using -`rust/fixtures/ide-api/mcp-protocol.json`. +the [stdio integration test](../../crates/ide-host/tests/mcp_protocol.rs) starts +a fresh helper for legacy initialization, modern discovery without initialization, +and modern discovery after initialization. It reads the protocol expectations +from [mcp-protocol.json](../../fixtures/ide-api/mcp-protocol.json) and checks +the listed names against the connection's tool exposure. ## Capabilities @@ -207,7 +210,7 @@ surface; it does not introduce a new plugin marketplace, sandbox or UI extension - `cargo test --manifest-path rust/Cargo.toml -p lithe-ide-host` - `cargo build --manifest-path rust/Cargo.toml -p lithe-ide-host --features mcp --bin lithe-mcp` -- `node --test scripts/ide-mcp/test-ide-mcp.mjs` +- `cargo test --manifest-path rust/Cargo.toml -p lithe-ide-host --features mcp --test mcp_protocol` - Windows: the host-api and run frontend tests, plus a native packaged run using the generated client configuration. - macOS: `IdeCapabilitiesTests`, followed by a packaged native run. diff --git a/rust/crates/ide-host/Cargo.toml b/rust/crates/ide-host/Cargo.toml index a9fc3540..a5959261 100644 --- a/rust/crates/ide-host/Cargo.toml +++ b/rust/crates/ide-host/Cargo.toml @@ -9,6 +9,10 @@ publish = false [features] mcp = ["dep:rmcp", "dep:reqwest"] +[package.metadata.lithe-ci] +hosts = ["windows"] +test-features = ["mcp"] + [dependencies] axum = "0.8" fs2 = "0.4" @@ -26,5 +30,11 @@ name = "lithe-mcp" path = "src/bin/lithe-mcp.rs" required-features = ["mcp"] +[[test]] +name = "mcp_protocol" +required-features = ["mcp"] + [dev-dependencies] reqwest = { version = "0.12", default-features = false, features = ["json"] } +tempfile = "3" +tokio = { version = "1", features = ["io-util", "process"] } diff --git a/rust/crates/ide-host/tests/mcp_protocol.rs b/rust/crates/ide-host/tests/mcp_protocol.rs new file mode 100644 index 00000000..42899162 --- /dev/null +++ b/rust/crates/ide-host/tests/mcp_protocol.rs @@ -0,0 +1,160 @@ +//! Exercises MCP discovery over the same stdio interface used by external clients. + +use serde_json::{json, Value}; +use std::{process::Stdio, time::Duration}; +use tokio::{ + io::{AsyncBufReadExt, AsyncWriteExt, BufReader, Lines}, + process::{ChildStdin, ChildStdout, Command}, + time::timeout, +}; + +struct Client { + input: ChildStdin, + output: Lines>, + id: u64, +} + +impl Client { + async fn send(&mut self, message: Value) { + self.input + .write_all(format!("{message}\n").as_bytes()) + .await + .expect("write MCP message"); + } + + async fn request(&mut self, method: &str, params: Value) -> Value { + self.id += 1; + self.send(json!({"jsonrpc": "2.0", "id": self.id, "method": method, "params": params})) + .await; + let line = self.output.next_line().await.unwrap().expect("MCP reply"); + let reply: Value = serde_json::from_str(&line).unwrap(); + assert_eq!(reply["id"], self.id, "{method}: {reply}"); + reply + .get("result") + .unwrap_or_else(|| panic!("{method}: {reply}")) + .clone() + } +} + +fn tool_names(tools: &Value) -> Vec<&str> { + let mut names: Vec<_> = tools + .as_array() + .unwrap() + .iter() + .map(|tool| tool["name"].as_str().unwrap()) + .collect(); + names.sort_unstable(); + names +} + +async fn check_session(mode: &'static str) { + let fixture: Value = + serde_json::from_str(include_str!("../../../fixtures/ide-api/mcp-protocol.json")).unwrap(); + assert!(fixture["modes"].as_array().unwrap().contains(&json!(mode))); + let hosts: Value = serde_json::from_str(include_str!( + "../../../fixtures/ide-api/host-tool-exposure.json" + )) + .unwrap(); + let directory = tempfile::tempdir().unwrap(); + let descriptor = directory.path().join("connection.json"); + let descriptor_path = descriptor.clone(); + let write_descriptor = + move |exposure: &Value| { + std::fs::write(&descriptor_path, json!({ + "endpoint": "http://127.0.0.1:1/call", "token": "fixture", "toolExposure": exposure + }).to_string()).unwrap(); + }; + write_descriptor(&hosts["macos"]); + let mut child = Command::new(env!("CARGO_BIN_EXE_lithe-mcp")) + .arg("--connection") + .arg(&descriptor) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .kill_on_drop(true) + .spawn() + .expect("start lithe-mcp"); + let mut client = Client { + input: child.stdin.take().unwrap(), + output: BufReader::new(child.stdout.take().unwrap()).lines(), + id: 0, + }; + + // Keep ownership of the child outside the client task so even a failed + // protocol assertion closes both pipes and reaps the helper. + let exchange = tokio::spawn(timeout(Duration::from_secs(10), async move { + let modern = &fixture["modernProtocolVersion"]; + if mode != "modern" { + let version = if mode == "legacy" { + &fixture["legacyProtocolVersion"] + } else { + modern + }; + let initialized = client + .request( + "initialize", + json!({ + "protocolVersion": version, "capabilities": {}, + "clientInfo": {"name": "fixture", "version": "1"} + }), + ) + .await; + assert_eq!(initialized["serverInfo"]["name"], "lithe"); + if mode == "legacy" { + assert_eq!(&initialized["protocolVersion"], version); + } + client + .send(json!({"jsonrpc": "2.0", "method": "notifications/initialized"})) + .await; + } + let mut params = json!({}); + if mode != "legacy" { + params = json!({"_meta": { + "io.modelcontextprotocol/protocolVersion": modern, + "io.modelcontextprotocol/clientCapabilities": {} + }}); + let discovery = client.request("server/discover", params.clone()).await; + assert!(discovery["supportedVersions"] + .as_array() + .unwrap() + .contains(modern)); + } + for platform in ["macos", "windows"] { + let exposure = &hosts[platform]; + write_descriptor(exposure); + let list = client.request("tools/list", params.clone()).await; + assert_eq!(list["ttlMs"], fixture["cachePolicy"]["ttlMs"]); + assert_eq!(list["cacheScope"], fixture["cachePolicy"]["cacheScope"]); + let names = tool_names(&list["tools"]); + assert_eq!(names, tool_names(exposure), "{platform} tool exposure"); + for provider in fixture["providerTools"].as_array().unwrap() { + assert!( + names.contains(&provider.as_str().unwrap()), + "missing {provider}" + ); + } + } + })) + .await; + timeout(Duration::from_secs(5), child.kill()) + .await + .expect("reap lithe-mcp before deadline") + .unwrap(); + exchange + .expect("MCP client assertions") + .expect("MCP session deadline"); +} + +#[tokio::test] +async fn legacy() { + check_session("legacy").await; +} + +#[tokio::test] +async fn modern() { + check_session("modern").await; +} + +#[tokio::test] +async fn modern_after_initialize() { + check_session("modern-after-initialize").await; +} diff --git a/rust/fixtures/ide-api/mcp-protocol.json b/rust/fixtures/ide-api/mcp-protocol.json index 3b8e32e0..0e8a849a 100644 --- a/rust/fixtures/ide-api/mcp-protocol.json +++ b/rust/fixtures/ide-api/mcp-protocol.json @@ -6,7 +6,6 @@ "ttlMs": 0, "cacheScope": "private" }, - "toolCount": 17, "modes": [ "legacy", "modern", From cc654e198892109404e10360e6203d7373b5e442 Mon Sep 17 00:00:00 2001 From: Li <2188718831@qq.com> Date: Fri, 27 Nov 2026 02:45:31 +0000 Subject: [PATCH 2/2] test(ide-host): check restricted tool discovery on a live connection Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- rust/crates/ide-host/tests/mcp_protocol.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/crates/ide-host/tests/mcp_protocol.rs b/rust/crates/ide-host/tests/mcp_protocol.rs index 42899162..5dab3e36 100644 --- a/rust/crates/ide-host/tests/mcp_protocol.rs +++ b/rust/crates/ide-host/tests/mcp_protocol.rs @@ -133,6 +133,12 @@ async fn check_session(mode: &'static str) { ); } } + // Both platforms currently expose the full catalog. A restricted + // descriptor makes a cached or unfiltered tools/list observable. + let restricted = json!([hosts["windows"][0]]); + write_descriptor(&restricted); + let list = client.request("tools/list", params).await; + assert_eq!(tool_names(&list["tools"]), tool_names(&restricted)); })) .await; timeout(Duration::from_secs(5), child.kill())