Skip to content

How to access the current working directory when an MCP server is launched via claude mcp add ... -- uvx my-mcp #1520

@SepineTam

Description

@SepineTam

Question

📄 Description

When developing a custom MCP server using the MCP Python SDK, I encountered an issue related to how Claude Code and uvx manage the working directory (cwd) context.

When adding the server via:

claude mcp add my-mcp -- uvx my-mcp

Claude Code launches the MCP server inside a uvx execution environment.
In this setup, calling:

import os
print(os.getcwd())

inside the MCP server returns a temporary cache path under ~/.cache/uv/...,
rather than the actual project directory (i.e., the workspace directory where Claude Code was started).


⚙️ Actual Behavior

  • The MCP server runs inside the uvx sandbox, so its cwd is not inherited from Claude Code’s current workspace.
  • There appears to be no standardized way (via the SDK or protocol) to obtain the actual working directory of the Claude Code session.
  • As a result, any file or project-level operation inside the MCP server loses context of the user’s workspace.

💡 Expected Behavior

It would be helpful if the SDK (or Claude Code client) exposed the working directory through one of the following mechanisms:

  1. Pass the current workspace path (cwd) as part of the MCP connection handshake payload;
  2. Make it available via an environment variable, e.g. CLAUDE_CODE_CWD;
  3. Provide a higher-level helper function, such as:
from mcp import get_workspace_dir
workspace = get_workspace_dir()

This would allow MCP developers to perform file operations relative to the same workspace Claude Code is operating in.


🧭 Environment

  • Claude Code version: 2.0.22
  • MCP Python SDK version: latest (as of Oct 2025)
  • Launch command: claude mcp add my-mcp -- uvx my-mcp
  • OS: macOS
  • Connection type: stdio

🧠 Additional Context

I’m developing a custom MCP server that needs to perform project-level operations such as reading configuration files, scanning directories, or generating code.
However, because Claude Code’s runtime context and the uvx environment differ, it’s currently not possible to determine the correct working directory from inside the MCP SDK.

If Claude Code already provides this information during connection setup, could the SDK expose it (e.g., in a connection context object or during the handshake)?
Otherwise, adding this capability would be highly valuable for developers building workspace-aware MCP servers.


✅ Suggested Enhancement

If feasible, please consider including the workspace path in the connection metadata that the MCP client (Claude Code) sends to the server, or provide SDK-level access to it for better integration with Claude Code’s environment.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions