use serde_json::{Value, json};
pub fn success(id: Value, result: Value) -> Value {
json!({ "jsonrpc": "2.0", "id": id, "result": result })
}
pub fn error(id: Value, code: i64, message: &str) -> Value {
json!({ "jsonrpc": "2.0", "id": id, "error": { "code": code, "message": message } })
}
pub struct ToolResult {
pub structured: Option<Value>,
pub text: String,
pub is_error: bool,
}
impl ToolResult {
pub fn ok(structured: Value, text: String) -> Self {
Self {
structured: Some(structured),
text,
is_error: false,
}
}
pub fn err(message: impl Into<String>) -> Self {
Self {
structured: None,
text: message.into(),
is_error: true,
}
}
pub fn into_payload(self) -> Value {
let mut obj = serde_json::Map::new();
obj.insert(
"content".into(),
json!([{ "type": "text", "text": self.text }]),
);
if let Some(structured) = self.structured {
obj.insert("structuredContent".into(), structured);
}
obj.insert("isError".into(), Value::Bool(self.is_error));
Value::Object(obj)
}
}