Repository navigation
Fix CLI dropping connection instead of reconnecting mid-session - #1
Merged
Merged
Conversation
`MCPService.run()` had its two stop paths inverted: a dropped *network* connection (`.connectionClosed` / NWError 54·57) called `return`, killing the relay loop so the MCP client saw the server disconnect and had to be restarted. Meanwhile stdin EOF — the client legitimately closing us — fell through and looped back to reconnect. Swap the behavior: a dropped network connection now reconnects (the app may have restarted, the machine slept, or Bonjour blipped), and stdin EOF now terminates cleanly. The decision is extracted into a pure `reconnectDecision(for:)` policy in ReconnectPolicy.swift so the mapping is unit-tested (the test target can't import the executable's top-level code, so the file is dual-membered into both targets). Also: - Cancel the NWBrowser when discovery times out, releasing its socket/FD before the loop creates a fresh one on retry (upstream mattt#174). - Shorten the discovery timeout 30s -> 10s so a missing app fails faster than the MCP client's own initialize timeout; the reconnect loop retries. - Remove the dead top-level NWBrowser (created, never started or used; MCPService creates its own). Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01XsyoHa1VekPHDCoxnURFcV
Follow-up to the connection-reliability fix, resolving findings from the multi-agent PR review: - Log mid-session drops at .warning (the release log level) and include the underlying error, so a drop — and especially a persistent reconnect loop — is visible in shipped builds. Previously logged at .debug/.info, invisible in release. - Extract the error→ProxyOutcome classification into a pure, unit-tested proxyOutcome(for:) in ReconnectPolicy.swift, and move StdioProxyError there so the test target can see it. Adds tests for the exact regression line (.connectionClosed → .connectionDropped) that the prior tests left uncovered. - Replace the 54/57 magic numbers with NWError .posix(.ECONNRESET/.ENOTCONN), and model the reconnect delay as Duration instead of a bare Double seconds. 12/12 tests pass; strict lint and full iMCP build clean. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01XsyoHa1VekPHDCoxnURFcV
This was referenced Aug 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
imcp-serverregularly drops out of Claude Code mid-session and only comes back after a manual restart. Root cause:MCPService.run()had its two stop paths inverted..connectionClosed, orNWError54/57) calledreturn— killing the relay loop, so the MCP client saw the server disconnect permanently.So the one case that should recover (transient network drop: app restart, sleep/wake, Bonjour blip) was the one that gave up.
Fix
Swap the behavior, routed through a pure, unit-tested policy:
The decision lives in a new
CLI/ReconnectPolicy.swift(ProxyOutcome→ReconnectDecision), extracted so it can be unit-tested. The test target can't import the executable's top-level code, so the file is dual-membered into both the CLI and test targets (the CLI target auto-includes it via its synchronized folder group).Also in this PR
NWBrowseron each discovery timeout, and therefore causemDNSResponderto run out of file descriptors? mattt/iMCP#174): cancel theNWBrowserwhen discovery times out, releasing its socket before the loop creates a fresh one on retry.NWBrowser(created, never started or used —MCPServicemakes its own).Tests
New
CLITests/ReconnectPolicyTests.swift(TDD: watched it fail red against a stub, then green):connectionDropped → reconnectnetworkTimedOut → reconnectstdinClosed → terminateVerification:
iMCPbuild ✅ ·imcp-serverTests6/6 ✅ ·swift format lint --strict✅🤖 Generated with Claude Code