#[non_exhaustive]pub struct CallToolResult {
pub content: Vec<ContentBlock>,
pub structured_content: Option<Value>,
pub is_error: Option<bool>,
pub meta: Option<Meta>,
}Expand description
The result of a tool call operation.
Contains the content returned by the tool execution and an optional flag indicating whether the operation resulted in an error.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.content: Vec<ContentBlock>The content returned by the tool (text, images, etc.)
structured_content: Option<Value>An optional JSON object that represents the structured result of the tool call
is_error: Option<bool>Whether this result represents an error condition
meta: Option<Meta>Optional protocol-level metadata for this result
Implementations§
Source§impl CallToolResult
impl CallToolResult
Sourcepub fn success(content: Vec<ContentBlock>) -> Self
pub fn success(content: Vec<ContentBlock>) -> Self
Create a successful tool result with unstructured content
Sourcepub fn error(content: Vec<ContentBlock>) -> Self
pub fn error(content: Vec<ContentBlock>) -> Self
Create a tool-level error result with caller-visible content.
§When to use this vs Err(ErrorData)
MCP distinguishes two failure modes for a call_tool invocation, and
the right one to use depends on whose problem it is:
-
Tool-level error —
Ok(CallToolResult::error(...)). The request was valid and routed to your tool, but executing the tool failed in a way the caller should see (a query returned no rows, an external API returned 500, the user’s input is plausible but produced no result, etc.). The caller’s MCP client renders thecontentyou provide; your message reaches the user. This is the right choice for almost every “the tool ran and didn’t work” case. -
Protocol error —
Err(ErrorData)with a JSON-RPC code. The server cannot route the request at all, or an infrastructure error makes the server itself unusable (ErrorCode::INTERNAL_ERROR,-32603). MCP clients typically render protocol errors opaquely (e.g. “Tool result missing due to internal error”) — the caller does not see your message.
§Example
use rmcp::model::{CallToolResult, Content, ErrorData};
async fn lookup(query: &str) -> Result<CallToolResult, ErrorData> {
// Caller passed a malformed query — the server can't run anything.
// This is a protocol error, the caller's client will render it
// as -32602 invalid_params:
if query.is_empty() {
return Err(ErrorData::invalid_params("query must be non-empty", None));
}
// Tool ran, no result. Caller should see the explanation:
let rows = run_query(query).await;
if rows.is_empty() {
return Ok(CallToolResult::error(vec![ContentBlock::text(
format!("no rows matched '{query}'"),
)]));
}
Ok(CallToolResult::success(vec![ContentBlock::text(format_rows(&rows))]))
}Sourcepub fn structured(value: Value) -> Self
pub fn structured(value: Value) -> Self
Sourcepub fn structured_error(value: Value) -> Self
pub fn structured_error(value: Value) -> Self
Create an error tool result with structured content
§Example
use rmcp::model::CallToolResult;
use serde_json::json;
let result = CallToolResult::structured_error(json!({
"error_code": "INVALID_INPUT",
"message": "Temperature value out of range",
"details": {
"min": -50,
"max": 50,
"provided": 100
}
}));Sourcepub fn into_typed<T>(self) -> Result<T, Error>where
T: DeserializeOwned,
pub fn into_typed<T>(self) -> Result<T, Error>where
T: DeserializeOwned,
Convert the structured_content part of response into a certain type.
§About json schema validation
Since rust is a strong type language, we don’t need to do json schema validation here.
But if you do have to validate the response data, you can use jsonschema crate.
Trait Implementations§
Source§impl Clone for CallToolResult
impl Clone for CallToolResult
Source§fn clone(&self) -> CallToolResult
fn clone(&self) -> CallToolResult
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CallToolResult
impl Debug for CallToolResult
Source§impl Default for CallToolResult
impl Default for CallToolResult
Source§fn default() -> CallToolResult
fn default() -> CallToolResult
Source§impl<'de> Deserialize<'de> for CallToolResult
impl<'de> Deserialize<'de> for CallToolResult
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl From<CallToolResult> for ServerResult
impl From<CallToolResult> for ServerResult
Source§fn from(value: CallToolResult) -> Self
fn from(value: CallToolResult) -> Self
Source§impl IntoCallToolResult for CallToolResult
Available on crate feature server only.
impl IntoCallToolResult for CallToolResult
server only.fn into_call_tool_result(self) -> Result<CallToolResult, ErrorData>
Source§impl JsonSchema for CallToolResult
impl JsonSchema for CallToolResult
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreSource§impl PartialEq for CallToolResult
impl PartialEq for CallToolResult
Source§fn eq(&self, other: &CallToolResult) -> bool
fn eq(&self, other: &CallToolResult) -> bool
self and other values to be equal, and is used by ==.