// `tools/list` 快照解析。同时接受裸结果 `{"tools":[...]}` 与
// JSON-RPC 响应 `{"result":{"tools":[...]}}` 两种形态。
///|
pub(all) struct ToolSpec {
name : String
description : String
input_schema : Json?
output_schema : Json?
annotations : Json?
}
///|
fn find_tools_array(root : Map[String, Json]) -> Array[Json]? {
match object_get_array(root, "tools") {
Some(items) => Some(items)
None =>
match object_get_object(root, "result") {
Some(result) => object_get_array(result, "tools")
None => None
}
}
}
///|
pub fn parse_tools_snapshot(text : String) -> Result[Array[ToolSpec], String] {
let json = match parse_json(text) {
Some(value) => value
None => return Err("invalid JSON")
}
let root = match json {
Object(obj) => obj
_ => return Err("top-level JSON must be an object")
}
let tools = match find_tools_array(root) {
Some(items) => items
None => return Err("missing tools array")
}
let specs : Array[ToolSpec] = []
for item in tools {
match item {
Object(tool) => {
let name = match object_get_string(tool, "name") {
Some(value) => value
None => ""
}
if name == "" {
continue
}
let description = match object_get_string(tool, "description") {
Some(value) => value
None => ""
}
let input_schema = match object_get(tool, "inputSchema") {
Some(value) => Some(value)
None => object_get(tool, "input_schema")
}
let output_schema = match object_get(tool, "outputSchema") {
Some(value) => Some(value)
None => object_get(tool, "output_schema")
}
let annotations = object_get(tool, "annotations")
specs.push({
name,
description,
input_schema,
output_schema,
annotations,
})
}
_ => ()
}
}
Ok(specs)
}