Skip to content

Commit 11093bc

Browse files
authored
feat: add type-safe elicitation schema support (#465) (#466)
* feat: add type-safe elicitation schema support (#465) Implement type-safe schema definitions for MCP elicitation requests, replacing generic `JsonObject` with strongly-typed primitive schemas per the [MCP 2025-06-18 specification](https://spec.modelcontextprotocol.io/specification/2025-06-18/server/elicitation/). Features: - Type-safe schema hierarchy (`StringSchema`, `NumberSchema`, `IntegerSchema`, `BooleanSchema`) - Builder pattern with fluent API and 20+ convenience methods - Build-time validation ensuring required fields exist in properties - Private fields enforcing invariants through validated constructors - Comprehensive validation support (range, length, format, enums) - Typed property methods for cleaner schema construction Benefits: - Compile-time type safety prevents invalid schema construction - 60-70% reduction in boilerplate through convenience methods - Enforces MCP specification requirement for primitive-only properties - Better IDE autocomplete and type inference - Runtime validation catches schema errors early Breaking changes: - `CreateElicitationRequestParam.requested_schema` changed from `JsonObject` to `ElicitationSchema` - `ElicitationSchemaBuilder::build()` now returns `Result` instead of direct value Fixes #465 * fix: fix RMCP compliance * feat: add conversion methods to ElicitationSchema Add from_json_schema() and from_type() methods to ElicitationSchema for easier type-to-schema conversion. This addresses feedback about improving ergonomics when working with generated schemas. Also make all struct fields public for better flexibility. * chore: change `StringFormat` to enum
1 parent d929e69 commit 11093bc

File tree

7 files changed

+2253
-137
lines changed

7 files changed

+2253
-137
lines changed

crates/rmcp/src/model.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{borrow::Cow, sync::Arc};
22
mod annotated;
33
mod capabilities;
44
mod content;
5+
mod elicitation_schema;
56
mod extension;
67
mod meta;
78
mod prompt;
@@ -11,6 +12,7 @@ mod tool;
1112
pub use annotated::*;
1213
pub use capabilities::*;
1314
pub use content::*;
15+
pub use elicitation_schema::*;
1416
pub use extension::*;
1517
pub use meta::*;
1618
pub use prompt::*;
@@ -1377,7 +1379,21 @@ pub enum ElicitationAction {
13771379
///
13781380
/// This structure contains everything needed to request interactive input from a user:
13791381
/// - A human-readable message explaining what information is needed
1380-
/// - A JSON schema defining the expected structure of the response
1382+
/// - A type-safe schema defining the expected structure of the response
1383+
///
1384+
/// # Example
1385+
///
1386+
/// ```rust
1387+
/// use rmcp::model::*;
1388+
///
1389+
/// let params = CreateElicitationRequestParam {
1390+
/// message: "Please provide your email".to_string(),
1391+
/// requested_schema: ElicitationSchema::builder()
1392+
/// .required_email("email")
1393+
/// .build()
1394+
/// .unwrap(),
1395+
/// };
1396+
/// ```
13811397
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
13821398
#[serde(rename_all = "camelCase")]
13831399
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
@@ -1387,10 +1403,10 @@ pub struct CreateElicitationRequestParam {
13871403
/// what information they need to provide.
13881404
pub message: String,
13891405

1390-
/// JSON Schema defining the expected structure and validation rules for the user's response.
1391-
/// This allows clients to validate input and provide appropriate UI controls.
1392-
/// Must be a valid JSON Schema Draft 2020-12 object.
1393-
pub requested_schema: JsonObject,
1406+
/// Type-safe schema defining the expected structure and validation rules for the user's response.
1407+
/// This enforces the MCP 2025-06-18 specification that elicitation schemas must be objects
1408+
/// with primitive-typed properties.
1409+
pub requested_schema: ElicitationSchema,
13941410
}
13951411

13961412
/// The result returned by a client in response to an elicitation request.

0 commit comments

Comments
 (0)