Skip to content

fix: optional mcp tools#171

Merged
NicoleMGomes merged 2 commits into
mainfrom
fix/optional-tools
Jun 18, 2026
Merged

fix: optional mcp tools#171
NicoleMGomes merged 2 commits into
mainfrom
fix/optional-tools

Conversation

@NicoleMGomes

@NicoleMGomes NicoleMGomes commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Disclaimer: Do not include SAP-internal or customer-specific information in this PR (e.g. internal system URLs, customer names, tenant IDs, or confidential configurations). This is a public repository.

Description

Fixes a bug in mcp_tool_to_langchain (agentgateway/converters.py) where all MCP tool parameters were unconditionally marked as required in the generated Pydantic args_schema, regardless of what the tool's input_schema declared as optional.

Root cause: The field mapping {k: (str, ...) for k in properties} used the Pydantic sentinel ... (required) for every field. Fields not listed in the JSON Schema "required" array were never treated as optional.

Fix: The converter now reads the "required" array from input_schema and maps fields accordingly — fields in "required" remain (str, ...), while all other fields become (str | None, None) (optional, defaulting to None).

This means LangChain agents and any Pydantic-aware consumer using mcp_tool_to_langchain will no longer raise validation errors when optional MCP tool parameters are omitted.

Related Issue

Closes #

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code refactoring
  • Dependency update

How to Test

  1. Define an MCPTool whose input_schema has both required and optional properties:
    tool = MCPTool(
        name="get_supplier_bid",
        server_name="ariba",
        description="Gets supplier bids",
        input_schema={
            "type": "object",
            "required": ["eventid"],
            "properties": {
                "eventid": {"description": "Event ID"},
                "datafetchmode": {"description": "Detail level"},
            },
        },
        url="https://example.com/mcp",
    )
  2. Convert it: langchain_tool = mcp_tool_to_langchain(tool, call_tool, get_user_token)
  3. Inspect langchain_tool.args_schema.model_fields:
    • eventid must be required (is_required() == True)
    • datafetchmode must be optional (is_required() == False)
  4. Run the unit tests: pytest tests/agentgateway/unit/test_converters.py -v

Checklist

  • I have read the Contributing Guidelines
  • I have verified that my changes solve the issue
  • I have added/updated automated tests to cover my changes
  • All tests pass locally
  • I have verified that my code follows the Code Guidelines
  • I have updated documentation (if applicable)
  • I have added type hints for all public APIs
  • My code does not contain sensitive information (credentials, tokens, etc.)
  • I have followed Conventional Commits for commit messages

Additional Notes

The fix is confined to the mcp_tool_to_langchain reference converter. The core AgentGatewayClient.call_mcp_tool() is unaffected — it still passes **kwargs through to the MCP server without schema validation.

@NicoleMGomes NicoleMGomes requested a review from a team as a code owner June 18, 2026 11:20

@thiagob thiagob left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgm

@NicoleMGomes NicoleMGomes merged commit af151b4 into main Jun 18, 2026
10 of 11 checks passed
@NicoleMGomes NicoleMGomes deleted the fix/optional-tools branch June 18, 2026 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants