Skip to content

refactor(mcp): one dispatcher seam replaces the _run_mcp_cmd patches #3760

Description

@dan-petty

Problem or Motivation

Part of #1330. The MCP tool tests patch the internals of the MCP server. They cannot run a tool on a fake of the edge below it.

Measured at origin/main 29309f0, in tests/ai/mcp/test_mcp.py, test_fastmcp_contracts.py and test_fastmcp_in_process.py (2,837 lines, 102 tests):

  • 35 patches aim at devops_cli names by string. 20 of them name _run_mcp_cmd. These are 18 in test_fastmcp_contracts.py, 1 in test_mcp.py and 1 in test_fastmcp_in_process.py. 4 name run_subprocess (test_mcp.py:226, :240, :251; test_fastmcp_in_process.py:138).
  • 7 more patch targets are not strings: patch.object(type(mcp), "instructions"), monkeypatch.setattr(main_module, "_delegate", ...), and five patch.object calls on InProcessDispatcher or its instance. A count of strings alone misses them.
  • The files import 12 private names, among them _run_mcp_cmd, _run_mcp_resource and _validate_mcp_arg.
  • test_fastmcp_in_process.py carries two @pytest.mark.asyncio markers. asyncio_mode = "auto" already applies them.
  • Other patches aim at devops_cli.ai.mcp.server.mcp (3), devops_cli.commands.mcp.run_mcp_server (5), mcp._list_tools (1) and schema_violations (1).
  • 20 more sites patch _run_mcp_cmd in 13 other test files: 16 sites in 10 files outside tests/ai/mcp/, and 4 sites in 3 files of tests/ai/mcp/.

Premise check. The 2026-10-07 text says that every process, uv run devops self-calls included, goes through the process fake, and that tests compare fake.requests. That cannot work. InProcessDispatcher._dispatch_typer (ai/mcp/dispatcher.py:333) runs the Typer app in the same process with CliRunner. It never calls run_subprocess. So the process fake never sees the argv of an MCP tool.

Dead code. Every call to _run_mcp_cmd in src passes a uv run devops argv: all calls in ai/mcp/server.py and the one in ai/mcp/generator.py:497. InProcessDispatcher.dispatch has one caller, server.py:73. So the branch that runs run_subprocess in _run_mcp_cmd (server.py:80-93) and InProcessDispatcher._dispatch_subprocess (dispatcher.py:391-415) run only in tests. Rule Q22 says to delete such lines. I checked that no caller in src or tests sends a non-devops argv. I did not remove the branch to see which tests fail.

Proposed Solution & Architectural Design

The dispatcher is the one place where an MCP tool leaves the MCP layer for the CLI. Make it the seam, as install_qdrant_client and install_embedder are for their edges.

This is a seam inside the process. docs/TESTING.md says that a test doubles only edges, and it lists uv run devops calling itself as a faked process edge. For the MCP layer that line is wrong, because nothing starts a process. The dispatcher is the only place where a test can assert the argv of a tool. The CLI app below it has its own tests. The contract suite holds the fake to the real dispatcher.

  1. Delete the process branches. ai/mcp/ then starts no process. _run_mcp_cmd and dispatch accept only a devops argv (uv run devops ... or devops ...). Any other argv raises a ValidationError that names the first token.
  2. Add install_mcp_dispatcher(override) in ai/mcp/dispatcher.py. get_mcp_dispatcher() returns the installed dispatcher. reset_mcp_dispatcher(), which the root conftest already calls after each test (tests/conftest.py:83), also removes it.
  3. Add DispatcherFake in tests/support/fakes/mcp_dispatcher.py, behind an mcp_dispatcher fixture. tests/support/bootstrap.py registers a new support module with no list to edit. It answers (exit_code, output) by argv pattern. It records each call as (argv, timeout, env) in calls. It can answer a timeout (exit code 124) and a failure. It holds a real ResourceSubscriptionManager as subscriptions, because _run_mcp_resource calls get_mcp_dispatcher().subscriptions.notify(...). It needs no hydrator: no caller in src reads it.
  4. A contract suite, tests/ai/mcp/test_dispatcher_contract.py, runs each case against the fake and against the real InProcessDispatcher. The cases are these. A command succeeds. A command fails with a non-zero code. A command is unknown. An argv is not a devops argv. A command outlasts its timeout; the test registers a hidden command on the app for this, as test_fastmcp_in_process.py does today. The real side reaches no edge. I did not run these cases on the real dispatcher. The implementer must confirm that each one needs no edge.
  5. Edit docs/TESTING.md. In the list of faked edges, replace "uv run devops calling itself" with the dispatcher seam and the reason above. Add a bullet for it next to the Qdrant and Embeddings bullets.
  6. Migrate the three files. A tool test calls the real tool through FastMCP's in-memory Client(mcp) or the tool function, with mcp_dispatcher installed. It compares mcp_dispatcher.calls with the expected argv and timeout. _run_mcp_cmd runs for real. So masking, truncation and the exit-code to ToolError mapping are tested in place.
  7. Replace the other internal patches:
    • mcp._list_tools (the fallback description of a tool): the registry test shows that every real tool has a description. If no public input reaches the fallback, delete the branch and its case (Q22).
    • schema_violations set to [] (the strict PR-number alias): look for a public input that passes the schema check and that the alias refuses. If none exists, test the alias with pydantic's TypeAdapter, a third-party object. Or delete the case, if the alias is only a second line of defence.
    • main_module._delegate (the -- marker through the lazy command proxy): move the case to tests/commands/. Run it through the cli fixture and the fake of the edge that sandbox exec reaches. I did not check which fake that is.
    • type(mcp).instructions (export of schemas with no instructions): if no public input gives empty instructions, delete the branch of export-schemas and its case (Q22).

Acceptance criteria

  • git grep -nE 'run_subprocess|import subprocess' -- src/devops_cli/ai/mcp returns no line.
  • The three files hold no patch of a devops_cli name: patch("devops_cli., monkeypatch.setattr("devops_cli. and patch.object on a devops_cli object return no line. They import no _-name from devops_cli. A case that needs another edge installs the fake of that edge.
  • Tests of the server start (run_mcp_server, devops mcp serve) record the start at FastMCP's own run method, a third-party object. They patch no devops_cli name. No server starts. The CLI cases check parsing, wiring, output and exit codes only, and move to tests/commands/test_mcp_serve.py.
  • A test shows the new refusal: _run_mcp_cmd(["kubectl", "get", "pods"]) raises ValidationError, and nothing runs.
  • Tests keep these checks: secret masking in output, and truncation at CONST_MCP_MAX_COMMAND_OUTPUT_CHARS. They also keep ToolError for a non-zero exit, the timeout text, and _validate_mcp_arg through the public tool.
  • test_mcp_helpers_and_error_branches is gone. Each of its checks lives in a test named after the behaviour it checks.
  • The two @pytest.mark.asyncio markers in test_fastmcp_in_process.py are gone.
  • The contract suite passes against both sides. The fake's cases and the real cases are the same list.
  • docs/TESTING.md names the dispatcher seam and no longer lists uv run devops calling itself as a faked process edge.
  • uv run devops ci passes. The pull request body gives the patch and private-import counts before and after.

Out of scope: file names and file splits (child F), and the 20 other sites that patch _run_mcp_cmd in 13 files. They keep working, because _run_mcp_cmd keeps its name. #1336 finds them.

Target Subsystem

src/devops_cli/ai/mcp/ (server.py, dispatcher.py), docs/TESTING.md, tests/support/fakes/, tests/ai/mcp/, tests/commands/test_mcp_serve.py.

Existing Tool Considered

No open tool fakes devops-cli's own in-process dispatch. FastMCP's in-memory Client(server) (https://gofastmcp.com/clients/transports#in-memory-transport) is the open tool for calling a tool as a client does, and the child uses it. Typer's CliRunner is the real side of the contract suite. The project process fake does not apply, because the dispatcher starts no process.

Own code: the hook (about 10 lines), DispatcherFake and its contract suite. Written cost: about 35,000 tokens to build, 8,000 tokens a year to keep and fix, and half a day of elapsed time. The cost of not building it: 20 patches of an internal name stay, and no test can assert the argv a tool sends.

Activity

  1. added
    source/agent
    type/refactor
    scope/mcp
    status/new
    value/medium
    effort/medium
    on Nov 6, 2026
  2. dan-petty commented on Nov 6, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/refactor. The refactor prefix of the title gives the type.
    • Scope: scope/mcp. The (mcp) scope of the title gives the scope.
    • Priority: P2-Medium. Test infrastructure improvement with no user-facing impact.
    • Value: Medium. Replaces 35+ monkeypatches with one seam, improving test maintainability.
    • Effort: Medium. Requires a new injection API, a fake, dead code removal, and updates across 13+ test files.
    • Related: refactor(mcp): the mcp tool bridge is generated from the typer app instead of 147 hand-written argv lists #997, which asks for a different change. Both refactor the MCP dispatcher area, but 997 generates the tool bridge from the typer app while the candidate adds a test injection seam.
  3. added
    priority/p2-medium
    on Nov 6, 2026
  4. added
    status/in-progress
    status/in-review
    and removed
    status/new
    status/in-progress
    on Nov 6, 2026
  5. removed
    status/in-review
    on Nov 6, 2026
  6. added a commit that references this issue on Nov 6, 2026
  7. devops-cli-service commented on Nov 6, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-06 19:47 UTC

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

    effort/mediumpriority/p2-mediumscope/mcpsource/agenttype/refactorvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions