Skip to content

refactor(mcp): the mcp tool bridge is generated from the typer app instead of 147 hand-written argv lists #997

Closed

Description

@dan-petty

Context & Rationale

ai/mcp/server.py is 3,019 lines. 152 functions carry @mcp.tool, and 2,064 of the lines are their bodies (13.6 lines on average). 147 of them call _run_mcp_cmd([..., "uv", "run", "devops", <group>, <command>, <flags>]); 146 map to a devops subcommand across 23 groups (ai 29, gh 24, k8s 15, pr 12, ...). Only 63 of the 146 tool names equal the dispatched path. Checked at 9c879ae.

  • InProcessDispatcher._dispatch_typer (ai/mcp/dispatcher.py:213-236) runs the CLI through typer.testing.CliRunner in production and returns scraped console text; 21 tools ask for --format json, the other ~126 return Rich table text.
  • register_handler (dispatcher.py:180) has zero callers; the "functional handler" path is dead.
  • server.py:72 branches on hasattr(run_subprocess, "assert_called"): production checks whether it is under a test mock.
  • Drift is already observable: k8s_pods(namespace) (:275) validates namespace and then runs devops k8s status without it. fix(mcp): mcp tools and resources call commands and options that exist #836 fixed 12 tools that called commands or options that did not exist.
  • tests/test_mcp.py: 32 tests, 26 patches of server._run_mcp_cmd, 54 assertions on argv lists. The tests pass while the real command rejects the flags.

Deletion test: delete the 2,064 lines and nothing reappears. The knowledge they repeat (names, options, types, help) already lives in Typer's command metadata.

Scope & Deliverables

Strategic Alignment

Scope added 2026-10-02: checked at 38517a8; the claims hold with moved lines. ai/mcp/server.py is 3,028 lines (#980 edited it). 152 functions carry @mcp.tool, 147 of them call _run_mcp_cmd, and the tool bodies are 2,065 lines. The five without a command are hydrate_tool_domain, security_intel_package, security_intel_network, scan_uv_audit and k8s_jaeger_info. _dispatch_typer is at ai/mcp/dispatcher.py:229-236, register_handler (:177) has no caller, the mock-attribute branch is at server.py:72, and k8s_pods (:279-286) still validates namespace and drops it; #956 (v0.2.25, in flight) changes it to run devops k8s pods -n <namespace>. tests/test_mcp.py has 32 tests. Of the scheduled items, #857 (ToolAnnotations) and #839 (failing output, nothing on the protocol stream) are in v0.2.28; #986, which records read-only and mutating command paths, and #411 (result models) are in the backlog.

Acceptance criteria

  • One module registers a FastMCP tool for each command in an exposure table held as data (command path, timeout class, read-only or mutating). Each tool's parameters come from the command's Typer parameters: name, type, default, required flag and help.
  • A parametrised test walks the table. For each tool it asserts that the input schema's properties match the command's parameters, and that the argv built from a full set of arguments parses with the real Click command (make_context succeeds). No test patches _run_mcp_cmd to assert an argv list. At 38517a8, 52 patches in 13 test files do, 26 of them in tests/test_mcp.py and 13 in tests/test_fastmcp_contracts.py.
  • A test fails when a tool accepts a parameter its command does not take, as k8s_pods(namespace) did at 38517a8.
  • A test fails when a command is in neither the exposure table nor its exclusion list, so exposing a new command is a recorded decision.
  • Only the five tools above stay hand-written. register_handler and the hasattr(run_subprocess, "assert_called") branch are deleted.
  • Domain gating (feat(mcp): lazy domain-gated mcp tool schema hydration #429) still hides a group's tools until hydrate_tool_domain names the group. The domain-gate tests and fix(mcp): mcp tools refuse coerced arguments before they run, with prescriptive errors that never echo values #862's coerced-argument tests (tests/test_fastmcp_contracts.py) still pass, changed only for renamed tools and for the argv assertions above.
  • Every generated tool keeps fix(ai): mcp command tools return the failing output and never write to the protocol stream #839's contract: a failing command returns its output and status, and nothing is written to the protocol stream. One test runs a generated tool whose command fails.
  • Every generated tool carries feat(mcp): mcp tools declare ToolAnnotations, and mutating tools require approval in devops-cli's own agent loop #857's readOnlyHint from the table's read-only column. A test asserts that each tool sets it.
  • A tool returns the command's --format json output where the command offers one, and its text otherwise. The PR lists which tools return JSON.
  • docs/MCP_TOOLS.md is regenerated.
  • The tests run offline and dispatch no real command that reaches a network or cluster.
  • changelog.d/997.md lists renamed tools under ### Changed. CHANGELOG.md is not edited.
  • uv run devops ci passes.
  • (person-run) Claude Code connected to devops mcp serve lists the tools, and review_findings, k8s_status and pr_check_readiness return the same output as their commands.

Key questions

Update 2026-10-08: refinement

The premise holds (157 tools; register_handler uncalled, ai/mcp/dispatcher.py:282). #839 shipped; this still lands after #857. #1330 owns deleting the mock branch (ai/mcp/server.py:88) and the patches; whichever lands second rebases the MCP tests. Generated tools keep #1356's deadline.

Activity

  1. added
    type/refactor
    scope/mcp
    priority/p2-medium
    on Oct 2, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Linked: #886. Its dry-run class (makes no external request) differs from #857's readOnlyHint (changes no state); if each command declares both once, this item's generated tools and #886's test read the same data. Found by the 2026-10-08 backlog review.

  3. added this to the v0.2.37 milestone on Oct 17, 2026
  4. dan-petty commented on Oct 17, 2026

    @dan-petty
    OwnerAuthor

    Moved to v0.2.37: v0.2.37 started with fewer than its 16 items and was topped up to 16 from Ready items, critical fixes and P0 features first. (devops roadmap reprioritize)

  5. added
    status/in-progress
    on Oct 17, 2026
  6. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1619, merged into release/v0.2.37 as a48a5fa.

    Summary

    Closes #997.

    Replaces 147 hand-written argv construction wrappers in src/devops_cli/ai/mcp/server.py with dynamic FastMCP tool generation driven by CONST_MCP_EXPOSURE_TABLE (146 commands) and src/devops_cli/ai/mcp/generator.py. Retains exactly 5 hand-written tools, removes dead functional handlers in dispatcher.py and test-detecting branches in server.py, derives canonical tool names with zero legacy shims, and adds comprehensive schema-matching and exhaustive CLI coverage tests.

    30 file(s) changed.

    How it was verified

    Check runs at 3ad2a75:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • zizmor: pass
    • devops-cli: pass
    • bandit: pass
    • CodeQL: pass
    • Analyze (python): pass
    • Rule Pack Tests: pass
    • Changed-Test Independence: pass
    • Fail-to-Pass Self-Check: pass
    • Tests & Coverage: pass
    • Fail-to-Pass Check: pass
    • Analyze (actions): pass
    • Static Analysis: pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-997-mcp-tool-bridge-from-typer.md:

    No Acceptance Criteria section.

  7. removed
    status/in-progress
    on Oct 30, 2026
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

    priority/p2-mediumscope/mcptype/refactor

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions