jcode icon indicating copy to clipboard operation
jcode copied to clipboard

Non-shared MCP servers can start in daemon cwd and shared pool

Open Helweg opened this issue 4 months ago • 2 comments

Summary

In daemon mode, workspace-sensitive MCP servers configured with shared: false can still start in the daemon's working directory instead of the session workspace. The global SharedMcpPool also attempts to start these non-shared servers, which can create an extra process with the wrong repository context.

This affects MCP servers that infer their project from cwd, such as semantic code indexers, language tooling, and repository-local automation.

Reproduction

  1. Configure a global stdio MCP server that reports or uses its current directory:
{
  "servers": {
    "workspace-server": {
      "command": "/path/to/workspace-sensitive-mcp",
      "shared": false
    }
  }
}
  1. Start Jcode's daemon from directory A.
  2. Open a Jcode session whose working directory is repository B.
  3. Connect the MCP server or let connect_all start it.

Actual behavior

  • The per-session subprocess inherits the Jcode daemon/process working directory rather than repository B.
  • SharedMcpPool::connect_all also starts enabled servers without checking shared, so a shared: false server may be started once globally in addition to its per-session instance.

For a code indexer, this can silently index the daemon directory or another repository and return irrelevant results.

Expected behavior

  • Owned/non-shared MCP subprocesses should use the session's project directory as their process cwd.
  • The global shared pool should ignore shared: false servers.
  • Shared servers should retain their current daemon-global behavior.
  • Sessions without a project directory should retain the existing inherited-cwd fallback.

Root cause

McpManager already retains the session project_dir for project-local MCP configuration lookup, but owned connections call McpClient::connect, whose tokio::process::Command has no current_dir set.

Separately, SharedMcpPool::connect_all filters enabled servers but not the shared flag.

Proposed fix

  • Add a client connection path that accepts an optional working directory and applies it with Command::current_dir.
  • Use the manager's project_dir for owned connections in both eager connect_all and connect-on-first-call paths.
  • Filter non-shared servers out of SharedMcpPool::connect_all.
  • Add client, manager, and shared-pool regression tests.

Related issues

This is a follow-up to #420 and #473, which established that session/project cwd should be used for configuration and client behavior. #481 is related to session cwd selection, but this report does not claim to fix that separate issue. Here, the manager already has the correct session directory and fails to propagate it to the MCP subprocess.

Helweg avatar Jul 22 '26 12:07 Helweg

I have a validated implementation ready on Helweg:fix/mcp-session-workspace-cwd: https://github.com/1jehuang/jcode/compare/master...Helweg:jcode:fix/mcp-session-workspace-cwd?expand=1

Validation: 35 focused MCP tests pass, cargo check -p jcode-base passes, and normal Clippy passes. GitHub's API is currently refusing pull-request creation for my account, so the compare link is ready for PR creation once that repository permission is available.

Helweg avatar Jul 22 '26 12:07 Helweg

Thanks @Helweg for the precise analysis — the root cause was exactly as described: owned connections never set current_dir, and the shared pool ignored the shared flag.

Fix is up in #562: owned (shared: false) servers now spawn with the session's project directory as cwd (with the inherited-cwd fallback when no project dir is known or it doesn't exist), and SharedMcpPool::connect_all skips non-shared servers. Includes client and pool regression tests.


— Jcode agent (automated triage), on behalf of @1jehuang

1jehuang avatar Jul 22 '26 19:07 1jehuang