zed
zed copied to clipboard
Starting MCP is unstable due to race conditions in setting env variable
Summary
I'm on a Mac and have configured mise to setup my tools such as node, npm, python and use fish shell. I have noticed that using mcp in zed is a hit or miss when starting it with the spotlight but always works when i start it from the terminal using zed cli. Looking closely at the logs I belive their is a race condition on setting the environment variable.
Here is a simple config for setting up a hello world mcp server.
{
"context_servers": {
"mcp-hello-world": {
"command": {
"path": "mise",
"args": ["exec", "--", "bunx", "-y", "[email protected]"]
}
}
}
Here is a log of it failing to start.
2025-06-04T08:47:48-07:00 INFO [zed] ========== starting zed ==========
2025-06-04T08:47:48-07:00 INFO [db] Opening main db
2025-06-04T08:47:48-07:00 INFO [db] Opening main db
2025-06-04T08:47:48-07:00 INFO [zed] Using git binary path: Some("/Applications/Zed Preview.app/Contents/MacOS/git")
2025-06-04T08:47:48-07:00 INFO [prompt_store::prompts] No prompt template overrides directory found at /Users/prabirshrestha/.config/zed/prompt_overrides. Using built-in prompts.
2025-06-04T08:47:48-07:00 INFO [assistant_tools::terminal_tool] agent selected bash for terminal tool
2025-06-04T08:47:48-07:00 INFO [extension_host] extensions updated. loading 10, reloading 0, unloading 0
2025-06-04T08:47:48-07:00 INFO [db] Opening main db
2025-06-04T08:47:48-07:00 INFO [context_server::client] starting context server (executable="mise", args=["exec", "--", "bunx", "-y", "[email protected]"])
2025-06-04T08:47:48-07:00 ERROR [project::context_server_store] mcp-hello-world context server failed to start: failed to spawn command. (path="mise", args=["exec", "--", "bunx", "-y", "[email protected]"])
2025-06-04T08:47:48-07:00 INFO [util] set environment variables from shell:/opt/homebrew/bin/fish, path:/Users/prabirshrestha/.local/share/mise/installs/bun/1.2.14/bin:/Users/prabirshrestha/.local/share/mise/installs/node/24.0.2/bin:/opt/homebrew/opt/mise/bin:/Users/prabirshrestha/.grit/bin:/Users/prabirshrestha/.cargo/bin:/Users/prabirshrestha/.bun/bin:/opt/homebrew/opt/[email protected]/libexec/bin:/opt/homebrew/sbin:/opt/homebrew/bin:/Users/prabirshrestha/.dotfiles/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/opt/podman/bin:/Users/prabirshrestha/.cache/lm-studio/bin
Using "path": "/opt/homebrew/opt/mise/bin/mise", always works.
Ideally using bunx directly without any references to mise should work.
more zed log
2025-06-04T08:59:42-07:00 INFO [zed] ========== starting zed ==========
2025-06-04T08:59:42-07:00 INFO [db] Opening main db
2025-06-04T08:59:42-07:00 INFO [db] Opening main db
2025-06-04T08:59:42-07:00 INFO [zed] Using git binary path: Some("/Applications/Zed Preview.app/Contents/MacOS/git")
2025-06-04T08:59:42-07:00 INFO [prompt_store::prompts] No prompt template overrides directory found at /Users/prabirshrestha/.config/zed/prompt_overrides. Using built-in prompts.
2025-06-04T08:59:42-07:00 INFO [assistant_tools::terminal_tool] agent selected bash for terminal tool
2025-06-04T08:59:42-07:00 INFO [extension_host] extensions updated. loading 10, reloading 0, unloading 0
2025-06-04T08:59:42-07:00 INFO [db] Opening main db
2025-06-04T08:59:42-07:00 INFO [context_server::client] starting context server (executable="bunx", args=["-y", "[email protected]"])
2025-06-04T08:59:42-07:00 ERROR [project::context_server_store] mcp-hello-world context server failed to start: failed to spawn command. (path="bunx", args=["-y", "[email protected]"])
2025-06-04T08:59:42-07:00 INFO [util] set environment variables from shell:/opt/homebrew/bin/fish, path:/Users/prabirshrestha/.local/share/mise/installs/bun/1.2.14/bin:/Users/prabirshrestha/.local/share/mise/installs/node/24.0.2/bin:/opt/homebrew/opt/mise/bin:/Users/prabirshrestha/.grit/bin:/Users/prabirshrestha/.cargo/bin:/Users/prabirshrestha/.bun/bin:/opt/homebrew/opt/[email protected]/libexec/bin:/opt/homebrew/sbin:/opt/homebrew/bin:/Users/prabirshrestha/.dotfiles/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/opt/podman/bin:/Users/prabirshrestha/.cache/lm-studio/bin
Zed Version and System Specs
Zed: v0.189.5 (Zed Preview) OS: macOS 15.5.0 Memory: 36 GiB Architecture: aarch64
Can you see if you can reproduce with v0.190.x (Zed Preview) that was just released an hour ago?
It includes the following change to how environments are loaded:
- https://github.com/zed-industries/zed/pull/31799
Zed: v0.190.0 (Zed Preview)
OS: macOS 15.5.0
Memory: 36 GiB
Architecture: aarch64
doesn't work with
"mcp-hello-world": {
"command": {
"path": "bunx",
"args": ["-y", "[email protected]"]
}
}
would be good to have more logs when error occurs.
mcp-hello-world context server failed to start: failed to spawn command.
Few things that could be added - exit code, env path, is there more detail than failed to spawn command such as command not found.
This could be very specific to the default shell.
When my shell is zsh it seems to work.
2025-06-04T16:32:00-07:00 INFO [zed] ========== starting zed ==========
2025-06-04T16:32:00-07:00 INFO [db] Opening main db
2025-06-04T16:32:00-07:00 INFO [db] Opening main db
2025-06-04T16:32:00-07:00 INFO [zed] Using git binary path: Some("/Applications/Zed Preview.app/Contents/MacOS/git")
2025-06-04T16:32:00-07:00 INFO [util] updating SHELL environment variable to value from passwd entry: "/bin/zsh"
2025-06-04T16:32:01-07:00 INFO [util] set environment variables from shell:/bin/zsh, path:/Users/prabirshrestha/.local/share/mise/installs/bun/1.2.14/bin:/Users/prabirshrestha/.local/share/mise/installs/node/24.0.2/bin:/Users/prabirshrestha/.grit/bin:/opt/homebrew/bin:/Users/prabirshrestha/.config/nvim/plugins/vim-themis/bin:/Users/prabirshrestha/.cargo/bin:/Users/prabirshrestha/go/bin:/Users/prabirshrestha/Library/Python/3.7/bin:/Users/prabirshrestha/.proto/shims:/Users/prabirshrestha/.proto/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/opt/podman/bin:/Users/prabirshrestha/.cargo/bin:/Users/prabirshrestha/.cache/lm-studio/bin
2025-06-04T16:32:01-07:00 INFO [prompt_store::prompts] No prompt template overrides directory found at /Users/prabirshrestha/.config/zed/prompt_overrides. Using built-in prompts.
2025-06-04T16:32:01-07:00 INFO [assistant_tools::terminal_tool] agent selected bash for terminal tool
2025-06-04T16:32:01-07:00 INFO [extension_host] extensions updated. loading 10, reloading 0, unloading 0
2025-06-04T16:32:01-07:00 INFO [db] Opening main db
2025-06-04T16:32:01-07:00 INFO [context_server::client] starting context server (executable="bunx", args=["-y", "[email protected]"])
2025-06-04T16:32:01-07:00 INFO [context_server] starting context server mcp-hello-world
2025-06-04T16:32:01-07:00 INFO [client] set status on client 0: Authenticating
but when it is fish it doesn't work.
025-06-04T16:33:48-07:00 INFO [zed] ========== starting zed ==========
2025-06-04T16:33:48-07:00 INFO [db] Opening main db
2025-06-04T16:33:48-07:00 INFO [db] Opening main db
2025-06-04T16:33:48-07:00 INFO [zed] Using git binary path: Some("/Applications/Zed Preview.app/Contents/MacOS/git")
2025-06-04T16:33:48-07:00 INFO [prompt_store::prompts] No prompt template overrides directory found at /Users/prabirshrestha/.config/zed/prompt_overrides. Using built-in prompts.
2025-06-04T16:33:48-07:00 INFO [assistant_tools::terminal_tool] agent selected bash for terminal tool
2025-06-04T16:33:48-07:00 INFO [extension_host] extensions updated. loading 10, reloading 0, unloading 0
2025-06-04T16:33:48-07:00 INFO [db] Opening main db
2025-06-04T16:33:48-07:00 INFO [context_server::client] starting context server (executable="bunx", args=["-y", "[email protected]"])
2025-06-04T16:33:48-07:00 ERROR [project::context_server_store] mcp-hello-world context server failed to start: failed to spawn command. (path="bunx", args=["-y", "[email protected]"])
2025-06-04T16:33:49-07:00 INFO [client] set status on client 0: Authenticating
2025-06-04T16:33:49-07:00 INFO [util] set environment variables from shell:/opt/homebrew/bin/fish, path:/Users/prabirshrestha/.local/share/mise/installs/bun/1.2.14/bin:/Users/prabirshrestha/.local/share/mise/installs/node/24.0.2/bin:/opt/homebrew/opt/mise/bin:/Users/prabirshrestha/.grit/bin:/Users/prabirshrestha/.cargo/bin:/Users/prabirshrestha/.bun/bin:/opt/homebrew/opt/[email protected]/libexec/bin:/opt/homebrew/sbin:/opt/homebrew/bin:/Users/prabirshrestha/.dotfiles/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/opt/podman/bin:/Users/prabirshrestha/.cache/lm-studio/bin
(not familiar with zed code but looked around on how start server and set env is impelmented).
looking at the PR and code in zed at how env is set and the mcp server is started, I don't think the PR would solve.
At least based on my logs it seems like it is setting the env correctly but after the mcp server has started thus causing the binary to not be found and failing.
Env seems to be set in the background and context server also seems to be set in the background so there is a potential race condition.
I think the right solution is to wait for the env to be set before starting the mcp server. or when mcp server is starting always read the env path and pass it to the process so it gets loaded correctly.
I was about to submit an issue and searching led me here. (yes it happens on v0.190.0-pre)
The issue I'm experiencing is similar but not exactly the same. It doesn't matter whether Zed is started from the launcher or terminal, it always ends up with this behavior:
- For MCP servers configured via extensions, they always work, showing the list of tools.
- For manually configured servers, they have a high chance of ending up with 0 tools.
Notably, flipping the switch off and on again solves it 100%. The tools are always loaded.
I have a local patch that solves it. I'm polishing it before sending a PR but so far it works perfectly for me. With the patch on I get the tools loaded 100% of the time. The race condition is gone.
While the symptoms are different, I believe the patch would solve this as well, so I'm not submitting a separate issue but commenting here instead.
Never mind. Looking more closely at your issue it seems to be unrelated and my patch won't solve it. In my case context servers never have issues starting up. They just start up too soon to get registered. Your logs show a spawn failure instead. I will submit another issue.
Edit: submitted #32132.
I'm experiencing a similar issue on 0.190.5 (linux). I have an mcp server (mcp-nixos) working in claude code; when I set it up in zed, it's recognized by zed the agent panel (it shows 18 new properly named tools), but they are not communicated to/not visible to the agent. The failure is silent in that there seems to be no detection that there's a rather important context (mcp tools) which exist according to zed, but are not being passed to agents at all.
@ammkrn not sure if this will help but suggest using visual studio code to see if it works there. few things i have noticed is different clients have different way of handling. i have noticied some client only handle strict agent names ie a-zA-Z-_ and not other chars, some may silently ignore, some will throw errors. Until Zed logging improves related to mcp servers it is very hard to figure out what is wrong. I probably need to start having this list and file another bug to improve the experience of MCP server development from Zed.