Skip to content

fix(ai): agent tools taken from the mcp server stop offering the model fastmcp's ctx parameter #1498

Closed

Description

@dan-petty

Filed at the owner's request from the 2026-10-09 review of GitHub Actions warnings and notices.

Problem

Checked at release/v0.2.32 (27cf6bd).

  • CI run https://github.com/dan-petty/devops-cli/actions/runs/37939949186/job/113851481357 (release/v0.2.31 at a6ac3f0, the same tree) reports PydanticJsonSchemaWarning: Default value <fastmcp.server.dependencies._OptionalCurrentContext object ...> is not JSON serializable; excluding default from JSON schema [non-serializable-default]. It appears in tests/test_ai_cmd.py::test_ai_extended_commands and ::test_ai_token_count_route_pipeline_bundle.
  • The stack captured at pydantic's GenerateJsonSchema.emit_warning runs from devops ai pipeline (commands/ai.py:1252 get_default_tools(), :1268) → MultiAgentPipeline.add_agent (ai/agents/pipeline.py:228) → add_tool (ai/agents/agent.py:592) → Tool.from_function (ai/tools/__init__.py:208) → pydantic-ai function_schema [measured].
  • get_mcp_agent_tools (ai/tools/mcp_bridge.py:12-22) gives agents the raw fn of every FastMCP tool: 181 of the 204 default tools [measured]. get_persona_tools falls back to the same list (ai/tools/registry.py:165).
  • FastMCP 4.0.11 rewrites the signature of a function that takes a Context in place. hydrate_tool_domain(domain: str, ctx: Context | None = None) (ai/mcp/server.py:3246-3247) ends up with ctx=OptionalCurrentContext() as its default (fastmcp/server/dependencies.py:433-434).
  • As a result, the pydantic-ai tool built from it offers the model a ctx argument. Its parameters schema is {domain, ctx: anyOf[$ref Context, null]}, with FastMCP's whole Context docstring under $defs: 2,164 characters. FastMCP's own published schema for the same tool (mcp.get_tool("hydrate_tool_domain").parameters) has only domain [measured]. Every pipeline stage sends this schema to the model.
  • hydrate_tool_domain is the only MCP tool that takes a Context today [measured]. Any future tool that takes one would leak the same way.
  • Reproduce: uv run pytest -p no:cacheprovider -q -n 0 --no-cov -W error::pydantic.json_schema.PydanticJsonSchemaWarning tests/test_ai_cmd.py fails those 2 tests [measured].

Proposed remedy

In get_mcp_agent_tools, wrap each fn in FastMCP's fastmcp.server.dependencies.without_injected_parameters. FastMCP provides this helper for schema generation. It drops Context and Depends parameters from the signature and injects them at call time, so with no MCP session ctx resolves to None. For hydrate_tool_domain, the wrapped tool's schema equals FastMCP's published schema [measured].

The larger alternative is to give agents the in-process MCP toolset (get_devops_mcp_toolset()). #857 and #997 touch the same bridge, so this change stays limited to the wrapper.

Acceptance criteria

  • A test checks every MCP-derived tool in get_default_tools(): its pydantic-ai parameters schema equals the schema FastMCP publishes for the same tool.
  • Called through the agent tool with no MCP session, hydrate_tool_domain still returns its result.
  • The reproduction above passes, and the CI pytest summary has no PydanticJsonSchemaWarning.
  • uv run devops ci passes.

Related: #857 (approval on the same default tools), #997, #1330 (tool tests on edge fakes).

Activity

  1. added
    type/bug
    scope/ai
    on Oct 9, 2026
  2. dan-petty-bot commented on Oct 9, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. It degrades model tool-calling quality and produces CI warnings, but only one tool is affected today and the tool still functions with ctx resolving to None. The failed run 37939949186 does not set P0: no failed run of this repository has that id. A person can set P0 on the board.
    • Value: Medium. Fixing it removes a spurious parameter and 2 KB of schema noise from every pipeline stage's model context, and prevents the same leak for any future Context-taking tool.
    • Effort: Low. The remedy is wrapping each fn with fastmcp's without_injected_parameters helper in get_mcp_agent_tools plus a schema-equality test, a small localized change.
  3. added this to the v0.2.38 milestone on Oct 20, 2026
  4. dan-petty commented on Oct 21, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1767, merged into release/v0.2.38 as be39289.

    Summary

    • Wrap each FastMCP tool callable returned by get_mcp_agent_tools() in src/devops_cli/ai/tools/mcp_bridge.py with fastmcp.server.dependencies.without_injected_parameters so Tool.from_function excludes FastMCP's injected ctx parameter from the JSON schema and stops emitting PydanticJsonSchemaWarning.
    • Resolve awaitable return values in Tool.execute() in src/devops_cli/ai/tools/__init__.py via _run_coroutine_sync so synchronous agent tool calls (such as hydrate_tool_domain outside an MCP session) return their concrete result.
    • Add test_mcp_derived_default_tools_match_published_fastmcp_schemas in tests/test_pydantic_ai_mcp.py verifying every MCP-derived tool in get_default_tools() matches FastMCP's published parameters schema without warning and executes hydrate_tool_domain without an MCP session.

    5 file(s) changed.

    How it was verified

    Check runs at c740049:

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

    Acceptance Criteria of docs/agent/tasks/task-1498-mcp-toolset-strip-ctx.md:

    No Acceptance Criteria section.

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

    scope/aitype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions