Skip to content

Commit 5ddd934

Browse files
committed
fix human input tool for gemini
1 parent 39b0c70 commit 5ddd934

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/mcp_agent/llm/providers/google_converter.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@ def _clean_schema_for_google(self, schema: Dict[str, Any]) -> Dict[str, Any]:
3535
"""
3636
Recursively removes unsupported JSON schema keywords for google.genai.types.Schema.
3737
Specifically removes 'additionalProperties', '$schema', 'exclusiveMaximum', and 'exclusiveMinimum'.
38+
Also resolves $ref references and inlines $defs.
3839
"""
40+
# First, resolve any $ref references in the schema
41+
schema = self._resolve_refs(schema, schema)
42+
3943
cleaned_schema = {}
4044
unsupported_keys = {
4145
"additionalProperties",
4246
"$schema",
4347
"exclusiveMaximum",
4448
"exclusiveMinimum",
49+
"$defs", # Remove $defs after resolving references
4550
}
4651
supported_string_formats = {"enum", "date-time"}
4752

@@ -66,6 +71,54 @@ def _clean_schema_for_google(self, schema: Dict[str, Any]) -> Dict[str, Any]:
6671
else:
6772
cleaned_schema[key] = value
6873
return cleaned_schema
74+
75+
def _resolve_refs(self, schema: Dict[str, Any], root_schema: Dict[str, Any]) -> Dict[str, Any]:
76+
"""
77+
Resolve $ref references in a JSON schema by inlining the referenced definitions.
78+
79+
Args:
80+
schema: The current schema fragment being processed
81+
root_schema: The root schema containing $defs
82+
83+
Returns:
84+
Schema with $ref references resolved
85+
"""
86+
if not isinstance(schema, dict):
87+
return schema
88+
89+
# If this is a $ref, resolve it
90+
if "$ref" in schema:
91+
ref_path = schema["$ref"]
92+
if ref_path.startswith("#/"):
93+
# Parse the reference path (e.g., "#/$defs/HumanInputRequest")
94+
path_parts = ref_path[2:].split("/") # Remove "#/" and split
95+
96+
# Navigate to the referenced definition
97+
ref_target = root_schema
98+
for part in path_parts:
99+
if part in ref_target:
100+
ref_target = ref_target[part]
101+
else:
102+
# If reference not found, return the original schema
103+
return schema
104+
105+
# Return the resolved definition (recursively resolve any nested refs)
106+
return self._resolve_refs(ref_target, root_schema)
107+
108+
# Otherwise, recursively process all values in the schema
109+
resolved = {}
110+
for key, value in schema.items():
111+
if isinstance(value, dict):
112+
resolved[key] = self._resolve_refs(value, root_schema)
113+
elif isinstance(value, list):
114+
resolved[key] = [
115+
self._resolve_refs(item, root_schema) if isinstance(item, dict) else item
116+
for item in value
117+
]
118+
else:
119+
resolved[key] = value
120+
121+
return resolved
69122

70123
def convert_to_google_content(
71124
self, messages: List[PromptMessageMultipart]

0 commit comments

Comments
 (0)