Similar to this report it seems that environmental variables created by direnv are not part of the cursor-agent environment.
This is very problematic for me because I need to use a specific GitHub personal access token with one project. We check in .cursor/mcp.json to our Git repository, and have this config:
Hey, thanks for the detailed report. This is a known issue. The CLI starts a non-interactive shell (zsh -c), so it only sources ~/.zshenv, not ~/.zshrc. Since most users put eval "$(direnv hook zsh)" in .zshrc, direnv never runs.
Workaround: move the direnv hook to ~/.zshenv:
# ~/.zshenv
eval "$(direnv hook zsh)"
This makes sure direnv loads even in non-interactive shell sessions.
One more thing. The ${env:GITHUB_PERSONAL_ACCESS_TOKEN} syntax in your mcp.json is VS Code-specific and might not resolve in the CLI. If the workaround above doesn’t fix the MCP config, try ${GITHUB_PERSONAL_ACCESS_TOKEN} instead, without the env: prefix.
The team is aware of the CLI environment inheritance issue. Your report helps with visibility and prioritization.
Removing the env: from ${env:GITHUB_PERSONAL_ACCESS_TOKEN} did allow GitHub MCP to authenticate, but it used my global GITHUB_PERSONAL_ACCESS_TOKEN instead of the one configured in .envrc, even after making the change to ~/.zshrenv. So no, the workaround suggested does not work.
Furthermore, removing env: from mcp.json is contradictory, because the cursor-agent mcp docs clearly state:
MCP in the CLI uses the same configuration as the editor
And if that is the case, then ENV interpolation should be using the env: prefix…? I don’t want to break my team’s ability to use Cusor (desktop) with this change.
Clearly there are some fundamental issues with cursor-agent and environment variables, and possibly some discrepancies between Cursor (desktop) and cusor-agent when it comes to MCP servers. This is very frustrating.
Hey, fair point. The .zshenv workaround doesn’t fully fix it. Even if the direnv hook loads, cursor-agent doesn’t guarantee that .envrc will activate for a specific project.
About the docs, you’re right. The CLI MCP page at MCP | Cursor Docs clearly says “MCP in the CLI uses the same configuration as the editor”, but in practice the ${env:...} syntax isn’t supported in the CLI.
@deanrie is there a way to automatically inject environmental variables specifically into Cursor’s local agent at all–I don’t need to use direnv and would gladly use a workaround!?
I want to solve the same problem woody has mentioned but am not tied to using DIRENV
# ~/.zshenv or ~/.bashrc
export GITHUB_PERSONAL_ACCESS_TOKEN="your-token"
If you have lots of project-specific variables, you can run source .envrc && cursor-agent. This loads variables from .envrc into the current session before the agent starts.
For MCP config in the CLI, use ${GITHUB_PERSONAL_ACCESS_TOKEN} instead of ${env:GITHUB_PERSONAL_ACCESS_TOKEN}. The env: syntax isn’t supported in the CLI yet.
@acehinnnqru same workarounds above. The issue is that cursor-agent starts a non-interactive shell and doesn’t inherit variables from .zshrc or direnv. Until there’s a fix, you need to make sure the variables are available in the environment before starting the agent.
The team is aware of both issues inheritance and the ${env:...} syntax. There’s no ETA yet, but your reports help with prioritization.
Thanks for adding suggestions for us w.r.t CLI. I was primarily referring to the agent that’s automatically dispatched through the agent window. I’m looking to do something like this: https://github.com/elviskahoro/elviskahoro/blob/main/mcp.json