Skip to content

arch: in-process MCP engine — eliminate subprocess indirection and enable real-time streaming #508

Open

Description

@admarble

Problem (User Impact)

Users running Sequant via MCP (from Claude Code, Cursor, or other MCP clients) get a degraded experience compared to CLI users:

  1. No real-time progress — MCP clients see nothing until the entire run completes. The server spawns a subprocess and polls log files
  2. Double resource usage — The MCP server process spawns a separate sequant run process, doubling memory and CPU
  3. Race condition on results — run.ts:74 in MCP tools watches for log file creation with a 5-minute window. If the run takes longer or the log path is unexpected, results are silently lost
  4. No streaming — MCP protocol supports notifications/progress but the current architecture can't use it because execution happens in a subprocess with no communication channel back

The MCP server is architecturally the most important growth vector (headless orchestration for AI-native workflows), but it's currently a second-class citizen.

Why Now

Acceptance Criteria

In-Process Execution

  • AC-1: MCP sequant_run tool calls RunOrchestrator.execute() directly in the same process — no subprocess spawn, no log file polling
  • AC-2: MCP server startup loads the same engine as the CLI. Single process, shared configuration
  • AC-3: Run results returned directly from the orchestrator — no log file intermediary. Log files still written (for sequant logs command) but not used as the communication channel
  • AC-4: Memory usage for MCP-initiated runs is comparable to CLI runs (not 2x)

Real-Time Progress Streaming

Robustness

Backwards Compatibility

  • AC-12: sequant serve CLI command continues to work identically from the user's perspective
  • AC-13: MCP tool schemas (sequant_run, sequant_status, sequant_logs) unchanged — no breaking changes for MCP clients

Scope & Gaps to Watch

  • Gap: Process isolation loss — Subprocess execution provided crash isolation — a buggy phase couldn't crash the MCP server. In-process execution means an unhandled exception in a phase could take down the server. Mitigation: ensure RunOrchestrator.execute() never throws unhandled — all errors are captured and returned as structured results
  • Gap: Long-running connections — MCP stdio transport assumes a persistent connection. If the MCP client disconnects mid-run, the orchestrator should continue (not abort) and save results to state. The next sequant_status call should find them
  • Gap: SSE transport — The MCP server has an SSE alternative. Verify that progress notifications work over both stdio and SSE transports
  • Gap: Resource cleanup — In subprocess mode, process exit guaranteed cleanup. In-process, the MCP server must explicitly clean up worktrees, locks, and temp files if a run is aborted. Verify shutdown handlers fire correctly
  • Gap: Run registry — src/mcp/run-registry.ts uses a module-level singleton Map. In-process execution should use the orchestrator's state directly instead of maintaining a parallel registry. Remove the registry or reduce it to a thin proxy
  • Risk: CLI binary resolution — run.ts:74 currently resolves the CLI binary path for subprocess execution. This logic becomes dead code — remove it to avoid confusion

Technical Notes

  • Current MCP execution flow: sequant_run tool → spawn("sequant", ["run", ...]) → poll log directory → parse log JSON → return result
  • Target flow: sequant_run tool → orchestrator.execute(config) → subscribe to events → stream notifications → return result
  • Current run-registry.ts: ~50 lines, tracks active runs by issue number. Replace with orchestrator.isRunning(issueNumber) query
  • MCP SDK progress notifications: server.notification({ method: "notifications/progress", params: { progressToken, progress, total } })
  • Log files should still be written (via event listener from arch: typed workflow event system for decoupled progress, logging, and MCP streaming #504) for sequant logs command — but they're no longer the communication mechanism

Dependencies

Batch Group

sequant run batch 3 (depends on both #503 and #504 completing first)

Activity

  1. added
    enhancement
    roadmap
    on Apr 8, 2026
  2. admarble commented on Apr 9, 2026

    @admarble
    ContributorAuthor

    Architecture Review: Recommend Deferral

    After an overcomplexity assessment of the #503-#508 target architecture, this issue should be deferred. The in-process MCP engine is a real improvement, but the cost/benefit doesn't justify it now.

    Why defer

    1. Dependency chain cost — This depends on both arch: extract RunOrchestrator — decouple execution engine from CLI wiring #503 (RunOrchestrator) AND arch: typed workflow event system for decoupled progress, logging, and MCP streaming #504 (Event System). It's the terminal node in the dependency graph. By the time prerequisites land, the architecture will have evolved and this issue's ACs will need revision anyway.

    2. The subprocess approach works — MCP is functional today. The double resource usage only matters under heavy MCP usage, which isn't happening at current adoption levels.

    3. The real bugs are small fixes — The two concrete problems (log polling race condition, double resource usage) don't require an architectural rewrite:

      • Log polling race condition (5-minute window): fixable with a file watcher or event-based completion signal — ~10 lines
      • Double resource usage: only matters at scale; current users aren't impacted

    Recommended action

    • Remove roadmap label, add future label
    • Remove from batch 3 grouping
    • File a separate small bug for the log polling race condition (the only user-facing issue)
    • Revisit when either:

    What this does NOT mean

    The RunOrchestrator (#503) API should still be designed so MCP can call it directly — that AC in #503 is correct. We're deferring the wiring, not closing the door.

  3. added 2 commits that reference this issue on Apr 9, 2026
  4. added
    backlog
    on Apr 15, 2026
  5. admarble commented on May 11, 2026

    @admarble
    ContributorAuthor

    → PARK — Backlog label already applied per author's own architecture review (2026-04-09). Defers in-process MCP engine until either (a) MCP usage justifies the rewrite, (b) #383 interactive relay becomes priority, or (c) a user reports MCP performance issues.

    Resume after: triggering condition from the architecture-review comment is met.

    Note: the review recommended filing a separate small bug for the log-polling race condition (the only concrete user-facing issue inside this issue's scope). Confirm that follow-up was filed before fully closing this out.

  6. admarble commented on May 12, 2026

    @admarble
    ContributorAuthor

    Supersedes prior assess from 2026-05-11 (PARK)

    → PARK — Backlog label remains the live state. None of the architecture-review's unparking conditions are met: MCP usage hasn't grown, #383 (interactive relay) is still open but not active, and no user has reported MCP performance issues.

    The architecture-review note about filing the log-polling-race condition as a separate bug is now resolved: filed as #631 (fix(mcp): log-file lookup races on concurrent runs and stale same-issue logs). #631 covers the concrete user-facing problem (readLatestRunLog time-window filter without runId correlation) without requiring this issue's full in-process rewrite.

    Resume after: a triggering condition from the 2026-04-09 architecture review is met.

  7. admarble commented on Jul 27, 2026

    @admarble
    ContributorAuthor

    Added AC-14 (Robustness): pre-provisioning abort paths in RunOrchestrator.run() — chain-resume fail-fast, --strict-preflight, and the #813 skills pre-flight — currently return via shutdown.dispose(), which discards registered cleanup tasks (issue-lock release, LogWriter finalize) rather than running them. In subprocess mode the process.on("exit") backstop releases locks and this is invisible; in-process (this issue's whole premise) the exit event never fires between runs, so aborts would leak issue locks and leave in-progress run logs unfinalized. Concretizes the existing "Gap: Resource cleanup" bullet. Surfaced during #813's QA (PR #821).

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

    backlogenhancementroadmap

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions