component_shape_mcp/
results.rs1use super::*;
2
3pub fn tool_structured_result(value: Value) -> ToolCallResult {
4 let text = match &value {
5 Value::String(value) => value.clone(),
6 _ => value.to_string(),
7 };
8 let mut result = ToolCallResult::success(vec![ContentBlock::text(text)]);
9 result.structured_content = Some(value);
10 result
11}
12
13pub fn tool_error_result(message: impl Into<String>) -> ToolCallResult {
18 let message = message.into();
19 tool_error_result_with_structured_content(
20 message.clone(),
21 json!({
22 "error": {
23 "kind": "error",
24 "message": message,
25 }
26 }),
27 )
28}
29
30pub fn tool_error_result_for(error: McpToolError) -> ToolCallResult {
32 let message = error.to_string();
33 tool_error_result_with_structured_content(
34 message,
35 json!({
36 "error": error.to_structured_value(),
37 }),
38 )
39}
40
41fn tool_error_result_with_structured_content(
42 message: String,
43 structured_content: Value,
44) -> ToolCallResult {
45 let mut result = ToolCallResult::error(vec![ContentBlock::text(message)]);
46 result.structured_content = Some(structured_content);
47 result
48}
49
50pub fn serialize_handler_response<Response, Error>(
51 result: Result<Response, Error>,
52) -> ToolCallResult
53where
54 Response: Serialize,
55 Error: fmt::Display,
56{
57 match result {
58 Ok(response) => serialize_response_value(response),
59 Err(error) => tool_error_result_for(McpToolError::handler(error.to_string())),
60 }
61}
62
63pub fn serialize_response_value<Response>(response: Response) -> ToolCallResult
64where
65 Response: Serialize,
66{
67 match serde_json::to_value(response) {
68 Ok(value) => tool_structured_result(value),
69 Err(error) => tool_error_result_for(McpToolError::handler(format!(
70 "failed to serialize response: {error}"
71 ))),
72 }
73}