gemini: Fix edge case when transforming MCP tool schema (#32373)
Closes #31766 Release Notes: - Fixed an issue where some MCP tools would not work when using Gemini
This commit is contained in:
parent
da9e958b15
commit
78fd2685d5
1 changed files with 30 additions and 8 deletions
|
@ -46,16 +46,20 @@ fn adapt_to_json_schema_subset(json: &mut Value) -> Result<()> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const KEYS_TO_REMOVE: [&str; 5] = [
|
const KEYS_TO_REMOVE: [(&str, fn(&Value) -> bool); 5] = [
|
||||||
"format",
|
("format", |value| value.is_string()),
|
||||||
"additionalProperties",
|
("additionalProperties", |value| value.is_boolean()),
|
||||||
"exclusiveMinimum",
|
("exclusiveMinimum", |value| value.is_number()),
|
||||||
"exclusiveMaximum",
|
("exclusiveMaximum", |value| value.is_number()),
|
||||||
"optional",
|
("optional", |value| value.is_boolean()),
|
||||||
];
|
];
|
||||||
for key in KEYS_TO_REMOVE {
|
for (key, predicate) in KEYS_TO_REMOVE {
|
||||||
|
if let Some(value) = obj.get(key) {
|
||||||
|
if predicate(value) {
|
||||||
obj.remove(key);
|
obj.remove(key);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If a type is not specified for an input parameter, add a default type
|
// If a type is not specified for an input parameter, add a default type
|
||||||
if matches!(obj.get("description"), Some(Value::String(_)))
|
if matches!(obj.get("description"), Some(Value::String(_)))
|
||||||
|
@ -153,6 +157,24 @@ mod tests {
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Ensure that we do not remove keys that are actually supported (e.g. "format" can just be used as another property)
|
||||||
|
let mut json = json!({
|
||||||
|
"description": "A test field",
|
||||||
|
"type": "integer",
|
||||||
|
"format": {},
|
||||||
|
});
|
||||||
|
|
||||||
|
adapt_to_json_schema_subset(&mut json).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
json,
|
||||||
|
json!({
|
||||||
|
"description": "A test field",
|
||||||
|
"type": "integer",
|
||||||
|
"format": {},
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue