Skip to content

Commit 81ff315

Browse files
authored
feat: Add MCP Elicitation support (#332)
* feat: implement MCP elicitation support for interactive user input Adds comprehensive elicitation functionality according to MCP 2025-06-18 specification: Core Features: - ElicitationAction enum (Accept, Decline, Cancel) - CreateElicitationRequestParam and CreateElicitationResult structures - Protocol version V_2025_06_18 with elicitation methods - Full JSON-RPC integration with method constants Capabilities Integration: - ElicitationCapability with schema validation support - ClientCapabilities builder pattern integration - enable_elicitation() and enable_elicitation_schema_validation() methods Handler Support: - create_elicitation method in ClientHandler and ServerHandler traits - Integration with existing request/response union types - Async/await compatible implementation Service Layer: - Basic create_elicitation method via macro expansion - Four convenience methods for common scenarios: * elicit_confirmation() - yes/no questions * elicit_text_input() - string input with optional requirements * elicit_choice() - selection from multiple options * elicit_structured_input() - complex data via JSON Schema Comprehensive Testing: - 11 test cases covering all functionality aspects - JSON serialization/deserialization validation - MCP specification compliance verification - Error handling and edge cases - Performance benchmarks - Capabilities integration tests All tests pass and code follows project standards. * feat: add typed elicitation API with enhanced error handling - Add new 'elicitation' feature that depends on 'client' and 'schemars' - Implement elicit<T>() method for type-safe elicitation with automatic schema generation - Remove convenience methods (elicit_confirmation, elicit_text_input, elicit_choice) - Add ElicitationError enum with detailed error variants: - Service: underlying service errors - UserDeclined: user cancelled or declined request - ParseError: response parsing failed with context - NoContent: no response content provided - Update documentation with comprehensive examples and error handling - Add comprehensive tests for typed elicitation and error handling * fix: correct elicitation direction to comply with MCP 2025-06-18 - Remove CreateElicitationRequest from ClientRequest - clients cannot initiate elicitation - Move elicit methods from client to server - servers now request user input - Add comprehensive direction tests verifying Server→Client→Server flow - Maintain CreateElicitationResult in ClientResult for proper responses - Update handlers to reflect correct message routing - Add elicitation feature flag for typed schema generation Fixes elicitation direction to match specification where servers request interactive user input from clients, not the reverse. * feat: add elicitation capability checking for server methods - Add supports_elicitation() method to check client capabilities - Add CapabilityNotSupported error variant to ElicitationError - Update elicit_structured_input() to check capabilities before execution - Update elicit<T>() method to check capabilities before execution - Add comprehensive tests for capability checking functionality - Tests verify that servers check client capabilities before sending elicitation requests - Ensures compliance with MCP 2025-06-18 specification requirement * fix: json rpc message schema * fix: doc tests * fix: cargo nightly fmt checks * fix: clippy * refactor: separate elicitation methods into dedicated impl block for RoleServer - Move (supports_elicitation, elicit_structured_input, elicit) to separate impl block - Move ElicitationError definition to elicitation methods section - Keep base methods (create_message, list_roots, notify_*) in main impl block with macro - Add section comments to distinguish general and elicitation-specific methods * revert: rollback LATEST protocol version to V_2025_03_26 * fix: remove protocol version assertions - Remove assertions for V_2025_06_18 protocol version * fix: fmt checks * feat: add timeout support for elicitation methods - Add peer_req_with_timeout macro variants for timeout-enabled methods - Implement create_elicitation_with_timeout() method - Implement elicit_with_timeout() for typed elicitation with timeout - Refactor elicit() to use elicit_with_timeout() internally - Add 8 comprehensive timeout tests covering validation, error handling, and realistic scenarios - Fix elicitation feature dependencies in Cargo.toml - Add proper feature gates for elicitation-specific code * feat: add timeout validation to prevent DoS attacks - Add InvalidTimeout error variant for comprehensive validation - Implement validate_timeout function with security limits (1ms-300s) - Integrate validation into peer_req_with_timeout macros - Add comprehensive security tests for timeout validation - Prevent DoS attacks through unreasonable timeout values * feat: separate UserDeclined and UserCancelled elicitation errors According to MCP specification and PR feedback, decline and cancel actions should be handled differently: - UserDeclined: explicit user rejection (clicked "Decline", "No", etc.) - UserCancelled: dismissal without explicit choice (closed dialog, Escape, etc.) Changes: - Split ElicitationError::UserDeclined into two distinct error types - Update error handling logic to map each ElicitationAction correctly - Improve documentation with proper action semantics - Add comprehensive tests for new error types and action mapping - Update examples to demonstrate proper error handling This provides better error granularity allowing servers to handle explicit declines vs cancellations appropriately as per MCP spec. * feat: add compile-time type safety for elicitation methods Add ElicitationSafe trait and elicit_safe\! macro to ensure elicit<T>() methods are only used with types that generate appropriate JSON object schemas, addressing type safety concerns from PR feedback. Features: - ElicitationSafe marker trait for compile-time constraints - elicit_safe\! macro for opt-in type safety declaration - Updated elicit<T> and elicit_with_timeout<T> to require ElicitationSafe bound - Comprehensive documentation with examples and rationale - Full test coverage for new type safety features This prevents common mistakes like: - elicit::<String>() - primitives not suitable for object schemas - elicit::<Vec<i32>>() - arrays don't match client expectations Breaking change: Existing code must add elicit_safe\!(TypeName) declarations for types used with elicit methods. This is an intentional safety improvement. * Revert "feat: add timeout validation to prevent DoS attacks" This reverts commit 8296242. * fix: correct doctest example in elicit_safe macro documentation - Remove invalid async/await usage in doctest example - Comment out the actual usage line to show intent without compilation errors - Maintain clear documentation of the macro's purpose and usage * refactor: remove redundant elicitation direction tests - Remove test_elicitation_not_in_client_request (duplicated functionality) - Remove redundant ServerRequest match in test_elicitation_direction_server_to_client - Direction compliance is already verified by the remaining comprehensive test - Reduces test fragility and maintenance burden * feat: add elicitation example with user name collection - Add elicitation server example demonstrating real MCP usage - Implement greet_user tool with context.peer.elicit::<T>() API - Show type-safe elicitation with elicit_safe! macro - Include reset_name tool and MCP Inspector instructions - Update examples documentation and dependencies * fix: add Default impl to ElicitationServer for clippy Resolves clippy::new_without_default warning by implementing Default trait for ElicitationServer struct.
1 parent 4e6c57e commit 81ff315

File tree

12 files changed

+2287
-4
lines changed

12 files changed

+2287
-4
lines changed

crates/rmcp/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ default = ["base64", "macros", "server"]
7979
client = ["dep:tokio-stream"]
8080
server = ["transport-async-rw", "dep:schemars"]
8181
macros = ["dep:rmcp-macros", "dep:paste"]
82+
elicitation = []
8283

8384
# reqwest http client
8485
__reqwest = ["dep:reqwest"]
@@ -201,3 +202,8 @@ path = "tests/test_message_schema.rs"
201202
name = "test_progress_subscriber"
202203
required-features = ["server", "client", "macros"]
203204
path = "tests/test_progress_subscriber.rs"
205+
206+
[[test]]
207+
name = "test_elicitation"
208+
required-features = ["elicitation", "client", "server"]
209+
path = "tests/test_elicitation.rs"

crates/rmcp/src/handler/client.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ impl<H: ClientHandler> Service<RoleClient> for H {
2121
.list_roots(context)
2222
.await
2323
.map(ClientResult::ListRootsResult),
24+
ServerRequest::CreateElicitationRequest(request) => self
25+
.create_elicitation(request.params, context)
26+
.await
27+
.map(ClientResult::CreateElicitationResult),
2428
}
2529
}
2630

@@ -86,6 +90,35 @@ pub trait ClientHandler: Sized + Send + Sync + 'static {
8690
std::future::ready(Ok(ListRootsResult::default()))
8791
}
8892

93+
/// Handle an elicitation request from a server asking for user input.
94+
///
95+
/// This method is called when a server needs interactive input from the user
96+
/// during tool execution. Implementations should present the message to the user,
97+
/// collect their input according to the requested schema, and return the result.
98+
///
99+
/// # Arguments
100+
/// * `request` - The elicitation request with message and schema
101+
/// * `context` - The request context
102+
///
103+
/// # Returns
104+
/// The user's response including action (accept/decline/cancel) and optional data
105+
///
106+
/// # Default Behavior
107+
/// The default implementation automatically declines all elicitation requests.
108+
/// Real clients should override this to provide user interaction.
109+
fn create_elicitation(
110+
&self,
111+
request: CreateElicitationRequestParam,
112+
context: RequestContext<RoleClient>,
113+
) -> impl Future<Output = Result<CreateElicitationResult, McpError>> + Send + '_ {
114+
// Default implementation declines all requests - real clients should override this
115+
let _ = (request, context);
116+
std::future::ready(Ok(CreateElicitationResult {
117+
action: ElicitationAction::Decline,
118+
content: None,
119+
}))
120+
}
121+
89122
fn on_cancelled(
90123
&self,
91124
params: CancelledNotificationParam,

crates/rmcp/src/model.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ impl std::fmt::Display for ProtocolVersion {
143143
}
144144

145145
impl ProtocolVersion {
146+
pub const V_2025_06_18: Self = Self(Cow::Borrowed("2025-06-18"));
146147
pub const V_2025_03_26: Self = Self(Cow::Borrowed("2025-03-26"));
147148
pub const V_2024_11_05: Self = Self(Cow::Borrowed("2024-11-05"));
148149
pub const LATEST: Self = Self::V_2025_03_26;
@@ -167,6 +168,7 @@ impl<'de> Deserialize<'de> for ProtocolVersion {
167168
match s.as_str() {
168169
"2024-11-05" => return Ok(ProtocolVersion::V_2024_11_05),
169170
"2025-03-26" => return Ok(ProtocolVersion::V_2025_03_26),
171+
"2025-06-18" => return Ok(ProtocolVersion::V_2025_06_18),
170172
_ => {}
171173
}
172174
Ok(ProtocolVersion(Cow::Owned(s)))
@@ -1173,6 +1175,75 @@ pub struct ListRootsResult {
11731175
const_string!(RootsListChangedNotificationMethod = "notifications/roots/list_changed");
11741176
pub type RootsListChangedNotification = NotificationNoParam<RootsListChangedNotificationMethod>;
11751177

1178+
// =============================================================================
1179+
// ELICITATION (INTERACTIVE USER INPUT)
1180+
// =============================================================================
1181+
1182+
// Method constants for elicitation operations.
1183+
// Elicitation allows servers to request interactive input from users during tool execution.
1184+
const_string!(ElicitationCreateRequestMethod = "elicitation/create");
1185+
const_string!(ElicitationResponseNotificationMethod = "notifications/elicitation/response");
1186+
1187+
/// Represents the possible actions a user can take in response to an elicitation request.
1188+
///
1189+
/// When a server requests user input through elicitation, the user can:
1190+
/// - Accept: Provide the requested information and continue
1191+
/// - Decline: Refuse to provide the information but continue the operation
1192+
/// - Cancel: Stop the entire operation
1193+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
1194+
#[serde(rename_all = "lowercase")]
1195+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1196+
pub enum ElicitationAction {
1197+
/// User accepts the request and provides the requested information
1198+
Accept,
1199+
/// User declines to provide the information but allows the operation to continue
1200+
Decline,
1201+
/// User cancels the entire operation
1202+
Cancel,
1203+
}
1204+
1205+
/// Parameters for creating an elicitation request to gather user input.
1206+
///
1207+
/// This structure contains everything needed to request interactive input from a user:
1208+
/// - A human-readable message explaining what information is needed
1209+
/// - A JSON schema defining the expected structure of the response
1210+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
1211+
#[serde(rename_all = "camelCase")]
1212+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1213+
pub struct CreateElicitationRequestParam {
1214+
/// Human-readable message explaining what input is needed from the user.
1215+
/// This should be clear and provide sufficient context for the user to understand
1216+
/// what information they need to provide.
1217+
pub message: String,
1218+
1219+
/// JSON Schema defining the expected structure and validation rules for the user's response.
1220+
/// This allows clients to validate input and provide appropriate UI controls.
1221+
/// Must be a valid JSON Schema Draft 2020-12 object.
1222+
pub requested_schema: JsonObject,
1223+
}
1224+
1225+
/// The result returned by a client in response to an elicitation request.
1226+
///
1227+
/// Contains the user's decision (accept/decline/cancel) and optionally their input data
1228+
/// if they chose to accept the request.
1229+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
1230+
#[serde(rename_all = "camelCase")]
1231+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1232+
pub struct CreateElicitationResult {
1233+
/// The user's decision on how to handle the elicitation request
1234+
pub action: ElicitationAction,
1235+
1236+
/// The actual data provided by the user, if they accepted the request.
1237+
/// Must conform to the JSON schema specified in the original request.
1238+
/// Only present when action is Accept.
1239+
#[serde(skip_serializing_if = "Option::is_none")]
1240+
pub content: Option<Value>,
1241+
}
1242+
1243+
/// Request type for creating an elicitation to gather user input
1244+
pub type CreateElicitationRequest =
1245+
Request<ElicitationCreateRequestMethod, CreateElicitationRequestParam>;
1246+
11761247
// =============================================================================
11771248
// TOOL EXECUTION RESULTS
11781249
// =============================================================================
@@ -1430,7 +1501,7 @@ ts_union!(
14301501
);
14311502

14321503
ts_union!(
1433-
export type ClientResult = CreateMessageResult | ListRootsResult | EmptyResult;
1504+
export type ClientResult = CreateMessageResult | ListRootsResult | CreateElicitationResult | EmptyResult;
14341505
);
14351506

14361507
impl ClientResult {
@@ -1445,7 +1516,8 @@ ts_union!(
14451516
export type ServerRequest =
14461517
| PingRequest
14471518
| CreateMessageRequest
1448-
| ListRootsRequest;
1519+
| ListRootsRequest
1520+
| CreateElicitationRequest;
14491521
);
14501522

14511523
ts_union!(
@@ -1470,6 +1542,7 @@ ts_union!(
14701542
| ReadResourceResult
14711543
| CallToolResult
14721544
| ListToolsResult
1545+
| CreateElicitationResult
14731546
| EmptyResult
14741547
;
14751548
);

crates/rmcp/src/model/capabilities.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ pub struct RootsCapabilities {
4040
pub list_changed: Option<bool>,
4141
}
4242

43+
/// Capability for handling elicitation requests from servers.
44+
///
45+
/// Elicitation allows servers to request interactive input from users during tool execution.
46+
/// This capability indicates that a client can handle elicitation requests and present
47+
/// appropriate UI to users for collecting the requested information.
48+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
49+
#[serde(rename_all = "camelCase")]
50+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
51+
pub struct ElicitationCapability {
52+
/// Whether the client supports JSON Schema validation for elicitation responses.
53+
/// When true, the client will validate user input against the requested_schema
54+
/// before sending the response back to the server.
55+
#[serde(skip_serializing_if = "Option::is_none")]
56+
pub schema_validation: Option<bool>,
57+
}
58+
4359
///
4460
/// # Builder
4561
/// ```rust
@@ -59,6 +75,9 @@ pub struct ClientCapabilities {
5975
pub roots: Option<RootsCapabilities>,
6076
#[serde(skip_serializing_if = "Option::is_none")]
6177
pub sampling: Option<JsonObject>,
78+
/// Capability to handle elicitation requests from servers for interactive user input
79+
#[serde(skip_serializing_if = "Option::is_none")]
80+
pub elicitation: Option<ElicitationCapability>,
6281
}
6382

6483
///
@@ -253,6 +272,7 @@ builder! {
253272
experimental: ExperimentalCapabilities,
254273
roots: RootsCapabilities,
255274
sampling: JsonObject,
275+
elicitation: ElicitationCapability,
256276
}
257277
}
258278

@@ -267,6 +287,21 @@ impl<const E: bool, const S: bool>
267287
}
268288
}
269289

290+
#[cfg(feature = "elicitation")]
291+
impl<const E: bool, const R: bool, const S: bool>
292+
ClientCapabilitiesBuilder<ClientCapabilitiesBuilderState<E, R, S, true>>
293+
{
294+
/// Enable JSON Schema validation for elicitation responses.
295+
/// When enabled, the client will validate user input against the requested_schema
296+
/// before sending responses back to the server.
297+
pub fn enable_elicitation_schema_validation(mut self) -> Self {
298+
if let Some(c) = self.elicitation.as_mut() {
299+
c.schema_validation = Some(true);
300+
}
301+
self
302+
}
303+
}
304+
270305
#[cfg(test)]
271306
mod test {
272307
use super::*;

crates/rmcp/src/model/meta.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ variant_extension! {
7474
PingRequest
7575
CreateMessageRequest
7676
ListRootsRequest
77+
CreateElicitationRequest
7778
}
7879
}
7980

0 commit comments

Comments
 (0)