fix(mcp): preserve session initialization and stdio ordering
Fix MCP session protocol state without serializing long-running tools. Initialized notifications cannot replace initialize. Only messages waiting for the initialize response are deferred; afterward ordinary requests execute concurrently, so ping/viewer_get_selection are not blocked by viewer_wait_for_selection.
Requests cancelled while awaiting initialize are marked before they register as active and are dropped on release. Pending records are scoped to the request instance so reused IDs cannot revive old work or steal another request's cancellation. EOF detaches immediately (aborting active handlers), clears deferred bookkeeping, and refuses subsequent input. The unrelated server.test.ts import changes have been reverted.
Validation
All test commands were prefixed with systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0. Typecheck and lint: CI per maintainer instruction; heavy unused-locals baseline deferred to CI.
pnpm build --filter=@ifc-lite/mcp --only: passed; subprocess tests use the rebuilt final CLI and prebuilt shepherd siblings, with no Rust/WASM rebuild.pnpm test --filter=@ifc-lite/mcp --only: 89 files passed; 770 tests passed, 5 fixture-dependent skips. Mutation round-trip and stdio-purity tests pass.- Focused regressions failed before the fix: later-chunk ping, queued cancellation, immediate EOF detach, and EOF-discarded queued work (4 failures).
node scripts/pre-handoff.mjs: 13 passed; retained-free viewer check skipped (apps/viewer/dist absent).- Both protocol changesets are minor for @ifc-lite/mcp (currently 0.x): requiring initialize and changing ordering/cancellation/EOF behavior are observable client protocol changes, even without new exported names. Minor is the appropriate 0.x compatibility bump; this is not represented as a patch. No public exported-symbol changes.
Mutation checks
Each following mutant failed a named #9537 transport test: remove initialization barrier; omit cancellation recording; ignore cancelled request at dispatch; delay/omit detach at EOF; omit deferred-map clearing; accept input after close; let an earlier initialize clear a later barrier; let an older queued ID erase the replacement ID's record. The existing initialized-notification bypass mutant also failed the handshake test in the implementation run.
Coverage is not exhaustive for generic parse/error/serialization branches. Real external MCP applications were not driven locally. The tests use controlled asynchronous handlers to assert actual response ordering and shutdown without a real 600-second wait.
Summary by CodeRabbit
- Bug Fixes
- Initialization notifications no longer mark a session as initialized by themselves. Requests requiring initialization remain unavailable until an initialize request succeeds.
- Requests received during initialization wait until the initialize response is sent; afterward, requests can run concurrently.
- Cancellation received before a queued request is dispatched prevents that request from running.
- The connection detaches promptly when standard input reaches end-of-file.
The generic CodeRabbit docstring-coverage check is not a repository gate and is intentionally not used to drive unrelated documentation changes.
š¤ Generated with Claude Code