|
71 | 71 | ) |
72 | 72 |
|
73 | 73 | mcp_tools = [adk_to_mcp_tool_type(tool) for tool in tools] |
| 74 | + |
| 75 | + |
| 76 | +def sanitize_mcp_schema_properties(node: dict) -> None: |
| 77 | + """Ensure additionalProperties is a boolean value to satisfy certain MCP clients. |
| 78 | +
|
| 79 | + This addresses issues with clients like Claude Desktop that fail when |
| 80 | + additionalProperties is a schema object instead of a boolean. |
| 81 | + """ |
| 82 | + if not isinstance(node, dict): |
| 83 | + return |
| 84 | + |
| 85 | + # Check and update the current node |
| 86 | + if "additionalProperties" in node: |
| 87 | + val = node["additionalProperties"] |
| 88 | + if not isinstance(val, bool): |
| 89 | + node["additionalProperties"] = True |
| 90 | + |
| 91 | + # Traverse children |
| 92 | + for key, child in node.items(): |
| 93 | + if isinstance(child, dict): |
| 94 | + sanitize_mcp_schema_properties(child) |
| 95 | + elif isinstance(child, list): |
| 96 | + for element in child: |
| 97 | + if isinstance(element, dict): |
| 98 | + sanitize_mcp_schema_properties(element) |
| 99 | + |
| 100 | + |
74 | 101 | # Update the inputSchema for tools that do not have parameters. |
75 | 102 | # TODO: This is a bug in the ADK and can be removed once it is fixed. |
76 | 103 | # https://github.com/google/adk-python/issues/948 |
|
83 | 110 | if "anyOf" in prop and prop.get("type") == "null": |
84 | 111 | del prop["type"] |
85 | 112 |
|
| 113 | + # Ensure additionalProperties is compatible with all MCP clients |
| 114 | + sanitize_mcp_schema_properties(tool.inputSchema) |
| 115 | + |
| 116 | + # Explicitly mark required fields for reporting tools to guide the LLM |
| 117 | + if tool.name == "run_report": |
| 118 | + tool.inputSchema["required"] = [ |
| 119 | + "property_id", |
| 120 | + "date_ranges", |
| 121 | + "dimensions", |
| 122 | + "metrics", |
| 123 | + ] |
| 124 | + elif tool.name == "run_realtime_report": |
| 125 | + tool.inputSchema["required"] = ["property_id", "dimensions", "metrics"] |
| 126 | + |
86 | 127 |
|
87 | 128 | @app.list_tools() |
88 | 129 | async def list_tools() -> list[mcp_types.Tool]: |
|
0 commit comments