mcp_rs_sdk/
helpers.rs

1//! Contains helper functions to simplify common tasks when building an MCP agent.
2
3use crate::types::{PartialResponse, Response, ToolCall, ToolResult};
4use serde::de::DeserializeOwned;
5use serde::Serialize;
6
7// Non-streaming response helpers
8
9/// Creates a simple text-based response.
10///
11/// Useful for sending informational messages or answers that are not the result of a tool call.
12pub fn create_content_response(content: impl Into<String>) -> Response {
13    Response {
14        content: Some(content.into()),
15        ..Default::default()
16    }
17}
18
19/// Creates a response indicating an application-level error.
20///
21/// This is for errors that should be displayed to the end-user, not for protocol errors.
22pub fn create_error_response(message: impl Into<String>) -> Response {
23    create_content_response(format!("Error: {}", message.into()))
24}
25
26/// Creates a response containing the result of a single function call.
27///
28/// This is a high-level helper that constructs the entire `Response` object.
29/// It serializes the `result` to a JSON string for you.
30pub fn create_function_response<T: Serialize>(
31    tool_call_id: impl Into<String>,
32    result: &T,
33) -> Result<Response, serde_json::Error> {
34    let tool_result = create_tool_result(tool_call_id, result)?;
35    Ok(Response {
36        tool_results: Some(vec![tool_result]),
37        ..Default::default()
38    })
39}
40
41// Streaming response helpers
42
43/// Creates a streaming chunk containing a piece of text content.
44pub fn create_streaming_content_chunk(content: impl Into<String>) -> PartialResponse {
45    PartialResponse {
46        response: create_content_response(content),
47    }
48}
49
50/// Creates a streaming chunk indicating an application-level error.
51pub fn create_streaming_error_chunk(message: impl Into<String>) -> PartialResponse {
52    PartialResponse {
53        response: create_error_response(message),
54    }
55}
56
57/// Creates a streaming chunk containing the result of a single function call.
58///
59/// This serializes the `result` to a JSON string for you.
60pub fn create_streaming_tool_result_chunk<T: Serialize>(
61    tool_call_id: impl Into<String>,
62    result: &T,
63) -> Result<PartialResponse, serde_json::Error> {
64    let response = create_function_response(tool_call_id, result)?;
65    Ok(PartialResponse { response })
66}
67
68// Core helpers
69
70/// Creates a `ToolResult` from a tool call ID and a serializable payload.
71///
72/// This is a convenience function to correctly format a tool result.
73/// It serializes the provided `content` into a JSON string.
74pub fn create_tool_result<T: Serialize>(
75    tool_call_id: impl Into<String>,
76    content: &T,
77) -> Result<ToolResult, serde_json::Error> {
78    let content_str = serde_json::to_string(content)?;
79    Ok(ToolResult {
80        tool_call_id: tool_call_id.into(),
81        content: content_str,
82    })
83}
84
85/// Parses the JSON arguments from a `ToolCall` into a specific type.
86///
87/// This function simplifies the common task of deserializing the `arguments`
88/// string of a `ToolCall` into a strongly-typed Rust struct.
89pub fn parse_function_args<T: DeserializeOwned>(
90    tool_call: &ToolCall,
91) -> Result<T, serde_json::Error> {
92    serde_json::from_str(&tool_call.function.arguments)
93}