Skip to content

fix: mcp_tool_to_langchain silently drops odata parameters whose name… - #355

Open
NicoleMGomes wants to merge 4 commits into
mainfrom
fix/mcp-parameters
Open

NicoleMGomes wants to merge 4 commits into
mainfrom
fix/mcp-parameters

Conversation

@NicoleMGomes

@NicoleMGomes NicoleMGomes commented Sep 29, 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

mcp_tool_to_langchain in converters.py was calling pydantic.create_model with OData property names directly. Pydantic v2 rejects field names starting with _ (raised as NameError in newer versions, silently dropped in older ones). Because OData CSDL §15.2 explicitly allows _ as a valid first character for identifiers (e.g. _VariantConfiguration, _Product), any agent using mcp_tool_to_langchain against an OData MCP server with _-prefixed parameters received a broken tool schema — those parameters were invisible to the LLM and tool calls would fail or produce wrong results.

Important: passing the JSON Schema dict directly as args_schema does not work — LangChain internally converts any dict back to a Pydantic model, so the problem persists. The fix instead builds an internal name_map that strips leading underscores before passing field names to create_model, then restores the original OData names inside run() before forwarding kwargs to call_tool. This is transparent to callers.

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. Create an MCPTool whose input_schema contains a _-prefixed property:
    from sap_cloud_sdk.agentgateway import MCPTool
    from sap_cloud_sdk.agentgateway.converters import mcp_tool_to_langchain
    from unittest.mock import AsyncMock
    
    tool = MCPTool(
        name="get_variant_config",
        server_name="s4hana",
        description="Get variant configuration",
        input_schema={
            "type": "object",
            "required": ["_VariantConfiguration"],
            "properties": {
                "_VariantConfiguration": {"type": "string"},
                "_Product": {"type": "string"},
            },
        },
        url="https://example.com/mcp",
    )
    lc_tool = mcp_tool_to_langchain(tool, AsyncMock(), lambda: "token")
  2. Verify "VariantConfiguration" (stripped) appears as a required field in lc_tool.args_schema.model_fields.
  3. Invoke the tool and verify call_tool receives _VariantConfiguration (restored):
    import asyncio
    call_tool = AsyncMock(return_value="ok")
    lc_tool = mcp_tool_to_langchain(tool, call_tool, lambda: "token")
    asyncio.run(lc_tool.arun({"VariantConfiguration": "VC001"}))
    assert "_VariantConfiguration" in call_tool.call_args.kwargs
  4. Run the unit tests: uv run pytest tests/agentgateway/unit/test_converters.py -v
  5. All 29 tests should pass.

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

Teams affected: any agent using mcp_tool_to_langchain against an OData MCP server (e.g. S/4HANA, EWM). The EWM warehouse agent shipped a local _sanitize_mcp_tool_schema workaround for this; that workaround is now redundant.

@NicoleMGomes
NicoleMGomes marked this pull request as ready for review September 29, 2026 16:43
@NicoleMGomes
NicoleMGomes requested a review from a team as a code owner September 29, 2026 16:43

This branch has not been deployed

No deployments
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