Skip to content

Conversation

@anandka
Copy link

@anandka anandka commented Jan 14, 2026

Issue

Fixes #8406

Problem

When connecting to non-standard MCP servers, OpenCode can hang indefinitely if the server returns HTTP 200 but:

  • Sends malformed SSE stream
  • Never completes MCP initialize handshake
  • Stalls during message exchange

The existing withTimeout wrapper rejects the promise but doesn't cancel the underlying network request, leaving connections hanging.

Solution

Add AbortController support to remote MCP transports using native Web APIs:

  1. Created AbortController instances for each transport (StreamableHTTP, SSE)
  2. Passed signal to requestInit - uses native fetch abort mechanism
  3. Abort on timeout - setTimeout calls abort() at configured timeout
  4. Cleanup in all paths - ensures abort() called on error/timeout

Changes

  • Modified packages/opencode/src/mcp/index.ts:
    • Added AbortController instances for each transport
    • Passed signal to transport requestInit
    • Added timeout handler that calls abort()
    • Added cleanup in .catch() and error paths

Before: OpenCode hung indefinitely
After: Connection aborted after 30s timeout, clean error message

Implementation Notes

  Add AbortController support to remote MCP transports (StreamableHTTP
  and SSE) to prevent indefinite hangs when servers send malformed
  responses or stall during handshake.

  - Pass AbortSignal to transport requestInit for native fetch abort
  - Set timeout to abort connections after configured timeout
  - Ensure cleanup on all error paths (timeout, error, success)

  Fixes anomalyco#8406
@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

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.

fix(mcp): connection hangs with non-standard SSE/HTTP servers which causes opencode to hang

1 participant