From 8fbb6178dd73ba886525aabb8efefc62ad69bb7a Mon Sep 17 00:00:00 2001 From: Alexey Samosadov <> Date: Tue, 7 Apr 2026 17:48:03 +0300 Subject: [PATCH] fix: coerce MCP integer arguments to native Python int ChromaDB requires native `int` for `n_results`, but the MCP JSON-RPC transport can deliver JSON integers as floats or strings depending on the client implementation. This causes `mempalace_search` (and any tool with integer params like `max_hops`, `last_n`) to fail with: "Expected requested number of results to be a int, got 3 in query." Fix: auto-coerce tool arguments based on the declared `input_schema` types before calling handlers. This covers all current and future tools generically. Co-Authored-By: Claude Opus 4.6 --- mempalace/mcp_server.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mempalace/mcp_server.py b/mempalace/mcp_server.py index 5902fab..fdafa71 100644 --- a/mempalace/mcp_server.py +++ b/mempalace/mcp_server.py @@ -725,6 +725,17 @@ def handle_request(request): "id": req_id, "error": {"code": -32601, "message": f"Unknown tool: {tool_name}"}, } + # Coerce argument types based on input_schema. + # MCP JSON transport may deliver integers as floats or strings; + # ChromaDB and Python slicing require native int. + schema_props = TOOLS[tool_name]["input_schema"].get("properties", {}) + for key, value in list(tool_args.items()): + prop_schema = schema_props.get(key, {}) + declared_type = prop_schema.get("type") + if declared_type == "integer" and not isinstance(value, int): + tool_args[key] = int(value) + elif declared_type == "number" and not isinstance(value, (int, float)): + tool_args[key] = float(value) try: result = TOOLS[tool_name]["handler"](**tool_args) return {