claude-code - 💡(How to fix) Fix MCP plugin disconnects when context is compacted (/compact) [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#47135Fetched 2026-04-13 05:40:32
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
labeled ×3commented ×1

The Telegram MCP plugin (and likely other stdio-based MCP plugins) disconnects every time context compaction occurs — either manually via /compact or automatically when approaching context limits.

Root Cause

The plugin server uses StdioServerTransport (stdin/stdout). When Claude Code compacts context, the stdin pipe is closed/reset, triggering the shutdown handler:

process.stdin.on("end", shutdown)
process.stdin.on("close", shutdown)

The shutdown() function then kills the bot process and exits. This is intentional for zombie prevention, but it causes the plugin to die during normal context management operations.

Fix Action

Fix / Workaround

  • In long conversations, context compaction is frequent — the plugin disconnects multiple times per session
  • Users lose the ability to send/receive Telegram messages mid-conversation
  • The only workaround is restarting Claude Code entirely

Code Example

process.stdin.on("end", shutdown)
process.stdin.on("close", shutdown)
RAW_BUFFERClick to expand / collapse

Description

The Telegram MCP plugin (and likely other stdio-based MCP plugins) disconnects every time context compaction occurs — either manually via /compact or automatically when approaching context limits.

Steps to Reproduce

  1. Start Claude Code with the Telegram channel plugin: claude --channels plugin:telegram@claude-plugins-official
  2. Use the Telegram MCP tools normally (reply, react, etc.)
  3. Trigger context compaction via /compact or let it happen automatically
  4. After compaction, the MCP tools become unavailable with "MCP server disconnected"

Root Cause Analysis

The plugin server uses StdioServerTransport (stdin/stdout). When Claude Code compacts context, the stdin pipe is closed/reset, triggering the shutdown handler:

process.stdin.on("end", shutdown)
process.stdin.on("close", shutdown)

The shutdown() function then kills the bot process and exits. This is intentional for zombie prevention, but it causes the plugin to die during normal context management operations.

Expected Behavior

MCP plugin connections should survive context compaction. The plugin process should remain alive and reconnect after compaction completes.

Actual Behavior

Every /compact or automatic context compression kills the MCP connection. The plugin process terminates. Users must restart Claude Code to restore the connection.

Impact

  • In long conversations, context compaction is frequent — the plugin disconnects multiple times per session
  • Users lose the ability to send/receive Telegram messages mid-conversation
  • The only workaround is restarting Claude Code entirely

Environment

  • Claude Code: latest (installed via npm)
  • OS: Linux (WSL2)
  • Plugin: plugin:telegram@claude-plugins-official v0.0.5
  • MCP SDK: @modelcontextprotocol/sdk ^1.0.0

Suggested Fix

Either:

  1. Preserve stdio pipes across context compaction (do not close/reopen MCP connections)
  2. Automatically reconnect MCP plugins after compaction
  3. Provide a mechanism for plugins to distinguish compaction reset from session ended

extent analysis

TL;DR

Implement a mechanism to automatically reconnect the MCP plugin after context compaction or modify the shutdown handler to distinguish between compaction resets and actual session endings.

Guidance

  • Review the shutdown() function to determine if it can be modified to handle compaction resets differently than actual session endings.
  • Consider implementing an automatic reconnect mechanism for the MCP plugin after context compaction, potentially using a retry mechanism with a backoff strategy.
  • Investigate preserving stdio pipes across context compaction to prevent the need for reconnection.
  • Evaluate the feasibility of adding a mechanism for plugins to distinguish compaction resets from session endings, potentially through additional events or signals.

Example

// Example of a potential reconnect mechanism
function reconnectMcpPlugin() {
  // Attempt to reconnect with a backoff strategy
  const retryDelay = 1000; // initial delay
  const maxRetries = 5;
  let retries = 0;
  function attemptReconnect() {
    // reconnect logic here
    if (retries < maxRetries) {
      retries++;
      setTimeout(attemptReconnect, retryDelay * retries);
    }
  }
  attemptReconnect();
}

Notes

The provided shutdown() function and StdioServerTransport usage suggest that the issue is related to the handling of stdin pipe closures. However, without further information about the shutdown() function and the context compaction process, it is difficult to provide a more specific solution.

Recommendation

Apply a workaround by implementing an automatic reconnect mechanism for the MCP plugin after context compaction, as this seems to be the most straightforward solution given the current information. This approach allows the plugin to recover from compaction-induced disconnections without requiring significant changes to the underlying architecture.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix MCP plugin disconnects when context is compacted (/compact) [1 comments, 2 participants]