Skip to content

Rebuild the shared engine on MCP runtime revision change instead of promoting sessions #1802

Open

Description

@jhrozek

Problem

PR #1673 (MCP source reconciliation, ADR 0351) publishes a new immutable direct-MCP runtime whenever ToolHive workloads change or an MCP server sends list_changed. Each run pins the runtime it started with.

The Service's shared engine (server.Config.Engine) is built once at startup and tagged with the startup revision (SharedEngineRevision). It is never rebuilt. After the first republication, engineAndEnvironmentFor (internal/adapter/server/service.go, the CASE 2 branch) promotes each default-FS session to its own per-session engine at its next run. That session keeps the engine until it is closed. From then on, the shared engine serves no runs.

This is correct: every run path checks the engine's revision against the pinned one, so no run uses the stale shared engine. But it costs more than it should:

  • Engine count: grows from 1 to N, one per open session that has run since the first republication.
  • Rebuild work: each later republication rebuilds every one of those engines at its session's next run (the CASE 1 revision check), instead of rebuilding one shared engine.
  • Stale wrappers: the shared engine keeps tool wrappers bound to a manager that has been closed. Nothing reaches them today, but a future code path that calls s.cfg.Engine directly would.

The cost matters most in multi-user mecak8s deployments with ToolHive polling enabled and many long-lived sessions.

PR #1673 records this as the accepted behaviour in ADR 0351 decision 6 and defers the fix to this issue.

Proposed direction

Hold the shared engine in a revision-tagged slot owned by the Service, and rebuild it once when an operation's pinned revision is newer than the slot's. Default-FS sessions stay on the shared engine instead of being promoted.

Points the design needs to settle:

  • How composition supplies the builder (a closure from internal/app, built under the operation's pinned runtime).
  • Runs still using the previous shared engine when the slot swaps: the old engine must stay valid until they finish. Reuse the runtime pin/retire model rather than adding a second lifetime mechanism.
  • capabilities() and any other reader of s.cfg.Engine must read through the slot.
  • Concurrency: two operations pinning the same new revision must build one engine, not two.

Acceptance criteria

  • After a runtime republication, default-FS sessions run on a rebuilt shared engine; no per-session engine is created for them solely because the revision changed.
  • Only one shared-engine build happens per published revision, under concurrent runs (race test).
  • A run that started on the previous shared engine completes on it; the previous engine is released only after that run settles.
  • ADR 0351 decision 6 is updated (or superseded) to describe the rebuilt shared engine, removing the promotion exception recorded by feat(mcp): reconcile sources and refresh session tools #1673.
  • ADR 0027 List 1 gets a row for the shared-engine slot.
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions