fix: mcp_tool_to_langchain parameters conversion error by NicoleMGomes · Pull Request #191 · SAP/cloud-sdk-python · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
20 changes: 18 additions & 2 deletions src/sap_cloud_sdk/agentgateway/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
if TYPE_CHECKING:
from langchain_core.tools import StructuredTool

_JSON_TYPE_MAP: dict[str, type] = {
"string": str,
"integer": int,
"number": float,
"boolean": bool,
"array": list,
"object": dict,
}


def mcp_tool_to_langchain(
mcp_tool: MCPTool,
Expand Down Expand Up @@ -83,8 +92,15 @@ async def run(**kwargs) -> str:
properties = mcp_tool.input_schema.get("properties", {})
required = set(mcp_tool.input_schema.get("required", []))
fields: dict[str, Any] = {
k: (str, ...) if k in required else (str | None, Field(default=None))
for k in properties
k: (
(_JSON_TYPE_MAP.get(v.get("type", ""), Any), ...)
if k in required
else (
_JSON_TYPE_MAP.get(v.get("type", ""), Any) | None,
Field(default=None),
)
)
for k, v in properties.items()
}
args_schema = create_model(f"{mcp_tool.name}_args", **fields) if fields else None

Expand Down
14 changes: 14 additions & 0 deletions src/sap_cloud_sdk/agentgateway/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ mcp_tool_to_langchain(
)
```

The converter maps each property's JSON Schema `"type"` to the corresponding Python type so Pydantic validates and forwards the correct native type to the MCP server:

| JSON Schema type | Python type |
|------------------|-------------|
| `"string"` | `str` |
| `"integer"` | `int` |
| `"number"` | `float` |
| `"boolean"` | `bool` |
| `"array"` | `list` |
| `"object"` | `dict` |
| missing / other | `Any` |

Optional fields (not listed in `"required"`) are typed as `T | None` with a `None` default.

## Concepts

### Agent Types
Expand Down
97 changes: 97 additions & 0 deletions tests/agentgateway/unit/test_converters.py
Loading