Repository navigation
Description
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_clinames by string. 20 of them name_run_mcp_cmd. These are 18 intest_fastmcp_contracts.py, 1 intest_mcp.pyand 1 intest_fastmcp_in_process.py. 4 namerun_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 fivepatch.objectcalls onInProcessDispatcheror its instance. A count of strings alone misses them. - The files import 12 private names, among them
_run_mcp_cmd,_run_mcp_resourceand_validate_mcp_arg. test_fastmcp_in_process.pycarries two@pytest.mark.asynciomarkers.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) andschema_violations(1). - 20 more sites patch
_run_mcp_cmdin 13 other test files: 16 sites in 10 files outsidetests/ai/mcp/, and 4 sites in 3 files oftests/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.
- Delete the process branches.
ai/mcp/then starts no process._run_mcp_cmdanddispatchaccept only adevopsargv (uv run devops ...ordevops ...). Any other argv raises aValidationErrorthat names the first token. - Add
install_mcp_dispatcher(override)inai/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. - Add
DispatcherFakeintests/support/fakes/mcp_dispatcher.py, behind anmcp_dispatcherfixture.tests/support/bootstrap.pyregisters 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)incalls. It can answer a timeout (exit code 124) and a failure. It holds a realResourceSubscriptionManagerassubscriptions, because_run_mcp_resourcecallsget_mcp_dispatcher().subscriptions.notify(...). It needs nohydrator: no caller insrcreads it. - A contract suite,
tests/ai/mcp/test_dispatcher_contract.py, runs each case against the fake and against the realInProcessDispatcher. The cases are these. A command succeeds. A command fails with a non-zero code. A command is unknown. An argv is not adevopsargv. A command outlasts its timeout; the test registers a hidden command on the app for this, astest_fastmcp_in_process.pydoes 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. - Edit
docs/TESTING.md. In the list of faked edges, replace "uv run devopscalling itself" with the dispatcher seam and the reason above. Add a bullet for it next to the Qdrant and Embeddings bullets. - Migrate the three files. A tool test calls the real tool through FastMCP's in-memory
Client(mcp)or the tool function, withmcp_dispatcherinstalled. It comparesmcp_dispatcher.callswith the expected argv and timeout._run_mcp_cmdruns for real. So masking, truncation and the exit-code toToolErrormapping are tested in place. - 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_violationsset 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'sTypeAdapter, 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 totests/commands/. Run it through theclifixture and the fake of the edge thatsandbox execreaches. 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 ofexport-schemasand its case (Q22).
Acceptance criteria
-
git grep -nE 'run_subprocess|import subprocess' -- src/devops_cli/ai/mcpreturns no line. - The three files hold no patch of a
devops_cliname:patch("devops_cli.,monkeypatch.setattr("devops_cli.andpatch.objecton adevops_cliobject return no line. They import no_-name fromdevops_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 ownrunmethod, a third-party object. They patch nodevops_cliname. No server starts. The CLI cases check parsing, wiring, output and exit codes only, and move totests/commands/test_mcp_serve.py. - A test shows the new refusal:
_run_mcp_cmd(["kubectl", "get", "pods"])raisesValidationError, and nothing runs. - Tests keep these checks: secret masking in output, and truncation at
CONST_MCP_MAX_COMMAND_OUTPUT_CHARS. They also keepToolErrorfor a non-zero exit, the timeout text, and_validate_mcp_argthrough the public tool. -
test_mcp_helpers_and_error_branchesis gone. Each of its checks lives in a test named after the behaviour it checks. - The two
@pytest.mark.asynciomarkers intest_fastmcp_in_process.pyare gone. - The contract suite passes against both sides. The fake's cases and the real cases are the same list.
-
docs/TESTING.mdnames the dispatcher seam and no longer listsuv run devopscalling itself as a faked process edge. -
uv run devops cipasses. 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
dan-petty commentedon Nov 6, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
refactorprefix of the title gives the type.(mcp)scope of the title gives the scope.Test infrastructure improvement with no user-facing impact.Replaces 35+ monkeypatches with one seam, improving test maintainability.Requires a new injection API, a fake, dead code removal, and updates across 13+ test files.Both refactor the MCP dispatcher area, but 997 generates the tool bridge from the typer app while the candidate adds a test injection seam.chore(main): release 0.72.0 (#3835)
devops-cli-service commentedon Nov 6, 2026
Released in v0.72.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.72.0
devops-cli-service commentedon Nov 6, 2026
Live since 2026-11-06 19:47 UTC