pub trait ResultExt<T> {
// Required methods
fn tool_err(self) -> Result<T, Error>;
fn tool_context(self, context: impl Into<String>) -> Result<T, Error>;
}Expand description
Extension trait for converting errors into tower-mcp tool errors.
Provides ergonomic error conversion methods on Result types,
similar to anyhow::Context. Import this trait to use .tool_err()
and .tool_context() on any Result whose error type implements Display.
§Examples
use tower_mcp::error::ResultExt;
fn query_database() -> tower_mcp::Result<String> {
let result: Result<String, std::io::Error> =
Err(std::io::Error::other("connection refused"));
let value = result.tool_context("database query failed")?;
Ok(value)
}Required Methods§
Sourcefn tool_err(self) -> Result<T, Error>
fn tool_err(self) -> Result<T, Error>
Convert the error into a tool error.
use tower_mcp::error::ResultExt;
let value: Result<i32, std::io::Error> = Err(std::io::Error::other("timeout"));
let value = value.tool_err()?;Sourcefn tool_context(self, context: impl Into<String>) -> Result<T, Error>
fn tool_context(self, context: impl Into<String>) -> Result<T, Error>
Convert the error into a tool error with additional context.
use tower_mcp::error::ResultExt;
let value: Result<i32, std::io::Error> = Err(std::io::Error::other("timeout"));
let value = value.tool_context("database query failed")?;Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.