rmcp/handler/server/wrapper/parameters.rs
1use schemars::JsonSchema;
2
3/// Parameter extractor for tools and prompts
4///
5/// When used in tool and prompt handlers, this wrapper extracts and deserializes
6/// parameters from the incoming request. The framework will automatically parse
7/// the JSON arguments from tool calls or prompt arguments and deserialize them
8/// into the specified type `P`.
9///
10/// The `#[serde(transparent)]` attribute ensures that the wrapper doesn't add
11/// an extra layer in the JSON structure - it directly delegates serialization
12/// and deserialization to the inner type `P`.
13///
14/// # Usage
15///
16/// Use `Parameters<T>` as a parameter in your tool or prompt handler functions:
17///
18/// ```rust
19/// # use rmcp::handler::server::wrapper::Parameters;
20/// # use schemars::JsonSchema;
21/// # use serde::{Deserialize, Serialize};
22/// #[derive(Deserialize, JsonSchema)]
23/// struct CalculationRequest {
24/// operation: String,
25/// a: f64,
26/// b: f64,
27/// }
28///
29/// // In a tool handler
30/// async fn calculate(params: Parameters<CalculationRequest>) -> Result<String, String> {
31/// let request = params.0; // Extract the inner value
32/// match request.operation.as_str() {
33/// "add" => Ok((request.a + request.b).to_string()),
34/// _ => Err("Unknown operation".to_string()),
35/// }
36/// }
37/// ```
38///
39/// The framework handles the extraction automatically:
40/// - For tools: Parses the `arguments` field from tool call requests
41/// - For prompts: Parses the `arguments` field from prompt requests
42/// - Returns appropriate error responses if deserialization fails
43#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
44#[serde(transparent)]
45pub struct Parameters<P>(pub P);
46
47impl<P: JsonSchema> JsonSchema for Parameters<P> {
48 fn schema_name() -> std::borrow::Cow<'static, str> {
49 P::schema_name()
50 }
51
52 fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
53 P::json_schema(generator)
54 }
55}