Skip to content

fix: bound MCP connect so mcp list cannot hang - #46800

Closed
RaviTharuma wants to merge 1 commit into
anomalyco:devfrom
RaviTharuma:fix/mcp-list-timeout
Closed

RaviTharuma wants to merge 1 commit into
anomalyco:devfrom
RaviTharuma:fix/mcp-list-timeout

Conversation

@RaviTharuma

@RaviTharuma RaviTharuma commented Sep 2, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #43484

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

mcp list can hang forever if an MCP server never finishes connecting. This times out each MCP create during status init and caps the wait on MCP.status() so a stuck plugin cannot block the command.

How did you verify your code works?

MCP status/list path with a slow or stuck create should return instead of blocking. Unrelated MCP behavior is unchanged.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Copilot AI lite review requested due to automatic review settings September 2, 2026 11:32
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:title labels Sep 2, 2026
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Hey! Your PR title Bound MCP connect so mcp list cannot hang doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The current timeout helper doesn’t cancel underlying connects/spawns (risking lingering sockets/processes and continued hangs), and the CLI timeout fallback suppresses useful error context for users.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR aims to ensure opencode mcp list can’t hang indefinitely when plugins (and their MCP connections) are enabled by bounding MCP initialization work with timeouts.

Changes:

  • Add a timeout wrapper around each create() call during MCP status initialization and mark timed-out servers as failed.
  • Add an overall timeout around MCP.status() in the mcp list CLI command to prevent the command from blocking forever.
File summaries
File Description
packages/opencode/src/mcp/index.ts Wrap per-server create() during status init in a timeout and record failures/timeouts in status.
packages/opencode/src/cli/cmd/mcp.ts Bound mcp list by timing out the MCP.status() call.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/opencode/src/cli/cmd/mcp.ts Resolved
Comment thread packages/opencode/src/mcp/index.ts Resolved
Comment thread packages/opencode/src/mcp/index.ts Resolved
@RaviTharuma RaviTharuma changed the title Bound MCP connect so mcp list cannot hang fix: bound MCP connect so mcp list cannot hang Sep 2, 2026
# Conflicts:
#	packages/opencode/src/cli/cmd/mcp.ts
#	packages/opencode/src/mcp/index.ts

@Hahaknight Hahaknight left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this — opencode mcp list blocking indefinitely on one bad server is a real papercut, and bounding create in the layer is the right instinct. A few things I'd check before merge:

1. The listState timeout wipes all statuses, not just the slow server (packages/opencode/src/cli/cmd/mcp.ts)

mcp.status() returns the whole status record; the orElse replaces it with {}. When it fires, the renderer's if (!status) branch shows every server — including ones that connected fine — as ○ not initialized. One slow server (or N servers each inside their own budget but >20s in aggregate) degrades the entire table into a misleading state. Per-server timeouts (mirroring the layer-side bound) would keep healthy servers rendering; alternatively surface an explicit "timed out" state instead of "not initialized".

2. Interrupt-on-timeout can leak an already-spawned local MCP child (packages/opencode/src/mcp/index.ts, create)

When the new outer timeout fires, create is interrupted mid-flight. The mcpClient.close() cleanup only runs via the inner gen's catchCause, which external interruption bypasses — on interrupt only finalizers run. So a local server that already spawned+connected and then hangs (e.g. in tool listing) gets interrupted with its child process left alive — the descendants/pid machinery elsewhere in this file suggests that's a known hazard here. Wrapping the body in Effect.onInterrupt (close whatever client/transport exists) would make the timeout safe. Worth a manual repro: stdio server that completes the handshake, then stalls on tools/list.

3. Root-cause question: both connection (connectTransport(transport, connectTimeout) at the two mcp.timeout ?? DEFAULT_TIMEOUT sites) and tool listing (McpCatalog.defs → listTools(client, timeout ?? DEFAULT_TIMEOUT), catalog.ts:39) are already individually bounded at 30s. Where was the hang actually observed? If one of those inner bounds isn't effective on some path, this outer bound masks it while the leak in (2) remains. A repro snippet or trace link would help decide whether the inner bound also needs fixing.

Nits

  • listState hardcodes 20 seconds while the layer path uses mcp.timeout ?? DEFAULT_TIMEOUT — a user who raises timeout still gets cut off at 20s in list, with the misleading status from (1).
  • Layer orElse reports "Failed to connect" even if the timeout hit during tool listing after a successful connect; including the key and elapsed time (connect timed out after 30s) would save a confusing debug session.

Direction is right; (1) and (2) are what I'd want addressed before this ships.

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Sep 2, 2026
@github-actions github-actions Bot closed this Sep 2, 2026
@RaviTharuma

Copy link
Copy Markdown
Author

The description now uses the PR template (Closes #43484). GitHub will not let me reopen this PR from here. Please reopen if the template looks ok, or say if you want a fresh PR from the same branch (fix/mcp-list-timeout).

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.

opencode mcp list hangs when plugins are enabled; --pure completes

3 participants