Extract MCP tool manifest builder
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
use crank_registry::PublishedAgentTool;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub fn tool_definitions(tool: &PublishedAgentTool) -> Vec<Value> {
|
||||
vec![tool_definition(
|
||||
&tool.tool_name,
|
||||
&tool.tool_title,
|
||||
&tool.tool_description,
|
||||
schema_to_json_schema(&tool.operation.input_schema),
|
||||
)]
|
||||
}
|
||||
|
||||
pub fn tool_definition(name: &str, title: &str, description: &str, input_schema: Value) -> Value {
|
||||
json!({
|
||||
"name": name,
|
||||
"title": title,
|
||||
"description": description,
|
||||
"inputSchema": input_schema
|
||||
})
|
||||
}
|
||||
|
||||
pub fn schema_to_json_schema(schema: &crank_schema::Schema) -> Value {
|
||||
match schema.kind {
|
||||
crank_schema::SchemaKind::Object => {
|
||||
let mut properties = serde_json::Map::new();
|
||||
let mut required = Vec::new();
|
||||
|
||||
for (field_name, field_schema) in &schema.fields {
|
||||
properties.insert(field_name.clone(), schema_to_json_schema(field_schema));
|
||||
|
||||
if field_schema.required {
|
||||
required.push(Value::String(field_name.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": properties,
|
||||
"required": required
|
||||
})
|
||||
}
|
||||
crank_schema::SchemaKind::Array => json!({
|
||||
"type": "array",
|
||||
"items": schema
|
||||
.items
|
||||
.as_deref()
|
||||
.map(schema_to_json_schema)
|
||||
.unwrap_or_else(|| json!({}))
|
||||
}),
|
||||
crank_schema::SchemaKind::String => json!({ "type": "string" }),
|
||||
crank_schema::SchemaKind::Integer => json!({ "type": "integer" }),
|
||||
crank_schema::SchemaKind::Number => json!({ "type": "number" }),
|
||||
crank_schema::SchemaKind::Boolean => json!({ "type": "boolean" }),
|
||||
crank_schema::SchemaKind::Enum => json!({
|
||||
"type": "string",
|
||||
"enum": schema.enum_values
|
||||
}),
|
||||
crank_schema::SchemaKind::Null => json!({ "type": "null" }),
|
||||
crank_schema::SchemaKind::Oneof => json!({
|
||||
"anyOf": schema.variants.iter().map(schema_to_json_schema).collect::<Vec<_>>()
|
||||
}),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user