use crate::mcp::{QualifiedMcpToolName, protocol::Tool};
use serde_json::{Value, json};
pub(crate) fn provider_tool_definition(qualified: &QualifiedMcpToolName, tool: &Tool) -> Value {
let name = qualified.as_str();
let description = tool
.description
.as_deref()
.or(tool.title.as_deref())
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToString::to_string)
.unwrap_or_else(|| format!("MCP tool from server {}", qualified.server()));
json!({
"type": "function",
"name": name,
"description": description,
"parameters": object_schema_or_default(&tool.input_schema),
})
}
fn object_schema_or_default(schema: &Value) -> Value {
if schema.as_object().and_then(|object| object.get("type"))
== Some(&Value::String("object".to_string()))
{
schema.clone()
} else {
default_schema()
}
}
fn default_schema() -> Value {
json!({"type":"object","properties":{},"additionalProperties":true})
}