From eb036f2b671d7fd4784c76a71b3e1d806b8964c8 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 25 Nov 2026 16:10:45 +0800 Subject: [PATCH] fix(mcp): ask agents to read the workspace roots before their first project task (#5822) The MCP server's instructions and get_workspace_roots only asked for the roots when the user asked about directories or a change looked like it might span projects. An agent that took its working directory for the whole workspace never got there, and missed the projects attached in the IDE. Both now say to call get_workspace_roots before the first project-related operation in a session, and spell out what the answer means: projectRoot is the project the connection serves and the default base for paths, roots is the workspace scope to search and read rather than a list of projects to change, and neither changes the agent's working directory or permissions. The instructions also say to ask again after the user switches or attaches a project. Clients that load tool descriptions lazily still see the rule in the initialize instructions. Fixes #5751 --- crates/ide-host/src/mcp/files.rs | 6 ++++-- crates/ide-mcp/src/protocol.rs | 9 +++++++-- crates/ide-mcp/tests/server.rs | 2 ++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/ide-host/src/mcp/files.rs b/crates/ide-host/src/mcp/files.rs index 2ea2944e4..57f5228cd 100644 --- a/crates/ide-host/src/mcp/files.rs +++ b/crates/ide-host/src/mcp/files.rs @@ -33,8 +33,10 @@ fn get_workspace_roots() -> Tool { "get_workspace_roots", "Returns the root directories of the IDE workspace: this project and the trusted projects attached to it in the same window, as absolute paths. A task works in one directory, but its workspace can hold several projects. -Call this when asked about the workspace, project or working directories, and before planning a change that may span -projects. Other tools reach an attached project by its root as `projectPath`.", +Call this before the first project-related operation in a session, and when asked about the workspace, project or +working directories. `projectRoot` is the project this connection serves, the default base for paths; `roots` is +every project in the workspace, the scope to search and read, not a list of projects to change. It doesn't change the +working directory or permissions. Other tools reach an attached project by its root as `projectPath`.", |call| async move { let root = call.root().to_path_buf(); let mut roots = vec![root.clone()]; diff --git a/crates/ide-mcp/src/protocol.rs b/crates/ide-mcp/src/protocol.rs index 11e9e8ed3..c78d446c9 100644 --- a/crates/ide-mcp/src/protocol.rs +++ b/crates/ide-mcp/src/protocol.rs @@ -168,8 +168,13 @@ impl Session<'_> { "instructions": format!( "Built-in OpenIDEA tools for {about}: they see the files as the IDE has them and, \ for a project open in a window, its editors. The project's workspace can include other \ - projects attached to it: call `get_workspace_roots` to list them before answering about \ - workspace directories or planning a change across projects." + projects attached to it, which a session's working directory doesn't show. Before the \ + first project-related operation in a session, call `get_workspace_roots`: its \ + `projectRoot` is the project this connection serves and the default base for paths, and \ + its `roots` are every project in the IDE workspace. That is the scope to search and read, \ + not a list of projects to change; it doesn't change your working directory or \ + permissions. Pass another root as `projectPath` to reach that project. Call it again \ + when the user switches projects or attaches one." ), }) } diff --git a/crates/ide-mcp/tests/server.rs b/crates/ide-mcp/tests/server.rs index eeb3a4295..21b4e7565 100644 --- a/crates/ide-mcp/tests/server.rs +++ b/crates/ide-mcp/tests/server.rs @@ -282,6 +282,8 @@ async fn a_folder_served_in_the_background_answers_initialize_list_and_call() { let init = rpc_for(&s, &inside, "initialize", json!({})).await; let instructions = init["result"]["instructions"].as_str().unwrap(); assert!(instructions.contains(inventory.to_str().unwrap()), "{instructions}"); + // Clients that load tool descriptions lazily still learn to establish the workspace first (#5751). + assert!(instructions.contains("first project-related operation in a session, call `get_workspace_roots`")); let list = rpc_for(&s, &inside, "tools/list", json!({})).await; assert_eq!(list["result"]["tools"][0]["description"], format!("Names {}", inventory.display()), "{list}"); let call = rpc_for(&s, &inside, "tools/call", json!({ "name": "where" })).await;