GeezerVison is showing github.com — service: v2 service spawns one local MCP server pair per cataloged project directory at startup · Issue #43845 · anomalyco/opencode
service: v2 service spawns one local MCP server pair per cataloged project directory at startup #43845DescriptionSummary
On startup, the opencode V2 background service (opencode2 serve --service) spawns one full set of local stdio MCP server processes per project directory discovered by its project catalog, instead of one set total. With ~53 directories in the catalog and 2 local MCP servers configured, this produced 51 duplicate MCP process pairs (~153 processes including grandchildren) in a ~70-second burst. The extra processes are never cleaned up and keep running until the service is manually restarted.
Environment
- opencode version: opencode2 v0.0.0-beta-17793 (
@opencode-ai/cli, npm)
- OS: macOS 26.x / Darwin Kernel Version 27.0.0, arm64 (Apple Silicon M1)
- Terminal: Ghostty (xterm-256color, truecolor)
- Shell: fish
- Install/channel: beta (npm global)
- Active plugins: none found in config
Reproduction
- Configure two local stdio MCP servers in
~/.config/opencode/opencode.jsonc (one uv run ... Python server, one native binary):
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"servers": {
"telegram": {
"type": "local",
"command": ["uv", "--directory", "/path/to/telegram-mcp", "run", "--locked", "python", "main.py"]
},
"cua-driver": {
"type": "local",
"command": ["cua-driver", "mcp"]
}
}
}
}
- Have many directories under
$HOME so the project catalog discovers dozens of projects (in this setup the catalog was rooted at /).
- Start or restart the service (
opencode2 service restart, or a fresh boot/login that starts it).
- Watch the process table:
ps -eo pid,ppid,lstart,command | grep -E 'telegram-mcp|cua-driver'
Expected Behavior
One process tree per configured local MCP server, created lazily and reused across projects/sessions; unused trees terminated when no longer needed.
Actual Behavior
Immediately after service start, one (uv telegram-mcp + cua-driver) pair was spawned roughly every 1–2 seconds for ~70 seconds, then spawning stopped — but all spawned pairs stayed alive indefinitely, all as direct children of the service PID:
51 × uv --directory .../telegram-mcp run --locked python main.py
51 × cua-driver mcp
51 × .venv/bin/python3 main.py (grandchildren)
Counts verified stable 15+ minutes later; only restarting the service removes them (same leak observed on 2026-08-18 with 43 pairs / 116 connects in 20s on an earlier beta).
Correlation evidence from ~/.local/share/opencode/log/opencode.log: right after service start it emitted 3,061 catalog.updated events covering exactly ~53 unique directories (/, /Users, /Users/kote, /Users/kote/Notes, home subdirectories...), matching the ~51 leaked pairs almost 1:1. No MCP errors are logged during the burst. A remote MCP server configured alongside these (Composio) is unaffected, consistent with only stdio servers being spawned.
Additional Context
- Impact: dozens of idle processes per service start (RAM, plus side effects — each duplicate telegram-mcp opens a Telegram MTProto session, each cua-driver connects to its daemon).
- Recurs on every service start; not tied to opening sessions.
- Workaround: restart the service after login, or keep the number of discoverable project directories small.
- Possibly related open issues: MCP servers spawn 2-4 duplicate processes per server on startup, causing TasksMax exhaustion and EAGAIN errors #31554, Bug: MCP servers spawn duplicate processes per session — 1 project = 8+ instances, 2+ projects = crash #29939, MCP servers spawned 8-10x per instance on Windows, consuming ~3.5 GB RAM on startup #26336, Local stdio MCP servers leak processes on disconnect / replace / rollback #26714, MCP server processes not cleaned up on exit (orphan processes) + eagerly starts all servers #30123, local MCP server child processes become orphaned on restart #30073, service: restart leaves stale daemon and duplicated MCP servers running #42270 — those describe per-session/per-restart duplication and orphaned children; this report pins a specific trigger on V2: one spawn set per catalog-discovered directory.
Reacted by Mario and Mario Activitygithub-actions commented on Aug 21, 2026 This issue might be a duplicate of existing issues. Please check:
You already noted these as "possibly related" — they may in fact be the same underlying bug or closely related root causes worth consolidating. firefoxmmx2 commented on Sep 12, 2026 Same symptom reproduced on the v2.0.2 stable line — one full local MCP set per directory, and the #46828 browse fix is not present in 2.0.x
Environment
- opencode v2.0.2 (
@opencode/cli, pnpm global), running opencode serve --service
- Linux
6.18.49-2-lts x86_64
- User-scope config with 3 local stdio MCP servers (
mysql + postgres via @bytebase/dbhub, jetbrains via supergateway) plus 1 remote (context7)
Deterministic reproduction (one request)
Reading any directory through the location-scoped fs endpoint used by the picker materializes a full Location runtime and starts a complete set of local MCP servers for that directory.
- Record the baseline of MCP-related processes under the service.
- Call the picker's endpoint for a single directory:
GET /api/fs/list?location[directory]=/tmp/opencode
- Within ~8 seconds the service spawns a new set of every local MCP server:
BEFORE: 8 MCP-related processes
GET /api/fs/list?location[directory]=/tmp/opencode -> 200
AFTER: 14 MCP-related processes (delta +6)
627672 579141 220180 npm exec @bytebase/dbhub@latest --dsn mysql://...***...
627673 579141 197480 npm exec supergateway --sse http://127.0.0.1:64343/sse
627674 579141 193580 npm exec @bytebase/dbhub@latest --dsn postgres://...***...
628094 627672 150616 node .../dbhub --dsn mysql://...***...
628101 627673 100344 node .../supergateway --sse http://127.0.0.1:64343/sse
628107 627674 150452 node .../dbhub --dsn postgres://...***...
That is roughly 1 GB RSS added per directory visited. Browsing a directory tree with N children starts N of these sets. All spawned processes are direct children of the service PID and are not cleaned up.
The fix for #46826 is not in v2.0.2
#46828 (which closed #46826 and #46679) added GET /api/browse/list so the workspace picker and /cd navigate without resolving a Location. On v2.0.2:
GET /api/browse/list returns 404
- the shipped binary contains no
browse route at all (only api/fs/list, api/fs/find, api/fs/read/)
- the full
/openapi.json (122 routes) has no browse entry
So on 2.0.x, directory browsing still goes through fs/list -> LocationMiddleware, the exact mechanism described in #46826 and #37844.
Ask
Was #46828 intended to land in the 2.0.x stable line? If yes, could it be picked/re-landed? If 2.0.x deliberately uses a different navigation path, what is the expected behavior when a browsed directory inherits user-scope local MCP servers?
Related: #46826, #46828, #46679, #37844 yudaiyan commented on Sep 22, 2026 Same symptom on v2.0.12 (Linux/WSL2): one local MCP set per touched directory, kept alive while idle
Environment:
- opencode v2.0.12 (
@opencode-ai/cli)
- Linux 6.6.87.2-microsoft-standard-WSL2, x86_64
- Global config with 3 local stdio MCP servers, all npm/
npx-based: tavily-mcp@latest, @playwright/mcp@latest, @fangjunjie/ssh-mcp-server
- No plugins involved
Setup: one background service (opencode serve --service) and TUIs opened across a few project directories.
The service spawned 5 full MCP sets, one per cwd, including directories with no active session:
| Location (cwd) |
npm exec parents |
set started |
| /opt/study/opencode |
3 |
09:54 |
| /root |
3 |
09:54 |
| /opt/work/hermes-session-manager |
3 |
09:55 |
| /opt/study |
3 |
09:57 |
| /tmp/opencode |
3 |
10:04 |
Each set is npm exec <pkg> (~105–111 MB) → sh -c → the actual node server (67–105 MB). Just the 15 npm exec parents accounted for 1.6 GB RSS; the full MCP tree was ≈ 2.8 GB while all sessions were idle. The service itself was 519 MB.
Sample from one set:
PPID PID RSS COMMAND
28166 28269 106 MB npm exec tavily-mcp@latest
28166 28270 108 MB npm exec @playwright/mcp@latest
28166 28271 108 MB npm exec @fangjunjie/ssh-mcp-server --host ...
28269 28378 76 MB node .../node_modules/.bin/tavily-mcp
28270 28370 105 MB node .../node_modules/.bin/playwright-mcp
28271 28362 67 MB node .../node_modules/.bin/ssh-mcp-server
Nothing is torn down when a Location goes idle — the processes only go away after a service restart. /root (the service's own cwd) had no active session at all but still held a full set.
Looks like the same root cause as #37844 (cached locations multiply MCP subprocesses). The multiplication is especially expensive with npm-based servers, where each copy costs ~190 MB including the npm exec wrapper. Idle-Location teardown, or one shared set per resolved config, would help a lot. opencodejr commented on Oct 28, 2026 Thanks for the detailed report. I reproduced the per-directory MCP duplication on 0.0.0-beta-17793, and it still happens on 2.2.0 and on the latest dev (a882cf9).
The script below does this:
- puts two stub stdio MCP servers in the global config and creates 20 project directories;
- starts
opencode serve;
- makes one MCP list request per directory.
After that, there is one full set of MCP processes per directory (20 of each stub), all children of the server. They are still running after a minute idle and only go away when the server stops.
One difference from your report: restarting the service on its own, with no client connected, spawned nothing in my setup, even with 20 known projects. In your case something seems to have loaded all ~53 directories at startup, probably a client connecting to the service or the beta's project catalog. If you know which client was connected when the burst happened, that would help.
Related issues, most closely related first:
Reproduction script (repro.sh)
#!/usr/bin/env bash
# Repro for #43845: does the service spawn one local MCP set per known project directory at startup?
# Result on beta-17793, 2.2.0 and dev a882cf98: no MCP spawn at bare startup, but each directory that is
# touched through the API gets its own full set of local MCP processes, which stay alive while the service runs.
# Usage: OC="opencode" bash repro.sh (or OC="bun ... packages/cli/src/index.ts")
set -u
OC=${OC:-opencode}
export HOME=/tmp/opencode/h43845
rm -rf "$HOME"; mkdir -p "$HOME/.config/opencode" "$HOME/bin"
cat > "$HOME/bin/stub-mcp.js" <<'JS'
// minimal stdio MCP server
const rl=require('readline').createInterface({input:process.stdin});
rl.on('line',l=>{let m;try{m=JSON.parse(l)}catch{return}
if(m.id===undefined)return;
let r={};
if(m.method==='initialize')r={protocolVersion:m.params?.protocolVersion||'2024-11-05',capabilities:{tools:{}},serverInfo:{name:process.argv[2],version:'1'}};
else if(m.method==='tools/list')r={tools:[{name:'ping',description:'ping',inputSchema:{type:'object',properties:{}}}]};
process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result:r})+'\n')});
setInterval(()=>{},1e9);
JS
cat > "$HOME/.config/opencode/opencode.jsonc" <<JSON
{ "mcp": { "servers": {
"stubA": { "type": "local", "command": ["node", "$HOME/bin/stub-mcp.js", "stubA-43845"] },
"stubB": { "type": "local", "command": ["node", "$HOME/bin/stub-mcp.js", "stubB-43845"] }
} } }
JSON
for i in $(seq 1 20); do d="$HOME/proj$i"; mkdir -p "$d"; git -C "$d" init -q; git -C "$d" -c user.email=a@b -c user.name=a commit -q --allow-empty -m init; done
count(){ pgrep -fc 'stub-mcp.js stubA-43845' || true; }
PORT=47845
export OPENCODE_PASSWORD=pw43845
CURL="curl -s -u opencode:pw43845"
start(){ ( cd "$HOME" && $OC serve --port $PORT >"$HOME/serve.log" 2>&1 & echo $! >"$HOME/pid" ); sleep 8; }
stop(){ pkill -f "serve --port $PORT"; sleep 2; pkill -f 'stub-mcp.js stub' ; sleep 1; }
start
echo "fresh start, after 8s: stubA processes = $(count)"
# make every project directory known to the server (one session-list read per directory)
# make every project directory known to the server: create one session in each (no prompt, no model needed)
for i in $(seq 1 20); do $CURL -o /dev/null -w '%{http_code} ' -X POST -H 'content-type: application/json' -H "x-opencode-directory: $HOME/proj$i" -d '{}' "http://127.0.0.1:$PORT/api/session"; done; echo
sleep 5
echo "after creating sessions in 20 directories: stubA processes = $(count)"
# list MCP servers in each directory (what a client does to show the MCP status per project)
for i in $(seq 1 20); do $CURL -o /dev/null -H "x-opencode-directory: $HOME/proj$i" "http://127.0.0.1:$PORT/api/mcp"; done
sleep 10
echo "after listing MCP in 20 directories: stubA processes = $(count)"
sleep 60
echo "60s later, idle: stubA processes = $(count)"
$CURL "http://127.0.0.1:$PORT/api/debug/location" | head -c 300; echo
stop
echo "after stop: $(count)"
start; sleep 30
echo "restart (no requests), after 38s: stubA processes = $(count)"
stop
Run it: bash repro.sh
crazycatBLG commented on Nov 9, 2026 Linux, opencode 2.2.6, three stdio MCP servers configured (local python + npx chromium).
After normal use, N duplicate sets of MCP processes exist — one full set per project directory touched. Each set = 4 processes (vault python, npm exec, sh -c wrapper, chrome-devtools-mcp binary), observed 12 sets / 48 processes.
No cleanup: at the time of writing the log has 75 mcp connected events (25 per server) and 0 close/disconnect events; sets survive 6+ min idle.
Memory: 1.47 GB RSS observed for 12 sets; per set 190–275 MB for fresh sets (older ones settle to ~123 MB).
Deterministic repro: cd <new-dir> && opencode mcp list → one new set appears; repeat in another fresh directory → +1 set. Measured 11 → 12 sets after a single call. Independently reproduced: 3 → 4 sets, then removed, back to 3.
Side effect of the same lazy boot: the first mcp list in a fresh directory prints "No MCP servers configured" because status is read before the servers finish connecting (~1.5 s).
opencode service restart clears all sets with no orphans left behind.
neriousy commented on Nov 13, 2026 Closing as a duplicate of #37844. The beta's startup flood, where every directory got loaded, was fixed in #46679 and #46826. On current dev, startup only resumes suspended sessions. What's left is that each loaded location starts its own set of local MCP servers right away, and #37844 tracks that. Until it's fixed, two things help: pool: true on an MCP server shares it across locations (#61185; local servers need a fixed cwd), and idle locations are unloaded after experimental.location_idle_timeout. Please post your process counts on #37844.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|