pub struct TypedToolWithOutput<TIn, TOut, F>where
TIn: DeserializeOwned + Send + Sync + 'static,
TOut: Serialize + Send + Sync + 'static,
F: Fn(TIn, RequestHandlerExtra) -> Pin<Box<dyn Future<Output = Result<TOut>> + Send>> + Send + Sync,{ /* private fields */ }Expand description
A typed tool with both input and output type safety
This variant provides type safety for both input arguments and return values. While output schemas are not part of the MCP protocol, they’re useful for testing, documentation, and API contracts.
Implementations§
Source§impl<TIn, TOut, F> TypedToolWithOutput<TIn, TOut, F>
impl<TIn, TOut, F> TypedToolWithOutput<TIn, TOut, F>
Sourcepub fn new(name: impl Into<String>, handler: F) -> Selfwhere
TIn: JsonSchema,
TOut: JsonSchema,
Available on crate feature schema-generation only.
pub fn new(name: impl Into<String>, handler: F) -> Selfwhere
TIn: JsonSchema,
TOut: JsonSchema,
schema-generation only.Create a new typed tool with automatic input and output schema generation
Sourcepub fn new_input_only(name: impl Into<String>, handler: F) -> Selfwhere
TIn: JsonSchema,
Available on crate feature schema-generation only.
pub fn new_input_only(name: impl Into<String>, handler: F) -> Selfwhere
TIn: JsonSchema,
schema-generation only.Create with only input schema generation (output schema omitted)
Sourcepub fn new_with_schemas(
name: impl Into<String>,
input_schema: Value,
output_schema: Option<Value>,
handler: F,
) -> Self
pub fn new_with_schemas( name: impl Into<String>, input_schema: Value, output_schema: Option<Value>, handler: F, ) -> Self
Create with manually provided schemas
Sourcepub fn with_description(self, description: impl Into<String>) -> Self
pub fn with_description(self, description: impl Into<String>) -> Self
Set the description for this tool
Sourcepub fn with_annotations(self, annotations: ToolAnnotations) -> Self
pub fn with_annotations(self, annotations: ToolAnnotations) -> Self
Set annotations for this tool.
These annotations will be merged with the auto-generated output schema annotation. User-provided hints (readOnlyHint, destructiveHint, etc.) will be combined with the output schema annotation.
§Example
use pmcp::server::typed_tool::TypedToolWithOutput;
use pmcp::types::ToolAnnotations;
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
#[derive(Debug, Deserialize, JsonSchema)]
struct QueryArgs { sql: String }
#[derive(Debug, Serialize, JsonSchema)]
struct QueryResult { rows: Vec<String> }
let tool = TypedToolWithOutput::new("query", |args: QueryArgs, _| {
Box::pin(async move {
Ok(QueryResult { rows: vec![] })
})
})
.with_description("Execute SQL query")
.with_annotations(
ToolAnnotations::new()
.with_read_only(true)
.with_idempotent(true)
);
// Tool now has both readOnlyHint and auto-generated pmcp:outputSchemaSourcepub fn destructive(self) -> Self
pub fn destructive(self) -> Self
Mark this tool as destructive (convenience method).
Sourcepub fn idempotent(self) -> Self
pub fn idempotent(self) -> Self
Mark this tool as idempotent (convenience method).
Sourcepub fn open_world(self) -> Self
pub fn open_world(self) -> Self
Mark this tool as interacting with external systems (convenience method).
Sourcepub fn output_schema(&self) -> Option<&Value>
pub fn output_schema(&self) -> Option<&Value>
Get the output schema (if any) for testing/documentation purposes