You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mcp-client: run stdio tool calls in the calling Session's working directory (opt-in perWorkingDirectory)
#13951
dsh-mcp-client starts one stdio child per config row, with a fixed cwd. Every Session and subagent shares that child, whatever folder it works in.
That breaks MCP servers that take their scope from the directory they start in. Code indexes are the usual case: they open the repository around their working directory. One real example is the Memtrace plugin. Its row has no cwd, so the child starts in the profile folder (~/.dsh/profiles/desktop), and every call fails:
memdb: server_info unavailable: no workspace owner is attached yet;
this session attaches when `memtrace start` publishes one
Pinning cwd in the row works for only one project at a time. A plugin can't fix this on its own: MCP stdio carries no per-call Session identity, and the client passes the child neither the Session's directory nor MCP roots. Roots wouldn't solve it anyway, because they are set per connection, not per call.
Proposal: an opt-in stdio option, perWorkingDirectory
Option off (the default): nothing changes. Servers that must run in one fixed folder keep using cwd alone.
Option on:
The child in cwd still lists tools and serves resources and server instructions, so tool registration is unchanged.
Each tool call goes to a child started in the calling Session's working directory, the one workingDirectory.ensure(exec.agent) returns. That is the same source bash uses for its default workdir.
A call with no Agent, without the working-directory service, or already in cwd stays on the main child.
Lifecycle:
Routed children start on first use from the same resolved config (bindingValue.config), with only cwd swapped.
They have no reconnect loop: a child that closes is dropped, and the next call respawns it.
A child closes after 10 minutes idle. At most 16 run at once; the least recently used idle child makes room, and a call is refused only when all of them are busy.
A credential refresh or plugin unload closes them all, and a child still connecting is cancelled.
Compatibility: Schemastery keeps unknown keys, so a row carrying perWorkingDirectory loads on a dsh that doesn't know the option, which ignores it. A plugin can ship the key before the client supports it.
The hook in the tool bridge (src/tools.ts)
exportinterfaceToolBridgeOptions{// .../** Pick the connection for one tool call; undefined keeps the listing connection. */route?: (execution: ToolExecution)=>Promise<ToolCallRoute|undefined>}exportinterfaceToolCallRoute{client: Client/** Lifetime of the leased connection, combined with the call's own signal. */signal: AbortSignalrelease(): void}// inside the registered tool's call():letroute: ToolCallRoute|undefinedtry{route=awaitopts.route?.(execution)constresult=await(route?.client??client).callTool({name: tool.name,arguments: args},{signal: AbortSignal.any([execution.signal,opts.signal, ...route===undefined ? [] : [route.signal]]),timeout: opts.toolCallTimeoutMs,toolDefinition: tool},)// ...}finally{route?.release()}
Routing in the connection supervisor (src/connection.ts)
The pool (src/working-directory-pool.ts, about 200 lines) keys children by directory, case-folded on Windows. Concurrent first calls share one spawn, and a caller that aborts while waiting doesn't cancel the spawn for the other callers. It tracks leases, so idle eviction never touches a child in use.
Security: resolve the command before spawning in a Session folder
On Windows, cross-spawn (used by the SDK's stdio transport) changes into options.cwd before it looks up the command, and which checks the current folder first. Without care, a Session working in an untrusted cloned repo that contains a memtrace.cmd would get that file run outside the sandbox. Before this option, cwd only ever came from operator config, so the problem couldn't arise.
So routed children spawn with the command already resolved to an absolute path, exactly as a spawn in the operator-chosen cwd would find it (src/command-path.ts):
exportfunctionresolveCommandPath(command: string,baseDir: string,env: Record<string,string|undefined>): string{if(isAbsolute(command))returncommandif(command.includes('/')||process.platform==='win32'&&command.includes('\\'))returnresolvePath(baseDir,command)constextensions=process.platform==='win32'
? (envValue(env,'PATHEXT')??'.COM;.EXE;.BAT;.CMD').split(';').filter(Boolean)
: []constnames=process.platform!=='win32' ? [command]
: extname(command)!=='' ? [command, ...extensions.map(extension=>command+extension)]
: extensions.map(extension=>command+extension)// Windows spawns search the working directory before PATH; the operator-chosen// directory keeps that order, the Session's directory is never searched.constsearched=(envValue(env,'PATH')??'').split(delimiter)if(process.platform==='win32')searched.unshift(baseDir)for(constdirofsearched){if(dir===''||!isAbsolute(dir))continuefor(constnameofnames){constcandidate=join(dir,name)if(executable(candidate))returncandidate}}thrownewError(`command ${JSON.stringify(command)} was not found on PATH`)}
args are passed as configured, so the docs say they must not depend on the working directory.
How it was tested
I wrote it against dsh-v0.2.2-rc.5, and it applies cleanly there.
17 vitest specs under tests/ pass (working-directory-pool.spec.ts, working-directory.spec.ts, with a fixture server that reports its cwd):
pool races: shared spawn, abort while waiting, eviction while connecting, idle timer, LRU cap, closeAll/reopen;
the real startConnection: routed call versus no Agent, option off, no working-directory service, no routing after dispose;
command resolution against a file planted in the Session folder.
Live in Desktop 0.2.2-rc.5 (hot-patched lib/index.js): DeepSeek subagents started in two different repositories each called mcp__memtrace__list_indexed_repositories. Each got its own repository, and dsh ran one memtrace mcp child per Session folder.
Not done: the full package suite, the repository typecheck, and verify-translation-pairing for the README pair.
Rebase: current master changed connection.ts, tools.ts, index.ts and the READMEs (OAuth, tool loading, error codes), so the patch needs rebasing. Happy to share the full patch if that helps.
Does a fixed policy (10 minutes idle, 16 children) fit, or would you rather expose it as config?
Tool lists come only from the child in cwd, which assumes every directory's child serves the same tools. That holds for code indexes. Should it be enforced, for example by comparing tool lists on the first call?
ensure() returns a path in the execution world. This assumes the Session shares the Host filesystem; is that true for every execution world you support?
The Memtrace plugin already ships the key (dsh-plugin-memtrace 0.3.0) and documents the stock-dsh fallback: restating the row with cwd.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Problem
dsh-mcp-clientstarts one stdio child per config row, with a fixedcwd. Every Session and subagent shares that child, whatever folder it works in.That breaks MCP servers that take their scope from the directory they start in. Code indexes are the usual case: they open the repository around their working directory. One real example is the Memtrace plugin. Its row has no
cwd, so the child starts in the profile folder (~/.dsh/profiles/desktop), and every call fails:Pinning
cwdin the row works for only one project at a time. A plugin can't fix this on its own: MCP stdio carries no per-call Session identity, and the client passes the child neither the Session's directory nor MCProots. Roots wouldn't solve it anyway, because they are set per connection, not per call.Proposal: an opt-in stdio option,
perWorkingDirectorycwdalone.cwdstill lists tools and serves resources and server instructions, so tool registration is unchanged.workingDirectory.ensure(exec.agent)returns. That is the same sourcebashuses for its defaultworkdir.working-directoryservice, or already incwdstays on the main child.bindingValue.config), with onlycwdswapped.perWorkingDirectoryloads on a dsh that doesn't know the option, which ignores it. A plugin can ship the key before the client supports it.The hook in the tool bridge (
src/tools.ts)Routing in the connection supervisor (
src/connection.ts)The pool (
src/working-directory-pool.ts, about 200 lines) keys children by directory, case-folded on Windows. Concurrent first calls share one spawn, and a caller that aborts while waiting doesn't cancel the spawn for the other callers. It tracks leases, so idle eviction never touches a child in use.Security: resolve the command before spawning in a Session folder
On Windows,
cross-spawn(used by the SDK's stdio transport) changes intooptions.cwdbefore it looks up the command, andwhichchecks the current folder first. Without care, a Session working in an untrusted cloned repo that contains amemtrace.cmdwould get that file run outside the sandbox. Before this option,cwdonly ever came from operator config, so the problem couldn't arise.So routed children spawn with the command already resolved to an absolute path, exactly as a spawn in the operator-chosen
cwdwould find it (src/command-path.ts):argsare passed as configured, so the docs say they must not depend on the working directory.How it was tested
dsh-v0.2.2-rc.5, and it applies cleanly there.tests/pass (working-directory-pool.spec.ts,working-directory.spec.ts, with a fixture server that reports itscwd):closeAll/reopen;startConnection: routed call versus no Agent, option off, noworking-directoryservice, no routing after dispose;lib/index.js): DeepSeek subagents started in two different repositories each calledmcp__memtrace__list_indexed_repositories. Each got its own repository, and dsh ran onememtrace mcpchild per Session folder.verify-translation-pairingfor the README pair.masterchangedconnection.ts,tools.ts,index.tsand the READMEs (OAuth, tool loading, error codes), so the patch needs rebasing. Happy to share the full patch if that helps.Related discussions
mcp.json): these choose which servers a project gets. This proposal is complementary: it keeps one configured server but runs it in each Session's directory. A project-scoped lifecycle, as Project-level MCP: make MCP servers a first-class, project-scoped capability #13149 describes, could reuse the same pool.workingDirectory.ensure(agent), which is the projection, notheader.cwd. That is why subagents started withcwd, including ones in git worktrees, get their own child..cmdlaunch on Windows): the resolver above turns a bare name such asmemtraceinto an absolute npm.cmdshim. On Desktop 0.2.2-rc.5 that launched fine through the SDK'scross-spawn; worth keeping in mind with that report.Open questions for the team
cwd, which assumes every directory's child serves the same tools. That holds for code indexes. Should it be enforced, for example by comparing tool lists on the first call?ensure()returns a path in the execution world. This assumes the Session shares the Host filesystem; is that true for every execution world you support?The Memtrace plugin already ships the key (dsh-plugin-memtrace 0.3.0) and documents the stock-dsh fallback: restating the row with
cwd.All reactions