Skip to content

fix(mcp): default stdio server cwd to the session directory#994

Open
Zehee wants to merge 1 commit into
MoonshotAI:mainfrom
Zehee:fix/mcp-stdio-cwd-in-web
Open

fix(mcp): default stdio server cwd to the session directory#994
Zehee wants to merge 1 commit into
MoonshotAI:mainfrom
Zehee:fix/mcp-stdio-cwd-in-web

Conversation

@Zehee

@Zehee Zehee commented Jun 23, 2026

Copy link
Copy Markdown

When kimi web launches the background server daemon, the server process runs from ~/.kimi-code/server. stdio MCP servers that omitted cwd therefore inherited that directory instead of the user's project directory, breaking workspace-relative storage.

loadMcpServers now assigns a sensible default cwd to every config source:

  • user-global ~/.kimi-code/mcp.json -> session working directory
  • repo-root .mcp.json -> repo root (unchanged)
  • project-local .kimi-code/mcp.json -> session working directory

Closes #982

Problem

When launching a session via kimi web, Kimi Code starts a long-lived background daemon whose working directory is intentionally set to ~/.kimi-code/server (to avoid pinning the caller's directory on Windows). As a result, any stdio MCP server that omits cwd in its config inherited the daemon's directory instead of the user's project directory.

Session-context tools were unaffected because they resolve the active workspace at request time, but storage-oriented tools (e.g. custom memory/note MCP servers) wrote data into a server-side workspace, making kimi web behave differently from local kimi.

Root cause

packages/agent-core/src/mcp/config-loader.ts only supplied a default stdioCwdBase for repo-root .mcp.json:

const [user, projectRoot, project] = await Promise.all([
  readMcpJson(paths.user),                                   // no default cwd
  readMcpJson(paths.projectRoot, { stdioCwdBase: dirname(paths.projectRoot) }),
  readMcpJson(paths.project),                                // no default cwd
]);

With cwd: undefined, StdioClientTransport falls back to process.cwd() of the spawning process — ~/.kimi-code/server in kimi web mode.

Fix

Assign a sensible default cwd to every MCP config source:

Config source Default cwd Reason
~/.kimi-code/mcp.json session working directory User-global servers should run in the current project/session context unless explicitly configured otherwise.
<projectRoot>/.mcp.json project root Preserves the existing Claude-compatible behavior where relative paths resolve from the repo root.
<cwd>/.kimi-code/mcp.json session working directory Project-local Kimi config should run in the project/session context.
const [user, projectRoot, project] = await Promise.all([
  readMcpJson(paths.user, { stdioCwdBase: input.cwd }),
  readMcpJson(paths.projectRoot, { stdioCwdBase: dirname(paths.projectRoot) }),
  readMcpJson(paths.project, { stdioCwdBase: input.cwd }),
]);

input.cwd is the session workDir, so the fix also answers the question of whether kimi web should inherit the CLI's project context: yes, and MCP server spawning is the place where that inheritance must happen.

Why not delay daemon startup?

The kimi web daemon is a singleton that can host sessions across multiple workspaces, so it cannot wait for a single project directory before starting. The correct fix is therefore to make each session supply its own project directory when configuring MCP servers, which is what this change does.

Test updates

Updated packages/agent-core/test/mcp/config-loader.test.ts to assert that user-global and project-local stdio entries receive a default cwd equal to the session directory.

Verification

pnpm exec vitest run packages/agent-core/test/mcp/config-loader.test.ts
pnpm exec vitest run packages/agent-core/test/mcp/connection-manager.test.ts

Both test suites pass.

Checklist

When kimi web launches the background server daemon, the server process
runs from ~/.kimi-code/server. stdio MCP servers that omitted cwd therefore
inherited that directory instead of the user's project directory, breaking
workspace-relative storage.

loadMcpServers now assigns a sensible default cwd to every config source:
- user-global ~/.kimi-code/mcp.json -> session working directory
- repo-root .mcp.json -> repo root (unchanged)
- project-local .kimi-code/mcp.json -> session working directory

Closes MoonshotAI#982
@changeset-bot

changeset-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 894b477

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/agent-core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kimi web starts MCP servers from the CLI installation directory, breaking workspace-relative MCP tools

1 participant