Skip to content

claude-code: the context-usage and initialization probes connect to every user-scope MCP server, then abort it (every turn) #6655

Closed

Description

@gabrielhamu

Summary

bb's claude-code provider starts short-lived claude processes to read session metadata.

  1. readIsolatedClaudeContextUsage (context-usage-probe.ts) resumes the thread's session in an isolated query(). On my machine this runs at the start of every turn: each user message and each background-task notification.
  2. probeClaudeInitialization (initialization-probe.ts, the probe from claude-code initialization probe leaves ~/.claude.json.tmp.* files in $HOME when the disk is slow #6141) starts with persistSession: false from ~/.bb/provider-maintenance-workspace.

Neither one restricts MCP. With settingSources: ["user", "project", "local"], every probe initializes every user-scope MCP server (8 on this machine). It also runs each server's headersHelper. Then it closes about 0.5–1 s later, while tools/list, prompts/list and resources/list are still in flight. The servers see a connection that drops before the request body arrives.

Evidence

Environment: bb 0.47.1, Claude Code 2.1.312, agent-sdk 0.3.245, macOS. Data from one Mac, 28–29 Oct.

  • Claude Code's MCP client logs (~/Library/Caches/claude-cli-nodejs/<cwd>/mcp-logs-<server>/) hold about 900 sessions for one server. Of these:
    • 741 logged HTTP connection closed after 0s (cleanly) right after connecting;
    • 154 logged Failed to fetch tools: … (CONNECTION_CLOSED);
    • none has a transcript.
  • Repro. I ran a process sampler while a background-task notification started a new turn. It showed bb-provider-bridge-worker spawning, in the same second, claude … --resume=<the thread's session> --permission-mode default --setting-sources=user,project,local … next to the long-lived thread process. The extra process lacks --include-partial-messages, --replay-user-messages and --settings. It exited about 1 s later. A new MCP client session opened and dropped at the same time.
  • The long-lived thread sessions never lost their tools, so nothing is functionally broken.

Impact

About 450 probes a day on one machine. Each one opens and aborts a connection to every configured MCP server and runs every headersHelper; ours reads the macOS Keychain.

On our side, each aborted request made the server log three errors (starlette ClientDisconnect, then the MCP SDK's stream echo and the stateless session's ClosedResourceError). That reached about 94% of our monthly error-tracking quota before we filtered it. Every MCP server a bb user configures takes the same load.

Suggestion

  • Initialization probe: it only needs the initialize response (commands, models, account). Pass strictMcpConfig: true with mcpServers: {}.
  • Context-usage probe: MCP tool definitions do count toward context, so dropping MCP here would change the number. Two possible fixes:
    • read the usage from the live session instead of an isolated resume;
    • cache it and refresh it only when the MCP or tool set changes, instead of on every turn.

Related: #6141 (the same initialization probe leaves ~/.claude.json.tmp.* files behind). Happy to share logs.

Activity

  1. added theissue type on Oct 30, 2026
  2. added
    providers
    provider-claude-code
    confirmed-repro
    on Oct 30, 2026
  3. bb-slop-cop commented on Oct 30, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority: Medium · Effort: Medium. Labels: providers, provider-claude-code, perf.
    • Report: Reproduction and root-cause analysis.
    • Verdict: REPRODUCED · high root-cause confidence · confirmed-repro.
    • Root cause: Short-lived metadata/context SDK queries inherit configured MCP servers, then close without waiting for their discovery to finish. Both clean-checkout runs observed fresh connections from each probe and three canceled discovery requests from context reporting.
    • Evidence: The real-SDK regression fails on trusted main in both checkouts; nine relevant existing tests pass. The report includes the complete runnable fixture. macOS headers-helper behavior and reported traffic/quota totals remain unverified.
    • No PR: A complete fix needs a context-reporting design decision that preserves MCP token accounting and live-session isolation, outside the simple-fix gate.

    AGENT GENERATED

  4. gwayne9955 commented on Nov 12, 2026

    @gwayne9955

    This has a worse effect than load when an MCP server uses OAuth with refresh-token rotation: the probes sign every Claude Code session out of that server, about once a day here.

    Mechanism

    1. Claude Code refreshes an MCP access token proactively in the last few minutes before it expires. All sessions on the machine share one stored credential and take a cross-process refresh lock.
    2. The process that starts the refresh is often one of these probes, because they start on every turn. The probe acquires the lock and sends the refresh. The server rotates the refresh token (one-time use) and returns 200.
    3. bb aborts the probe about 1 s after it starts, before Claude Code writes the new tokens to the keychain. The stored refresh token is now one the server already deleted.
    4. The next session that refreshes gets invalid_grant: refresh token does not exist. Claude Code then logs No valid tokens in storage, clearing stored tokens and clears the shared credential, so every session needs a new sign-in.

    Evidence (bb 0.49.0 desktop, claude-code/2.1.319 (sdk-cli, agent-sdk/0.3.318), macOS; one internal HTTP MCP server behind a FastMCP OAuth proxy; 2026-11-07 to 2026-11-12)

    • From ~/Library/Caches/claude-cli-nodejs/<cwd>/mcp-logs-<server>/: 5 refreshes started by a probe whose log ends after Starting token refresh / Found client info, with no result. Each probe's log for every MCP server stops at the same moment, about 0.3 to 1.2 s after it starts, and none has a transcript.
    • The server's access logs show a 200 from /token for each of those refreshes. The next refresh from a different session got a 401 (invalid_grant) 1 s to 5 min later.
    • In one more case (11-10) a probe logged Saving tokens and its log ended in the same millisecond. A failure followed 3 s later.
    • Together these account for all 7 invalid_grant failures that week, in 5 separate sign-out events.
    • 4 of the 5 lost rotations came from the context-usage probe in thread worktrees. One (11-09) came from the initialization probe in ~/.bb/provider-maintenance-workspace, so both probes need the fix.
    • Refreshes started by long-lived thread sessions never failed (38 of 38). Probes that happened to finish the refresh before being aborted saved fine (22).

    #7447 (strictMcpConfig: true on both probes) fixes this, because the probes then never connect to the OAuth server. I applied the same two-line change to a local 0.49.0 bundle and will report back if any lost rotation shows up after it.

    AGENT GENERATED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reproperfprovider-claude-codeproviders

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions