diff --git a/README.md b/README.md index d242653..28a5656 100644 --- a/README.md +++ b/README.md @@ -159,29 +159,20 @@ The following code snippet demonstrates how an MCP message, represented as a JSO ```rs -pub fn handle_message(message_payload: &str)->Result { - - // Deserialize message into ClientMessage. - // ClientMessage is an enum defined in schema_utils. - let message = ClientMessage::from_str(&message_payload)?; - - // Check if the message is a Request - if let ClientMessage::Request(message_object) = message { - - // Check if it's a standard ClientRequest (not a CustomRequest) - if let RequestFromClient::ClientRequest(client_request) = message_object.request { - - // Check if it's an InitializeRequest - if let rust_mcp_schema::ClientRequest::InitializeRequest(initialize_request) = client_request { - - // Process the InitializeRequest (and eventually send back a InitializedNotification back to the server) - handle_initialize_request(initialize_request); - - } - } +pub fn handle_messagew(message_payload: &str) -> std::result::Result<(), RpcError> { + // Deserialize message into ClientMessage. + let message = ClientMessage::from_str(&message_payload)?; + + // Check if the message is a Request + if let ClientMessage::Request(message_object) = message { + // Check if it's an InitializeRequest + if let ClientRequest::InitializeRequest(initialize_request) = client_request { + // Process the InitializeRequest (and eventually send back a InitializedNotification back to the server) + handle_initialize_request(initialize_request); } - } + Ok(()) +} ``` @@ -194,70 +185,74 @@ In response to an InitializeRequest, the MCP Server is expected to return an Ini This code snippet demonstrates how to create an InitializeRequest, serialize it into a string, and send it back to the client via the transport layer. ```rs - // create InitializeResult object - let initial_result = InitializeResult { - capabilities: ServerCapabilities { - experimental: None, - logging: None, - prompts: Some(ServerCapabilitiesPrompts { - list_changed: Some(true), - }), - resources: Some(ServerCapabilitiesResources { - list_changed: Some(true), - subscribe: Some(true), - }), - tools: Some(ServerCapabilitiesTools { - list_changed: Some(true), - }), - }, - instructions: Some(String::from("mcp server instructions....")), - meta: Some( - vec![ - ("meta 1".to_string(), Value::String("meta-value".to_string())), - ("meta 2".to_string(), Value::Number(serde_json::Number::from(225))), - ("feature-xyz".to_string(), Value::Bool(true)), - ] - .into_iter() - .collect(), - ), - protocol_version: LATEST_PROTOCOL_VERSION.to_string(), - server_info: Implementation { - name: String::from("example-servers/everything"), - version: String::from("1.0.0"), - }, - }; - - + // create InitializeResult object + let initial_result = InitializeResult { + capabilities: ServerCapabilities { + logging: None, + prompts: Some(ServerCapabilitiesPrompts {list_changed: Some(true)}), + resources: Some(ServerCapabilitiesResources {list_changed: Some(true),subscribe: Some(true)}), + tools: Some(ServerCapabilitiesTools { list_changed: Some(true)}), + completions: None, + tasks: Some(ServerTasks { cancel: Some(Map::new()), list: Some(Map::new()), requests: None }), + experimental: None, + }, + instructions: Some(String::from("mcp server instructions....")), + meta: Some( + json!({ + "meta 1": serde_json::Value::String("meta-value".into()), + "meta 2": serde_json::Value::Number(serde_json::Number::from(225)), + "feature-xyz": serde_json::Value::Bool(true) + }).as_object().unwrap().to_owned(), + ), + protocol_version: ProtocolVersion::V2025_11_25.into(), + server_info: Implementation { + name: String::from("rust mcp server"), + title: Some("Cool mcp server!".into()), + version: String::from("1.0.0"), + description: Some("your rust mcp server description....".into()), + icons: vec![], + website_url: Some("https://github.com/rust-mcp-stack/rust-mcp-schema".into()), + }, + }; + + // JsonrpcResultResponse vs JsonrpcResponse // Create a ServerMessage (a message intended to be sent from the server) - let message: ServerMessage = initial_result.to_message(request_id).unwrap(); + let message = ServerJsonrpcResponse::new(RequestId::Integer(0), initial_result.into()); - // Serialize the MCP message into a valid JSON string for sending to the client - let json_payload = message.to_string(); + // Serialize the MCP message into a valid JSON string for sending to the client + let json_payload = serde_json::to_string(&message).unwrap(); - println!("{}", json_payload); + println!("{}", json_payload); ``` output: ```json { - "id": 15, + "id": 0, "jsonrpc": "2.0", "result": { "capabilities": { "prompts": { "listChanged": true }, "resources": { "listChanged": true, "subscribe": true }, + "tasks": { "cancel": {}, "list": {} }, "tools": { "listChanged": true } }, "instructions": "mcp server instructions....", - "_meta": { "feature-xyz": true, "meta 1": "meta-value", "meta 2": 225 }, - "protocolVersion": "2024-11-05", - "serverInfo": { "name": "example-servers/everything", "version": "1.0.0" } + "protocolVersion": "2025-11-25", + "serverInfo": { + "description": "your rust mcp server description....", + "name": "rust mcp server", + "title": "Cool mcp server!", + "version": "1.0.0", + "websiteUrl": "https://github.com/rust-mcp-stack/rust-mcp-schema" + }, + "_meta": { "feature-xyz": true, "meta 1": "meta-value", "meta 2": 225 } } } ``` -### Detecting an `InitializeResult` Response Message in an MCP Client. +### Detecting an `InitializeResult` Response Message in an MCP Client: ```rs fn handle_message(message_payload: &str) -> std::result::Result<(), AppError> { @@ -265,20 +260,15 @@ fn handle_message(message_payload: &str) -> std::result::Result<(), AppError> { // ServerMessage represents a message sent by an MCP Server and received by an MCP Client. let mcp_message = ServerMessage::from_str(message_payload)?; - // Check if the message is a Response type of message - if let ServerMessage::Response(message_object) = mcp_message { - - // Check if it's a standard ServerResult (not a CustomResult) - if let ResultFromServer::ServerResult(server_response) = message_object.result { - - // Check if it's a InitializeResult type of response - if let ServerResult::InitializeResult(server_response){ - //Process the InitializeResult and send an InitializedNotification back to the server for acknowledgment. - handle_initialize_result(initialize_request); - - } + // Check if the message is a Response type of message + if let ServerMessage::Response(server_response) = mcp_message { + // Check if it's a InitializeResult response + if let ServerResult::InitializeResult(initialize_result) = server_response.result { + //Process the InitializeResult and send an InitializedNotification back to the server for acknowledgment. + handle_initialize_result(initialize_request); } - } + } + Ok(()) } ``` @@ -295,31 +285,21 @@ The following code example illustrates how to detect an InitializeRequest messag ```rs fn handle_message(message_payload: &str) -> std::result::Result<(), AppError> { - -// Deserialize json string into JsonrpcMessage - let message: JsonrpcMessage = serde_json::from_str(message_payload)?; + // Deserialize json string into JsonrpcMessage + let message: JsonrpcMessage = serde_json::from_str(message_payload).unwrap(); // Check it's a Request type of message if let JsonrpcMessage::Request(client_message) = message { + let client_request: ClientRequest = serde_json::from_value(client_message.params.into()).unwrap(); // Check method to detect is a "initialize" request - if client_message.method == "initialize" { - + if let ClientRequest::InitializeRequest(initialize_request) = client_request { // Now that we can handle the message, we simply print out the details. - println!("{} request received!", "initialize"); - - if let Some(params) = client_message.params { - // print out params meta - println!("params.meta : {:?} ", params.meta); - - let params: InitializeRequestParams = serde_json::from_value(Value::Object(params.extra.unwrap())).unwrap(); + println!("Initialize request received!"); - // print out InitializeRequestParams param values - println!( - "client_info : {:?} \ncapabilities: {:?} \nprotocol_version: {:?}", - params.client_info, params.capabilities, params.protocol_version - ); - } + println!("Client name : {:?} ", initialize_request.params.client_info.name); + println!("Client version : {:?} ", initialize_request.params.client_info.version); + } } diff --git a/examples/mcp_client_handle_message.rs b/examples/mcp_client_handle_message.rs index e3fc590..2e5095c 100644 --- a/examples/mcp_client_handle_message.rs +++ b/examples/mcp_client_handle_message.rs @@ -1,35 +1,5 @@ -#[cfg(feature = "latest")] -mod schema { - pub use rust_mcp_schema::schema_utils::*; - pub use rust_mcp_schema::*; -} - -#[cfg(feature = "2025_06_18")] -mod schema { - pub use rust_mcp_schema::mcp_2025_06_18::schema_utils::*; - pub use rust_mcp_schema::mcp_2025_06_18::*; -} - -#[cfg(feature = "2024_11_05")] -mod schema { - pub use rust_mcp_schema::mcp_2024_11_05::schema_utils::*; - pub use rust_mcp_schema::mcp_2024_11_05::*; -} - -#[cfg(feature = "2025_03_26")] -mod schema { - pub use rust_mcp_schema::mcp_2025_03_26::schema_utils::*; - pub use rust_mcp_schema::mcp_2025_03_26::*; -} - -#[cfg(feature = "draft")] -mod schema { - pub use rust_mcp_schema::mcp_draft::schema_utils::*; - pub use rust_mcp_schema::mcp_draft::*; -} - -use schema::*; - +#![cfg_attr(rustfmt, rustfmt_skip)] +use rust_mcp_schema::{schema_utils::*, *}; use std::str::FromStr; type AppError = RpcError; @@ -56,162 +26,66 @@ const SAMPLE_PAYLOAD: &str = r#" } "#; -fn main() -> std::result::Result<(), AppError> { - handle_message(SAMPLE_PAYLOAD)?; - Ok(()) +fn main() { + if let Err(error) = handle_message(SAMPLE_PAYLOAD) { + eprintln!("Error occurred: {:?}", error); + } } -/// Deserialize the JSON-RPC message into the appropriate MCP type and print it with dbg!() macro . + + +// Determine if the message is an Error fn handle_message(message_payload: &str) -> std::result::Result<(), AppError> { // Deserialize message into ServerMessage. - // ServerMessage represents a message sent by an MCP Server and received by an MCP Client. + //ServerMessage is a message sent by an MCP Server to an MCP Client. let mcp_message = ServerMessage::from_str(message_payload)?; - match mcp_message { - // Check if the message is a Request - ServerMessage::Request(server_message) => match server_message.request { - // Check if it's a standard ServerRequest (not a CustomRequest) - RequestFromServer::ServerRequest(server_request) => { - // Handle different ServerNotifications - match server_request { - ServerRequest::PingRequest(ping_request) => { - dbg!(ping_request); - } - ServerRequest::CreateMessageRequest(create_message_request) => { - dbg!(create_message_request); - } - ServerRequest::ListRootsRequest(list_roots_request) => { - dbg!(list_roots_request); - } - #[cfg(any(feature = "2025_06_18", feature = "2025_11_25", feature = "draft"))] - ServerRequest::ElicitRequest(elicit_request) => { - dbg!(elicit_request); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerRequest::GetTaskRequest(get_task_request) => { - dbg!(get_task_request); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerRequest::GetTaskPayloadRequest(get_task_payload_request) => { - dbg!(get_task_payload_request); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerRequest::CancelTaskRequest(cancel_task_request) => { - dbg!(cancel_task_request); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerRequest::ListTasksRequest(list_tasks_request) => { - dbg!(list_tasks_request); - } - } - } - // Check if it's a CustomRequest; the value can be deserialized into your own custom types. - RequestFromServer::CustomRequest(value) => { - dbg!(value); - } + // Determine if the message is a Request + ServerMessage::Request(request) => match request { + ServerJsonrpcRequest::PingRequest(ping_request) => println!("Ping request received: {:?}", ping_request), + ServerJsonrpcRequest::GetTaskRequest(get_task_request) => println!("GetTaskRequest request received: {:?}", get_task_request), + ServerJsonrpcRequest::GetTaskPayloadRequest(get_task_payload_request) => println!("GetTaskPayloadRequest request received: {:?}", get_task_payload_request), + ServerJsonrpcRequest::CancelTaskRequest(cancel_task_request) => println!("CancelTaskRequest request received: {:?}", cancel_task_request), + ServerJsonrpcRequest::ListTasksRequest(list_tasks_request) => println!("ListTasksRequest request received: {:?}", list_tasks_request), + ServerJsonrpcRequest::CreateMessageRequest(create_message_request) => println!("CreateMessageRequest request received: {:?}", create_message_request), + ServerJsonrpcRequest::ListRootsRequest(list_roots_request) => println!("ListRootsRequest request received: {:?}", list_roots_request), + ServerJsonrpcRequest::ElicitRequest(elicit_request) => println!("ElicitRequest request received: {:?}", elicit_request), + ServerJsonrpcRequest::CustomRequest(jsonrpc_request) => println!("CustomRequest request received: {:?}", jsonrpc_request), }, - // Check if the message is a Notification - ServerMessage::Notification(server_message) => match server_message.notification { - // Check if it's a standard ServerNotification (not a CustomNotification) - NotificationFromServer::ServerNotification(server_notification) => { - // Handle different ServerNotifications - match server_notification { - ServerNotification::CancelledNotification(cancelled_notification) => { - dbg!(cancelled_notification); - } - ServerNotification::ProgressNotification(progress_notification) => { - dbg!(progress_notification); - } - ServerNotification::ResourceListChangedNotification(resource_list_changed_notification) => { - dbg!(resource_list_changed_notification); - } - ServerNotification::ResourceUpdatedNotification(resource_updated_notification) => { - dbg!(resource_updated_notification); - } - ServerNotification::PromptListChangedNotification(prompt_list_changed_notification) => { - dbg!(prompt_list_changed_notification); - } - ServerNotification::ToolListChangedNotification(tool_list_changed_notification) => { - dbg!(tool_list_changed_notification); - } - ServerNotification::LoggingMessageNotification(logging_message_notification) => { - dbg!(logging_message_notification); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerNotification::TaskStatusNotification(task_status_notification) => { - dbg!(task_status_notification); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerNotification::ElicitationCompleteNotification(elicitation_complete_notification) => { - dbg!(elicitation_complete_notification); - } - } - } - // Check if it's a CustomNotification; the value can be deserialized into your custom types. - NotificationFromServer::CustomNotification(value) => { - dbg!(value); - } + // Determine if the message is a Notification + ServerMessage::Notification(notification) => match notification { + ServerJsonrpcNotification::CancelledNotification(cancelled_notification) => println!("CancelledNotification notification received: {:?}", cancelled_notification), + ServerJsonrpcNotification::ProgressNotification(progress_notification) => println!("ProgressNotification notification received: {:?}", progress_notification), + ServerJsonrpcNotification::ResourceListChangedNotification(resource_list_changed_notification) => println!("ResourceListChangedNotification notification received: {:?}",resource_list_changed_notification), + ServerJsonrpcNotification::ResourceUpdatedNotification(resource_updated_notification) => println!("ResourceUpdatedNotification notification received: {:?}",resource_updated_notification), + ServerJsonrpcNotification::PromptListChangedNotification(prompt_list_changed_notification) => println!("PromptListChangedNotification notification received: {:?}",prompt_list_changed_notification), + ServerJsonrpcNotification::ToolListChangedNotification(tool_list_changed_notification) => println!("ToolListChangedNotification notification received: {:?}",tool_list_changed_notification), + ServerJsonrpcNotification::TaskStatusNotification(task_status_notification) => {println!("TaskStatusNotification notification received: {:?}", task_status_notification)} + ServerJsonrpcNotification::LoggingMessageNotification(logging_message_notification) => println!("LoggingMessageNotification notification received: {:?}",logging_message_notification), + ServerJsonrpcNotification::ElicitationCompleteNotification(elicitation_complete_notification) => println!("ElicitationCompleteNotification notification received: {:?}",elicitation_complete_notification), + ServerJsonrpcNotification::CustomNotification(jsonrpc_notification) => println!("CustomNotification notification received: {:?}", jsonrpc_notification) }, - // Check if the message is a Response - ServerMessage::Response(server_message) => match server_message.result { - // Check if it's a standard ServerResult (not a CustomResult) - ResultFromServer::ServerResult(server_result) => match server_result { - ServerResult::Result(result) => { - dbg!(result); - } - ServerResult::InitializeResult(initialize_result) => { - dbg!(initialize_result); - } - ServerResult::ListResourcesResult(list_resources_result) => { - dbg!(list_resources_result); - } - ServerResult::ListResourceTemplatesResult(list_resource_templates_result) => { - dbg!(list_resource_templates_result); - } - ServerResult::ReadResourceResult(read_resource_result) => { - dbg!(read_resource_result); - } - ServerResult::ListPromptsResult(list_prompts_result) => { - dbg!(list_prompts_result); - } - ServerResult::GetPromptResult(get_prompt_result) => { - dbg!(get_prompt_result); - } - ServerResult::ListToolsResult(list_tools_result) => { - dbg!(list_tools_result); - } - ServerResult::CallToolResult(call_tool_result) => { - dbg!(call_tool_result); - } - ServerResult::CompleteResult(complete_result) => { - dbg!(complete_result); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerResult::GetTaskResult(get_task_result) => { - dbg!(get_task_result); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerResult::GetTaskPayloadResult(get_task_payload_result) => { - dbg!(get_task_payload_result); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerResult::CancelTaskResult(cancel_task_result) => { - dbg!(cancel_task_result); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ServerResult::ListTasksResult(list_tasks_result) => { - dbg!(list_tasks_result); - } - }, - // Check if it's a CustomResult; the value can be deserialized into your custom types. - ResultFromServer::CustomResult(value) => { - dbg!(value); - } + // Determine if the message is a Response + ServerMessage::Response(response) => match &response.result { + ServerResult::InitializeResult(_initialize_result) => println!("InitializeResult response received: {:?}", response), + ServerResult::ListResourcesResult(_list_resources_result) => println!("ListResourcesResult response received: {:?}", response), + ServerResult::ListResourceTemplatesResult(_list_resource_templates_result) => println!("ListResourceTemplatesResult response received: {:?}", response), + ServerResult::ReadResourceResult(_read_resource_result) => println!("ReadResourceResult response received: {:?}", response), + ServerResult::ListPromptsResult(_list_prompts_result) => println!("ListPromptsResult response received: {:?}", response), + ServerResult::GetPromptResult(_get_prompt_result) => println!("GetPromptResult response received: {:?}", response), + ServerResult::ListToolsResult(_list_tools_result) => println!("ListToolsResult response received: {:?}", response), + ServerResult::CallToolResult(_call_tool_result) => println!("CallToolResult response received: {:?}", response), + ServerResult::GetTaskResult(_get_task_result) => println!("GetTaskResult response received: {:?}", response), + ServerResult::CancelTaskResult(_cancel_task_result) => println!("CancelTaskResult response received: {:?}", response), + ServerResult::ListTasksResult(_list_tasks_result) => println!("ListTasksResult response received: {:?}", response), + ServerResult::CompleteResult(_complete_result) => println!("CompleteResult response received: {:?}", response), + ServerResult::Result(_generic_result) => println!("Generic Result response received: {:?}", response), + ServerResult::GetTaskPayloadResult(_generic_result) => println!("Generic Result response received: {:?}", response), }, - // Check if it's a Error message - ServerMessage::Error(server_message) => { - dbg!(server_message); + ServerMessage::Error(error_response) => { + println!("Error response received: {:?}", error_response) } } + Ok(()) } diff --git a/examples/mcp_server_handle_message.rs b/examples/mcp_server_handle_message.rs index 6b3e430..7a1b208 100644 --- a/examples/mcp_server_handle_message.rs +++ b/examples/mcp_server_handle_message.rs @@ -1,37 +1,9 @@ +#![cfg_attr(rustfmt, rustfmt_skip)] #[cfg(feature = "latest")] -mod schema { - pub use rust_mcp_schema::schema_utils::*; - pub use rust_mcp_schema::*; -} - -#[cfg(feature = "2025_06_18")] -mod schema { - pub use rust_mcp_schema::mcp_2025_06_18::schema_utils::*; - pub use rust_mcp_schema::mcp_2025_06_18::*; -} - -#[cfg(feature = "2024_11_05")] -mod schema { - pub use rust_mcp_schema::mcp_2024_11_05::schema_utils::*; - pub use rust_mcp_schema::mcp_2024_11_05::*; -} - -#[cfg(feature = "2025_03_26")] -mod schema { - pub use rust_mcp_schema::mcp_2025_03_26::schema_utils::*; - pub use rust_mcp_schema::mcp_2025_03_26::*; -} - -#[cfg(feature = "draft")] -mod schema { - pub use rust_mcp_schema::mcp_draft::schema_utils::*; - pub use rust_mcp_schema::mcp_draft::*; -} - -use schema::*; - +use rust_mcp_schema::{schema_utils::*, *}; use std::str::FromStr; +#[cfg(feature = "latest")] type AppError = RpcError; const SAMPLE_PAYLOAD: &str = r#" @@ -55,11 +27,14 @@ const SAMPLE_PAYLOAD: &str = r#" } "#; -fn main() -> std::result::Result<(), AppError> { - handle_message(SAMPLE_PAYLOAD)?; - Ok(()) +fn main() { + #[cfg(feature = "latest")] + if let Err(error) = handle_message(SAMPLE_PAYLOAD) { + eprintln!("Error occurred: {:?}", error); + } } +#[cfg(feature = "latest")] /// Deserialize the JSON-RPC message into the appropriate MCP type and print it with dbg!() macro . fn handle_message(message_payload: &str) -> std::result::Result<(), AppError> { // Deserialize message into ClientMessage. @@ -67,149 +42,52 @@ fn handle_message(message_payload: &str) -> std::result::Result<(), AppError> { let mcp_message = ClientMessage::from_str(message_payload)?; match mcp_message { - // Check if the message is a Request - ClientMessage::Request(client_message) => match client_message.request { - // Check if it's a standard ClientRequest (not a CustomRequest) - RequestFromClient::ClientRequest(client_request) => match client_request { - ClientRequest::InitializeRequest(initialize_request) => { - dbg!(initialize_request); - } - ClientRequest::PingRequest(ping_request) => { - dbg!(ping_request); - } - ClientRequest::ListResourcesRequest(list_resources_request) => { - dbg!(list_resources_request); - } - ClientRequest::ListResourceTemplatesRequest(list_resource_templates_request) => { - dbg!(list_resource_templates_request); - } - ClientRequest::ReadResourceRequest(read_resource_request) => { - dbg!(read_resource_request); - } - ClientRequest::SubscribeRequest(subscribe_request) => { - dbg!(subscribe_request); - } - ClientRequest::UnsubscribeRequest(unsubscribe_request) => { - dbg!(unsubscribe_request); - } - ClientRequest::ListPromptsRequest(list_prompts_request) => { - dbg!(list_prompts_request); - } - ClientRequest::GetPromptRequest(get_prompt_request) => { - dbg!(get_prompt_request); - } - ClientRequest::ListToolsRequest(list_tools_request) => { - dbg!(list_tools_request); - } - ClientRequest::CallToolRequest(call_tool_request) => { - dbg!(call_tool_request); - } - ClientRequest::SetLevelRequest(set_level_request) => { - dbg!(set_level_request); - } - ClientRequest::CompleteRequest(complete_request) => { - dbg!(complete_request); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ClientRequest::GetTaskRequest(get_task_request) => { - dbg!(get_task_request); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ClientRequest::GetTaskPayloadRequest(get_task_payload_request) => { - dbg!(get_task_payload_request); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ClientRequest::CancelTaskRequest(cancel_task_request) => { - dbg!(cancel_task_request); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ClientRequest::ListTasksRequest(list_tasks_request) => { - dbg!(list_tasks_request); - } - }, - - // Check if it's a CustomRequest; the value can be deserialized into your own custom types. - RequestFromClient::CustomRequest(value) => { - dbg!(value); - } + // Determine if the message is a Request + ClientMessage::Request(request) => match request { + ClientJsonrpcRequest::InitializeRequest(initialize_request) => println!("InitializeRequest request received: {:?}", initialize_request), + ClientJsonrpcRequest::PingRequest(ping_request) => println!("PingRequest request received: {:?}", ping_request), + ClientJsonrpcRequest::ListResourcesRequest(list_resources_request) => println!("ListResourcesRequest request received: {:?}", list_resources_request), + ClientJsonrpcRequest::ListResourceTemplatesRequest(list_resource_templates_request) => println!("ListResourceTemplatesRequest request received: {:?}",list_resource_templates_request), + ClientJsonrpcRequest::ReadResourceRequest(read_resource_request) => println!("ReadResourceRequest request received: {:?}", read_resource_request), + ClientJsonrpcRequest::SubscribeRequest(subscribe_request) => println!("SubscribeRequest request received: {:?}", subscribe_request), + ClientJsonrpcRequest::UnsubscribeRequest(unsubscribe_request) => println!("UnsubscribeRequest request received: {:?}", unsubscribe_request), + ClientJsonrpcRequest::ListPromptsRequest(list_prompts_request) => println!("ListPromptsRequest request received: {:?}", list_prompts_request), + ClientJsonrpcRequest::GetPromptRequest(get_prompt_request) => println!("GetPromptRequest request received: {:?}", get_prompt_request), + ClientJsonrpcRequest::ListToolsRequest(list_tools_request) => println!("ListToolsRequest request received: {:?}", list_tools_request), + ClientJsonrpcRequest::CallToolRequest(call_tool_request) => println!("CallToolRequest request received: {:?}", call_tool_request), + ClientJsonrpcRequest::GetTaskRequest(get_task_request) => println!("GetTaskRequest request received: {:?}", get_task_request), + ClientJsonrpcRequest::GetTaskPayloadRequest(get_task_payload_request) => println!("GetTaskPayloadRequest request received: {:?}", get_task_payload_request), + ClientJsonrpcRequest::CancelTaskRequest(cancel_task_request) => println!("CancelTaskRequest request received: {:?}", cancel_task_request), + ClientJsonrpcRequest::ListTasksRequest(list_tasks_request) => println!("ListTasksRequest request received: {:?}", list_tasks_request), + ClientJsonrpcRequest::SetLevelRequest(set_level_request) => println!("SetLevelRequest request received: {:?}", set_level_request), + ClientJsonrpcRequest::CompleteRequest(complete_request) => println!("CompleteRequest request received: {:?}", complete_request), + ClientJsonrpcRequest::CustomRequest(jsonrpc_request) => println!("CustomRequest request received: {:?}", jsonrpc_request), }, - - // Check if the message is a Notification - ClientMessage::Notification(client_message) => match client_message.notification { - // Check if it's a standard ClientNotification (not a CustomNotification) - NotificationFromClient::ClientNotification(client_notification) => { - // Handle different ClientNotifications - match client_notification { - ClientNotification::CancelledNotification(cancelled_notification) => { - dbg!(cancelled_notification); - } - ClientNotification::InitializedNotification(initialized_notification) => { - dbg!(initialized_notification); - } - ClientNotification::ProgressNotification(progress_notification) => { - dbg!(progress_notification); - } - ClientNotification::RootsListChangedNotification(progress_notification) => { - dbg!(progress_notification); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ClientNotification::TaskStatusNotification(task_status_notification) => { - dbg!(task_status_notification); - } - } - } - - // Check if it's a CustomNotification; the value can be deserialized into your custom types. - NotificationFromClient::CustomNotification(value) => { - dbg!(value); - } + // Determine if the message is a Notification + ClientMessage::Notification(notification) => match notification { + ClientJsonrpcNotification::CancelledNotification(cancelled_notification) => println!("CancelledNotification notification received: {:?}", cancelled_notification), + ClientJsonrpcNotification::InitializedNotification(initialized_notification) => println!("InitializedNotification notification received: {:?}",initialized_notification), + ClientJsonrpcNotification::ProgressNotification(progress_notification) => println!("ProgressNotification notification received: {:?}", progress_notification), + ClientJsonrpcNotification::TaskStatusNotification(task_status_notification) => println!("TaskStatusNotification notification received: {:?}", task_status_notification), + ClientJsonrpcNotification::RootsListChangedNotification(roots_list_changed_notification) => println!("RootsListChangedNotification notification received: {:?}",roots_list_changed_notification), + ClientJsonrpcNotification::CustomNotification(jsonrpc_notification) => println!("CustomNotification notification received: {:?}", jsonrpc_notification), }, - - // Check if the message is a Response - ClientMessage::Response(client_message) => match client_message.result { - // Check if it's a standard ClientResult (not a CustomResult) - ResultFromClient::ClientResult(client_result) => match client_result { - ClientResult::Result(_) => { - dbg!(client_result); - } - ClientResult::CreateMessageResult(create_message_result) => { - dbg!(create_message_result); - } - ClientResult::ListRootsResult(list_roots_result) => { - dbg!(list_roots_result); - } - #[cfg(any(feature = "2025_06_18", feature = "2025_11_25", feature = "draft"))] - ClientResult::ElicitResult(elicit_result) => { - dbg!(elicit_result); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ClientResult::GetTaskResult(get_task_result) => { - dbg!(get_task_result); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ClientResult::GetTaskPayloadResult(get_task_payload_result) => { - dbg!(get_task_payload_result); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ClientResult::CancelTaskResult(cancel_task_result) => { - dbg!(cancel_task_result); - } - #[cfg(any(feature = "2025_11_25", feature = "draft"))] - ClientResult::ListTasksResult(list_tasks_result) => { - dbg!(list_tasks_result); - } - }, - - // Check if it's a CustomResult; the value can be deserialized into your custom types. - ResultFromClient::CustomResult(value) => { - dbg!(value); - } + // Determine if the message is a Response + ClientMessage::Response(response) => match &response.result { + ClientResult::GetTaskResult(_get_task_result) => println!("GetTaskResult response received: {:?}", response), + ClientResult::CancelTaskResult(_cancel_task_result) => println!("CancelTaskResult response received: {:?}", response), + ClientResult::ListTasksResult(_list_tasks_result) => println!("ListTasksResult response received: {:?}", response), + ClientResult::CreateMessageResult(_create_message_result) => println!("CreateMessageResult response received: {:?}", response), + ClientResult::ListRootsResult(_list_roots_result) => println!("ListRootsResult response received: {:?}", response), + ClientResult::ElicitResult(_elicit_result) => println!("ElicitResult response received: {:?}", response), + ClientResult::Result(_generic_result) => println!("Generic Result response received: {:?}", response), + ClientResult::GetTaskPayloadResult(_generic_result) => println!("Generic Result response received: {:?}", response), }, - - // Check if it's a Error message - ClientMessage::Error(client_error) => { - dbg!(client_error); + // Determine if the message is an Error + ClientMessage::Error(error_response) => { + println!("Error response received: {:?}", error_response) } } + Ok(()) } diff --git a/scripts/run_clippy.sh b/scripts/run_clippy.sh index c0759b3..f760b06 100755 --- a/scripts/run_clippy.sh +++ b/scripts/run_clippy.sh @@ -3,21 +3,31 @@ # common features COMMON_FEATURES=("schema_utils") -# schema versions features (passed to clippy one at a time) -SCHEMA_VERSION_FEATURES=("2025_11_25", "2025_06_18", "2025_03_26", "2024_11_05", "draft") +# schema versions features +SCHEMA_VERSION_FEATURES=("2025_11_25" "2025_06_18" "2025_03_26" "2024_11_05" "draft") # space-separated string COMMON_FEATURES_STR="${COMMON_FEATURES[*]}" -for FEATURE in "${SCHEMA_VERSION_FEATURES[@]}"; do - echo "🚀 Running Clippy with: --features \"$COMMON_FEATURES_STR $FEATURE\"" - cargo clippy --all-targets --no-default-features --features "$COMMON_FEATURES_STR $FEATURE" -- -A deprecated -D warnings +run_clippy() { + local target_flag="$1" # "" for default, "--bins", "--tests", "--examples" + echo "🚀 Running Clippy $target_flag with features \"$COMMON_FEATURES_STR $FEATURE\"" + cargo clippy $target_flag --no-default-features --features "$COMMON_FEATURES_STR $FEATURE" -- -A deprecated -D warnings - # stop on failure if [ $? -ne 0 ]; then - echo "❌ Clippy failed for: --features \"$COMMON_FEATURES_STR $FEATURE\"" + echo "❌ Clippy failed for $target_flag with features \"$COMMON_FEATURES_STR $FEATURE\"" exit 1 fi +} + +for FEATURE in "${SCHEMA_VERSION_FEATURES[@]}"; do + # Run Clippy (exclude examples) + run_clippy "--lib --bins --tests" + + # Run Clippy for examples only for 2025_11_25 + if [ "$FEATURE" == "2025_11_25" ]; then + run_clippy "--examples" + fi done echo "✅ All Clippy lints have passed!" diff --git a/scripts/run_test.sh b/scripts/run_test.sh index 43a5900..d8f065f 100755 --- a/scripts/run_test.sh +++ b/scripts/run_test.sh @@ -4,32 +4,44 @@ COMMON_FEATURES=("schema_utils") # schema versions features (tested one at a time) -SCHEMA_VERSION_FEATURES=("2025_11_25", "2025_06_18", "2025_03_26", "2024_11_05") #// TODO: add the "draft" tests back +SCHEMA_VERSION_FEATURES=("2025_11_25" "2025_06_18" "2025_03_26" "2024_11_05" "draft") # space-separated string COMMON_FEATURES_STR="${COMMON_FEATURES[*]}" -for FEATURE in "${SCHEMA_VERSION_FEATURES[@]}"; do - echo "🚀 Running tests with: --features \"$COMMON_FEATURES_STR $FEATURE\"" - cargo nextest run --no-default-features --features "$COMMON_FEATURES_STR $FEATURE" +run_nextest() { + local target_flag="$1" # "--lib --bins --tests" or "--examples" + echo "🚀 Running tests $target_flag with features \"$COMMON_FEATURES_STR $FEATURE\"" + + cargo nextest run --no-tests=pass $target_flag \ + --no-default-features \ + --features "$COMMON_FEATURES_STR $FEATURE" - # stop on failure if [ $? -ne 0 ]; then - echo "❌ Tests failed for: --features \"$COMMON_FEATURES_STR $FEATURE\"" + echo "❌ Tests failed for $target_flag with features \"$COMMON_FEATURES_STR $FEATURE\"" exit 1 fi +} - # stop on failure - if [ $? -ne 0 ]; then - echo "❌ Tests failed for: --features \"$COMMON_FEATURES_STR $FEATURE\"" - exit 1 +for FEATURE in "${SCHEMA_VERSION_FEATURES[@]}"; do + # Run lib + bin + integration tests (NO examples) + run_nextest "--lib --bins --tests" + + # Run example tests only for 2025_11_25 + if [ "$FEATURE" == "2025_11_25" ]; then + run_nextest "--examples" fi done -# Get the first feature from the array +# Documentation tests (only once, only the latest schema) FEATURE="${SCHEMA_VERSION_FEATURES[0]}" echo echo "🚀 Running documentation tests with: --features \"$COMMON_FEATURES_STR $FEATURE\"" cargo test --doc --no-default-features --features "$COMMON_FEATURES_STR $FEATURE" +if [ $? -ne 0 ]; then + echo "❌ Documentation tests failed" + exit 1 +fi + echo "✅ All tests passed!" diff --git a/src/generated_schema/2024_11_05/mcp_schema.rs b/src/generated_schema/2024_11_05/mcp_schema.rs index 7bc16f9..bbde685 100644 --- a/src/generated_schema/2024_11_05/mcp_schema.rs +++ b/src/generated_schema/2024_11_05/mcp_schema.rs @@ -7,8 +7,8 @@ /// modify or extend the implementations as needed, but please do so at your own risk. /// /// Generated from : -/// Hash : 391c69f42f21bab3a5df0923b45b9ad3174414b9 -/// Generated at : 2025-12-01 18:52:52 +/// Hash : 77572f31dae0644acd1c537133bc49846c0abdb8 +/// Generated at : 2025-12-13 16:34:16 /// ---------------------------------------------------------------------------- /// use super::validators as validate; @@ -189,12 +189,12 @@ impl CallToolRequest { &self.method } /// returns "tools/call" - pub fn method_value() -> ::std::string::String { - "tools/call".to_string() + pub fn method_value() -> &'static str { + "tools/call" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/call".to_string() + pub fn method_name() -> &'static str { + "tools/call" } } ///CallToolRequestParams @@ -383,12 +383,12 @@ impl CancelledNotification { &self.method } /// returns "notifications/cancelled" - pub fn method_value() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_value() -> &'static str { + "notifications/cancelled" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_name() -> &'static str { + "notifications/cancelled" } } ///CancelledNotificationParams @@ -796,12 +796,12 @@ impl CompleteRequest { &self.method } /// returns "completion/complete" - pub fn method_value() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_value() -> &'static str { + "completion/complete" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_name() -> &'static str { + "completion/complete" } } ///The argument's information @@ -1099,12 +1099,12 @@ impl CreateMessageRequest { &self.method } /// returns "sampling/createMessage" - pub fn method_value() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_value() -> &'static str { + "sampling/createMessage" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_name() -> &'static str { + "sampling/createMessage" } } ///CreateMessageRequestParams @@ -1366,12 +1366,12 @@ impl EmbeddedResource { &self.type_ } /// returns "resource" - pub fn type_value() -> ::std::string::String { - "resource".to_string() + pub fn type_value() -> &'static str { + "resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "resource".to_string() + pub fn type_name() -> &'static str { + "resource" } } ///EmbeddedResourceAnnotations @@ -1511,12 +1511,12 @@ impl GetPromptRequest { &self.method } /// returns "prompts/get" - pub fn method_value() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_value() -> &'static str { + "prompts/get" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_name() -> &'static str { + "prompts/get" } } ///GetPromptRequestParams @@ -1672,12 +1672,12 @@ impl ImageContent { &self.type_ } /// returns "image" - pub fn type_value() -> ::std::string::String { - "image".to_string() + pub fn type_value() -> &'static str { + "image" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "image".to_string() + pub fn type_name() -> &'static str { + "image" } } ///ImageContentAnnotations @@ -1834,12 +1834,12 @@ impl InitializeRequest { &self.method } /// returns "initialize" - pub fn method_value() -> ::std::string::String { - "initialize".to_string() + pub fn method_value() -> &'static str { + "initialize" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "initialize".to_string() + pub fn method_name() -> &'static str { + "initialize" } } ///InitializeRequestParams @@ -1980,12 +1980,12 @@ impl InitializedNotification { &self.method } /// returns "notifications/initialized" - pub fn method_value() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_value() -> &'static str { + "notifications/initialized" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_name() -> &'static str { + "notifications/initialized" } } ///InitializedNotificationParams @@ -2420,12 +2420,12 @@ impl ListPromptsRequest { &self.method } /// returns "prompts/list" - pub fn method_value() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_value() -> &'static str { + "prompts/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_name() -> &'static str { + "prompts/list" } } ///ListPromptsRequestParams @@ -2540,12 +2540,12 @@ impl ListResourceTemplatesRequest { &self.method } /// returns "resources/templates/list" - pub fn method_value() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_value() -> &'static str { + "resources/templates/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_name() -> &'static str { + "resources/templates/list" } } ///ListResourceTemplatesRequestParams @@ -2661,12 +2661,12 @@ impl ListResourcesRequest { &self.method } /// returns "resources/list" - pub fn method_value() -> ::std::string::String { - "resources/list".to_string() + pub fn method_value() -> &'static str { + "resources/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/list".to_string() + pub fn method_name() -> &'static str { + "resources/list" } } ///ListResourcesRequestParams @@ -2792,12 +2792,12 @@ impl ListRootsRequest { &self.method } /// returns "roots/list" - pub fn method_value() -> ::std::string::String { - "roots/list".to_string() + pub fn method_value() -> &'static str { + "roots/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "roots/list".to_string() + pub fn method_name() -> &'static str { + "roots/list" } } ///ListRootsRequestParams @@ -2934,12 +2934,12 @@ impl ListToolsRequest { &self.method } /// returns "tools/list" - pub fn method_value() -> ::std::string::String { - "tools/list".to_string() + pub fn method_value() -> &'static str { + "tools/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/list".to_string() + pub fn method_name() -> &'static str { + "tools/list" } } ///ListToolsRequestParams @@ -3121,12 +3121,12 @@ impl LoggingMessageNotification { &self.method } /// returns "notifications/message" - pub fn method_value() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_value() -> &'static str { + "notifications/message" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_name() -> &'static str { + "notifications/message" } } ///LoggingMessageNotificationParams @@ -3464,12 +3464,12 @@ impl PingRequest { &self.method } /// returns "ping" - pub fn method_value() -> ::std::string::String { - "ping".to_string() + pub fn method_value() -> &'static str { + "ping" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "ping".to_string() + pub fn method_name() -> &'static str { + "ping" } } ///PingRequestParams @@ -3582,12 +3582,12 @@ impl ProgressNotification { &self.method } /// returns "notifications/progress" - pub fn method_value() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_value() -> &'static str { + "notifications/progress" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_name() -> &'static str { + "notifications/progress" } } ///ProgressNotificationParams @@ -3782,12 +3782,12 @@ impl PromptListChangedNotification { &self.method } /// returns "notifications/prompts/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/prompts/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/prompts/list_changed" } } ///PromptListChangedNotificationParams @@ -3941,12 +3941,12 @@ impl PromptReference { &self.type_ } /// returns "ref/prompt" - pub fn type_value() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_value() -> &'static str { + "ref/prompt" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_name() -> &'static str { + "ref/prompt" } } ///ReadResourceContent @@ -4033,12 +4033,12 @@ impl ReadResourceRequest { &self.method } /// returns "resources/read" - pub fn method_value() -> ::std::string::String { - "resources/read".to_string() + pub fn method_value() -> &'static str { + "resources/read" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/read".to_string() + pub fn method_name() -> &'static str { + "resources/read" } } ///ReadResourceRequestParams @@ -4415,12 +4415,12 @@ impl ResourceListChangedNotification { &self.method } /// returns "notifications/resources/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/list_changed" } } ///ResourceListChangedNotificationParams @@ -4493,12 +4493,12 @@ impl ResourceReference { &self.type_ } /// returns "ref/resource" - pub fn type_value() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_value() -> &'static str { + "ref/resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_name() -> &'static str { + "ref/resource" } } ///A template description for resources available on the server. @@ -4656,12 +4656,12 @@ impl ResourceUpdatedNotification { &self.method } /// returns "notifications/resources/updated" - pub fn method_value() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/updated" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/updated" } } ///ResourceUpdatedNotificationParams @@ -4833,12 +4833,12 @@ impl RootsListChangedNotification { &self.method } /// returns "notifications/roots/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/roots/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/roots/list_changed" } } ///RootsListChangedNotificationParams @@ -5402,12 +5402,12 @@ impl SetLevelRequest { &self.method } /// returns "logging/setLevel" - pub fn method_value() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_value() -> &'static str { + "logging/setLevel" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_name() -> &'static str { + "logging/setLevel" } } ///SetLevelRequestParams @@ -5485,12 +5485,12 @@ impl SubscribeRequest { &self.method } /// returns "resources/subscribe" - pub fn method_value() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_value() -> &'static str { + "resources/subscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_name() -> &'static str { + "resources/subscribe" } } ///SubscribeRequestParams @@ -5582,12 +5582,12 @@ impl TextContent { &self.type_ } /// returns "text" - pub fn type_value() -> ::std::string::String { - "text".to_string() + pub fn type_value() -> &'static str { + "text" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "text".to_string() + pub fn type_name() -> &'static str { + "text" } } ///TextContentAnnotations @@ -5788,12 +5788,12 @@ impl ToolInputSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///An optional notification from the server to the client, informing it that the list of tools it offers has changed. This may be issued by servers without any previous subscription from the client. @@ -5845,12 +5845,12 @@ impl ToolListChangedNotification { &self.method } /// returns "notifications/tools/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/tools/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/tools/list_changed" } } ///ToolListChangedNotificationParams @@ -5930,12 +5930,12 @@ impl UnsubscribeRequest { &self.method } /// returns "resources/unsubscribe" - pub fn method_value() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_value() -> &'static str { + "resources/unsubscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_name() -> &'static str { + "resources/unsubscribe" } } ///UnsubscribeRequestParams @@ -6201,6 +6201,72 @@ impl ServerNotification { } } } +/// Converts any serializable struct into a `GenericResult`. +/// This is used internally to convert ServerResult and ClientResult variants +/// into GenericResult (Result) +fn into_result(value: T) -> GenericResult +where + T: serde::Serialize, +{ + let json_value = serde_json::to_value(value).unwrap_or(serde_json::Value::Null); + if let serde_json::Value::Object(mut map) = json_value { + let meta = map.remove("_meta").and_then(|v| match v { + serde_json::Value::Object(obj) => Some(obj), + _ => None, + }); + let extra = if map.is_empty() { None } else { Some(map) }; + GenericResult { meta, extra } + } else { + GenericResult { meta: None, extra: None } + } +} +impl From for GenericResult { + fn from(value: InitializeResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourcesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourceTemplatesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ReadResourceResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListPromptsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: GetPromptResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListToolsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CallToolResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CompleteResult) -> Self { + into_result(value) + } +} +/// Alias to avoid conflicts with Rust's standard `Result` type. +pub type GenericResult = Result; /// Deprecating the old auto-generated verbose names. /// These were renamed to clearer, shorter names in v0.8.0. /// The old names are deprecated but kept for backward-compatibility for a smooth migration period. diff --git a/src/generated_schema/2025_03_26/mcp_schema.rs b/src/generated_schema/2025_03_26/mcp_schema.rs index 87edea1..ba51201 100644 --- a/src/generated_schema/2025_03_26/mcp_schema.rs +++ b/src/generated_schema/2025_03_26/mcp_schema.rs @@ -7,8 +7,8 @@ /// modify or extend the implementations as needed, but please do so at your own risk. /// /// Generated from : -/// Hash : 391c69f42f21bab3a5df0923b45b9ad3174414b9 -/// Generated at : 2025-12-01 18:52:54 +/// Hash : 77572f31dae0644acd1c537133bc49846c0abdb8 +/// Generated at : 2025-12-13 16:34:17 /// ---------------------------------------------------------------------------- /// use super::validators as validate; @@ -126,12 +126,12 @@ impl AudioContent { &self.type_ } /// returns "audio" - pub fn type_value() -> ::std::string::String { - "audio".to_string() + pub fn type_value() -> &'static str { + "audio" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "audio".to_string() + pub fn type_name() -> &'static str { + "audio" } } ///BlobResourceContents @@ -227,12 +227,12 @@ impl CallToolRequest { &self.method } /// returns "tools/call" - pub fn method_value() -> ::std::string::String { - "tools/call".to_string() + pub fn method_value() -> &'static str { + "tools/call" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/call".to_string() + pub fn method_name() -> &'static str { + "tools/call" } } ///CallToolRequestParams @@ -433,12 +433,12 @@ impl CancelledNotification { &self.method } /// returns "notifications/cancelled" - pub fn method_value() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_value() -> &'static str { + "notifications/cancelled" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_name() -> &'static str { + "notifications/cancelled" } } ///CancelledNotificationParams @@ -846,12 +846,12 @@ impl CompleteRequest { &self.method } /// returns "completion/complete" - pub fn method_value() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_value() -> &'static str { + "completion/complete" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_name() -> &'static str { + "completion/complete" } } ///The argument's information @@ -1191,12 +1191,12 @@ impl CreateMessageRequest { &self.method } /// returns "sampling/createMessage" - pub fn method_value() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_value() -> &'static str { + "sampling/createMessage" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_name() -> &'static str { + "sampling/createMessage" } } ///CreateMessageRequestParams @@ -1415,12 +1415,12 @@ impl EmbeddedResource { &self.type_ } /// returns "resource" - pub fn type_value() -> ::std::string::String { - "resource".to_string() + pub fn type_value() -> &'static str { + "resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "resource".to_string() + pub fn type_name() -> &'static str { + "resource" } } ///EmbeddedResourceResource @@ -1526,12 +1526,12 @@ impl GetPromptRequest { &self.method } /// returns "prompts/get" - pub fn method_value() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_value() -> &'static str { + "prompts/get" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_name() -> &'static str { + "prompts/get" } } ///GetPromptRequestParams @@ -1674,12 +1674,12 @@ impl ImageContent { &self.type_ } /// returns "image" - pub fn type_value() -> ::std::string::String { - "image".to_string() + pub fn type_value() -> &'static str { + "image" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "image".to_string() + pub fn type_name() -> &'static str { + "image" } } ///Describes the name and version of an MCP implementation. @@ -1802,12 +1802,12 @@ impl InitializeRequest { &self.method } /// returns "initialize" - pub fn method_value() -> ::std::string::String { - "initialize".to_string() + pub fn method_value() -> &'static str { + "initialize" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "initialize".to_string() + pub fn method_name() -> &'static str { + "initialize" } } ///InitializeRequestParams @@ -1948,12 +1948,12 @@ impl InitializedNotification { &self.method } /// returns "notifications/initialized" - pub fn method_value() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_value() -> &'static str { + "notifications/initialized" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_name() -> &'static str { + "notifications/initialized" } } ///InitializedNotificationParams @@ -2521,12 +2521,12 @@ impl ListPromptsRequest { &self.method } /// returns "prompts/list" - pub fn method_value() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_value() -> &'static str { + "prompts/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_name() -> &'static str { + "prompts/list" } } ///ListPromptsRequestParams @@ -2641,12 +2641,12 @@ impl ListResourceTemplatesRequest { &self.method } /// returns "resources/templates/list" - pub fn method_value() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_value() -> &'static str { + "resources/templates/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_name() -> &'static str { + "resources/templates/list" } } ///ListResourceTemplatesRequestParams @@ -2762,12 +2762,12 @@ impl ListResourcesRequest { &self.method } /// returns "resources/list" - pub fn method_value() -> ::std::string::String { - "resources/list".to_string() + pub fn method_value() -> &'static str { + "resources/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/list".to_string() + pub fn method_name() -> &'static str { + "resources/list" } } ///ListResourcesRequestParams @@ -2893,12 +2893,12 @@ impl ListRootsRequest { &self.method } /// returns "roots/list" - pub fn method_value() -> ::std::string::String { - "roots/list".to_string() + pub fn method_value() -> &'static str { + "roots/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "roots/list".to_string() + pub fn method_name() -> &'static str { + "roots/list" } } ///ListRootsRequestParams @@ -3035,12 +3035,12 @@ impl ListToolsRequest { &self.method } /// returns "tools/list" - pub fn method_value() -> ::std::string::String { - "tools/list".to_string() + pub fn method_value() -> &'static str { + "tools/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/list".to_string() + pub fn method_name() -> &'static str { + "tools/list" } } ///ListToolsRequestParams @@ -3222,12 +3222,12 @@ impl LoggingMessageNotification { &self.method } /// returns "notifications/message" - pub fn method_value() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_value() -> &'static str { + "notifications/message" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_name() -> &'static str { + "notifications/message" } } ///LoggingMessageNotificationParams @@ -3565,12 +3565,12 @@ impl PingRequest { &self.method } /// returns "ping" - pub fn method_value() -> ::std::string::String { - "ping".to_string() + pub fn method_value() -> &'static str { + "ping" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "ping".to_string() + pub fn method_name() -> &'static str { + "ping" } } ///PingRequestParams @@ -3687,12 +3687,12 @@ impl ProgressNotification { &self.method } /// returns "notifications/progress" - pub fn method_value() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_value() -> &'static str { + "notifications/progress" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_name() -> &'static str { + "notifications/progress" } } ///ProgressNotificationParams @@ -3894,12 +3894,12 @@ impl PromptListChangedNotification { &self.method } /// returns "notifications/prompts/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/prompts/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/prompts/list_changed" } } ///PromptListChangedNotificationParams @@ -4065,12 +4065,12 @@ impl PromptReference { &self.type_ } /// returns "ref/prompt" - pub fn type_value() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_value() -> &'static str { + "ref/prompt" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_name() -> &'static str { + "ref/prompt" } } ///ReadResourceContent @@ -4157,12 +4157,12 @@ impl ReadResourceRequest { &self.method } /// returns "resources/read" - pub fn method_value() -> ::std::string::String { - "resources/read".to_string() + pub fn method_value() -> &'static str { + "resources/read" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/read".to_string() + pub fn method_name() -> &'static str { + "resources/read" } } ///ReadResourceRequestParams @@ -4492,12 +4492,12 @@ impl ResourceListChangedNotification { &self.method } /// returns "notifications/resources/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/list_changed" } } ///ResourceListChangedNotificationParams @@ -4570,12 +4570,12 @@ impl ResourceReference { &self.type_ } /// returns "ref/resource" - pub fn type_value() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_value() -> &'static str { + "ref/resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_name() -> &'static str { + "ref/resource" } } ///A template description for resources available on the server. @@ -4686,12 +4686,12 @@ impl ResourceUpdatedNotification { &self.method } /// returns "notifications/resources/updated" - pub fn method_value() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/updated" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/updated" } } ///ResourceUpdatedNotificationParams @@ -4863,12 +4863,12 @@ impl RootsListChangedNotification { &self.method } /// returns "notifications/roots/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/roots/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/roots/list_changed" } } ///RootsListChangedNotificationParams @@ -5452,12 +5452,12 @@ impl SetLevelRequest { &self.method } /// returns "logging/setLevel" - pub fn method_value() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_value() -> &'static str { + "logging/setLevel" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_name() -> &'static str { + "logging/setLevel" } } ///SetLevelRequestParams @@ -5535,12 +5535,12 @@ impl SubscribeRequest { &self.method } /// returns "resources/subscribe" - pub fn method_value() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_value() -> &'static str { + "resources/subscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_name() -> &'static str { + "resources/subscribe" } } ///SubscribeRequestParams @@ -5619,12 +5619,12 @@ impl TextContent { &self.type_ } /// returns "text" - pub fn type_value() -> ::std::string::String { - "text".to_string() + pub fn type_value() -> &'static str { + "text" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "text".to_string() + pub fn type_name() -> &'static str { + "text" } } ///TextResourceContents @@ -5866,12 +5866,12 @@ impl ToolInputSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///An optional notification from the server to the client, informing it that the list of tools it offers has changed. This may be issued by servers without any previous subscription from the client. @@ -5923,12 +5923,12 @@ impl ToolListChangedNotification { &self.method } /// returns "notifications/tools/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/tools/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/tools/list_changed" } } ///ToolListChangedNotificationParams @@ -6008,12 +6008,12 @@ impl UnsubscribeRequest { &self.method } /// returns "resources/unsubscribe" - pub fn method_value() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_value() -> &'static str { + "resources/unsubscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_name() -> &'static str { + "resources/unsubscribe" } } ///UnsubscribeRequestParams @@ -6279,6 +6279,72 @@ impl ServerNotification { } } } +/// Converts any serializable struct into a `GenericResult`. +/// This is used internally to convert ServerResult and ClientResult variants +/// into GenericResult (Result) +fn into_result(value: T) -> GenericResult +where + T: serde::Serialize, +{ + let json_value = serde_json::to_value(value).unwrap_or(serde_json::Value::Null); + if let serde_json::Value::Object(mut map) = json_value { + let meta = map.remove("_meta").and_then(|v| match v { + serde_json::Value::Object(obj) => Some(obj), + _ => None, + }); + let extra = if map.is_empty() { None } else { Some(map) }; + GenericResult { meta, extra } + } else { + GenericResult { meta: None, extra: None } + } +} +impl From for GenericResult { + fn from(value: InitializeResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourcesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourceTemplatesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ReadResourceResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListPromptsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: GetPromptResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListToolsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CallToolResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CompleteResult) -> Self { + into_result(value) + } +} +/// Alias to avoid conflicts with Rust's standard `Result` type. +pub type GenericResult = Result; /// Deprecating the old auto-generated verbose names. /// These were renamed to clearer, shorter names in v0.8.0. /// The old names are deprecated but kept for backward-compatibility for a smooth migration period. diff --git a/src/generated_schema/2025_06_18/mcp_schema.rs b/src/generated_schema/2025_06_18/mcp_schema.rs index 1d6ff2a..2cb13d0 100644 --- a/src/generated_schema/2025_06_18/mcp_schema.rs +++ b/src/generated_schema/2025_06_18/mcp_schema.rs @@ -7,8 +7,8 @@ /// modify or extend the implementations as needed, but please do so at your own risk. /// /// Generated from : -/// Hash : 391c69f42f21bab3a5df0923b45b9ad3174414b9 -/// Generated at : 2025-12-01 18:52:54 +/// Hash : 77572f31dae0644acd1c537133bc49846c0abdb8 +/// Generated at : 2025-12-13 16:34:18 /// ---------------------------------------------------------------------------- /// use super::validators as validate; @@ -146,12 +146,12 @@ impl AudioContent { &self.type_ } /// returns "audio" - pub fn type_value() -> ::std::string::String { - "audio".to_string() + pub fn type_value() -> &'static str { + "audio" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "audio".to_string() + pub fn type_name() -> &'static str { + "audio" } } ///Base interface for metadata with name (identifier) and title (display name) properties. @@ -294,12 +294,12 @@ impl BooleanSchema { &self.type_ } /// returns "boolean" - pub fn type_value() -> ::std::string::String { - "boolean".to_string() + pub fn type_value() -> &'static str { + "boolean" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "boolean".to_string() + pub fn type_name() -> &'static str { + "boolean" } } ///Used by the client to invoke a tool provided by the server. @@ -355,12 +355,12 @@ impl CallToolRequest { &self.method } /// returns "tools/call" - pub fn method_value() -> ::std::string::String { - "tools/call".to_string() + pub fn method_value() -> &'static str { + "tools/call" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/call".to_string() + pub fn method_name() -> &'static str { + "tools/call" } } ///CallToolRequestParams @@ -511,12 +511,12 @@ impl CancelledNotification { &self.method } /// returns "notifications/cancelled" - pub fn method_value() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_value() -> &'static str { + "notifications/cancelled" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_name() -> &'static str { + "notifications/cancelled" } } ///CancelledNotificationParams @@ -954,12 +954,12 @@ impl CompleteRequest { &self.method } /// returns "completion/complete" - pub fn method_value() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_value() -> &'static str { + "completion/complete" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_name() -> &'static str { + "completion/complete" } } ///The argument's information @@ -1400,12 +1400,12 @@ impl CreateMessageRequest { &self.method } /// returns "sampling/createMessage" - pub fn method_value() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_value() -> &'static str { + "sampling/createMessage" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_name() -> &'static str { + "sampling/createMessage" } } ///CreateMessageRequestParams @@ -1644,12 +1644,12 @@ impl ElicitRequest { &self.method } /// returns "elicitation/create" - pub fn method_value() -> ::std::string::String { - "elicitation/create".to_string() + pub fn method_value() -> &'static str { + "elicitation/create" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "elicitation/create".to_string() + pub fn method_name() -> &'static str { + "elicitation/create" } } ///ElicitRequestParams @@ -1762,12 +1762,12 @@ impl ElicitRequestedSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///The client's response to an elicitation request. @@ -1965,12 +1965,12 @@ impl EmbeddedResource { &self.type_ } /// returns "resource" - pub fn type_value() -> ::std::string::String { - "resource".to_string() + pub fn type_value() -> &'static str { + "resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "resource".to_string() + pub fn type_name() -> &'static str { + "resource" } } ///EmbeddedResourceResource @@ -2089,12 +2089,12 @@ impl EnumSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///Used by the client to get a prompt provided by the server. @@ -2154,12 +2154,12 @@ impl GetPromptRequest { &self.method } /// returns "prompts/get" - pub fn method_value() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_value() -> &'static str { + "prompts/get" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_name() -> &'static str { + "prompts/get" } } ///GetPromptRequestParams @@ -2312,12 +2312,12 @@ impl ImageContent { &self.type_ } /// returns "image" - pub fn type_value() -> ::std::string::String { - "image".to_string() + pub fn type_value() -> &'static str { + "image" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "image".to_string() + pub fn type_name() -> &'static str { + "image" } } ///Describes the name and version of an MCP implementation, with an optional title for UI representation. @@ -2453,12 +2453,12 @@ impl InitializeRequest { &self.method } /// returns "initialize" - pub fn method_value() -> ::std::string::String { - "initialize".to_string() + pub fn method_value() -> &'static str { + "initialize" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "initialize".to_string() + pub fn method_name() -> &'static str { + "initialize" } } ///InitializeRequestParams @@ -2599,12 +2599,12 @@ impl InitializedNotification { &self.method } /// returns "notifications/initialized" - pub fn method_value() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_value() -> &'static str { + "notifications/initialized" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_name() -> &'static str { + "notifications/initialized" } } ///InitializedNotificationParams @@ -3048,12 +3048,12 @@ impl ListPromptsRequest { &self.method } /// returns "prompts/list" - pub fn method_value() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_value() -> &'static str { + "prompts/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_name() -> &'static str { + "prompts/list" } } ///ListPromptsRequestParams @@ -3168,12 +3168,12 @@ impl ListResourceTemplatesRequest { &self.method } /// returns "resources/templates/list" - pub fn method_value() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_value() -> &'static str { + "resources/templates/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_name() -> &'static str { + "resources/templates/list" } } ///ListResourceTemplatesRequestParams @@ -3289,12 +3289,12 @@ impl ListResourcesRequest { &self.method } /// returns "resources/list" - pub fn method_value() -> ::std::string::String { - "resources/list".to_string() + pub fn method_value() -> &'static str { + "resources/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/list".to_string() + pub fn method_name() -> &'static str { + "resources/list" } } ///ListResourcesRequestParams @@ -3422,12 +3422,12 @@ impl ListRootsRequest { &self.method } /// returns "roots/list" - pub fn method_value() -> ::std::string::String { - "roots/list".to_string() + pub fn method_value() -> &'static str { + "roots/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "roots/list".to_string() + pub fn method_name() -> &'static str { + "roots/list" } } ///ListRootsRequestParams @@ -3570,12 +3570,12 @@ impl ListToolsRequest { &self.method } /// returns "tools/list" - pub fn method_value() -> ::std::string::String { - "tools/list".to_string() + pub fn method_value() -> &'static str { + "tools/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/list".to_string() + pub fn method_name() -> &'static str { + "tools/list" } } ///ListToolsRequestParams @@ -3757,12 +3757,12 @@ impl LoggingMessageNotification { &self.method } /// returns "notifications/message" - pub fn method_value() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_value() -> &'static str { + "notifications/message" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_name() -> &'static str { + "notifications/message" } } ///LoggingMessageNotificationParams @@ -4178,12 +4178,12 @@ impl PingRequest { &self.method } /// returns "ping" - pub fn method_value() -> ::std::string::String { - "ping".to_string() + pub fn method_value() -> &'static str { + "ping" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "ping".to_string() + pub fn method_name() -> &'static str { + "ping" } } ///PingRequestParams @@ -4359,12 +4359,12 @@ impl ProgressNotification { &self.method } /// returns "notifications/progress" - pub fn method_value() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_value() -> &'static str { + "notifications/progress" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_name() -> &'static str { + "notifications/progress" } } ///ProgressNotificationParams @@ -4596,12 +4596,12 @@ impl PromptListChangedNotification { &self.method } /// returns "notifications/prompts/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/prompts/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/prompts/list_changed" } } ///PromptListChangedNotificationParams @@ -4715,12 +4715,12 @@ impl PromptReference { &self.type_ } /// returns "ref/prompt" - pub fn type_value() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_value() -> &'static str { + "ref/prompt" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_name() -> &'static str { + "ref/prompt" } } ///ReadResourceContent @@ -4807,12 +4807,12 @@ impl ReadResourceRequest { &self.method } /// returns "resources/read" - pub fn method_value() -> ::std::string::String { - "resources/read".to_string() + pub fn method_value() -> &'static str { + "resources/read" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/read".to_string() + pub fn method_name() -> &'static str { + "resources/read" } } ///ReadResourceRequestParams @@ -5245,12 +5245,12 @@ impl ResourceLink { &self.type_ } /// returns "resource_link" - pub fn type_value() -> ::std::string::String { - "resource_link".to_string() + pub fn type_value() -> &'static str { + "resource_link" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "resource_link".to_string() + pub fn type_name() -> &'static str { + "resource_link" } } ///An optional notification from the server to the client, informing it that the list of resources it can read from has changed. This may be issued by servers without any previous subscription from the client. @@ -5302,12 +5302,12 @@ impl ResourceListChangedNotification { &self.method } /// returns "notifications/resources/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/list_changed" } } ///ResourceListChangedNotificationParams @@ -5455,12 +5455,12 @@ impl ResourceTemplateReference { &self.type_ } /// returns "ref/resource" - pub fn type_value() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_value() -> &'static str { + "ref/resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_name() -> &'static str { + "ref/resource" } } ///A notification from the server to the client, informing it that a resource has changed and may need to be read again. This should only be sent if the client previously sent a resources/subscribe request. @@ -5514,12 +5514,12 @@ impl ResourceUpdatedNotification { &self.method } /// returns "notifications/resources/updated" - pub fn method_value() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/updated" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/updated" } } ///ResourceUpdatedNotificationParams @@ -5699,12 +5699,12 @@ impl RootsListChangedNotification { &self.method } /// returns "notifications/roots/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/roots/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/roots/list_changed" } } ///RootsListChangedNotificationParams @@ -6297,12 +6297,12 @@ impl SetLevelRequest { &self.method } /// returns "logging/setLevel" - pub fn method_value() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_value() -> &'static str { + "logging/setLevel" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_name() -> &'static str { + "logging/setLevel" } } ///SetLevelRequestParams @@ -6405,12 +6405,12 @@ impl StringSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///StringSchemaFormat @@ -6501,12 +6501,12 @@ impl SubscribeRequest { &self.method } /// returns "resources/subscribe" - pub fn method_value() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_value() -> &'static str { + "resources/subscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_name() -> &'static str { + "resources/subscribe" } } ///SubscribeRequestParams @@ -6598,12 +6598,12 @@ impl TextContent { &self.type_ } /// returns "text" - pub fn type_value() -> ::std::string::String { - "text".to_string() + pub fn type_value() -> &'static str { + "text" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "text".to_string() + pub fn type_name() -> &'static str { + "text" } } ///TextResourceContents @@ -6901,12 +6901,12 @@ impl ToolInputSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///An optional notification from the server to the client, informing it that the list of tools it offers has changed. This may be issued by servers without any previous subscription from the client. @@ -6958,12 +6958,12 @@ impl ToolListChangedNotification { &self.method } /// returns "notifications/tools/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/tools/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/tools/list_changed" } } ///ToolListChangedNotificationParams @@ -7057,12 +7057,12 @@ impl ToolOutputSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///Sent from the client to request cancellation of resources/updated notifications from the server. This should follow a previous resources/subscribe request. @@ -7116,12 +7116,12 @@ impl UnsubscribeRequest { &self.method } /// returns "resources/unsubscribe" - pub fn method_value() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_value() -> &'static str { + "resources/unsubscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_name() -> &'static str { + "resources/unsubscribe" } } ///UnsubscribeRequestParams @@ -7392,6 +7392,72 @@ impl ServerNotification { } } } +/// Converts any serializable struct into a `GenericResult`. +/// This is used internally to convert ServerResult and ClientResult variants +/// into GenericResult (Result) +fn into_result(value: T) -> GenericResult +where + T: serde::Serialize, +{ + let json_value = serde_json::to_value(value).unwrap_or(serde_json::Value::Null); + if let serde_json::Value::Object(mut map) = json_value { + let meta = map.remove("_meta").and_then(|v| match v { + serde_json::Value::Object(obj) => Some(obj), + _ => None, + }); + let extra = if map.is_empty() { None } else { Some(map) }; + GenericResult { meta, extra } + } else { + GenericResult { meta: None, extra: None } + } +} +impl From for GenericResult { + fn from(value: InitializeResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourcesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourceTemplatesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ReadResourceResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListPromptsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: GetPromptResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListToolsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CallToolResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CompleteResult) -> Self { + into_result(value) + } +} +/// Alias to avoid conflicts with Rust's standard `Result` type. +pub type GenericResult = Result; /// Deprecating the old auto-generated verbose names. /// These were renamed to clearer, shorter names in v0.8.0. /// The old names are deprecated but kept for backward-compatibility for a smooth migration period. diff --git a/src/generated_schema/2025_11_25/mcp_schema.rs b/src/generated_schema/2025_11_25/mcp_schema.rs index 3183de0..6c1ce53 100644 --- a/src/generated_schema/2025_11_25/mcp_schema.rs +++ b/src/generated_schema/2025_11_25/mcp_schema.rs @@ -7,13 +7,13 @@ /// modify or extend the implementations as needed, but please do so at your own risk. /// /// Generated from : -/// Hash : 391c69f42f21bab3a5df0923b45b9ad3174414b9 -/// Generated at : 2025-12-01 18:52:55 +/// Hash : 77572f31dae0644acd1c537133bc49846c0abdb8 +/// Generated at : 2025-12-13 16:34:19 /// ---------------------------------------------------------------------------- /// use super::validators as validate; /// MCP Protocol Version -pub const LATEST_PROTOCOL_VERSION: &str = "DRAFT-2025-v3"; +pub const LATEST_PROTOCOL_VERSION: &str = "2025-11-25"; /// JSON-RPC Version pub const JSONRPC_VERSION: &str = "2.0"; /// Parse error. Invalid JSON was received. An error occurred while parsing the JSON text. @@ -148,12 +148,12 @@ impl AudioContent { &self.type_ } /// returns "audio" - pub fn type_value() -> ::std::string::String { - "audio".to_string() + pub fn type_value() -> &'static str { + "audio" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "audio".to_string() + pub fn type_name() -> &'static str { + "audio" } } ///Base interface for metadata with name (identifier) and title (display name) properties. @@ -296,12 +296,12 @@ impl BooleanSchema { &self.type_ } /// returns "boolean" - pub fn type_value() -> ::std::string::String { - "boolean".to_string() + pub fn type_value() -> &'static str { + "boolean" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "boolean".to_string() + pub fn type_name() -> &'static str { + "boolean" } } ///See [General fields: _meta](https://modelcontextprotocol.io/specification/2025-11-25/basic/index#meta) for notes on _meta usage. @@ -388,12 +388,12 @@ impl CallToolRequest { &self.method } /// returns "tools/call" - pub fn method_value() -> ::std::string::String { - "tools/call".to_string() + pub fn method_value() -> &'static str { + "tools/call" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/call".to_string() + pub fn method_name() -> &'static str { + "tools/call" } } ///Parameters for a tools/call request. @@ -608,12 +608,12 @@ impl CancelTaskRequest { &self.method } /// returns "tasks/cancel" - pub fn method_value() -> ::std::string::String { - "tasks/cancel".to_string() + pub fn method_value() -> &'static str { + "tasks/cancel" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tasks/cancel".to_string() + pub fn method_name() -> &'static str { + "tasks/cancel" } } ///The response to a tasks/cancel request. @@ -710,12 +710,12 @@ impl CancelledNotification { &self.method } /// returns "notifications/cancelled" - pub fn method_value() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_value() -> &'static str { + "notifications/cancelled" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_name() -> &'static str { + "notifications/cancelled" } } ///Parameters for a notifications/cancelled notification. @@ -1177,24 +1177,19 @@ impl ::std::convert::From for ClientRequest { #[serde(untagged)] pub enum ClientResult { GetTaskResult(GetTaskResult), - GetTaskPayloadResult(GetTaskPayloadResult), CancelTaskResult(CancelTaskResult), ListTasksResult(ListTasksResult), CreateMessageResult(CreateMessageResult), ListRootsResult(ListRootsResult), ElicitResult(ElicitResult), Result(Result), + GetTaskPayloadResult(GetTaskPayloadResult), } impl ::std::convert::From for ClientResult { fn from(value: GetTaskResult) -> Self { Self::GetTaskResult(value) } } -impl ::std::convert::From for ClientResult { - fn from(value: GetTaskPayloadResult) -> Self { - Self::GetTaskPayloadResult(value) - } -} impl ::std::convert::From for ClientResult { fn from(value: CancelTaskResult) -> Self { Self::CancelTaskResult(value) @@ -1225,6 +1220,11 @@ impl ::std::convert::From for ClientResult { Self::Result(value) } } +impl ::std::convert::From for ClientResult { + fn from(value: GetTaskPayloadResult) -> Self { + Self::GetTaskPayloadResult(value) + } +} ///Present if the client supports listing roots. /// ///
JSON schema @@ -1491,12 +1491,12 @@ impl CompleteRequest { &self.method } /// returns "completion/complete" - pub fn method_value() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_value() -> &'static str { + "completion/complete" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_name() -> &'static str { + "completion/complete" } } ///The argument's information @@ -1977,12 +1977,12 @@ impl CreateMessageRequest { &self.method } /// returns "sampling/createMessage" - pub fn method_value() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_value() -> &'static str { + "sampling/createMessage" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_name() -> &'static str { + "sampling/createMessage" } } ///Parameters for a sampling/createMessage request. @@ -2358,12 +2358,12 @@ impl ElicitFormSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///A request from the server to elicit additional information from the user via the client. @@ -2424,12 +2424,12 @@ impl ElicitRequest { &self.method } /// returns "elicitation/create" - pub fn method_value() -> ::std::string::String { - "elicitation/create".to_string() + pub fn method_value() -> &'static str { + "elicitation/create" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "elicitation/create".to_string() + pub fn method_name() -> &'static str { + "elicitation/create" } } ///The parameters for a request to elicit non-sensitive information from the user via a form in the client. @@ -2544,12 +2544,12 @@ impl ElicitRequestFormParams { &self.mode } /// returns "form" - pub fn mode_value() -> ::std::string::String { - "form".to_string() + pub fn mode_value() -> &'static str { + "form" } #[deprecated(since = "0.8.0", note = "Use `mode_value()` instead.")] - pub fn mode_name() -> ::std::string::String { - "form".to_string() + pub fn mode_name() -> &'static str { + "form" } } ///The parameters for a request to elicit additional information from the user via the client. @@ -2682,12 +2682,12 @@ impl ElicitRequestUrlParams { &self.mode } /// returns "url" - pub fn mode_value() -> ::std::string::String { - "url".to_string() + pub fn mode_value() -> &'static str { + "url" } #[deprecated(since = "0.8.0", note = "Use `mode_value()` instead.")] - pub fn mode_name() -> ::std::string::String { - "url".to_string() + pub fn mode_name() -> &'static str { + "url" } } ///The client's response to an elicitation request. @@ -2952,12 +2952,12 @@ impl ElicitationCompleteNotification { &self.method } /// returns "notifications/elicitation/complete" - pub fn method_value() -> ::std::string::String { - "notifications/elicitation/complete".to_string() + pub fn method_value() -> &'static str { + "notifications/elicitation/complete" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/elicitation/complete".to_string() + pub fn method_name() -> &'static str { + "notifications/elicitation/complete" } } /**The contents of a resource, embedded into a prompt or tool call result. @@ -3031,12 +3031,12 @@ impl EmbeddedResource { &self.type_ } /// returns "resource" - pub fn type_value() -> ::std::string::String { - "resource".to_string() + pub fn type_value() -> &'static str { + "resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "resource".to_string() + pub fn type_name() -> &'static str { + "resource" } } ///EmbeddedResourceResource @@ -3229,12 +3229,12 @@ impl GetPromptRequest { &self.method } /// returns "prompts/get" - pub fn method_value() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_value() -> &'static str { + "prompts/get" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_name() -> &'static str { + "prompts/get" } } ///Parameters for a prompts/get request. @@ -3443,12 +3443,12 @@ impl GetTaskPayloadRequest { &self.method } /// returns "tasks/result" - pub fn method_value() -> ::std::string::String { - "tasks/result".to_string() + pub fn method_value() -> &'static str { + "tasks/result" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tasks/result".to_string() + pub fn method_name() -> &'static str { + "tasks/result" } } /**The response to a tasks/result request. @@ -3547,12 +3547,12 @@ impl GetTaskRequest { &self.method } /// returns "tasks/get" - pub fn method_value() -> ::std::string::String { - "tasks/get".to_string() + pub fn method_value() -> &'static str { + "tasks/get" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tasks/get".to_string() + pub fn method_name() -> &'static str { + "tasks/get" } } ///The response to a tasks/get request. @@ -3798,12 +3798,12 @@ impl ImageContent { &self.type_ } /// returns "image" - pub fn type_value() -> ::std::string::String { - "image".to_string() + pub fn type_value() -> &'static str { + "image" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "image".to_string() + pub fn type_name() -> &'static str { + "image" } } ///Describes the MCP implementation. @@ -4002,12 +4002,12 @@ impl InitializeRequest { &self.method } /// returns "initialize" - pub fn method_value() -> ::std::string::String { - "initialize".to_string() + pub fn method_value() -> &'static str { + "initialize" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "initialize".to_string() + pub fn method_name() -> &'static str { + "initialize" } } ///Parameters for an initialize request. @@ -4165,12 +4165,12 @@ impl InitializedNotification { &self.method } /// returns "notifications/initialized" - pub fn method_value() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_value() -> &'static str { + "notifications/initialized" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_name() -> &'static str { + "notifications/initialized" } } ///A response to a request that indicates an error occurred. @@ -4544,12 +4544,12 @@ impl LegacyTitledEnumSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///Sent from the client to request a list of prompts and prompt templates the server has. @@ -4610,12 +4610,12 @@ impl ListPromptsRequest { &self.method } /// returns "prompts/list" - pub fn method_value() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_value() -> &'static str { + "prompts/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_name() -> &'static str { + "prompts/list" } } ///The server's response to a prompts/list request from the client. @@ -4718,12 +4718,12 @@ impl ListResourceTemplatesRequest { &self.method } /// returns "resources/templates/list" - pub fn method_value() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_value() -> &'static str { + "resources/templates/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_name() -> &'static str { + "resources/templates/list" } } ///The server's response to a resources/templates/list request from the client. @@ -4827,12 +4827,12 @@ impl ListResourcesRequest { &self.method } /// returns "resources/list" - pub fn method_value() -> ::std::string::String { - "resources/list".to_string() + pub fn method_value() -> &'static str { + "resources/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/list".to_string() + pub fn method_name() -> &'static str { + "resources/list" } } ///The server's response to a resources/list request from the client. @@ -4940,12 +4940,12 @@ impl ListRootsRequest { &self.method } /// returns "roots/list" - pub fn method_value() -> ::std::string::String { - "roots/list".to_string() + pub fn method_value() -> &'static str { + "roots/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "roots/list".to_string() + pub fn method_name() -> &'static str { + "roots/list" } } /**The client's response to a roots/list request from the server. @@ -5042,12 +5042,12 @@ impl ListTasksRequest { &self.method } /// returns "tasks/list" - pub fn method_value() -> ::std::string::String { - "tasks/list".to_string() + pub fn method_value() -> &'static str { + "tasks/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tasks/list".to_string() + pub fn method_name() -> &'static str { + "tasks/list" } } ///The response to a tasks/list request. @@ -5150,12 +5150,12 @@ impl ListToolsRequest { &self.method } /// returns "tools/list" - pub fn method_value() -> ::std::string::String { - "tools/list".to_string() + pub fn method_value() -> &'static str { + "tools/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/list".to_string() + pub fn method_name() -> &'static str { + "tools/list" } } ///The server's response to a tools/list request from the client. @@ -5308,12 +5308,12 @@ impl LoggingMessageNotification { &self.method } /// returns "notifications/message" - pub fn method_value() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_value() -> &'static str { + "notifications/message" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_name() -> &'static str { + "notifications/message" } } ///Parameters for a notifications/message notification. @@ -5854,12 +5854,12 @@ impl PingRequest { &self.method } /// returns "ping" - pub fn method_value() -> ::std::string::String { - "ping".to_string() + pub fn method_value() -> &'static str { + "ping" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "ping".to_string() + pub fn method_name() -> &'static str { + "ping" } } /**Restricted schema definitions that only allow primitive types @@ -6003,12 +6003,12 @@ impl ProgressNotification { &self.method } /// returns "notifications/progress" - pub fn method_value() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_value() -> &'static str { + "notifications/progress" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_name() -> &'static str { + "notifications/progress" } } ///Parameters for a notifications/progress notification. @@ -6268,12 +6268,12 @@ impl PromptListChangedNotification { &self.method } /// returns "notifications/prompts/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/prompts/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/prompts/list_changed" } } /**Describes a message returned as part of a prompt. @@ -6361,12 +6361,12 @@ impl PromptReference { &self.type_ } /// returns "ref/prompt" - pub fn type_value() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_value() -> &'static str { + "ref/prompt" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_name() -> &'static str { + "ref/prompt" } } ///ReadResourceContent @@ -6486,12 +6486,12 @@ impl ReadResourceRequest { &self.method } /// returns "resources/read" - pub fn method_value() -> ::std::string::String { - "resources/read".to_string() + pub fn method_value() -> &'static str { + "resources/read" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/read".to_string() + pub fn method_name() -> &'static str { + "resources/read" } } ///Parameters for a resources/read request. @@ -6984,12 +6984,12 @@ impl ResourceLink { &self.type_ } /// returns "resource_link" - pub fn type_value() -> ::std::string::String { - "resource_link".to_string() + pub fn type_value() -> &'static str { + "resource_link" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "resource_link".to_string() + pub fn type_name() -> &'static str { + "resource_link" } } ///An optional notification from the server to the client, informing it that the list of resources it can read from has changed. This may be issued by servers without any previous subscription from the client. @@ -7044,12 +7044,12 @@ impl ResourceListChangedNotification { &self.method } /// returns "notifications/resources/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/list_changed" } } ///See [General fields: _meta](https://modelcontextprotocol.io/specification/2025-11-25/basic/index#meta) for notes on _meta usage. @@ -7252,12 +7252,12 @@ impl ResourceTemplateReference { &self.type_ } /// returns "ref/resource" - pub fn type_value() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_value() -> &'static str { + "ref/resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_name() -> &'static str { + "ref/resource" } } ///A notification from the server to the client, informing it that a resource has changed and may need to be read again. This should only be sent if the client previously sent a resources/subscribe request. @@ -7312,12 +7312,12 @@ impl ResourceUpdatedNotification { &self.method } /// returns "notifications/resources/updated" - pub fn method_value() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/updated" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/updated" } } ///Parameters for a notifications/resources/updated notification. @@ -7509,12 +7509,12 @@ impl RootsListChangedNotification { &self.method } /// returns "notifications/roots/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/roots/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/roots/list_changed" } } ///RpcError @@ -8192,11 +8192,11 @@ pub enum ServerResult { ListToolsResult(ListToolsResult), CallToolResult(CallToolResult), GetTaskResult(GetTaskResult), - GetTaskPayloadResult(GetTaskPayloadResult), CancelTaskResult(CancelTaskResult), ListTasksResult(ListTasksResult), CompleteResult(CompleteResult), Result(Result), + GetTaskPayloadResult(GetTaskPayloadResult), } impl ::std::convert::From for ServerResult { fn from(value: InitializeResult) -> Self { @@ -8243,11 +8243,6 @@ impl ::std::convert::From for ServerResult { Self::GetTaskResult(value) } } -impl ::std::convert::From for ServerResult { - fn from(value: GetTaskPayloadResult) -> Self { - Self::GetTaskPayloadResult(value) - } -} impl ::std::convert::From for ServerResult { fn from(value: CancelTaskResult) -> Self { Self::CancelTaskResult(value) @@ -8268,6 +8263,11 @@ impl ::std::convert::From for ServerResult { Self::Result(value) } } +impl ::std::convert::From for ServerResult { + fn from(value: GetTaskPayloadResult) -> Self { + Self::GetTaskPayloadResult(value) + } +} ///Specifies which request types can be augmented with tasks. /// ///
JSON schema @@ -8456,12 +8456,12 @@ impl SetLevelRequest { &self.method } /// returns "logging/setLevel" - pub fn method_value() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_value() -> &'static str { + "logging/setLevel" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_name() -> &'static str { + "logging/setLevel" } } ///Parameters for a logging/setLevel request. @@ -8618,12 +8618,12 @@ impl StringSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///StringSchemaFormat @@ -8747,12 +8747,12 @@ impl SubscribeRequest { &self.method } /// returns "resources/subscribe" - pub fn method_value() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_value() -> &'static str { + "resources/subscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_name() -> &'static str { + "resources/subscribe" } } ///Parameters for a resources/subscribe request. @@ -9050,12 +9050,12 @@ impl TaskStatusNotification { &self.method } /// returns "notifications/tasks/status" - pub fn method_value() -> ::std::string::String { - "notifications/tasks/status".to_string() + pub fn method_value() -> &'static str { + "notifications/tasks/status" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/tasks/status".to_string() + pub fn method_name() -> &'static str { + "notifications/tasks/status" } } ///Parameters for a notifications/tasks/status notification. @@ -9168,12 +9168,12 @@ impl TextContent { &self.type_ } /// returns "text" - pub fn type_value() -> ::std::string::String { - "text".to_string() + pub fn type_value() -> &'static str { + "text" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "text".to_string() + pub fn type_name() -> &'static str { + "text" } } ///TextResourceContents @@ -9341,12 +9341,12 @@ impl TitledMultiSelectEnumSchema { &self.type_ } /// returns "array" - pub fn type_value() -> ::std::string::String { - "array".to_string() + pub fn type_value() -> &'static str { + "array" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "array".to_string() + pub fn type_name() -> &'static str { + "array" } } ///Schema for array items with enum options and display labels. @@ -9514,12 +9514,12 @@ impl TitledSingleSelectEnumSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///TitledSingleSelectEnumSchemaOneOfItem @@ -9980,12 +9980,12 @@ impl ToolInputSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///An optional notification from the server to the client, informing it that the list of tools it offers has changed. This may be issued by servers without any previous subscription from the client. @@ -10040,12 +10040,12 @@ impl ToolListChangedNotification { &self.method } /// returns "notifications/tools/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/tools/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/tools/list_changed" } } /**An optional JSON Schema object defining the structure of the tool's output returned in @@ -10122,12 +10122,12 @@ impl ToolOutputSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///The result of a tool use, provided by the user back to the assistant. @@ -10229,12 +10229,12 @@ impl ToolResultContent { &self.type_ } /// returns "tool_result" - pub fn type_value() -> ::std::string::String { - "tool_result".to_string() + pub fn type_value() -> &'static str { + "tool_result" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "tool_result".to_string() + pub fn type_name() -> &'static str { + "tool_result" } } ///A request from the assistant to call a tool. @@ -10314,12 +10314,12 @@ impl ToolUseContent { &self.type_ } /// returns "tool_use" - pub fn type_value() -> ::std::string::String { - "tool_use".to_string() + pub fn type_value() -> &'static str { + "tool_use" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "tool_use".to_string() + pub fn type_name() -> &'static str { + "tool_use" } } ///See [General fields: _meta](https://modelcontextprotocol.io/specification/2025-11-25/basic/index#meta) for notes on _meta usage. @@ -10406,12 +10406,12 @@ impl UnsubscribeRequest { &self.method } /// returns "resources/unsubscribe" - pub fn method_value() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_value() -> &'static str { + "resources/unsubscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_name() -> &'static str { + "resources/unsubscribe" } } ///Parameters for a resources/unsubscribe request. @@ -10562,12 +10562,12 @@ impl UntitledMultiSelectEnumSchema { &self.type_ } /// returns "array" - pub fn type_value() -> ::std::string::String { - "array".to_string() + pub fn type_value() -> &'static str { + "array" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "array".to_string() + pub fn type_name() -> &'static str { + "array" } } ///Schema for the array items. @@ -10620,12 +10620,12 @@ impl UntitledMultiSelectEnumSchemaItems { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///Schema for single-selection enumeration without display titles for options. @@ -10704,12 +10704,12 @@ impl UntitledSingleSelectEnumSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///UrlElicitError @@ -11186,6 +11186,92 @@ impl ServerNotification { } } } +/// Converts any serializable struct into a `GenericResult`. +/// This is used internally to convert ServerResult and ClientResult variants +/// into GenericResult (Result) +fn into_result(value: T) -> GenericResult +where + T: serde::Serialize, +{ + let json_value = serde_json::to_value(value).unwrap_or(serde_json::Value::Null); + if let serde_json::Value::Object(mut map) = json_value { + let meta = map.remove("_meta").and_then(|v| match v { + serde_json::Value::Object(obj) => Some(obj), + _ => None, + }); + let extra = if map.is_empty() { None } else { Some(map) }; + GenericResult { meta, extra } + } else { + GenericResult { meta: None, extra: None } + } +} +impl From for GenericResult { + fn from(value: InitializeResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourcesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourceTemplatesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ReadResourceResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListPromptsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: GetPromptResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListToolsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CallToolResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: GetTaskResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: GetTaskPayloadResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CancelTaskResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListTasksResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CompleteResult) -> Self { + into_result(value) + } +} +/// Alias to avoid conflicts with Rust's standard `Result` type. +pub type GenericResult = Result; /// Deprecating the old auto-generated verbose names. /// These were renamed to clearer, shorter names in v0.8.0. /// The old names are deprecated but kept for backward-compatibility for a smooth migration period. diff --git a/src/generated_schema/2025_11_25/schema_utils.rs b/src/generated_schema/2025_11_25/schema_utils.rs index d04c6a3..a35796d 100644 --- a/src/generated_schema/2025_11_25/schema_utils.rs +++ b/src/generated_schema/2025_11_25/schema_utils.rs @@ -234,12 +234,15 @@ impl ClientMessage { /// Returns `true` if message is an `InitializeRequest`. pub fn is_initialize_request(&self) -> bool { - matches!(self, Self::Request(request) if request.request.is_initialize_request()) + matches!(self, Self::Request(ClientJsonrpcRequest::InitializeRequest(_))) } /// Returns `true` if the message is an `InitializedNotification` pub fn is_initialized_notification(&self) -> bool { - matches!(self, Self::Notification(notofication) if notofication.notification.is_initialized_notification()) + matches!( + self, + Self::Notification(ClientJsonrpcNotification::InitializedNotification(_)) + ) } } @@ -266,7 +269,26 @@ impl RpcMessage for ClientMessage { fn request_id(&self) -> Option<&RequestId> { match self { // If the message is a request, return the associated request ID - ClientMessage::Request(client_jsonrpc_request) => Some(&client_jsonrpc_request.id), + ClientMessage::Request(client_jsonrpc_request) => match client_jsonrpc_request { + ClientJsonrpcRequest::InitializeRequest(request) => Some(&request.id), + ClientJsonrpcRequest::PingRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListResourcesRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListResourceTemplatesRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ReadResourceRequest(request) => Some(&request.id), + ClientJsonrpcRequest::SubscribeRequest(request) => Some(&request.id), + ClientJsonrpcRequest::UnsubscribeRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListPromptsRequest(request) => Some(&request.id), + ClientJsonrpcRequest::GetPromptRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListToolsRequest(request) => Some(&request.id), + ClientJsonrpcRequest::CallToolRequest(request) => Some(&request.id), + ClientJsonrpcRequest::GetTaskRequest(request) => Some(&request.id), + ClientJsonrpcRequest::GetTaskPayloadRequest(request) => Some(&request.id), + ClientJsonrpcRequest::CancelTaskRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListTasksRequest(request) => Some(&request.id), + ClientJsonrpcRequest::SetLevelRequest(request) => Some(&request.id), + ClientJsonrpcRequest::CompleteRequest(request) => Some(&request.id), + ClientJsonrpcRequest::CustomRequest(request) => Some(&request.id), + }, // Notifications do not have request IDs ClientMessage::Notification(_) => None, // If the message is a response, return the associated request ID @@ -324,26 +346,136 @@ impl McpMessage for ClientMessage { //**************************// /// "Similar to JsonrpcRequest , but with the variants restricted to client-side requests." -#[derive(Clone, Debug)] -pub struct ClientJsonrpcRequest { - pub id: RequestId, - jsonrpc: ::std::string::String, - pub method: String, - pub request: RequestFromClient, +#[derive(Clone, Debug, ::serde::Serialize, ::serde::Deserialize)] +#[serde(untagged)] +pub enum ClientJsonrpcRequest { + InitializeRequest(InitializeRequest), + PingRequest(PingRequest), + ListResourcesRequest(ListResourcesRequest), + ListResourceTemplatesRequest(ListResourceTemplatesRequest), + ReadResourceRequest(ReadResourceRequest), + SubscribeRequest(SubscribeRequest), + UnsubscribeRequest(UnsubscribeRequest), + ListPromptsRequest(ListPromptsRequest), + GetPromptRequest(GetPromptRequest), + ListToolsRequest(ListToolsRequest), + CallToolRequest(CallToolRequest), + GetTaskRequest(GetTaskRequest), + GetTaskPayloadRequest(GetTaskPayloadRequest), + CancelTaskRequest(CancelTaskRequest), + ListTasksRequest(ListTasksRequest), + SetLevelRequest(SetLevelRequest), + CompleteRequest(CompleteRequest), + CustomRequest(JsonrpcRequest), } impl ClientJsonrpcRequest { pub fn new(id: RequestId, request: RequestFromClient) -> Self { - let method = request.method().to_string(); - Self { - id, - jsonrpc: JSONRPC_VERSION.to_string(), - method, - request, + match request { + RequestFromClient::InitializeRequest(params) => Self::InitializeRequest(InitializeRequest::new(id, params)), + RequestFromClient::PingRequest(params) => Self::PingRequest(PingRequest::new(id, params)), + RequestFromClient::ListResourcesRequest(params) => { + Self::ListResourcesRequest(ListResourcesRequest::new(id, params)) + } + RequestFromClient::ListResourceTemplatesRequest(params) => { + Self::ListResourceTemplatesRequest(ListResourceTemplatesRequest::new(id, params)) + } + RequestFromClient::ReadResourceRequest(params) => { + Self::ReadResourceRequest(ReadResourceRequest::new(id, params)) + } + RequestFromClient::SubscribeRequest(params) => Self::SubscribeRequest(SubscribeRequest::new(id, params)), + RequestFromClient::UnsubscribeRequest(params) => Self::UnsubscribeRequest(UnsubscribeRequest::new(id, params)), + RequestFromClient::ListPromptsRequest(params) => Self::ListPromptsRequest(ListPromptsRequest::new(id, params)), + RequestFromClient::GetPromptRequest(params) => Self::GetPromptRequest(GetPromptRequest::new(id, params)), + RequestFromClient::ListToolsRequest(params) => Self::ListToolsRequest(ListToolsRequest::new(id, params)), + RequestFromClient::CallToolRequest(params) => Self::CallToolRequest(CallToolRequest::new(id, params)), + RequestFromClient::GetTaskRequest(params) => Self::GetTaskRequest(GetTaskRequest::new(id, params)), + RequestFromClient::GetTaskPayloadRequest(params) => { + Self::GetTaskPayloadRequest(GetTaskPayloadRequest::new(id, params)) + } + RequestFromClient::CancelTaskRequest(params) => Self::CancelTaskRequest(CancelTaskRequest::new(id, params)), + RequestFromClient::ListTasksRequest(params) => Self::ListTasksRequest(ListTasksRequest::new(id, params)), + RequestFromClient::SetLevelRequest(params) => Self::SetLevelRequest(SetLevelRequest::new(id, params)), + RequestFromClient::CompleteRequest(params) => Self::CompleteRequest(CompleteRequest::new(id, params)), + RequestFromClient::CustomRequest(params) => { + Self::CustomRequest(JsonrpcRequest::new(id, params.method, params.params)) + } } } pub fn jsonrpc(&self) -> &::std::string::String { - &self.jsonrpc + match self { + ClientJsonrpcRequest::InitializeRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::PingRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListResourcesRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListResourceTemplatesRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ReadResourceRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::SubscribeRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::UnsubscribeRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListPromptsRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::GetPromptRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListToolsRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::CallToolRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::GetTaskRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::GetTaskPayloadRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::CancelTaskRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListTasksRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::SetLevelRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::CompleteRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::CustomRequest(request) => request.jsonrpc(), + } + } + + pub fn request_id(&self) -> &RequestId { + match self { + ClientJsonrpcRequest::InitializeRequest(request) => &request.id, + ClientJsonrpcRequest::PingRequest(request) => &request.id, + ClientJsonrpcRequest::ListResourcesRequest(request) => &request.id, + ClientJsonrpcRequest::ListResourceTemplatesRequest(request) => &request.id, + ClientJsonrpcRequest::ReadResourceRequest(request) => &request.id, + ClientJsonrpcRequest::SubscribeRequest(request) => &request.id, + ClientJsonrpcRequest::UnsubscribeRequest(request) => &request.id, + ClientJsonrpcRequest::ListPromptsRequest(request) => &request.id, + ClientJsonrpcRequest::GetPromptRequest(request) => &request.id, + ClientJsonrpcRequest::ListToolsRequest(request) => &request.id, + ClientJsonrpcRequest::CallToolRequest(request) => &request.id, + ClientJsonrpcRequest::GetTaskRequest(request) => &request.id, + ClientJsonrpcRequest::GetTaskPayloadRequest(request) => &request.id, + ClientJsonrpcRequest::CancelTaskRequest(request) => &request.id, + ClientJsonrpcRequest::ListTasksRequest(request) => &request.id, + ClientJsonrpcRequest::SetLevelRequest(request) => &request.id, + ClientJsonrpcRequest::CompleteRequest(request) => &request.id, + ClientJsonrpcRequest::CustomRequest(request) => &request.id, + } + } +} + +impl From for RequestFromClient { + fn from(request: ClientJsonrpcRequest) -> Self { + match request { + ClientJsonrpcRequest::InitializeRequest(request) => Self::InitializeRequest(request.params), + ClientJsonrpcRequest::PingRequest(request) => Self::PingRequest(request.params), + ClientJsonrpcRequest::ListResourcesRequest(request) => Self::ListResourcesRequest(request.params), + ClientJsonrpcRequest::ListResourceTemplatesRequest(request) => { + Self::ListResourceTemplatesRequest(request.params) + } + ClientJsonrpcRequest::ReadResourceRequest(request) => Self::ReadResourceRequest(request.params), + ClientJsonrpcRequest::SubscribeRequest(request) => Self::SubscribeRequest(request.params), + ClientJsonrpcRequest::UnsubscribeRequest(request) => Self::UnsubscribeRequest(request.params), + ClientJsonrpcRequest::ListPromptsRequest(request) => Self::ListPromptsRequest(request.params), + ClientJsonrpcRequest::GetPromptRequest(request) => Self::GetPromptRequest(request.params), + ClientJsonrpcRequest::ListToolsRequest(request) => Self::ListToolsRequest(request.params), + ClientJsonrpcRequest::CallToolRequest(request) => Self::CallToolRequest(request.params), + ClientJsonrpcRequest::GetTaskRequest(request) => Self::GetTaskRequest(request.params), + ClientJsonrpcRequest::GetTaskPayloadRequest(request) => Self::GetTaskPayloadRequest(request.params), + ClientJsonrpcRequest::CancelTaskRequest(request) => Self::CancelTaskRequest(request.params), + ClientJsonrpcRequest::ListTasksRequest(request) => Self::ListTasksRequest(request.params), + ClientJsonrpcRequest::SetLevelRequest(request) => Self::SetLevelRequest(request.params), + ClientJsonrpcRequest::CompleteRequest(request) => Self::CompleteRequest(request.params), + ClientJsonrpcRequest::CustomRequest(request) => Self::CustomRequest(CustomRequest { + method: request.method, + params: request.params, + }), + } } } @@ -395,88 +527,158 @@ impl FromStr for ClientJsonrpcRequest { /// To determine standard and custom request from the client side /// Custom requests are of type serde_json::Value and can be deserialized into any custom type. #[allow(clippy::large_enum_variant)] -#[derive(::serde::Serialize, Clone, Debug)] +#[derive(::serde::Serialize, ::serde::Deserialize, Clone, Debug)] #[serde(untagged)] pub enum RequestFromClient { - ClientRequest(ClientRequest), - CustomRequest(serde_json::Value), -} - -impl TryFrom for ClientRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> result::Result { - if let RequestFromClient::ClientRequest(client_request) = value { - Ok(client_request) - } else { - Err(RpcError::internal_error().with_message("Not a ClientRequest".to_string())) - } - } + InitializeRequest(InitializeRequestParams), + PingRequest(Option), + ListResourcesRequest(Option), + ListResourceTemplatesRequest(Option), + ReadResourceRequest(ReadResourceRequestParams), + SubscribeRequest(SubscribeRequestParams), + UnsubscribeRequest(UnsubscribeRequestParams), + ListPromptsRequest(Option), + GetPromptRequest(GetPromptRequestParams), + ListToolsRequest(Option), + CallToolRequest(CallToolRequestParams), + GetTaskRequest(GetTaskParams), + GetTaskPayloadRequest(GetTaskPayloadParams), + CancelTaskRequest(CancelTaskParams), + ListTasksRequest(Option), + SetLevelRequest(SetLevelRequestParams), + CompleteRequest(CompleteRequestParams), + CustomRequest(CustomRequest), } impl RequestFromClient { pub fn method(&self) -> &str { match self { - RequestFromClient::ClientRequest(request) => request.method(), - RequestFromClient::CustomRequest(request) => request["method"].as_str().unwrap(), + RequestFromClient::InitializeRequest(_request) => InitializeRequest::method_value(), + RequestFromClient::PingRequest(_request) => PingRequest::method_value(), + RequestFromClient::ListResourcesRequest(_request) => ListResourcesRequest::method_value(), + RequestFromClient::ListResourceTemplatesRequest(_request) => ListResourceTemplatesRequest::method_value(), + RequestFromClient::ReadResourceRequest(_request) => ReadResourceRequest::method_value(), + RequestFromClient::SubscribeRequest(_request) => SubscribeRequest::method_value(), + RequestFromClient::UnsubscribeRequest(_request) => UnsubscribeRequest::method_value(), + RequestFromClient::ListPromptsRequest(_request) => ListPromptsRequest::method_value(), + RequestFromClient::GetPromptRequest(_request) => GetPromptRequest::method_value(), + RequestFromClient::ListToolsRequest(_request) => ListToolsRequest::method_value(), + RequestFromClient::CallToolRequest(_request) => CallToolRequest::method_value(), + RequestFromClient::GetTaskRequest(_request) => GetTaskRequest::method_value(), + RequestFromClient::GetTaskPayloadRequest(_request) => GetTaskPayloadRequest::method_value(), + RequestFromClient::CancelTaskRequest(_request) => CancelTaskRequest::method_value(), + RequestFromClient::ListTasksRequest(_request) => ListTasksRequest::method_value(), + RequestFromClient::SetLevelRequest(_request) => SetLevelRequest::method_value(), + RequestFromClient::CompleteRequest(_request) => CompleteRequest::method_value(), + RequestFromClient::CustomRequest(request) => request.method.as_str(), } } /// Returns `true` if the request is an `InitializeRequest`. pub fn is_initialize_request(&self) -> bool { - matches!(self, RequestFromClient::ClientRequest(ClientRequest::InitializeRequest(_))) + matches!(self, RequestFromClient::InitializeRequest(_)) } } -impl From for RequestFromClient { - fn from(value: ClientRequest) -> Self { - Self::ClientRequest(value) - } -} +// impl From for RequestFromClient { +// fn from(value: ClientRequest) -> Self { +// Self::ClientRequest(value) +// } +// } -impl From for RequestFromClient { - fn from(value: serde_json::Value) -> Self { - Self::CustomRequest(value) - } -} +// impl From for RequestFromClient { +// fn from(value: serde_json::Value) -> Self { +// Self::CustomRequest(value) +// } +// } -impl<'de> serde::Deserialize<'de> for RequestFromClient { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; +// impl<'de> serde::Deserialize<'de> for RequestFromClient { +// fn deserialize(deserializer: D) -> core::result::Result +// where +// D: serde::Deserializer<'de>, +// { +// let raw_value = Value::deserialize(deserializer)?; - let client_result = ClientRequest::deserialize(&raw_value); +// let client_result = ClientRequest::deserialize(&raw_value); - match client_result { - Ok(client_request) => Ok(Self::ClientRequest(client_request)), - Err(_) => Ok(Self::CustomRequest(raw_value)), - } - } -} +// match client_result { +// Ok(client_request) => Ok(Self::ClientRequest(client_request)), +// Err(_) => Ok(Self::CustomRequest(raw_value)), +// } +// } +// } //*******************************// //** ClientJsonrpcNotification **// //*******************************// /// "Similar to JsonrpcNotification , but with the variants restricted to client-side notifications." -#[derive(Clone, Debug)] -pub struct ClientJsonrpcNotification { - jsonrpc: ::std::string::String, - pub method: ::std::string::String, - pub notification: NotificationFromClient, +#[derive(Clone, Debug, ::serde::Deserialize, ::serde::Serialize)] +#[serde(untagged)] +pub enum ClientJsonrpcNotification { + CancelledNotification(CancelledNotification), + InitializedNotification(InitializedNotification), + ProgressNotification(ProgressNotification), + TaskStatusNotification(TaskStatusNotification), + RootsListChangedNotification(RootsListChangedNotification), + CustomNotification(JsonrpcNotification), } impl ClientJsonrpcNotification { pub fn new(notification: NotificationFromClient) -> Self { - let method = notification.method().to_string(); - Self { - jsonrpc: JSONRPC_VERSION.to_string(), - method, - notification, + match notification { + NotificationFromClient::CancelledNotification(params) => { + Self::CancelledNotification(CancelledNotification::new(params)) + } + NotificationFromClient::InitializedNotification(params) => { + Self::InitializedNotification(InitializedNotification::new(params)) + } + NotificationFromClient::ProgressNotification(params) => { + Self::ProgressNotification(ProgressNotification::new(params)) + } + NotificationFromClient::TaskStatusNotification(params) => { + Self::TaskStatusNotification(TaskStatusNotification::new(params)) + } + NotificationFromClient::RootsListChangedNotification(params) => { + Self::RootsListChangedNotification(RootsListChangedNotification::new(params)) + } + NotificationFromClient::CustomNotification(params) => { + Self::CustomNotification(JsonrpcNotification::new(params.method, params.params)) + } } } pub fn jsonrpc(&self) -> &::std::string::String { - &self.jsonrpc + match self { + ClientJsonrpcNotification::CancelledNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::InitializedNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::ProgressNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::TaskStatusNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::RootsListChangedNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::CustomNotification(notification) => notification.jsonrpc(), + } + } +} + +impl From for NotificationFromClient { + fn from(notification: ClientJsonrpcNotification) -> Self { + match notification { + ClientJsonrpcNotification::CancelledNotification(notification) => { + Self::CancelledNotification(notification.params) + } + ClientJsonrpcNotification::InitializedNotification(notification) => { + Self::InitializedNotification(notification.params) + } + ClientJsonrpcNotification::ProgressNotification(notification) => Self::ProgressNotification(notification.params), + ClientJsonrpcNotification::TaskStatusNotification(notification) => { + Self::TaskStatusNotification(notification.params) + } + ClientJsonrpcNotification::RootsListChangedNotification(notification) => { + Self::RootsListChangedNotification(notification.params) + } + ClientJsonrpcNotification::CustomNotification(notification) => Self::CustomNotification(CustomNotification { + method: notification.method, + params: notification.params, + }), + } } } @@ -506,53 +708,45 @@ impl FromStr for ClientJsonrpcNotification { /// To determine standard and custom notifications received from the MCP Client /// Custom notifications are of type serde_json::Value and can be deserialized into any custom type. -#[derive(::serde::Serialize, Clone, Debug)] +#[derive(::serde::Serialize, ::serde::Deserialize, Clone, Debug)] #[serde(untagged)] pub enum NotificationFromClient { - ClientNotification(ClientNotification), - CustomNotification(serde_json::Value), -} - -impl TryFrom for ClientNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> result::Result { - if let NotificationFromClient::ClientNotification(client_notification) = value { - Ok(client_notification) - } else { - Err(RpcError::internal_error().with_message("Not a ClientNotification".to_string())) - } - } -} + CancelledNotification(CancelledNotificationParams), + InitializedNotification(Option), + ProgressNotification(ProgressNotificationParams), + TaskStatusNotification(TaskStatusNotificationParams), + RootsListChangedNotification(Option), + CustomNotification(CustomNotification), +} + +// impl TryFrom for ClientNotification { +// type Error = RpcError; +// fn try_from(value: NotificationFromClient) -> result::Result { +// if let NotificationFromClient::ClientNotification(client_notification) = value { +// Ok(client_notification) +// } else { +// Err(RpcError::internal_error().with_message("Not a ClientNotification".to_string())) +// } +// } +// } impl NotificationFromClient { /// Returns `true` if the message is an `InitializedNotification` pub fn is_initialized_notification(&self) -> bool { - matches!( - self, - NotificationFromClient::ClientNotification(ClientNotification::InitializedNotification(_)) - ) + matches!(self, NotificationFromClient::InitializedNotification(_)) } - fn method(&self) -> &str { + //TODO: 'static + pub fn method(&self) -> &str { match self { - NotificationFromClient::ClientNotification(notification) => notification.method(), - NotificationFromClient::CustomNotification(notification) => notification["method"].as_str().unwrap(), - } - } -} - -impl<'de> serde::Deserialize<'de> for NotificationFromClient { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let result = ClientNotification::deserialize(&raw_value); - - match result { - Ok(client_notification) => Ok(Self::ClientNotification(client_notification)), - Err(_) => Ok(Self::CustomNotification(raw_value)), + NotificationFromClient::CancelledNotification(_notification) => CancelledNotification::method_value(), + NotificationFromClient::InitializedNotification(_notification) => InitializedNotification::method_value(), + NotificationFromClient::ProgressNotification(_notification) => ProgressNotification::method_value(), + NotificationFromClient::TaskStatusNotification(_notification) => TaskStatusNotification::method_value(), + NotificationFromClient::RootsListChangedNotification(_notification) => { + RootsListChangedNotification::method_value() + } + NotificationFromClient::CustomNotification(notification) => notification.method.as_str(), } } } @@ -605,55 +799,7 @@ impl FromStr for ClientJsonrpcResponse { //** ResultFromClient **// //*******************************// -/// To determine standard and custom results from the client side -/// Custom results (CustomResult) are of type serde_json::Value and can be deserialized into any custom type. -#[allow(clippy::large_enum_variant)] -#[derive(::serde::Serialize, Clone, Debug)] -#[serde(untagged)] -pub enum ResultFromClient { - ClientResult(ClientResult), - /// **Deprecated**: Use `ClientResult::Result` with extra attributes instead. - CustomResult(serde_json::Value), -} - -impl TryFrom for ClientResult { - type Error = RpcError; - fn try_from(value: ResultFromClient) -> result::Result { - if let ResultFromClient::ClientResult(client_result) = value { - Ok(client_result) - } else { - Err(RpcError::internal_error().with_message("Not a ClientResult".to_string())) - } - } -} - -impl From for ResultFromClient { - fn from(value: ClientResult) -> Self { - Self::ClientResult(value) - } -} - -impl From for ResultFromClient { - fn from(value: serde_json::Value) -> Self { - Self::CustomResult(value) - } -} - -impl<'de> serde::Deserialize<'de> for ResultFromClient { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let result = ClientResult::deserialize(&raw_value); - - match result { - Ok(client_result) => Ok(Self::ClientResult(client_result)), - Err(_) => Ok(Self::CustomResult(raw_value)), - } - } -} +pub type ResultFromClient = ClientResult; //*******************************// //** ClientMessage **// @@ -802,7 +948,17 @@ impl RpcMessage for ServerMessage { fn request_id(&self) -> Option<&RequestId> { match self { // If the message is a request, return the associated request ID - ServerMessage::Request(server_jsonrpc_request) => Some(&server_jsonrpc_request.id), + ServerMessage::Request(server_jsonrpc_request) => match server_jsonrpc_request { + ServerJsonrpcRequest::PingRequest(request) => Some(&request.id), + ServerJsonrpcRequest::GetTaskRequest(request) => Some(&request.id), + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => Some(&request.id), + ServerJsonrpcRequest::CancelTaskRequest(request) => Some(&request.id), + ServerJsonrpcRequest::ListTasksRequest(request) => Some(&request.id), + ServerJsonrpcRequest::CreateMessageRequest(request) => Some(&request.id), + ServerJsonrpcRequest::ListRootsRequest(request) => Some(&request.id), + ServerJsonrpcRequest::ElicitRequest(request) => Some(&request.id), + ServerJsonrpcRequest::CustomRequest(request) => Some(&request.id), + }, // Notifications do not have request IDs ServerMessage::Notification(_) => None, // If the message is a response, return the associated request ID @@ -815,7 +971,18 @@ impl RpcMessage for ServerMessage { fn jsonrpc(&self) -> &str { match self { // If the message is a request, return the associated request ID - ServerMessage::Request(server_jsonrpc_request) => server_jsonrpc_request.jsonrpc(), + ServerMessage::Request(server_jsonrpc_request) => match server_jsonrpc_request { + ServerJsonrpcRequest::PingRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::GetTaskRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CancelTaskRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ListTasksRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CreateMessageRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ListRootsRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ElicitRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CustomRequest(request) => request.jsonrpc(), + }, + // Notifications do not have request IDs ServerMessage::Notification(notification) => notification.jsonrpc(), // If the message is a response, return the associated request ID @@ -883,27 +1050,58 @@ impl Display for ServerMessage { //**************************// /// "Similar to JsonrpcRequest , but with the variants restricted to client-side requests." -#[derive(Clone, Debug)] +#[derive(Clone, Debug, ::serde::Serialize, ::serde::Deserialize)] #[allow(clippy::large_enum_variant)] -pub struct ServerJsonrpcRequest { - pub id: RequestId, - jsonrpc: ::std::string::String, - pub method: String, - pub request: RequestFromServer, +#[serde(untagged)] +pub enum ServerJsonrpcRequest { + PingRequest(PingRequest), + GetTaskRequest(GetTaskRequest), + GetTaskPayloadRequest(GetTaskPayloadRequest), + CancelTaskRequest(CancelTaskRequest), + ListTasksRequest(ListTasksRequest), + CreateMessageRequest(CreateMessageRequest), + ListRootsRequest(ListRootsRequest), + ElicitRequest(ElicitRequest), + CustomRequest(JsonrpcRequest), } impl ServerJsonrpcRequest { - pub fn new(id: RequestId, request: RequestFromServer) -> Self { - let method = request.method().to_string(); - Self { - id, - jsonrpc: JSONRPC_VERSION.to_string(), - method, - request, + // pub fn new(id: RequestId, request: RequestFromServer) -> Self { + // let method = request.method().to_string(); + // Self { + // id, + // jsonrpc: JSONRPC_VERSION.to_string(), + // method, + // request, + // } + // } + + pub fn request_id(&self) -> &RequestId { + match self { + ServerJsonrpcRequest::PingRequest(request) => &request.id, + ServerJsonrpcRequest::GetTaskRequest(request) => &request.id, + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => &request.id, + ServerJsonrpcRequest::CancelTaskRequest(request) => &request.id, + ServerJsonrpcRequest::ListTasksRequest(request) => &request.id, + ServerJsonrpcRequest::CreateMessageRequest(request) => &request.id, + ServerJsonrpcRequest::ListRootsRequest(request) => &request.id, + ServerJsonrpcRequest::ElicitRequest(request) => &request.id, + ServerJsonrpcRequest::CustomRequest(request) => &request.id, } } + pub fn jsonrpc(&self) -> &::std::string::String { - &self.jsonrpc + match self { + ServerJsonrpcRequest::PingRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::GetTaskRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CancelTaskRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ListTasksRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CreateMessageRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ListRootsRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ElicitRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CustomRequest(request) => request.jsonrpc(), + } } } @@ -926,26 +1124,49 @@ impl FromStr for ServerJsonrpcRequest { .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() })))) } } + +#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)] +pub struct CustomRequest { + pub method: ::std::string::String, + #[serde(default, skip_serializing_if = "::std::option::Option::is_none")] + pub params: ::std::option::Option<::serde_json::Map<::std::string::String, ::serde_json::Value>>, +} + //*************************// //** Request From Server **// //*************************// /// To determine standard and custom request from the server side /// Custom requests are of type serde_json::Value and can be deserialized into any custom type. -#[derive(::serde::Serialize, Clone, Debug)] +#[derive(::serde::Serialize, ::serde::Deserialize, Clone, Debug)] #[serde(untagged)] pub enum RequestFromServer { - ServerRequest(ServerRequest), - CustomRequest(serde_json::Value), -} - -impl TryFrom for ServerRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> result::Result { - if let RequestFromServer::ServerRequest(server_request) = value { - Ok(server_request) - } else { - Err(RpcError::internal_error().with_message("Not a ServerRequest".to_string())) + PingRequest(Option), + GetTaskRequest(GetTaskParams), + GetTaskPayloadRequest(GetTaskPayloadParams), + CancelTaskRequest(CancelTaskParams), + ListTasksRequest(Option), + CreateMessageRequest(CreateMessageRequestParams), + ListRootsRequest(Option), + ElicitRequest(ElicitRequestParams), + CustomRequest(CustomRequest), +} + +impl From for RequestFromServer { + fn from(request: ServerJsonrpcRequest) -> Self { + match request { + ServerJsonrpcRequest::PingRequest(request) => Self::PingRequest(request.params), + ServerJsonrpcRequest::GetTaskRequest(request) => Self::GetTaskRequest(request.params), + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => Self::GetTaskPayloadRequest(request.params), + ServerJsonrpcRequest::CancelTaskRequest(request) => Self::CancelTaskRequest(request.params), + ServerJsonrpcRequest::ListTasksRequest(request) => Self::ListTasksRequest(request.params), + ServerJsonrpcRequest::CreateMessageRequest(request) => Self::CreateMessageRequest(request.params), + ServerJsonrpcRequest::ListRootsRequest(request) => Self::ListRootsRequest(request.params), + ServerJsonrpcRequest::ElicitRequest(request) => Self::ElicitRequest(request.params), + ServerJsonrpcRequest::CustomRequest(request) => Self::CustomRequest(CustomRequest { + method: request.method, + params: request.params, + }), } } } @@ -953,36 +1174,15 @@ impl TryFrom for ServerRequest { impl RequestFromServer { pub fn method(&self) -> &str { match self { - RequestFromServer::ServerRequest(request) => request.method(), - RequestFromServer::CustomRequest(request) => request["method"].as_str().unwrap(), - } - } -} - -impl From for RequestFromServer { - fn from(value: ServerRequest) -> Self { - Self::ServerRequest(value) - } -} - -impl From for RequestFromServer { - fn from(value: serde_json::Value) -> Self { - Self::CustomRequest(value) - } -} - -impl<'de> serde::Deserialize<'de> for RequestFromServer { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let server_result = ServerRequest::deserialize(&raw_value); - - match server_result { - Ok(server_request) => Ok(Self::ServerRequest(server_request)), - Err(_) => Ok(Self::CustomRequest(raw_value)), + RequestFromServer::PingRequest(_request) => PingRequest::method_value(), + RequestFromServer::GetTaskRequest(_request) => GetTaskRequest::method_value(), + RequestFromServer::GetTaskPayloadRequest(_request) => GetTaskPayloadRequest::method_value(), + RequestFromServer::CancelTaskRequest(_request) => CancelTaskRequest::method_value(), + RequestFromServer::ListTasksRequest(_request) => ListTasksRequest::method_value(), + RequestFromServer::CreateMessageRequest(_request) => CreateMessageRequest::method_value(), + RequestFromServer::ListRootsRequest(_request) => ListRootsRequest::method_value(), + RequestFromServer::ElicitRequest(_request) => ElicitRequest::method_value(), + RequestFromServer::CustomRequest(request) => request.method.as_str(), } } } @@ -992,24 +1192,106 @@ impl<'de> serde::Deserialize<'de> for RequestFromServer { //*******************************// /// "Similar to JsonrpcNotification , but with the variants restricted to server-side notifications." -#[derive(Clone, Debug)] -pub struct ServerJsonrpcNotification { - jsonrpc: ::std::string::String, - pub method: ::std::string::String, - pub notification: NotificationFromServer, +#[derive(Clone, Debug, ::serde::Deserialize, ::serde::Serialize)] +#[serde(untagged)] +pub enum ServerJsonrpcNotification { + CancelledNotification(CancelledNotification), + ProgressNotification(ProgressNotification), + ResourceListChangedNotification(ResourceListChangedNotification), + ResourceUpdatedNotification(ResourceUpdatedNotification), + PromptListChangedNotification(PromptListChangedNotification), + ToolListChangedNotification(ToolListChangedNotification), + TaskStatusNotification(TaskStatusNotification), + LoggingMessageNotification(LoggingMessageNotification), + ElicitationCompleteNotification(ElicitationCompleteNotification), + CustomNotification(JsonrpcNotification), +} + +impl From for NotificationFromServer { + fn from(notification: ServerJsonrpcNotification) -> Self { + match notification { + ServerJsonrpcNotification::CancelledNotification(notification) => { + Self::CancelledNotification(notification.params) + } + ServerJsonrpcNotification::ProgressNotification(notification) => Self::ProgressNotification(notification.params), + ServerJsonrpcNotification::ResourceListChangedNotification(notification) => { + Self::ResourceListChangedNotification(notification.params) + } + ServerJsonrpcNotification::ResourceUpdatedNotification(notification) => { + Self::ResourceUpdatedNotification(notification.params) + } + ServerJsonrpcNotification::PromptListChangedNotification(notification) => { + Self::PromptListChangedNotification(notification.params) + } + ServerJsonrpcNotification::ToolListChangedNotification(notification) => { + Self::ToolListChangedNotification(notification.params) + } + ServerJsonrpcNotification::TaskStatusNotification(notification) => { + Self::TaskStatusNotification(notification.params) + } + ServerJsonrpcNotification::LoggingMessageNotification(notification) => { + Self::LoggingMessageNotification(notification.params) + } + ServerJsonrpcNotification::ElicitationCompleteNotification(notification) => { + Self::ElicitationCompleteNotification(notification.params) + } + ServerJsonrpcNotification::CustomNotification(notification) => Self::CustomNotification(CustomNotification { + method: notification.method, + params: notification.params, + }), + } + } } +//TODO: check do we need from_message() or this impl ServerJsonrpcNotification { pub fn new(notification: NotificationFromServer) -> Self { - let method = notification.method().to_string(); - Self { - jsonrpc: JSONRPC_VERSION.to_string(), - method, - notification, + match notification { + NotificationFromServer::CancelledNotification(params) => { + Self::CancelledNotification(CancelledNotification::new(params)) + } + NotificationFromServer::ProgressNotification(params) => { + Self::ProgressNotification(ProgressNotification::new(params)) + } + NotificationFromServer::ResourceListChangedNotification(params) => { + Self::ResourceListChangedNotification(ResourceListChangedNotification::new(params)) + } + NotificationFromServer::ResourceUpdatedNotification(params) => { + Self::ResourceUpdatedNotification(ResourceUpdatedNotification::new(params)) + } + NotificationFromServer::PromptListChangedNotification(params) => { + Self::PromptListChangedNotification(PromptListChangedNotification::new(params)) + } + NotificationFromServer::ToolListChangedNotification(params) => { + Self::ToolListChangedNotification(ToolListChangedNotification::new(params)) + } + NotificationFromServer::TaskStatusNotification(params) => { + Self::TaskStatusNotification(TaskStatusNotification::new(params)) + } + NotificationFromServer::LoggingMessageNotification(params) => { + Self::LoggingMessageNotification(LoggingMessageNotification::new(params)) + } + NotificationFromServer::ElicitationCompleteNotification(params) => { + Self::ElicitationCompleteNotification(ElicitationCompleteNotification::new(params)) + } + NotificationFromServer::CustomNotification(params) => { + Self::CustomNotification(JsonrpcNotification::new(params.method, params.params)) + } } } pub fn jsonrpc(&self) -> &::std::string::String { - &self.jsonrpc + match self { + ServerJsonrpcNotification::CancelledNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ProgressNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ResourceListChangedNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ResourceUpdatedNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::PromptListChangedNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ToolListChangedNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::TaskStatusNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::LoggingMessageNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ElicitationCompleteNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::CustomNotification(notification) => notification.jsonrpc(), + } } } @@ -1038,45 +1320,34 @@ impl FromStr for ServerJsonrpcNotification { /// To determine standard and custom notifications received from the MCP Server /// Custom notifications are of type serde_json::Value and can be deserialized into any custom type. -#[derive(::serde::Serialize, Clone, Debug)] +#[derive(::serde::Serialize, ::serde::Deserialize, Clone, Debug)] #[serde(untagged)] pub enum NotificationFromServer { - ServerNotification(ServerNotification), - CustomNotification(serde_json::Value), -} - -impl TryFrom for ServerNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> result::Result { - if let NotificationFromServer::ServerNotification(server_notification) = value { - Ok(server_notification) - } else { - Err(RpcError::internal_error().with_message("Not a ServerNotification".to_string())) - } - } + CancelledNotification(CancelledNotificationParams), + ProgressNotification(ProgressNotificationParams), + ResourceListChangedNotification(Option), + ResourceUpdatedNotification(ResourceUpdatedNotificationParams), + PromptListChangedNotification(Option), + ToolListChangedNotification(Option), + TaskStatusNotification(TaskStatusNotificationParams), + LoggingMessageNotification(LoggingMessageNotificationParams), + ElicitationCompleteNotification(ElicitCompleteParams), + CustomNotification(CustomNotification), } impl NotificationFromServer { pub fn method(&self) -> &str { match self { - NotificationFromServer::ServerNotification(notification) => notification.method(), - NotificationFromServer::CustomNotification(notification) => notification["method"].as_str().unwrap(), - } - } -} - -impl<'de> serde::Deserialize<'de> for NotificationFromServer { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let result = ServerNotification::deserialize(&raw_value); - - match result { - Ok(client_notification) => Ok(Self::ServerNotification(client_notification)), - Err(_) => Ok(Self::CustomNotification(raw_value)), + NotificationFromServer::CancelledNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ProgressNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ResourceListChangedNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ResourceUpdatedNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::PromptListChangedNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ToolListChangedNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::TaskStatusNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::LoggingMessageNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ElicitationCompleteNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::CustomNotification(params) => params.method.as_str(), } } } @@ -1128,56 +1399,7 @@ impl FromStr for ServerJsonrpcResponse { //*******************************// //** ResultFromServer **// //*******************************// - -/// To determine standard and custom results from the server side -/// Custom results (CustomResult) are of type serde_json::Value and can be deserialized into any custom type. -#[allow(clippy::large_enum_variant)] -#[derive(::serde::Serialize, Clone, Debug)] -#[serde(untagged)] -pub enum ResultFromServer { - ServerResult(ServerResult), - /// **Deprecated**: Use `ServerResult::Result` with extra attributes instead. - CustomResult(serde_json::Value), -} - -impl TryFrom for ServerResult { - type Error = RpcError; - fn try_from(value: ResultFromServer) -> result::Result { - if let ResultFromServer::ServerResult(server_result) = value { - Ok(server_result) - } else { - Err(RpcError::internal_error().with_message("Not a ServerResult".to_string())) - } - } -} - -impl From for ResultFromServer { - fn from(value: ServerResult) -> Self { - Self::ServerResult(value) - } -} - -impl From for ResultFromServer { - fn from(value: serde_json::Value) -> Self { - Self::CustomResult(value) - } -} - -impl<'de> serde::Deserialize<'de> for ResultFromServer { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let result = ServerResult::deserialize(&raw_value); - - match result { - Ok(server_result) => Ok(Self::ServerResult(server_result)), - Err(_) => Ok(Self::CustomResult(raw_value)), - } - } -} +pub type ResultFromServer = ServerResult; //***************************// //** impl for JsonrpcErrorResponse **// @@ -1277,10 +1499,38 @@ impl FromMessage for ServerMessage { MessageFromServer::RequestFromServer(request_from_server) => { let request_id = request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new( - request_id, - request_from_server, - ))) + + let rpc_message = match request_from_server { + RequestFromServer::PingRequest(params) => { + ServerJsonrpcRequest::PingRequest(PingRequest::new(request_id, params)) + } + RequestFromServer::GetTaskRequest(params) => { + ServerJsonrpcRequest::GetTaskRequest(GetTaskRequest::new(request_id, params)) + } + RequestFromServer::GetTaskPayloadRequest(params) => { + ServerJsonrpcRequest::GetTaskPayloadRequest(GetTaskPayloadRequest::new(request_id, params)) + } + RequestFromServer::CancelTaskRequest(params) => { + ServerJsonrpcRequest::CancelTaskRequest(CancelTaskRequest::new(request_id, params)) + } + RequestFromServer::ListTasksRequest(params) => { + ServerJsonrpcRequest::ListTasksRequest(ListTasksRequest::new(request_id, params)) + } + RequestFromServer::CreateMessageRequest(params) => { + ServerJsonrpcRequest::CreateMessageRequest(CreateMessageRequest::new(request_id, params)) + } + RequestFromServer::ListRootsRequest(params) => { + ServerJsonrpcRequest::ListRootsRequest(ListRootsRequest::new(request_id, params)) + } + RequestFromServer::ElicitRequest(params) => { + ServerJsonrpcRequest::ElicitRequest(ElicitRequest::new(request_id, params)) + } + RequestFromServer::CustomRequest(params) => { + ServerJsonrpcRequest::CustomRequest(JsonrpcRequest::new(request_id, params.method, params.params)) + } + }; + + Ok(ServerMessage::Request(rpc_message)) } MessageFromServer::ResultFromServer(result_from_server) => { let request_id = @@ -1299,9 +1549,10 @@ impl FromMessage for ServerMessage { notification_from_server, ))) } - MessageFromServer::Error(jsonrpc_error_error) => { - Ok(ServerMessage::Error(JsonrpcErrorResponse::new(jsonrpc_error_error, request_id))) - } + MessageFromServer::Error(jsonrpc_error_error) => Ok(ServerMessage::Error(JsonrpcErrorResponse::new( + jsonrpc_error_error, + request_id, + ))), } } } @@ -1326,12 +1577,15 @@ pub enum MessageFromClient { impl MessageFromClient { /// Returns `true` if the message is an `InitializeRequest`. pub fn is_initialize_request(&self) -> bool { - matches!(self, Self::RequestFromClient(request) if request.is_initialize_request()) + matches!(self, Self::RequestFromClient(RequestFromClient::InitializeRequest(_))) } /// Returns `true` if the message is an `InitializedNotification` pub fn is_initialized_notification(&self) -> bool { - matches!(self, Self::NotificationFromClient(notofication) if notofication.is_initialized_notification()) + matches!( + self, + Self::NotificationFromClient(NotificationFromClient::InitializedNotification(_)) + ) } } @@ -1415,9 +1669,10 @@ impl FromMessage for ClientMessage { notification_from_client, ))) } - MessageFromClient::Error(jsonrpc_error_error) => { - Ok(ClientMessage::Error(JsonrpcErrorResponse::new(jsonrpc_error_error, request_id))) - } + MessageFromClient::Error(jsonrpc_error_error) => Ok(ClientMessage::Error(JsonrpcErrorResponse::new( + jsonrpc_error_error, + request_id, + ))), } } } @@ -1818,7 +2073,6 @@ impl FromStr for StringSchemaFormat { } } - // Helper: handle all single-select enum variants fn try_from_enum_schema(map: &serde_json::Map) -> result::Result { // All enum schemas should have type: "string" (or missing, but usually present) @@ -1948,403 +2202,41 @@ impl TryFrom<&serde_json::Map> for PrimitiveSchemaDefinition { } } -#[deprecated(since = "0.4.0", note = "This trait was renamed to RpcMessage. Use RpcMessage instead.")] -pub type RPCMessage = (); -#[deprecated(since = "0.4.0", note = "This trait was renamed to McpMessage. Use McpMessage instead.")] -pub type MCPMessage = (); +pub type CustomNotification = CustomRequest; /// BEGIN AUTO GENERATED -impl ::serde::Serialize for ClientJsonrpcRequest { +impl ::serde::Serialize for ServerJsonrpcResponse { fn serialize(&self, serializer: S) -> std::result::Result where S: ::serde::Serializer, { - let mut state = serializer.serialize_struct("JsonrpcRequest", 4)?; + let mut state = serializer.serialize_struct("JsonrpcResponse", 3)?; state.serialize_field("id", &self.id)?; state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("method", &self.method)?; - use ClientRequest::*; - match &self.request { - RequestFromClient::ClientRequest(message) => match message { - InitializeRequest(msg) => state.serialize_field("params", &msg.params)?, - PingRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ListResourcesRequest(msg) => state.serialize_field("params", &msg.params)?, - ListResourceTemplatesRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ReadResourceRequest(msg) => state.serialize_field("params", &msg.params)?, - SubscribeRequest(msg) => state.serialize_field("params", &msg.params)?, - UnsubscribeRequest(msg) => state.serialize_field("params", &msg.params)?, - ListPromptsRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - GetPromptRequest(msg) => state.serialize_field("params", &msg.params)?, - ListToolsRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - CallToolRequest(msg) => state.serialize_field("params", &msg.params)?, - GetTaskRequest(msg) => state.serialize_field("params", &msg.params)?, - GetTaskPayloadRequest(msg) => state.serialize_field("params", &msg.params)?, - CancelTaskRequest(msg) => state.serialize_field("params", &msg.params)?, - ListTasksRequest(msg) => state.serialize_field("params", &msg.params)?, - SetLevelRequest(msg) => state.serialize_field("params", &msg.params)?, - CompleteRequest(msg) => state.serialize_field("params", &msg.params)?, - }, - RequestFromClient::CustomRequest(value) => state.serialize_field("params", value)?, - } + state.serialize_field("result", &self.result)?; state.end() } } -impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcRequest { +impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcResponse { fn deserialize(deserializer: D) -> std::result::Result where D: ::serde::Deserializer<'de>, { use serde::de::{self, MapAccess, Visitor}; use std::fmt; - struct ClientJsonrpcRequestVisitor; - impl<'de> Visitor<'de> for ClientJsonrpcRequestVisitor { - type Value = ClientJsonrpcRequest; + struct ServerJsonrpcResultVisitor; + impl<'de> Visitor<'de> for ServerJsonrpcResultVisitor { + type Value = ServerJsonrpcResponse; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC request object") + formatter.write_str("a valid JSON-RPC response object") } - fn visit_map(self, mut map: M) -> std::result::Result + fn visit_map(self, mut map: M) -> std::result::Result where M: MapAccess<'de>, { let mut id: Option = None; let mut jsonrpc: Option = None; - let mut method: Option = None; - let mut params: Option = None; - while let Some(key) = map.next_key::()? { - match key.as_str() { - "id" => id = Some(map.next_value()?), - "jsonrpc" => jsonrpc = Some(map.next_value()?), - "method" => method = Some(map.next_value()?), - "params" => params = Some(map.next_value()?), - _ => { - return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"])); - } - } - } - let id = id.ok_or_else(|| de::Error::missing_field("id"))?; - let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?; - let method = method.ok_or_else(|| de::Error::missing_field("method"))?; - let params = params.unwrap_or_default(); - let req_object = json!({ "method" : method, "params" : params }); - let request = serde_json::from_value::(req_object).map_err(de::Error::custom)?; - Ok(ClientJsonrpcRequest { - id, - jsonrpc, - method, - request, - }) - } - } - deserializer.deserialize_struct( - "JsonrpcRequest", - &["id", "jsonrpc", "method", "params"], - ClientJsonrpcRequestVisitor, - ) - } -} -impl ::serde::Serialize for ServerJsonrpcRequest { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: ::serde::Serializer, - { - let mut state = serializer.serialize_struct("JsonrpcRequest", 4)?; - state.serialize_field("id", &self.id)?; - state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("method", &self.method)?; - use ServerRequest::*; - match &self.request { - RequestFromServer::ServerRequest(message) => match message { - PingRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - GetTaskRequest(msg) => state.serialize_field("params", &msg.params)?, - GetTaskPayloadRequest(msg) => state.serialize_field("params", &msg.params)?, - CancelTaskRequest(msg) => state.serialize_field("params", &msg.params)?, - ListTasksRequest(msg) => state.serialize_field("params", &msg.params)?, - CreateMessageRequest(msg) => state.serialize_field("params", &msg.params)?, - ListRootsRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ElicitRequest(msg) => state.serialize_field("params", &msg.params)?, - }, - RequestFromServer::CustomRequest(value) => state.serialize_field("params", value)?, - } - state.end() - } -} -impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcRequest { - fn deserialize(deserializer: D) -> std::result::Result - where - D: ::serde::Deserializer<'de>, - { - use serde::de::{self, MapAccess, Visitor}; - use std::fmt; - struct ServerJsonrpcRequestVisitor; - impl<'de> Visitor<'de> for ServerJsonrpcRequestVisitor { - type Value = ServerJsonrpcRequest; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC request object") - } - fn visit_map(self, mut map: M) -> std::result::Result - where - M: MapAccess<'de>, - { - let mut id: Option = None; - let mut jsonrpc: Option = None; - let mut method: Option = None; - let mut params: Option = None; - while let Some(key) = map.next_key::()? { - match key.as_str() { - "id" => id = Some(map.next_value()?), - "jsonrpc" => jsonrpc = Some(map.next_value()?), - "method" => method = Some(map.next_value()?), - "params" => params = Some(map.next_value()?), - _ => { - return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"])); - } - } - } - let id = id.ok_or_else(|| de::Error::missing_field("id"))?; - let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?; - let method = method.ok_or_else(|| de::Error::missing_field("method"))?; - let params = params.unwrap_or_default(); - let req_object = json!({ "method" : method, "params" : params }); - let request = serde_json::from_value::(req_object).map_err(de::Error::custom)?; - Ok(ServerJsonrpcRequest { - id, - jsonrpc, - method, - request, - }) - } - } - deserializer.deserialize_struct( - "JsonrpcRequest", - &["id", "jsonrpc", "method", "params"], - ServerJsonrpcRequestVisitor, - ) - } -} -impl ::serde::Serialize for ClientJsonrpcNotification { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: ::serde::Serializer, - { - let mut state = serializer.serialize_struct("JsonrpcNotification", 3)?; - state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("method", &self.method)?; - use ClientNotification::*; - match &self.notification { - NotificationFromClient::ClientNotification(message) => match message { - CancelledNotification(msg) => state.serialize_field("params", &msg.params)?, - InitializedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ProgressNotification(msg) => state.serialize_field("params", &msg.params)?, - TaskStatusNotification(msg) => state.serialize_field("params", &msg.params)?, - RootsListChangedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - }, - NotificationFromClient::CustomNotification(value) => state.serialize_field("params", value)?, - } - state.end() - } -} -impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcNotification { - fn deserialize(deserializer: D) -> std::result::Result - where - D: ::serde::Deserializer<'de>, - { - use serde::de::{self, MapAccess, Visitor}; - use std::fmt; - struct ClientJsonrpcNotificationVisitor; - impl<'de> Visitor<'de> for ClientJsonrpcNotificationVisitor { - type Value = ClientJsonrpcNotification; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC notification object") - } - fn visit_map(self, mut map: M) -> std::result::Result - where - M: MapAccess<'de>, - { - let mut jsonrpc: Option = None; - let mut method: Option = None; - let mut params: Option = None; - while let Some(key) = map.next_key::()? { - match key.as_str() { - "jsonrpc" => jsonrpc = Some(map.next_value()?), - "method" => method = Some(map.next_value()?), - "params" => params = Some(map.next_value()?), - _ => { - return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"])); - } - } - } - let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?; - let method = method.ok_or_else(|| de::Error::missing_field("method"))?; - let params = params.unwrap_or_default(); - let req_object = json!({ "method" : method, "params" : params }); - let notification = - serde_json::from_value::(req_object).map_err(de::Error::custom)?; - Ok(ClientJsonrpcNotification { - jsonrpc, - method, - notification, - }) - } - } - deserializer.deserialize_struct( - "JsonrpcRequest", - &["jsonrpc", "method", "params"], - ClientJsonrpcNotificationVisitor, - ) - } -} -impl ::serde::Serialize for ServerJsonrpcNotification { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: ::serde::Serializer, - { - let mut state = serializer.serialize_struct("JsonrpcNotification", 3)?; - state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("method", &self.method)?; - use ServerNotification::*; - match &self.notification { - NotificationFromServer::ServerNotification(message) => match message { - CancelledNotification(msg) => state.serialize_field("params", &msg.params)?, - ProgressNotification(msg) => state.serialize_field("params", &msg.params)?, - ResourceListChangedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ResourceUpdatedNotification(msg) => state.serialize_field("params", &msg.params)?, - PromptListChangedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ToolListChangedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - TaskStatusNotification(msg) => state.serialize_field("params", &msg.params)?, - LoggingMessageNotification(msg) => state.serialize_field("params", &msg.params)?, - ElicitationCompleteNotification(msg) => state.serialize_field("params", &msg.params)?, - }, - NotificationFromServer::CustomNotification(value) => state.serialize_field("params", value)?, - } - state.end() - } -} -impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcNotification { - fn deserialize(deserializer: D) -> std::result::Result - where - D: ::serde::Deserializer<'de>, - { - use serde::de::{self, MapAccess, Visitor}; - use std::fmt; - struct ServerJsonrpcNotificationVisitor; - impl<'de> Visitor<'de> for ServerJsonrpcNotificationVisitor { - type Value = ServerJsonrpcNotification; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC notification object") - } - fn visit_map(self, mut map: M) -> std::result::Result - where - M: MapAccess<'de>, - { - let mut jsonrpc: Option = None; - let mut method: Option = None; - let mut params: Option = None; - while let Some(key) = map.next_key::()? { - match key.as_str() { - "jsonrpc" => jsonrpc = Some(map.next_value()?), - "method" => method = Some(map.next_value()?), - "params" => params = Some(map.next_value()?), - _ => { - return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"])); - } - } - } - let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?; - let method = method.ok_or_else(|| de::Error::missing_field("method"))?; - let params = params.unwrap_or_default(); - let req_object = json!({ "method" : method, "params" : params }); - let notification = - serde_json::from_value::(req_object).map_err(de::Error::custom)?; - Ok(ServerJsonrpcNotification { - jsonrpc, - method, - notification, - }) - } - } - deserializer.deserialize_struct( - "JsonrpcRequest", - &["jsonrpc", "method", "params"], - ServerJsonrpcNotificationVisitor, - ) - } -} -impl ::serde::Serialize for ServerJsonrpcResponse { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: ::serde::Serializer, - { - let mut state = serializer.serialize_struct("JsonrpcResponse", 3)?; - state.serialize_field("id", &self.id)?; - state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("result", &self.result)?; - state.end() - } -} -impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcResponse { - fn deserialize(deserializer: D) -> std::result::Result - where - D: ::serde::Deserializer<'de>, - { - use serde::de::{self, MapAccess, Visitor}; - use std::fmt; - struct ServerJsonrpcResultVisitor; - impl<'de> Visitor<'de> for ServerJsonrpcResultVisitor { - type Value = ServerJsonrpcResponse; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC response object") - } - fn visit_map(self, mut map: M) -> std::result::Result - where - M: MapAccess<'de>, - { - let mut id: Option = None; - let mut jsonrpc: Option = None; - let mut result: Option = None; + let mut result: Option = None; while let Some(key) = map.next_key::()? { match key.as_str() { "id" => id = Some(map.next_value()?), @@ -2417,417 +2309,92 @@ impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcResponse { deserializer.deserialize_struct("JsonrpcResponse", &["id", "jsonrpc", "result"], ClientJsonrpcResultVisitor) } } -impl From for RequestFromClient { - fn from(value: InitializeRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: PingRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: ListResourcesRequest) -> Self { - Self::ClientRequest(value.into()) - } +/// Enum representing SDK error codes. +#[allow(non_camel_case_types)] +pub enum SdkErrorCodes { + CONNECTION_CLOSED = -32000, + REQUEST_TIMEOUT = -32001, + RESOURCE_NOT_FOUND = -32002, + BAD_REQUEST = -32015, + SESSION_NOT_FOUND = -32016, + INVALID_REQUEST = -32600, + METHOD_NOT_FOUND = -32601, + INVALID_PARAMS = -32602, + INTERNAL_ERROR = -32603, + PARSE_ERROR = -32700, + URL_ELICITATION_REQUIRED = -32042, } -impl From for RequestFromClient { - fn from(value: ListResourceTemplatesRequest) -> Self { - Self::ClientRequest(value.into()) +impl core::fmt::Display for SdkErrorCodes { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + SdkErrorCodes::CONNECTION_CLOSED => write!(f, "Connection closed"), + SdkErrorCodes::REQUEST_TIMEOUT => write!(f, "Request timeout"), + SdkErrorCodes::INVALID_REQUEST => write!(f, "Invalid request"), + SdkErrorCodes::METHOD_NOT_FOUND => write!(f, "Method not found"), + SdkErrorCodes::INVALID_PARAMS => write!(f, "Invalid params"), + SdkErrorCodes::INTERNAL_ERROR => write!(f, "Internal error"), + SdkErrorCodes::PARSE_ERROR => write!(f, "Parse Error"), + SdkErrorCodes::RESOURCE_NOT_FOUND => write!(f, "Resource not found"), + SdkErrorCodes::BAD_REQUEST => write!(f, "Bad request"), + SdkErrorCodes::SESSION_NOT_FOUND => write!(f, "Session not found"), + SdkErrorCodes::URL_ELICITATION_REQUIRED => { + write!( + f, + "A required URL was not provided. Please supply the requested URL to continue." + ) + } + } } } -impl From for RequestFromClient { - fn from(value: ReadResourceRequest) -> Self { - Self::ClientRequest(value.into()) +impl From for i64 { + fn from(code: SdkErrorCodes) -> Self { + code as i64 } } -impl From for RequestFromClient { - fn from(value: SubscribeRequest) -> Self { - Self::ClientRequest(value.into()) - } +#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)] +pub struct SdkError { + ///The error type that occurred. + pub code: i64, + ///Additional information about the error. + pub data: ::std::option::Option<::serde_json::Value>, + ///A short description of the error. + pub message: ::std::string::String, } -impl From for RequestFromClient { - fn from(value: UnsubscribeRequest) -> Self { - Self::ClientRequest(value.into()) +impl core::fmt::Display for SdkError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "MCP error {}: {}", self.code, self.message) } } -impl From for RequestFromClient { - fn from(value: ListPromptsRequest) -> Self { - Self::ClientRequest(value.into()) +impl std::error::Error for SdkError { + fn description(&self) -> &str { + &self.message } } -impl From for RequestFromClient { - fn from(value: GetPromptRequest) -> Self { - Self::ClientRequest(value.into()) +impl SdkError { + pub fn new( + error_code: SdkErrorCodes, + message: ::std::string::String, + data: ::std::option::Option<::serde_json::Value>, + ) -> Self { + Self { + code: error_code.into(), + data, + message, + } } -} -impl From for RequestFromClient { - fn from(value: ListToolsRequest) -> Self { - Self::ClientRequest(value.into()) + pub fn connection_closed() -> Self { + Self { + code: SdkErrorCodes::CONNECTION_CLOSED.into(), + data: None, + message: SdkErrorCodes::CONNECTION_CLOSED.to_string(), + } } -} -impl From for RequestFromClient { - fn from(value: CallToolRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: GetTaskRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: GetTaskPayloadRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: CancelTaskRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: ListTasksRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: SetLevelRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: CompleteRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: InitializeRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: PingRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListResourcesRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListResourceTemplatesRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ReadResourceRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: SubscribeRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: UnsubscribeRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListPromptsRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetPromptRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListToolsRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CallToolRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetTaskRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetTaskPayloadRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CancelTaskRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListTasksRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: SetLevelRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CompleteRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: CancelledNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: InitializedNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: ProgressNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: TaskStatusNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: RootsListChangedNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: CancelledNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: InitializedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: ProgressNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: TaskStatusNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: RootsListChangedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CancelledNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: InitializedNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ProgressNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: TaskStatusNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: RootsListChangedNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: Result) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: GetTaskResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: GetTaskPayloadResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: CancelTaskResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: ListTasksResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: CreateMessageResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: ListRootsResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: ElicitResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: Result) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetTaskResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetTaskPayloadResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CancelTaskResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListTasksResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CreateMessageResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListRootsResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ElicitResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -/// Enum representing SDK error codes. -#[allow(non_camel_case_types)] -pub enum SdkErrorCodes { - CONNECTION_CLOSED = -32000, - REQUEST_TIMEOUT = -32001, - RESOURCE_NOT_FOUND = -32002, - BAD_REQUEST = -32015, - SESSION_NOT_FOUND = -32016, - INVALID_REQUEST = -32600, - METHOD_NOT_FOUND = -32601, - INVALID_PARAMS = -32602, - INTERNAL_ERROR = -32603, - PARSE_ERROR = -32700, - URL_ELICITATION_REQUIRED = -32042, -} -impl core::fmt::Display for SdkErrorCodes { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - SdkErrorCodes::CONNECTION_CLOSED => write!(f, "Connection closed"), - SdkErrorCodes::REQUEST_TIMEOUT => write!(f, "Request timeout"), - SdkErrorCodes::INVALID_REQUEST => write!(f, "Invalid request"), - SdkErrorCodes::METHOD_NOT_FOUND => write!(f, "Method not found"), - SdkErrorCodes::INVALID_PARAMS => write!(f, "Invalid params"), - SdkErrorCodes::INTERNAL_ERROR => write!(f, "Internal error"), - SdkErrorCodes::PARSE_ERROR => write!(f, "Parse Error"), - SdkErrorCodes::RESOURCE_NOT_FOUND => write!(f, "Resource not found"), - SdkErrorCodes::BAD_REQUEST => write!(f, "Bad request"), - SdkErrorCodes::SESSION_NOT_FOUND => write!(f, "Session not found"), - SdkErrorCodes::URL_ELICITATION_REQUIRED => { - write!( - f, - "A required URL was not provided. Please supply the requested URL to continue." - ) - } - } - } -} -impl From for i64 { - fn from(code: SdkErrorCodes) -> Self { - code as i64 - } -} -#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)] -pub struct SdkError { - ///The error type that occurred. - pub code: i64, - ///Additional information about the error. - pub data: ::std::option::Option<::serde_json::Value>, - ///A short description of the error. - pub message: ::std::string::String, -} -impl core::fmt::Display for SdkError { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "MCP error {}: {}", self.code, self.message) - } -} -impl std::error::Error for SdkError { - fn description(&self) -> &str { - &self.message - } -} -impl SdkError { - pub fn new( - error_code: SdkErrorCodes, - message: ::std::string::String, - data: ::std::option::Option<::serde_json::Value>, - ) -> Self { - Self { - code: error_code.into(), - data, - message, - } - } - pub fn connection_closed() -> Self { - Self { - code: SdkErrorCodes::CONNECTION_CLOSED.into(), - data: None, - message: SdkErrorCodes::CONNECTION_CLOSED.to_string(), - } - } - pub fn request_timeout(timeout: u128) -> Self { - Self { - code: SdkErrorCodes::REQUEST_TIMEOUT.into(), - data: Some(json!({ "timeout" : timeout })), - message: SdkErrorCodes::REQUEST_TIMEOUT.to_string(), - } + pub fn request_timeout(timeout: u128) -> Self { + Self { + code: SdkErrorCodes::REQUEST_TIMEOUT.into(), + data: Some(json!({ "timeout" : timeout })), + message: SdkErrorCodes::REQUEST_TIMEOUT.to_string(), + } } pub fn session_not_found() -> Self { Self { @@ -3017,1528 +2584,117 @@ impl RpcError { /// ``` pub fn invalid_request() -> Self { Self { - code: RpcErrorCodes::INVALID_REQUEST.into(), - data: None, - message: "Invalid request".to_string(), - } - } - /// Creates a new `RpcError` for "Internal error". - /// - /// # Example - /// ``` - /// use rust_mcp_schema::RpcError; - /// - /// let error = RpcError::internal_error(); - /// assert_eq!(error.code, -32603); - /// ``` - pub fn internal_error() -> Self { - Self { - code: RpcErrorCodes::INTERNAL_ERROR.into(), - data: None, - message: "Internal error".to_string(), - } - } - /// Creates a new `RpcError` for "Parse error". - /// - /// # Example - /// ``` - /// use rust_mcp_schema::RpcError; - /// - /// let error = RpcError::parse_error(); - /// assert_eq!(error.code, -32700); - /// ``` - pub fn parse_error() -> Self { - Self { - code: RpcErrorCodes::PARSE_ERROR.into(), - data: None, - message: "Parse error".to_string(), - } - } - /// Sets a custom error message. - /// - /// # Example - /// ``` - /// use rust_mcp_schema::RpcError; - /// - /// let error = RpcError::invalid_request().with_message("Request format is invalid".to_string()); - /// assert_eq!(error.message, "Request format is invalid".to_string()); - /// ``` - pub fn with_message(mut self, message: String) -> Self { - self.message = message; - self - } - /// Attaches optional data to the error. - /// - /// # Example - /// ``` - /// use serde_json::json; - /// use rust_mcp_schema::RpcError; - /// - /// let error = RpcError::invalid_request().with_data(Some(json!({"reason": "Missing ID"}))); - /// assert!(error.data.is_some()); - /// ``` - pub fn with_data(mut self, data: ::std::option::Option<::serde_json::Value>) -> Self { - self.data = data; - self - } -} -impl std::error::Error for RpcError { - fn description(&self) -> &str { - &self.message - } -} -impl Display for RpcError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {err}")) - ) - } -} -impl FromStr for RpcError { - type Err = RpcError; - fn from_str(s: &str) -> std::result::Result { - serde_json::from_str(s) - .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() })))) - } -} -///Constructs a new `JsonrpcErrorResponse` using the provided arguments. -impl JsonrpcErrorResponse { - pub fn create( - id: Option, - error_code: RpcErrorCodes, - error_message: ::std::string::String, - error_data: ::std::option::Option<::serde_json::Value>, - ) -> Self { - Self::new(RpcError::new(error_code, error_message, error_data), id) - } -} -impl From for NotificationFromServer { - fn from(value: CancelledNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ProgressNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ResourceListChangedNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ResourceUpdatedNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: PromptListChangedNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ToolListChangedNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: TaskStatusNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: LoggingMessageNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ElicitationCompleteNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: CancelledNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ProgressNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ResourceListChangedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ResourceUpdatedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: PromptListChangedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ToolListChangedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: TaskStatusNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: LoggingMessageNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ElicitationCompleteNotification) -> Self { - Self::new(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CancelledNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ProgressNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ResourceListChangedNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ResourceUpdatedNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: PromptListChangedNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ToolListChangedNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: TaskStatusNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: LoggingMessageNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ElicitationCompleteNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: PingRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: GetTaskRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: GetTaskPayloadRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: CancelTaskRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: ListTasksRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: CreateMessageRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: ListRootsRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: ElicitRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: PingRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetTaskRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetTaskPayloadRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CancelTaskRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListTasksRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CreateMessageRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListRootsRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ElicitRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: Result) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: InitializeResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListResourcesResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListResourceTemplatesResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ReadResourceResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListPromptsResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: GetPromptResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListToolsResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: CallToolResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: GetTaskResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: GetTaskPayloadResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: CancelTaskResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListTasksResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: CompleteResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: Result) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: InitializeResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListResourcesResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListResourceTemplatesResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ReadResourceResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListPromptsResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetPromptResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListToolsResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CallToolResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetTaskResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetTaskPayloadResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CancelTaskResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListTasksResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CompleteResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: InitializeRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for InitializeRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: PingRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for PingRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListResourcesRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListResourcesRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message( - message: ListResourceTemplatesRequest, - request_id: Option, - ) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListResourceTemplatesRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ReadResourceRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ReadResourceRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: SubscribeRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for SubscribeRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: UnsubscribeRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for UnsubscribeRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListPromptsRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListPromptsRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetPromptRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetPromptRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListToolsRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListToolsRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CallToolRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CallToolRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetTaskRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetTaskRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetTaskPayloadRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetTaskPayloadRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CancelTaskRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CancelTaskRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListTasksRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListTasksRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: SetLevelRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for SetLevelRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CompleteRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CompleteRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: Result, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for Result { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetTaskResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetTaskResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetTaskPayloadResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetTaskPayloadResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CancelTaskResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CancelTaskResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListTasksResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListTasksResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CreateMessageResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CreateMessageResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListRootsResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListRootsResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ElicitResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ElicitResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CancelledNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for CancelledNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: InitializedNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for InitializedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ProgressNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ProgressNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: TaskStatusNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for TaskStatusNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message( - message: RootsListChangedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for RootsListChangedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: PingRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for PingRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetTaskRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetTaskRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetTaskPayloadRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetTaskPayloadRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CancelTaskRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CancelTaskRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListTasksRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListTasksRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CreateMessageRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CreateMessageRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListRootsRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListRootsRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ElicitRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ElicitRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: Result, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for Result { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: InitializeResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for InitializeResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListResourcesResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListResourcesResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ListResourceTemplatesResult, - request_id: Option, - ) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListResourceTemplatesResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ReadResourceResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ReadResourceResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListPromptsResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListPromptsResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetPromptResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetPromptResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListToolsResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListToolsResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CallToolResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CallToolResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetTaskResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetTaskResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetTaskPayloadResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetTaskPayloadResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CancelTaskResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CancelTaskResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListTasksResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListTasksResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CompleteResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CompleteResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CancelledNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for CancelledNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ProgressNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ProgressNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ResourceListChangedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ResourceListChangedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ResourceUpdatedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ResourceUpdatedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: PromptListChangedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for PromptListChangedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ToolListChangedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ToolListChangedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: TaskStatusNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for TaskStatusNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: LoggingMessageNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for LoggingMessageNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ElicitationCompleteNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ElicitationCompleteNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl TryFrom for InitializeRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::InitializeRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a InitializeRequest".to_string())) - } - } -} -impl TryFrom for PingRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::PingRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a PingRequest".to_string())) - } - } -} -impl TryFrom for ListResourcesRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListResourcesRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListResourcesRequest".to_string())) - } - } -} -impl TryFrom for ListResourceTemplatesRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListResourceTemplatesRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListResourceTemplatesRequest".to_string())) - } - } -} -impl TryFrom for ReadResourceRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ReadResourceRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ReadResourceRequest".to_string())) - } - } -} -impl TryFrom for SubscribeRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::SubscribeRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a SubscribeRequest".to_string())) - } - } -} -impl TryFrom for UnsubscribeRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::UnsubscribeRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a UnsubscribeRequest".to_string())) - } - } -} -impl TryFrom for ListPromptsRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListPromptsRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListPromptsRequest".to_string())) - } - } -} -impl TryFrom for GetPromptRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::GetPromptRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetPromptRequest".to_string())) - } - } -} -impl TryFrom for ListToolsRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListToolsRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListToolsRequest".to_string())) + code: RpcErrorCodes::INVALID_REQUEST.into(), + data: None, + message: "Invalid request".to_string(), } } -} -impl TryFrom for CallToolRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::CallToolRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CallToolRequest".to_string())) + /// Creates a new `RpcError` for "Internal error". + /// + /// # Example + /// ``` + /// use rust_mcp_schema::RpcError; + /// + /// let error = RpcError::internal_error(); + /// assert_eq!(error.code, -32603); + /// ``` + pub fn internal_error() -> Self { + Self { + code: RpcErrorCodes::INTERNAL_ERROR.into(), + data: None, + message: "Internal error".to_string(), } } -} -impl TryFrom for GetTaskRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::GetTaskRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetTaskRequest".to_string())) + /// Creates a new `RpcError` for "Parse error". + /// + /// # Example + /// ``` + /// use rust_mcp_schema::RpcError; + /// + /// let error = RpcError::parse_error(); + /// assert_eq!(error.code, -32700); + /// ``` + pub fn parse_error() -> Self { + Self { + code: RpcErrorCodes::PARSE_ERROR.into(), + data: None, + message: "Parse error".to_string(), } } -} -impl TryFrom for GetTaskPayloadRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::GetTaskPayloadRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetTaskPayloadRequest".to_string())) - } + /// Sets a custom error message. + /// + /// # Example + /// ``` + /// use rust_mcp_schema::RpcError; + /// + /// let error = RpcError::invalid_request().with_message("Request format is invalid".to_string()); + /// assert_eq!(error.message, "Request format is invalid".to_string()); + /// ``` + pub fn with_message(mut self, message: String) -> Self { + self.message = message; + self + } + /// Attaches optional data to the error. + /// + /// # Example + /// ``` + /// use serde_json::json; + /// use rust_mcp_schema::RpcError; + /// + /// let error = RpcError::invalid_request().with_data(Some(json!({"reason": "Missing ID"}))); + /// assert!(error.data.is_some()); + /// ``` + pub fn with_data(mut self, data: ::std::option::Option<::serde_json::Value>) -> Self { + self.data = data; + self } } -impl TryFrom for CancelTaskRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::CancelTaskRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CancelTaskRequest".to_string())) - } +impl std::error::Error for RpcError { + fn description(&self) -> &str { + &self.message } } -impl TryFrom for ListTasksRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListTasksRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListTasksRequest".to_string())) - } +impl Display for RpcError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {err}")) + ) } } -impl TryFrom for SetLevelRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::SetLevelRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a SetLevelRequest".to_string())) - } +impl FromStr for RpcError { + type Err = RpcError; + fn from_str(s: &str) -> std::result::Result { + serde_json::from_str(s) + .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() })))) } } -impl TryFrom for CompleteRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::CompleteRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CompleteRequest".to_string())) - } +///Constructs a new `JsonrpcErrorResponse` using the provided arguments. +impl JsonrpcErrorResponse { + pub fn create( + id: Option, + error_code: RpcErrorCodes, + error_message: ::std::string::String, + error_data: ::std::option::Option<::serde_json::Value>, + ) -> Self { + Self::new(RpcError::new(error_code, error_message, error_data), id) } } -impl TryFrom for Result { +impl TryFrom for GenericResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::Result(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a Result".to_string())) + match value { + ClientResult::GetTaskPayloadResult(result) => Ok(result.into()), + ClientResult::Result(result) => Ok(result), + _ => Err(RpcError::internal_error().with_message("Not a Result".to_string())), } } } impl TryFrom for GetTaskResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::GetTaskResult(result) = matched_type { + if let ClientResult::GetTaskResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetTaskResult".to_string())) @@ -4548,8 +2704,7 @@ impl TryFrom for GetTaskResult { impl TryFrom for GetTaskPayloadResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::GetTaskPayloadResult(result) = matched_type { + if let ClientResult::GetTaskPayloadResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetTaskPayloadResult".to_string())) @@ -4559,8 +2714,7 @@ impl TryFrom for GetTaskPayloadResult { impl TryFrom for CancelTaskResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::CancelTaskResult(result) = matched_type { + if let ClientResult::CancelTaskResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CancelTaskResult".to_string())) @@ -4570,8 +2724,7 @@ impl TryFrom for CancelTaskResult { impl TryFrom for ListTasksResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::ListTasksResult(result) = matched_type { + if let ClientResult::ListTasksResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListTasksResult".to_string())) @@ -4581,8 +2734,7 @@ impl TryFrom for ListTasksResult { impl TryFrom for CreateMessageResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::CreateMessageResult(result) = matched_type { + if let ClientResult::CreateMessageResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CreateMessageResult".to_string())) @@ -4592,8 +2744,7 @@ impl TryFrom for CreateMessageResult { impl TryFrom for ListRootsResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::ListRootsResult(result) = matched_type { + if let ClientResult::ListRootsResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListRootsResult".to_string())) @@ -4603,173 +2754,27 @@ impl TryFrom for ListRootsResult { impl TryFrom for ElicitResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::ElicitResult(result) = matched_type { + if let ClientResult::ElicitResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ElicitResult".to_string())) } } } -impl TryFrom for CancelledNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::CancelledNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CancelledNotification".to_string())) - } - } -} -impl TryFrom for InitializedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::InitializedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a InitializedNotification".to_string())) - } - } -} -impl TryFrom for ProgressNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::ProgressNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ProgressNotification".to_string())) - } - } -} -impl TryFrom for TaskStatusNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::TaskStatusNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a TaskStatusNotification".to_string())) - } - } -} -impl TryFrom for RootsListChangedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::RootsListChangedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a RootsListChangedNotification".to_string())) - } - } -} -impl TryFrom for PingRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::PingRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a PingRequest".to_string())) - } - } -} -impl TryFrom for GetTaskRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::GetTaskRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetTaskRequest".to_string())) - } - } -} -impl TryFrom for GetTaskPayloadRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::GetTaskPayloadRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetTaskPayloadRequest".to_string())) - } - } -} -impl TryFrom for CancelTaskRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::CancelTaskRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CancelTaskRequest".to_string())) - } - } -} -impl TryFrom for ListTasksRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::ListTasksRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListTasksRequest".to_string())) - } - } -} -impl TryFrom for CreateMessageRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::CreateMessageRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CreateMessageRequest".to_string())) - } - } -} -impl TryFrom for ListRootsRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::ListRootsRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListRootsRequest".to_string())) - } - } -} -impl TryFrom for ElicitRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::ElicitRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ElicitRequest".to_string())) - } - } -} -impl TryFrom for Result { +impl TryFrom for GenericResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::Result(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a Result".to_string())) + match value { + ServerResult::GetTaskPayloadResult(result) => Ok(result.into()), + ServerResult::Result(result) => Ok(result), + _ => Err(RpcError::internal_error().with_message("Not a Result".to_string())), } } } impl TryFrom for InitializeResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::InitializeResult(result) = matched_type { + if let ServerResult::InitializeResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a InitializeResult".to_string())) @@ -4779,8 +2784,7 @@ impl TryFrom for InitializeResult { impl TryFrom for ListResourcesResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListResourcesResult(result) = matched_type { + if let ServerResult::ListResourcesResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListResourcesResult".to_string())) @@ -4790,8 +2794,7 @@ impl TryFrom for ListResourcesResult { impl TryFrom for ListResourceTemplatesResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListResourceTemplatesResult(result) = matched_type { + if let ServerResult::ListResourceTemplatesResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListResourceTemplatesResult".to_string())) @@ -4801,8 +2804,7 @@ impl TryFrom for ListResourceTemplatesResult { impl TryFrom for ReadResourceResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ReadResourceResult(result) = matched_type { + if let ServerResult::ReadResourceResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ReadResourceResult".to_string())) @@ -4812,8 +2814,7 @@ impl TryFrom for ReadResourceResult { impl TryFrom for ListPromptsResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListPromptsResult(result) = matched_type { + if let ServerResult::ListPromptsResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListPromptsResult".to_string())) @@ -4823,8 +2824,7 @@ impl TryFrom for ListPromptsResult { impl TryFrom for GetPromptResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::GetPromptResult(result) = matched_type { + if let ServerResult::GetPromptResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetPromptResult".to_string())) @@ -4834,8 +2834,7 @@ impl TryFrom for GetPromptResult { impl TryFrom for ListToolsResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListToolsResult(result) = matched_type { + if let ServerResult::ListToolsResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListToolsResult".to_string())) @@ -4845,8 +2844,7 @@ impl TryFrom for ListToolsResult { impl TryFrom for CallToolResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::CallToolResult(result) = matched_type { + if let ServerResult::CallToolResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CallToolResult".to_string())) @@ -4856,8 +2854,7 @@ impl TryFrom for CallToolResult { impl TryFrom for GetTaskResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::GetTaskResult(result) = matched_type { + if let ServerResult::GetTaskResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetTaskResult".to_string())) @@ -4867,8 +2864,7 @@ impl TryFrom for GetTaskResult { impl TryFrom for GetTaskPayloadResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::GetTaskPayloadResult(result) = matched_type { + if let ServerResult::GetTaskPayloadResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetTaskPayloadResult".to_string())) @@ -4878,8 +2874,7 @@ impl TryFrom for GetTaskPayloadResult { impl TryFrom for CancelTaskResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::CancelTaskResult(result) = matched_type { + if let ServerResult::CancelTaskResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CancelTaskResult".to_string())) @@ -4889,8 +2884,7 @@ impl TryFrom for CancelTaskResult { impl TryFrom for ListTasksResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListTasksResult(result) = matched_type { + if let ServerResult::ListTasksResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListTasksResult".to_string())) @@ -4900,113 +2894,13 @@ impl TryFrom for ListTasksResult { impl TryFrom for CompleteResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::CompleteResult(result) = matched_type { + if let ServerResult::CompleteResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CompleteResult".to_string())) } } } -impl TryFrom for CancelledNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::CancelledNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CancelledNotification".to_string())) - } - } -} -impl TryFrom for ProgressNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ProgressNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ProgressNotification".to_string())) - } - } -} -impl TryFrom for ResourceListChangedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ResourceListChangedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ResourceListChangedNotification".to_string())) - } - } -} -impl TryFrom for ResourceUpdatedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ResourceUpdatedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ResourceUpdatedNotification".to_string())) - } - } -} -impl TryFrom for PromptListChangedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::PromptListChangedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a PromptListChangedNotification".to_string())) - } - } -} -impl TryFrom for ToolListChangedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ToolListChangedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ToolListChangedNotification".to_string())) - } - } -} -impl TryFrom for TaskStatusNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::TaskStatusNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a TaskStatusNotification".to_string())) - } - } -} -impl TryFrom for LoggingMessageNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::LoggingMessageNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a LoggingMessageNotification".to_string())) - } - } -} -impl TryFrom for ElicitationCompleteNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ElicitationCompleteNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ElicitationCompleteNotification".to_string())) - } - } -} impl ContentBlock { ///Create a ContentBlock::TextContent pub fn text_content(text: ::std::string::String) -> Self { @@ -5158,74 +3052,111 @@ impl CallToolResult { self } } +impl ServerRequest { + pub fn request_id(&self) -> &RequestId { + match self { + ServerRequest::PingRequest(request) => &request.id, + ServerRequest::GetTaskRequest(request) => &request.id, + ServerRequest::GetTaskPayloadRequest(request) => &request.id, + ServerRequest::CancelTaskRequest(request) => &request.id, + ServerRequest::ListTasksRequest(request) => &request.id, + ServerRequest::CreateMessageRequest(request) => &request.id, + ServerRequest::ListRootsRequest(request) => &request.id, + ServerRequest::ElicitRequest(request) => &request.id, + } + } +} +impl ClientRequest { + pub fn request_id(&self) -> &RequestId { + match self { + ClientRequest::InitializeRequest(request) => &request.id, + ClientRequest::PingRequest(request) => &request.id, + ClientRequest::ListResourcesRequest(request) => &request.id, + ClientRequest::ListResourceTemplatesRequest(request) => &request.id, + ClientRequest::ReadResourceRequest(request) => &request.id, + ClientRequest::SubscribeRequest(request) => &request.id, + ClientRequest::UnsubscribeRequest(request) => &request.id, + ClientRequest::ListPromptsRequest(request) => &request.id, + ClientRequest::GetPromptRequest(request) => &request.id, + ClientRequest::ListToolsRequest(request) => &request.id, + ClientRequest::CallToolRequest(request) => &request.id, + ClientRequest::GetTaskRequest(request) => &request.id, + ClientRequest::GetTaskPayloadRequest(request) => &request.id, + ClientRequest::CancelTaskRequest(request) => &request.id, + ClientRequest::ListTasksRequest(request) => &request.id, + ClientRequest::SetLevelRequest(request) => &request.id, + ClientRequest::CompleteRequest(request) => &request.id, + } + } +} /// END AUTO GENERATED #[cfg(test)] mod tests { - // use super::*; - // use serde_json::json; - - // #[test] - // fn test_detect_message_type() { - // // standard request - // let message = ClientJsonrpcRequest::new(RequestId::Integer(0), PingRequest::new(None).into()); - // let result = detect_message_type(&json!(message)); - // assert!(matches!(result, MessageTypes::Request)); - - // // custom request - - // let result = detect_message_type(&json!({ - // "id":0, - // "method":"add_numbers", - // "params":{}, - // "jsonrpc":"2.0" - // })); - // assert!(matches!(result, MessageTypes::Request)); - - // // standard notification - // let message = ClientJsonrpcNotification::new(RootsListChangedNotification::new(None).into()); - // let result = detect_message_type(&json!(message)); - // assert!(matches!(result, MessageTypes::Notification)); - - // // custom notification - // let result = detect_message_type(&json!({ - // "method":"notifications/email_sent", - // "jsonrpc":"2.0" - // })); - // assert!(matches!(result, MessageTypes::Notification)); - - // // standard response - // let message = ClientJsonrpcResponse::new( - // RequestId::Integer(0), - // ListRootsResult { - // meta: None, - // roots: vec![], - // } - // .into(), - // ); - // let result = detect_message_type(&json!(message)); - // assert!(matches!(result, MessageTypes::Response)); - - // //custom response - - // let result = detect_message_type(&json!({ - // "id":1, - // "jsonrpc":"2.0", - // "result":"{}", - // })); - // assert!(matches!(result, MessageTypes::Response)); - - // // error message - // let message = JsonrpcErrorResponse::create( - // RequestId::Integer(0), - // RpcErrorCodes::INVALID_PARAMS, - // "Invalid params!".to_string(), - // None, - // ); - // let result = detect_message_type(&json!(message)); - // assert!(matches!(result, MessageTypes::Error)); - - // // default - // let result = detect_message_type(&json!({})); - // assert!(matches!(result, MessageTypes::Request)); - // } + use super::*; + use serde_json::json; + + #[test] + fn test_detect_message_type() { + // standard request + let message = ClientJsonrpcRequest::new(RequestId::Integer(0), RequestFromClient::PingRequest(None)); + let result = detect_message_type(&json!(message)); + assert!(matches!(result, MessageTypes::Request)); + + // custom request + + let result = detect_message_type(&json!({ + "id":0, + "method":"add_numbers", + "params":{}, + "jsonrpc":"2.0" + })); + assert!(matches!(result, MessageTypes::Request)); + + // standard notification + let message = ClientJsonrpcNotification::new(NotificationFromClient::RootsListChangedNotification(None)); + let result = detect_message_type(&json!(message)); + assert!(matches!(result, MessageTypes::Notification)); + + // custom notification + let result = detect_message_type(&json!({ + "method":"notifications/email_sent", + "jsonrpc":"2.0" + })); + assert!(matches!(result, MessageTypes::Notification)); + + // standard response + let message = ClientJsonrpcResponse::new( + RequestId::Integer(0), + ListRootsResult { + meta: None, + roots: vec![], + } + .into(), + ); + let result = detect_message_type(&json!(message)); + assert!(matches!(result, MessageTypes::Response)); + + //custom response + + let result = detect_message_type(&json!({ + "id":1, + "jsonrpc":"2.0", + "result":"{}", + })); + assert!(matches!(result, MessageTypes::Response)); + + // error message + let message = JsonrpcErrorResponse::create( + Some(RequestId::Integer(0)), + RpcErrorCodes::INVALID_PARAMS, + "Invalid params!".to_string(), + None, + ); + let result = detect_message_type(&json!(message)); + assert!(matches!(result, MessageTypes::Error)); + + // default + let result = detect_message_type(&json!({})); + assert!(matches!(result, MessageTypes::Request)); + } } diff --git a/src/generated_schema/draft/mcp_schema.rs b/src/generated_schema/draft/mcp_schema.rs index e245962..3aaa0e9 100644 --- a/src/generated_schema/draft/mcp_schema.rs +++ b/src/generated_schema/draft/mcp_schema.rs @@ -7,13 +7,13 @@ /// modify or extend the implementations as needed, but please do so at your own risk. /// /// Generated from : -/// Hash : 391c69f42f21bab3a5df0923b45b9ad3174414b9 -/// Generated at : 2025-12-01 18:52:56 +/// Hash : 77572f31dae0644acd1c537133bc49846c0abdb8 +/// Generated at : 2025-12-13 16:34:20 /// ---------------------------------------------------------------------------- /// use super::validators as validate; /// MCP Protocol Version -pub const LATEST_PROTOCOL_VERSION: &str = "DRAFT-2025-v3"; +pub const LATEST_PROTOCOL_VERSION: &str = "DRAFT-2026-v1"; /// JSON-RPC Version pub const JSONRPC_VERSION: &str = "2.0"; /// Parse error. Invalid JSON was received. An error occurred while parsing the JSON text. @@ -148,12 +148,12 @@ impl AudioContent { &self.type_ } /// returns "audio" - pub fn type_value() -> ::std::string::String { - "audio".to_string() + pub fn type_value() -> &'static str { + "audio" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "audio".to_string() + pub fn type_name() -> &'static str { + "audio" } } ///Base interface for metadata with name (identifier) and title (display name) properties. @@ -296,12 +296,12 @@ impl BooleanSchema { &self.type_ } /// returns "boolean" - pub fn type_value() -> ::std::string::String { - "boolean".to_string() + pub fn type_value() -> &'static str { + "boolean" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "boolean".to_string() + pub fn type_name() -> &'static str { + "boolean" } } ///See [General fields: _meta](https://modelcontextprotocol.io/specification/draft/basic/index#meta) for notes on _meta usage. @@ -388,12 +388,12 @@ impl CallToolRequest { &self.method } /// returns "tools/call" - pub fn method_value() -> ::std::string::String { - "tools/call".to_string() + pub fn method_value() -> &'static str { + "tools/call" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/call".to_string() + pub fn method_name() -> &'static str { + "tools/call" } } ///Parameters for a tools/call request. @@ -608,12 +608,12 @@ impl CancelTaskRequest { &self.method } /// returns "tasks/cancel" - pub fn method_value() -> ::std::string::String { - "tasks/cancel".to_string() + pub fn method_value() -> &'static str { + "tasks/cancel" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tasks/cancel".to_string() + pub fn method_name() -> &'static str { + "tasks/cancel" } } ///The response to a tasks/cancel request. @@ -710,12 +710,12 @@ impl CancelledNotification { &self.method } /// returns "notifications/cancelled" - pub fn method_value() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_value() -> &'static str { + "notifications/cancelled" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/cancelled".to_string() + pub fn method_name() -> &'static str { + "notifications/cancelled" } } ///Parameters for a notifications/cancelled notification. @@ -1177,24 +1177,19 @@ impl ::std::convert::From for ClientRequest { #[serde(untagged)] pub enum ClientResult { GetTaskResult(GetTaskResult), - GetTaskPayloadResult(GetTaskPayloadResult), CancelTaskResult(CancelTaskResult), ListTasksResult(ListTasksResult), CreateMessageResult(CreateMessageResult), ListRootsResult(ListRootsResult), ElicitResult(ElicitResult), Result(Result), + GetTaskPayloadResult(GetTaskPayloadResult), } impl ::std::convert::From for ClientResult { fn from(value: GetTaskResult) -> Self { Self::GetTaskResult(value) } } -impl ::std::convert::From for ClientResult { - fn from(value: GetTaskPayloadResult) -> Self { - Self::GetTaskPayloadResult(value) - } -} impl ::std::convert::From for ClientResult { fn from(value: CancelTaskResult) -> Self { Self::CancelTaskResult(value) @@ -1225,6 +1220,11 @@ impl ::std::convert::From for ClientResult { Self::Result(value) } } +impl ::std::convert::From for ClientResult { + fn from(value: GetTaskPayloadResult) -> Self { + Self::GetTaskPayloadResult(value) + } +} ///Present if the client supports listing roots. /// ///
JSON schema @@ -1491,12 +1491,12 @@ impl CompleteRequest { &self.method } /// returns "completion/complete" - pub fn method_value() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_value() -> &'static str { + "completion/complete" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "completion/complete".to_string() + pub fn method_name() -> &'static str { + "completion/complete" } } ///The argument's information @@ -1977,12 +1977,12 @@ impl CreateMessageRequest { &self.method } /// returns "sampling/createMessage" - pub fn method_value() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_value() -> &'static str { + "sampling/createMessage" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "sampling/createMessage".to_string() + pub fn method_name() -> &'static str { + "sampling/createMessage" } } ///Parameters for a sampling/createMessage request. @@ -2358,12 +2358,12 @@ impl ElicitFormSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///A request from the server to elicit additional information from the user via the client. @@ -2424,12 +2424,12 @@ impl ElicitRequest { &self.method } /// returns "elicitation/create" - pub fn method_value() -> ::std::string::String { - "elicitation/create".to_string() + pub fn method_value() -> &'static str { + "elicitation/create" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "elicitation/create".to_string() + pub fn method_name() -> &'static str { + "elicitation/create" } } ///The parameters for a request to elicit non-sensitive information from the user via a form in the client. @@ -2544,12 +2544,12 @@ impl ElicitRequestFormParams { &self.mode } /// returns "form" - pub fn mode_value() -> ::std::string::String { - "form".to_string() + pub fn mode_value() -> &'static str { + "form" } #[deprecated(since = "0.8.0", note = "Use `mode_value()` instead.")] - pub fn mode_name() -> ::std::string::String { - "form".to_string() + pub fn mode_name() -> &'static str { + "form" } } ///The parameters for a request to elicit additional information from the user via the client. @@ -2682,12 +2682,12 @@ impl ElicitRequestUrlParams { &self.mode } /// returns "url" - pub fn mode_value() -> ::std::string::String { - "url".to_string() + pub fn mode_value() -> &'static str { + "url" } #[deprecated(since = "0.8.0", note = "Use `mode_value()` instead.")] - pub fn mode_name() -> ::std::string::String { - "url".to_string() + pub fn mode_name() -> &'static str { + "url" } } ///The client's response to an elicitation request. @@ -2952,12 +2952,12 @@ impl ElicitationCompleteNotification { &self.method } /// returns "notifications/elicitation/complete" - pub fn method_value() -> ::std::string::String { - "notifications/elicitation/complete".to_string() + pub fn method_value() -> &'static str { + "notifications/elicitation/complete" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/elicitation/complete".to_string() + pub fn method_name() -> &'static str { + "notifications/elicitation/complete" } } /**The contents of a resource, embedded into a prompt or tool call result. @@ -3031,12 +3031,12 @@ impl EmbeddedResource { &self.type_ } /// returns "resource" - pub fn type_value() -> ::std::string::String { - "resource".to_string() + pub fn type_value() -> &'static str { + "resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "resource".to_string() + pub fn type_name() -> &'static str { + "resource" } } ///EmbeddedResourceResource @@ -3229,12 +3229,12 @@ impl GetPromptRequest { &self.method } /// returns "prompts/get" - pub fn method_value() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_value() -> &'static str { + "prompts/get" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/get".to_string() + pub fn method_name() -> &'static str { + "prompts/get" } } ///Parameters for a prompts/get request. @@ -3443,12 +3443,12 @@ impl GetTaskPayloadRequest { &self.method } /// returns "tasks/result" - pub fn method_value() -> ::std::string::String { - "tasks/result".to_string() + pub fn method_value() -> &'static str { + "tasks/result" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tasks/result".to_string() + pub fn method_name() -> &'static str { + "tasks/result" } } /**The response to a tasks/result request. @@ -3547,12 +3547,12 @@ impl GetTaskRequest { &self.method } /// returns "tasks/get" - pub fn method_value() -> ::std::string::String { - "tasks/get".to_string() + pub fn method_value() -> &'static str { + "tasks/get" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tasks/get".to_string() + pub fn method_name() -> &'static str { + "tasks/get" } } ///The response to a tasks/get request. @@ -3798,12 +3798,12 @@ impl ImageContent { &self.type_ } /// returns "image" - pub fn type_value() -> ::std::string::String { - "image".to_string() + pub fn type_value() -> &'static str { + "image" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "image".to_string() + pub fn type_name() -> &'static str { + "image" } } ///Describes the MCP implementation. @@ -4002,12 +4002,12 @@ impl InitializeRequest { &self.method } /// returns "initialize" - pub fn method_value() -> ::std::string::String { - "initialize".to_string() + pub fn method_value() -> &'static str { + "initialize" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "initialize".to_string() + pub fn method_name() -> &'static str { + "initialize" } } ///Parameters for an initialize request. @@ -4165,12 +4165,12 @@ impl InitializedNotification { &self.method } /// returns "notifications/initialized" - pub fn method_value() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_value() -> &'static str { + "notifications/initialized" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/initialized".to_string() + pub fn method_name() -> &'static str { + "notifications/initialized" } } ///A response to a request that indicates an error occurred. @@ -4544,12 +4544,12 @@ impl LegacyTitledEnumSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///Sent from the client to request a list of prompts and prompt templates the server has. @@ -4610,12 +4610,12 @@ impl ListPromptsRequest { &self.method } /// returns "prompts/list" - pub fn method_value() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_value() -> &'static str { + "prompts/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "prompts/list".to_string() + pub fn method_name() -> &'static str { + "prompts/list" } } ///The server's response to a prompts/list request from the client. @@ -4718,12 +4718,12 @@ impl ListResourceTemplatesRequest { &self.method } /// returns "resources/templates/list" - pub fn method_value() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_value() -> &'static str { + "resources/templates/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/templates/list".to_string() + pub fn method_name() -> &'static str { + "resources/templates/list" } } ///The server's response to a resources/templates/list request from the client. @@ -4827,12 +4827,12 @@ impl ListResourcesRequest { &self.method } /// returns "resources/list" - pub fn method_value() -> ::std::string::String { - "resources/list".to_string() + pub fn method_value() -> &'static str { + "resources/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/list".to_string() + pub fn method_name() -> &'static str { + "resources/list" } } ///The server's response to a resources/list request from the client. @@ -4940,12 +4940,12 @@ impl ListRootsRequest { &self.method } /// returns "roots/list" - pub fn method_value() -> ::std::string::String { - "roots/list".to_string() + pub fn method_value() -> &'static str { + "roots/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "roots/list".to_string() + pub fn method_name() -> &'static str { + "roots/list" } } /**The client's response to a roots/list request from the server. @@ -5042,12 +5042,12 @@ impl ListTasksRequest { &self.method } /// returns "tasks/list" - pub fn method_value() -> ::std::string::String { - "tasks/list".to_string() + pub fn method_value() -> &'static str { + "tasks/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tasks/list".to_string() + pub fn method_name() -> &'static str { + "tasks/list" } } ///The response to a tasks/list request. @@ -5150,12 +5150,12 @@ impl ListToolsRequest { &self.method } /// returns "tools/list" - pub fn method_value() -> ::std::string::String { - "tools/list".to_string() + pub fn method_value() -> &'static str { + "tools/list" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "tools/list".to_string() + pub fn method_name() -> &'static str { + "tools/list" } } ///The server's response to a tools/list request from the client. @@ -5308,12 +5308,12 @@ impl LoggingMessageNotification { &self.method } /// returns "notifications/message" - pub fn method_value() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_value() -> &'static str { + "notifications/message" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/message".to_string() + pub fn method_name() -> &'static str { + "notifications/message" } } ///Parameters for a notifications/message notification. @@ -5854,12 +5854,12 @@ impl PingRequest { &self.method } /// returns "ping" - pub fn method_value() -> ::std::string::String { - "ping".to_string() + pub fn method_value() -> &'static str { + "ping" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "ping".to_string() + pub fn method_name() -> &'static str { + "ping" } } /**Restricted schema definitions that only allow primitive types @@ -6003,12 +6003,12 @@ impl ProgressNotification { &self.method } /// returns "notifications/progress" - pub fn method_value() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_value() -> &'static str { + "notifications/progress" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/progress".to_string() + pub fn method_name() -> &'static str { + "notifications/progress" } } ///Parameters for a notifications/progress notification. @@ -6268,12 +6268,12 @@ impl PromptListChangedNotification { &self.method } /// returns "notifications/prompts/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/prompts/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/prompts/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/prompts/list_changed" } } /**Describes a message returned as part of a prompt. @@ -6361,12 +6361,12 @@ impl PromptReference { &self.type_ } /// returns "ref/prompt" - pub fn type_value() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_value() -> &'static str { + "ref/prompt" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/prompt".to_string() + pub fn type_name() -> &'static str { + "ref/prompt" } } ///ReadResourceContent @@ -6486,12 +6486,12 @@ impl ReadResourceRequest { &self.method } /// returns "resources/read" - pub fn method_value() -> ::std::string::String { - "resources/read".to_string() + pub fn method_value() -> &'static str { + "resources/read" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/read".to_string() + pub fn method_name() -> &'static str { + "resources/read" } } ///Parameters for a resources/read request. @@ -6984,12 +6984,12 @@ impl ResourceLink { &self.type_ } /// returns "resource_link" - pub fn type_value() -> ::std::string::String { - "resource_link".to_string() + pub fn type_value() -> &'static str { + "resource_link" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "resource_link".to_string() + pub fn type_name() -> &'static str { + "resource_link" } } ///An optional notification from the server to the client, informing it that the list of resources it can read from has changed. This may be issued by servers without any previous subscription from the client. @@ -7044,12 +7044,12 @@ impl ResourceListChangedNotification { &self.method } /// returns "notifications/resources/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/list_changed" } } ///See [General fields: _meta](https://modelcontextprotocol.io/specification/draft/basic/index#meta) for notes on _meta usage. @@ -7252,12 +7252,12 @@ impl ResourceTemplateReference { &self.type_ } /// returns "ref/resource" - pub fn type_value() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_value() -> &'static str { + "ref/resource" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "ref/resource".to_string() + pub fn type_name() -> &'static str { + "ref/resource" } } ///A notification from the server to the client, informing it that a resource has changed and may need to be read again. This should only be sent if the client previously sent a resources/subscribe request. @@ -7312,12 +7312,12 @@ impl ResourceUpdatedNotification { &self.method } /// returns "notifications/resources/updated" - pub fn method_value() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_value() -> &'static str { + "notifications/resources/updated" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/resources/updated".to_string() + pub fn method_name() -> &'static str { + "notifications/resources/updated" } } ///Parameters for a notifications/resources/updated notification. @@ -7509,12 +7509,12 @@ impl RootsListChangedNotification { &self.method } /// returns "notifications/roots/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/roots/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/roots/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/roots/list_changed" } } ///RpcError @@ -8192,11 +8192,11 @@ pub enum ServerResult { ListToolsResult(ListToolsResult), CallToolResult(CallToolResult), GetTaskResult(GetTaskResult), - GetTaskPayloadResult(GetTaskPayloadResult), CancelTaskResult(CancelTaskResult), ListTasksResult(ListTasksResult), CompleteResult(CompleteResult), Result(Result), + GetTaskPayloadResult(GetTaskPayloadResult), } impl ::std::convert::From for ServerResult { fn from(value: InitializeResult) -> Self { @@ -8243,11 +8243,6 @@ impl ::std::convert::From for ServerResult { Self::GetTaskResult(value) } } -impl ::std::convert::From for ServerResult { - fn from(value: GetTaskPayloadResult) -> Self { - Self::GetTaskPayloadResult(value) - } -} impl ::std::convert::From for ServerResult { fn from(value: CancelTaskResult) -> Self { Self::CancelTaskResult(value) @@ -8268,6 +8263,11 @@ impl ::std::convert::From for ServerResult { Self::Result(value) } } +impl ::std::convert::From for ServerResult { + fn from(value: GetTaskPayloadResult) -> Self { + Self::GetTaskPayloadResult(value) + } +} ///Specifies which request types can be augmented with tasks. /// ///
JSON schema @@ -8456,12 +8456,12 @@ impl SetLevelRequest { &self.method } /// returns "logging/setLevel" - pub fn method_value() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_value() -> &'static str { + "logging/setLevel" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "logging/setLevel".to_string() + pub fn method_name() -> &'static str { + "logging/setLevel" } } ///Parameters for a logging/setLevel request. @@ -8618,12 +8618,12 @@ impl StringSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///StringSchemaFormat @@ -8747,12 +8747,12 @@ impl SubscribeRequest { &self.method } /// returns "resources/subscribe" - pub fn method_value() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_value() -> &'static str { + "resources/subscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/subscribe".to_string() + pub fn method_name() -> &'static str { + "resources/subscribe" } } ///Parameters for a resources/subscribe request. @@ -9050,12 +9050,12 @@ impl TaskStatusNotification { &self.method } /// returns "notifications/tasks/status" - pub fn method_value() -> ::std::string::String { - "notifications/tasks/status".to_string() + pub fn method_value() -> &'static str { + "notifications/tasks/status" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/tasks/status".to_string() + pub fn method_name() -> &'static str { + "notifications/tasks/status" } } ///Parameters for a notifications/tasks/status notification. @@ -9168,12 +9168,12 @@ impl TextContent { &self.type_ } /// returns "text" - pub fn type_value() -> ::std::string::String { - "text".to_string() + pub fn type_value() -> &'static str { + "text" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "text".to_string() + pub fn type_name() -> &'static str { + "text" } } ///TextResourceContents @@ -9341,12 +9341,12 @@ impl TitledMultiSelectEnumSchema { &self.type_ } /// returns "array" - pub fn type_value() -> ::std::string::String { - "array".to_string() + pub fn type_value() -> &'static str { + "array" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "array".to_string() + pub fn type_name() -> &'static str { + "array" } } ///Schema for array items with enum options and display labels. @@ -9514,12 +9514,12 @@ impl TitledSingleSelectEnumSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///TitledSingleSelectEnumSchemaOneOfItem @@ -9980,12 +9980,12 @@ impl ToolInputSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///An optional notification from the server to the client, informing it that the list of tools it offers has changed. This may be issued by servers without any previous subscription from the client. @@ -10040,12 +10040,12 @@ impl ToolListChangedNotification { &self.method } /// returns "notifications/tools/list_changed" - pub fn method_value() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_value() -> &'static str { + "notifications/tools/list_changed" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "notifications/tools/list_changed".to_string() + pub fn method_name() -> &'static str { + "notifications/tools/list_changed" } } /**An optional JSON Schema object defining the structure of the tool's output returned in @@ -10122,12 +10122,12 @@ impl ToolOutputSchema { &self.type_ } /// returns "object" - pub fn type_value() -> ::std::string::String { - "object".to_string() + pub fn type_value() -> &'static str { + "object" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "object".to_string() + pub fn type_name() -> &'static str { + "object" } } ///The result of a tool use, provided by the user back to the assistant. @@ -10229,12 +10229,12 @@ impl ToolResultContent { &self.type_ } /// returns "tool_result" - pub fn type_value() -> ::std::string::String { - "tool_result".to_string() + pub fn type_value() -> &'static str { + "tool_result" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "tool_result".to_string() + pub fn type_name() -> &'static str { + "tool_result" } } ///A request from the assistant to call a tool. @@ -10314,12 +10314,12 @@ impl ToolUseContent { &self.type_ } /// returns "tool_use" - pub fn type_value() -> ::std::string::String { - "tool_use".to_string() + pub fn type_value() -> &'static str { + "tool_use" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "tool_use".to_string() + pub fn type_name() -> &'static str { + "tool_use" } } ///See [General fields: _meta](https://modelcontextprotocol.io/specification/draft/basic/index#meta) for notes on _meta usage. @@ -10406,12 +10406,12 @@ impl UnsubscribeRequest { &self.method } /// returns "resources/unsubscribe" - pub fn method_value() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_value() -> &'static str { + "resources/unsubscribe" } #[deprecated(since = "0.8.0", note = "Use `method_value()` instead.")] - pub fn method_name() -> ::std::string::String { - "resources/unsubscribe".to_string() + pub fn method_name() -> &'static str { + "resources/unsubscribe" } } ///Parameters for a resources/unsubscribe request. @@ -10562,12 +10562,12 @@ impl UntitledMultiSelectEnumSchema { &self.type_ } /// returns "array" - pub fn type_value() -> ::std::string::String { - "array".to_string() + pub fn type_value() -> &'static str { + "array" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "array".to_string() + pub fn type_name() -> &'static str { + "array" } } ///Schema for the array items. @@ -10620,12 +10620,12 @@ impl UntitledMultiSelectEnumSchemaItems { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///Schema for single-selection enumeration without display titles for options. @@ -10704,12 +10704,12 @@ impl UntitledSingleSelectEnumSchema { &self.type_ } /// returns "string" - pub fn type_value() -> ::std::string::String { - "string".to_string() + pub fn type_value() -> &'static str { + "string" } #[deprecated(since = "0.8.0", note = "Use `type_value()` instead.")] - pub fn type_name() -> ::std::string::String { - "string".to_string() + pub fn type_name() -> &'static str { + "string" } } ///UrlElicitError @@ -11186,6 +11186,92 @@ impl ServerNotification { } } } +/// Converts any serializable struct into a `GenericResult`. +/// This is used internally to convert ServerResult and ClientResult variants +/// into GenericResult (Result) +fn into_result(value: T) -> GenericResult +where + T: serde::Serialize, +{ + let json_value = serde_json::to_value(value).unwrap_or(serde_json::Value::Null); + if let serde_json::Value::Object(mut map) = json_value { + let meta = map.remove("_meta").and_then(|v| match v { + serde_json::Value::Object(obj) => Some(obj), + _ => None, + }); + let extra = if map.is_empty() { None } else { Some(map) }; + GenericResult { meta, extra } + } else { + GenericResult { meta: None, extra: None } + } +} +impl From for GenericResult { + fn from(value: InitializeResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourcesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListResourceTemplatesResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ReadResourceResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListPromptsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: GetPromptResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListToolsResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CallToolResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: GetTaskResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: GetTaskPayloadResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CancelTaskResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: ListTasksResult) -> Self { + into_result(value) + } +} +impl From for GenericResult { + fn from(value: CompleteResult) -> Self { + into_result(value) + } +} +/// Alias to avoid conflicts with Rust's standard `Result` type. +pub type GenericResult = Result; /// Deprecating the old auto-generated verbose names. /// These were renamed to clearer, shorter names in v0.8.0. /// The old names are deprecated but kept for backward-compatibility for a smooth migration period. diff --git a/src/generated_schema/draft/schema_utils.rs b/src/generated_schema/draft/schema_utils.rs index d30a976..3e95624 100644 --- a/src/generated_schema/draft/schema_utils.rs +++ b/src/generated_schema/draft/schema_utils.rs @@ -234,12 +234,15 @@ impl ClientMessage { /// Returns `true` if message is an `InitializeRequest`. pub fn is_initialize_request(&self) -> bool { - matches!(self, Self::Request(request) if request.request.is_initialize_request()) + matches!(self, Self::Request(ClientJsonrpcRequest::InitializeRequest(_))) } /// Returns `true` if the message is an `InitializedNotification` pub fn is_initialized_notification(&self) -> bool { - matches!(self, Self::Notification(notofication) if notofication.notification.is_initialized_notification()) + matches!( + self, + Self::Notification(ClientJsonrpcNotification::InitializedNotification(_)) + ) } } @@ -266,7 +269,26 @@ impl RpcMessage for ClientMessage { fn request_id(&self) -> Option<&RequestId> { match self { // If the message is a request, return the associated request ID - ClientMessage::Request(client_jsonrpc_request) => Some(&client_jsonrpc_request.id), + ClientMessage::Request(client_jsonrpc_request) => match client_jsonrpc_request { + ClientJsonrpcRequest::InitializeRequest(request) => Some(&request.id), + ClientJsonrpcRequest::PingRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListResourcesRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListResourceTemplatesRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ReadResourceRequest(request) => Some(&request.id), + ClientJsonrpcRequest::SubscribeRequest(request) => Some(&request.id), + ClientJsonrpcRequest::UnsubscribeRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListPromptsRequest(request) => Some(&request.id), + ClientJsonrpcRequest::GetPromptRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListToolsRequest(request) => Some(&request.id), + ClientJsonrpcRequest::CallToolRequest(request) => Some(&request.id), + ClientJsonrpcRequest::GetTaskRequest(request) => Some(&request.id), + ClientJsonrpcRequest::GetTaskPayloadRequest(request) => Some(&request.id), + ClientJsonrpcRequest::CancelTaskRequest(request) => Some(&request.id), + ClientJsonrpcRequest::ListTasksRequest(request) => Some(&request.id), + ClientJsonrpcRequest::SetLevelRequest(request) => Some(&request.id), + ClientJsonrpcRequest::CompleteRequest(request) => Some(&request.id), + ClientJsonrpcRequest::CustomRequest(request) => Some(&request.id), + }, // Notifications do not have request IDs ClientMessage::Notification(_) => None, // If the message is a response, return the associated request ID @@ -324,26 +346,136 @@ impl McpMessage for ClientMessage { //**************************// /// "Similar to JsonrpcRequest , but with the variants restricted to client-side requests." -#[derive(Clone, Debug)] -pub struct ClientJsonrpcRequest { - pub id: RequestId, - jsonrpc: ::std::string::String, - pub method: String, - pub request: RequestFromClient, +#[derive(Clone, Debug, ::serde::Serialize, ::serde::Deserialize)] +#[serde(untagged)] +pub enum ClientJsonrpcRequest { + InitializeRequest(InitializeRequest), + PingRequest(PingRequest), + ListResourcesRequest(ListResourcesRequest), + ListResourceTemplatesRequest(ListResourceTemplatesRequest), + ReadResourceRequest(ReadResourceRequest), + SubscribeRequest(SubscribeRequest), + UnsubscribeRequest(UnsubscribeRequest), + ListPromptsRequest(ListPromptsRequest), + GetPromptRequest(GetPromptRequest), + ListToolsRequest(ListToolsRequest), + CallToolRequest(CallToolRequest), + GetTaskRequest(GetTaskRequest), + GetTaskPayloadRequest(GetTaskPayloadRequest), + CancelTaskRequest(CancelTaskRequest), + ListTasksRequest(ListTasksRequest), + SetLevelRequest(SetLevelRequest), + CompleteRequest(CompleteRequest), + CustomRequest(JsonrpcRequest), } impl ClientJsonrpcRequest { pub fn new(id: RequestId, request: RequestFromClient) -> Self { - let method = request.method().to_string(); - Self { - id, - jsonrpc: JSONRPC_VERSION.to_string(), - method, - request, + match request { + RequestFromClient::InitializeRequest(params) => Self::InitializeRequest(InitializeRequest::new(id, params)), + RequestFromClient::PingRequest(params) => Self::PingRequest(PingRequest::new(id, params)), + RequestFromClient::ListResourcesRequest(params) => { + Self::ListResourcesRequest(ListResourcesRequest::new(id, params)) + } + RequestFromClient::ListResourceTemplatesRequest(params) => { + Self::ListResourceTemplatesRequest(ListResourceTemplatesRequest::new(id, params)) + } + RequestFromClient::ReadResourceRequest(params) => { + Self::ReadResourceRequest(ReadResourceRequest::new(id, params)) + } + RequestFromClient::SubscribeRequest(params) => Self::SubscribeRequest(SubscribeRequest::new(id, params)), + RequestFromClient::UnsubscribeRequest(params) => Self::UnsubscribeRequest(UnsubscribeRequest::new(id, params)), + RequestFromClient::ListPromptsRequest(params) => Self::ListPromptsRequest(ListPromptsRequest::new(id, params)), + RequestFromClient::GetPromptRequest(params) => Self::GetPromptRequest(GetPromptRequest::new(id, params)), + RequestFromClient::ListToolsRequest(params) => Self::ListToolsRequest(ListToolsRequest::new(id, params)), + RequestFromClient::CallToolRequest(params) => Self::CallToolRequest(CallToolRequest::new(id, params)), + RequestFromClient::GetTaskRequest(params) => Self::GetTaskRequest(GetTaskRequest::new(id, params)), + RequestFromClient::GetTaskPayloadRequest(params) => { + Self::GetTaskPayloadRequest(GetTaskPayloadRequest::new(id, params)) + } + RequestFromClient::CancelTaskRequest(params) => Self::CancelTaskRequest(CancelTaskRequest::new(id, params)), + RequestFromClient::ListTasksRequest(params) => Self::ListTasksRequest(ListTasksRequest::new(id, params)), + RequestFromClient::SetLevelRequest(params) => Self::SetLevelRequest(SetLevelRequest::new(id, params)), + RequestFromClient::CompleteRequest(params) => Self::CompleteRequest(CompleteRequest::new(id, params)), + RequestFromClient::CustomRequest(params) => { + Self::CustomRequest(JsonrpcRequest::new(id, params.method, params.params)) + } } } pub fn jsonrpc(&self) -> &::std::string::String { - &self.jsonrpc + match self { + ClientJsonrpcRequest::InitializeRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::PingRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListResourcesRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListResourceTemplatesRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ReadResourceRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::SubscribeRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::UnsubscribeRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListPromptsRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::GetPromptRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListToolsRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::CallToolRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::GetTaskRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::GetTaskPayloadRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::CancelTaskRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::ListTasksRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::SetLevelRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::CompleteRequest(request) => request.jsonrpc(), + ClientJsonrpcRequest::CustomRequest(request) => request.jsonrpc(), + } + } + + pub fn request_id(&self) -> &RequestId { + match self { + ClientJsonrpcRequest::InitializeRequest(request) => &request.id, + ClientJsonrpcRequest::PingRequest(request) => &request.id, + ClientJsonrpcRequest::ListResourcesRequest(request) => &request.id, + ClientJsonrpcRequest::ListResourceTemplatesRequest(request) => &request.id, + ClientJsonrpcRequest::ReadResourceRequest(request) => &request.id, + ClientJsonrpcRequest::SubscribeRequest(request) => &request.id, + ClientJsonrpcRequest::UnsubscribeRequest(request) => &request.id, + ClientJsonrpcRequest::ListPromptsRequest(request) => &request.id, + ClientJsonrpcRequest::GetPromptRequest(request) => &request.id, + ClientJsonrpcRequest::ListToolsRequest(request) => &request.id, + ClientJsonrpcRequest::CallToolRequest(request) => &request.id, + ClientJsonrpcRequest::GetTaskRequest(request) => &request.id, + ClientJsonrpcRequest::GetTaskPayloadRequest(request) => &request.id, + ClientJsonrpcRequest::CancelTaskRequest(request) => &request.id, + ClientJsonrpcRequest::ListTasksRequest(request) => &request.id, + ClientJsonrpcRequest::SetLevelRequest(request) => &request.id, + ClientJsonrpcRequest::CompleteRequest(request) => &request.id, + ClientJsonrpcRequest::CustomRequest(request) => &request.id, + } + } +} + +impl From for RequestFromClient { + fn from(request: ClientJsonrpcRequest) -> Self { + match request { + ClientJsonrpcRequest::InitializeRequest(request) => Self::InitializeRequest(request.params), + ClientJsonrpcRequest::PingRequest(request) => Self::PingRequest(request.params), + ClientJsonrpcRequest::ListResourcesRequest(request) => Self::ListResourcesRequest(request.params), + ClientJsonrpcRequest::ListResourceTemplatesRequest(request) => { + Self::ListResourceTemplatesRequest(request.params) + } + ClientJsonrpcRequest::ReadResourceRequest(request) => Self::ReadResourceRequest(request.params), + ClientJsonrpcRequest::SubscribeRequest(request) => Self::SubscribeRequest(request.params), + ClientJsonrpcRequest::UnsubscribeRequest(request) => Self::UnsubscribeRequest(request.params), + ClientJsonrpcRequest::ListPromptsRequest(request) => Self::ListPromptsRequest(request.params), + ClientJsonrpcRequest::GetPromptRequest(request) => Self::GetPromptRequest(request.params), + ClientJsonrpcRequest::ListToolsRequest(request) => Self::ListToolsRequest(request.params), + ClientJsonrpcRequest::CallToolRequest(request) => Self::CallToolRequest(request.params), + ClientJsonrpcRequest::GetTaskRequest(request) => Self::GetTaskRequest(request.params), + ClientJsonrpcRequest::GetTaskPayloadRequest(request) => Self::GetTaskPayloadRequest(request.params), + ClientJsonrpcRequest::CancelTaskRequest(request) => Self::CancelTaskRequest(request.params), + ClientJsonrpcRequest::ListTasksRequest(request) => Self::ListTasksRequest(request.params), + ClientJsonrpcRequest::SetLevelRequest(request) => Self::SetLevelRequest(request.params), + ClientJsonrpcRequest::CompleteRequest(request) => Self::CompleteRequest(request.params), + ClientJsonrpcRequest::CustomRequest(request) => Self::CustomRequest(CustomRequest { + method: request.method, + params: request.params, + }), + } } } @@ -395,88 +527,158 @@ impl FromStr for ClientJsonrpcRequest { /// To determine standard and custom request from the client side /// Custom requests are of type serde_json::Value and can be deserialized into any custom type. #[allow(clippy::large_enum_variant)] -#[derive(::serde::Serialize, Clone, Debug)] +#[derive(::serde::Serialize, ::serde::Deserialize, Clone, Debug)] #[serde(untagged)] pub enum RequestFromClient { - ClientRequest(ClientRequest), - CustomRequest(serde_json::Value), -} - -impl TryFrom for ClientRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> result::Result { - if let RequestFromClient::ClientRequest(client_request) = value { - Ok(client_request) - } else { - Err(RpcError::internal_error().with_message("Not a ClientRequest".to_string())) - } - } + InitializeRequest(InitializeRequestParams), + PingRequest(Option), + ListResourcesRequest(Option), + ListResourceTemplatesRequest(Option), + ReadResourceRequest(ReadResourceRequestParams), + SubscribeRequest(SubscribeRequestParams), + UnsubscribeRequest(UnsubscribeRequestParams), + ListPromptsRequest(Option), + GetPromptRequest(GetPromptRequestParams), + ListToolsRequest(Option), + CallToolRequest(CallToolRequestParams), + GetTaskRequest(GetTaskParams), + GetTaskPayloadRequest(GetTaskPayloadParams), + CancelTaskRequest(CancelTaskParams), + ListTasksRequest(Option), + SetLevelRequest(SetLevelRequestParams), + CompleteRequest(CompleteRequestParams), + CustomRequest(CustomRequest), } impl RequestFromClient { pub fn method(&self) -> &str { match self { - RequestFromClient::ClientRequest(request) => request.method(), - RequestFromClient::CustomRequest(request) => request["method"].as_str().unwrap(), + RequestFromClient::InitializeRequest(_request) => InitializeRequest::method_value(), + RequestFromClient::PingRequest(_request) => PingRequest::method_value(), + RequestFromClient::ListResourcesRequest(_request) => ListResourcesRequest::method_value(), + RequestFromClient::ListResourceTemplatesRequest(_request) => ListResourceTemplatesRequest::method_value(), + RequestFromClient::ReadResourceRequest(_request) => ReadResourceRequest::method_value(), + RequestFromClient::SubscribeRequest(_request) => SubscribeRequest::method_value(), + RequestFromClient::UnsubscribeRequest(_request) => UnsubscribeRequest::method_value(), + RequestFromClient::ListPromptsRequest(_request) => ListPromptsRequest::method_value(), + RequestFromClient::GetPromptRequest(_request) => GetPromptRequest::method_value(), + RequestFromClient::ListToolsRequest(_request) => ListToolsRequest::method_value(), + RequestFromClient::CallToolRequest(_request) => CallToolRequest::method_value(), + RequestFromClient::GetTaskRequest(_request) => GetTaskRequest::method_value(), + RequestFromClient::GetTaskPayloadRequest(_request) => GetTaskPayloadRequest::method_value(), + RequestFromClient::CancelTaskRequest(_request) => CancelTaskRequest::method_value(), + RequestFromClient::ListTasksRequest(_request) => ListTasksRequest::method_value(), + RequestFromClient::SetLevelRequest(_request) => SetLevelRequest::method_value(), + RequestFromClient::CompleteRequest(_request) => CompleteRequest::method_value(), + RequestFromClient::CustomRequest(request) => request.method.as_str(), } } /// Returns `true` if the request is an `InitializeRequest`. pub fn is_initialize_request(&self) -> bool { - matches!(self, RequestFromClient::ClientRequest(ClientRequest::InitializeRequest(_))) + matches!(self, RequestFromClient::InitializeRequest(_)) } } -impl From for RequestFromClient { - fn from(value: ClientRequest) -> Self { - Self::ClientRequest(value) - } -} +// impl From for RequestFromClient { +// fn from(value: ClientRequest) -> Self { +// Self::ClientRequest(value) +// } +// } -impl From for RequestFromClient { - fn from(value: serde_json::Value) -> Self { - Self::CustomRequest(value) - } -} +// impl From for RequestFromClient { +// fn from(value: serde_json::Value) -> Self { +// Self::CustomRequest(value) +// } +// } -impl<'de> serde::Deserialize<'de> for RequestFromClient { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; +// impl<'de> serde::Deserialize<'de> for RequestFromClient { +// fn deserialize(deserializer: D) -> core::result::Result +// where +// D: serde::Deserializer<'de>, +// { +// let raw_value = Value::deserialize(deserializer)?; - let client_result = ClientRequest::deserialize(&raw_value); +// let client_result = ClientRequest::deserialize(&raw_value); - match client_result { - Ok(client_request) => Ok(Self::ClientRequest(client_request)), - Err(_) => Ok(Self::CustomRequest(raw_value)), - } - } -} +// match client_result { +// Ok(client_request) => Ok(Self::ClientRequest(client_request)), +// Err(_) => Ok(Self::CustomRequest(raw_value)), +// } +// } +// } //*******************************// //** ClientJsonrpcNotification **// //*******************************// /// "Similar to JsonrpcNotification , but with the variants restricted to client-side notifications." -#[derive(Clone, Debug)] -pub struct ClientJsonrpcNotification { - jsonrpc: ::std::string::String, - pub method: ::std::string::String, - pub notification: NotificationFromClient, +#[derive(Clone, Debug, ::serde::Deserialize, ::serde::Serialize)] +#[serde(untagged)] +pub enum ClientJsonrpcNotification { + CancelledNotification(CancelledNotification), + InitializedNotification(InitializedNotification), + ProgressNotification(ProgressNotification), + TaskStatusNotification(TaskStatusNotification), + RootsListChangedNotification(RootsListChangedNotification), + CustomNotification(JsonrpcNotification), } impl ClientJsonrpcNotification { pub fn new(notification: NotificationFromClient) -> Self { - let method = notification.method().to_string(); - Self { - jsonrpc: JSONRPC_VERSION.to_string(), - method, - notification, + match notification { + NotificationFromClient::CancelledNotification(params) => { + Self::CancelledNotification(CancelledNotification::new(params)) + } + NotificationFromClient::InitializedNotification(params) => { + Self::InitializedNotification(InitializedNotification::new(params)) + } + NotificationFromClient::ProgressNotification(params) => { + Self::ProgressNotification(ProgressNotification::new(params)) + } + NotificationFromClient::TaskStatusNotification(params) => { + Self::TaskStatusNotification(TaskStatusNotification::new(params)) + } + NotificationFromClient::RootsListChangedNotification(params) => { + Self::RootsListChangedNotification(RootsListChangedNotification::new(params)) + } + NotificationFromClient::CustomNotification(params) => { + Self::CustomNotification(JsonrpcNotification::new(params.method, params.params)) + } } } pub fn jsonrpc(&self) -> &::std::string::String { - &self.jsonrpc + match self { + ClientJsonrpcNotification::CancelledNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::InitializedNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::ProgressNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::TaskStatusNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::RootsListChangedNotification(notification) => notification.jsonrpc(), + ClientJsonrpcNotification::CustomNotification(notification) => notification.jsonrpc(), + } + } +} + +impl From for NotificationFromClient { + fn from(notification: ClientJsonrpcNotification) -> Self { + match notification { + ClientJsonrpcNotification::CancelledNotification(notification) => { + Self::CancelledNotification(notification.params) + } + ClientJsonrpcNotification::InitializedNotification(notification) => { + Self::InitializedNotification(notification.params) + } + ClientJsonrpcNotification::ProgressNotification(notification) => Self::ProgressNotification(notification.params), + ClientJsonrpcNotification::TaskStatusNotification(notification) => { + Self::TaskStatusNotification(notification.params) + } + ClientJsonrpcNotification::RootsListChangedNotification(notification) => { + Self::RootsListChangedNotification(notification.params) + } + ClientJsonrpcNotification::CustomNotification(notification) => Self::CustomNotification(CustomNotification { + method: notification.method, + params: notification.params, + }), + } } } @@ -506,53 +708,45 @@ impl FromStr for ClientJsonrpcNotification { /// To determine standard and custom notifications received from the MCP Client /// Custom notifications are of type serde_json::Value and can be deserialized into any custom type. -#[derive(::serde::Serialize, Clone, Debug)] +#[derive(::serde::Serialize, ::serde::Deserialize, Clone, Debug)] #[serde(untagged)] pub enum NotificationFromClient { - ClientNotification(ClientNotification), - CustomNotification(serde_json::Value), -} - -impl TryFrom for ClientNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> result::Result { - if let NotificationFromClient::ClientNotification(client_notification) = value { - Ok(client_notification) - } else { - Err(RpcError::internal_error().with_message("Not a ClientNotification".to_string())) - } - } -} + CancelledNotification(CancelledNotificationParams), + InitializedNotification(Option), + ProgressNotification(ProgressNotificationParams), + TaskStatusNotification(TaskStatusNotificationParams), + RootsListChangedNotification(Option), + CustomNotification(CustomNotification), +} + +// impl TryFrom for ClientNotification { +// type Error = RpcError; +// fn try_from(value: NotificationFromClient) -> result::Result { +// if let NotificationFromClient::ClientNotification(client_notification) = value { +// Ok(client_notification) +// } else { +// Err(RpcError::internal_error().with_message("Not a ClientNotification".to_string())) +// } +// } +// } impl NotificationFromClient { /// Returns `true` if the message is an `InitializedNotification` pub fn is_initialized_notification(&self) -> bool { - matches!( - self, - NotificationFromClient::ClientNotification(ClientNotification::InitializedNotification(_)) - ) + matches!(self, NotificationFromClient::InitializedNotification(_)) } - fn method(&self) -> &str { + //TODO: 'static + pub fn method(&self) -> &str { match self { - NotificationFromClient::ClientNotification(notification) => notification.method(), - NotificationFromClient::CustomNotification(notification) => notification["method"].as_str().unwrap(), - } - } -} - -impl<'de> serde::Deserialize<'de> for NotificationFromClient { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let result = ClientNotification::deserialize(&raw_value); - - match result { - Ok(client_notification) => Ok(Self::ClientNotification(client_notification)), - Err(_) => Ok(Self::CustomNotification(raw_value)), + NotificationFromClient::CancelledNotification(_notification) => CancelledNotification::method_value(), + NotificationFromClient::InitializedNotification(_notification) => InitializedNotification::method_value(), + NotificationFromClient::ProgressNotification(_notification) => ProgressNotification::method_value(), + NotificationFromClient::TaskStatusNotification(_notification) => TaskStatusNotification::method_value(), + NotificationFromClient::RootsListChangedNotification(_notification) => { + RootsListChangedNotification::method_value() + } + NotificationFromClient::CustomNotification(notification) => notification.method.as_str(), } } } @@ -605,55 +799,7 @@ impl FromStr for ClientJsonrpcResponse { //** ResultFromClient **// //*******************************// -/// To determine standard and custom results from the client side -/// Custom results (CustomResult) are of type serde_json::Value and can be deserialized into any custom type. -#[allow(clippy::large_enum_variant)] -#[derive(::serde::Serialize, Clone, Debug)] -#[serde(untagged)] -pub enum ResultFromClient { - ClientResult(ClientResult), - /// **Deprecated**: Use `ClientResult::Result` with extra attributes instead. - CustomResult(serde_json::Value), -} - -impl TryFrom for ClientResult { - type Error = RpcError; - fn try_from(value: ResultFromClient) -> result::Result { - if let ResultFromClient::ClientResult(client_result) = value { - Ok(client_result) - } else { - Err(RpcError::internal_error().with_message("Not a ClientResult".to_string())) - } - } -} - -impl From for ResultFromClient { - fn from(value: ClientResult) -> Self { - Self::ClientResult(value) - } -} - -impl From for ResultFromClient { - fn from(value: serde_json::Value) -> Self { - Self::CustomResult(value) - } -} - -impl<'de> serde::Deserialize<'de> for ResultFromClient { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let result = ClientResult::deserialize(&raw_value); - - match result { - Ok(client_result) => Ok(Self::ClientResult(client_result)), - Err(_) => Ok(Self::CustomResult(raw_value)), - } - } -} +pub type ResultFromClient = ClientResult; //*******************************// //** ClientMessage **// @@ -802,7 +948,17 @@ impl RpcMessage for ServerMessage { fn request_id(&self) -> Option<&RequestId> { match self { // If the message is a request, return the associated request ID - ServerMessage::Request(server_jsonrpc_request) => Some(&server_jsonrpc_request.id), + ServerMessage::Request(server_jsonrpc_request) => match server_jsonrpc_request { + ServerJsonrpcRequest::PingRequest(request) => Some(&request.id), + ServerJsonrpcRequest::GetTaskRequest(request) => Some(&request.id), + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => Some(&request.id), + ServerJsonrpcRequest::CancelTaskRequest(request) => Some(&request.id), + ServerJsonrpcRequest::ListTasksRequest(request) => Some(&request.id), + ServerJsonrpcRequest::CreateMessageRequest(request) => Some(&request.id), + ServerJsonrpcRequest::ListRootsRequest(request) => Some(&request.id), + ServerJsonrpcRequest::ElicitRequest(request) => Some(&request.id), + ServerJsonrpcRequest::CustomRequest(request) => Some(&request.id), + }, // Notifications do not have request IDs ServerMessage::Notification(_) => None, // If the message is a response, return the associated request ID @@ -815,7 +971,18 @@ impl RpcMessage for ServerMessage { fn jsonrpc(&self) -> &str { match self { // If the message is a request, return the associated request ID - ServerMessage::Request(server_jsonrpc_request) => server_jsonrpc_request.jsonrpc(), + ServerMessage::Request(server_jsonrpc_request) => match server_jsonrpc_request { + ServerJsonrpcRequest::PingRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::GetTaskRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CancelTaskRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ListTasksRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CreateMessageRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ListRootsRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ElicitRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CustomRequest(request) => request.jsonrpc(), + }, + // Notifications do not have request IDs ServerMessage::Notification(notification) => notification.jsonrpc(), // If the message is a response, return the associated request ID @@ -883,27 +1050,58 @@ impl Display for ServerMessage { //**************************// /// "Similar to JsonrpcRequest , but with the variants restricted to client-side requests." -#[derive(Clone, Debug)] +#[derive(Clone, Debug, ::serde::Serialize, ::serde::Deserialize)] #[allow(clippy::large_enum_variant)] -pub struct ServerJsonrpcRequest { - pub id: RequestId, - jsonrpc: ::std::string::String, - pub method: String, - pub request: RequestFromServer, +#[serde(untagged)] +pub enum ServerJsonrpcRequest { + PingRequest(PingRequest), + GetTaskRequest(GetTaskRequest), + GetTaskPayloadRequest(GetTaskPayloadRequest), + CancelTaskRequest(CancelTaskRequest), + ListTasksRequest(ListTasksRequest), + CreateMessageRequest(CreateMessageRequest), + ListRootsRequest(ListRootsRequest), + ElicitRequest(ElicitRequest), + CustomRequest(JsonrpcRequest), } impl ServerJsonrpcRequest { - pub fn new(id: RequestId, request: RequestFromServer) -> Self { - let method = request.method().to_string(); - Self { - id, - jsonrpc: JSONRPC_VERSION.to_string(), - method, - request, + // pub fn new(id: RequestId, request: RequestFromServer) -> Self { + // let method = request.method().to_string(); + // Self { + // id, + // jsonrpc: JSONRPC_VERSION.to_string(), + // method, + // request, + // } + // } + + pub fn request_id(&self) -> &RequestId { + match self { + ServerJsonrpcRequest::PingRequest(request) => &request.id, + ServerJsonrpcRequest::GetTaskRequest(request) => &request.id, + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => &request.id, + ServerJsonrpcRequest::CancelTaskRequest(request) => &request.id, + ServerJsonrpcRequest::ListTasksRequest(request) => &request.id, + ServerJsonrpcRequest::CreateMessageRequest(request) => &request.id, + ServerJsonrpcRequest::ListRootsRequest(request) => &request.id, + ServerJsonrpcRequest::ElicitRequest(request) => &request.id, + ServerJsonrpcRequest::CustomRequest(request) => &request.id, } } + pub fn jsonrpc(&self) -> &::std::string::String { - &self.jsonrpc + match self { + ServerJsonrpcRequest::PingRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::GetTaskRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CancelTaskRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ListTasksRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CreateMessageRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ListRootsRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::ElicitRequest(request) => request.jsonrpc(), + ServerJsonrpcRequest::CustomRequest(request) => request.jsonrpc(), + } } } @@ -926,26 +1124,49 @@ impl FromStr for ServerJsonrpcRequest { .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() })))) } } + +#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)] +pub struct CustomRequest { + pub method: ::std::string::String, + #[serde(default, skip_serializing_if = "::std::option::Option::is_none")] + pub params: ::std::option::Option<::serde_json::Map<::std::string::String, ::serde_json::Value>>, +} + //*************************// //** Request From Server **// //*************************// /// To determine standard and custom request from the server side /// Custom requests are of type serde_json::Value and can be deserialized into any custom type. -#[derive(::serde::Serialize, Clone, Debug)] +#[derive(::serde::Serialize, ::serde::Deserialize, Clone, Debug)] #[serde(untagged)] pub enum RequestFromServer { - ServerRequest(ServerRequest), - CustomRequest(serde_json::Value), -} - -impl TryFrom for ServerRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> result::Result { - if let RequestFromServer::ServerRequest(server_request) = value { - Ok(server_request) - } else { - Err(RpcError::internal_error().with_message("Not a ServerRequest".to_string())) + PingRequest(Option), + GetTaskRequest(GetTaskParams), + GetTaskPayloadRequest(GetTaskPayloadParams), + CancelTaskRequest(CancelTaskParams), + ListTasksRequest(Option), + CreateMessageRequest(CreateMessageRequestParams), + ListRootsRequest(Option), + ElicitRequest(ElicitRequestParams), + CustomRequest(CustomRequest), +} + +impl From for RequestFromServer { + fn from(request: ServerJsonrpcRequest) -> Self { + match request { + ServerJsonrpcRequest::PingRequest(request) => Self::PingRequest(request.params), + ServerJsonrpcRequest::GetTaskRequest(request) => Self::GetTaskRequest(request.params), + ServerJsonrpcRequest::GetTaskPayloadRequest(request) => Self::GetTaskPayloadRequest(request.params), + ServerJsonrpcRequest::CancelTaskRequest(request) => Self::CancelTaskRequest(request.params), + ServerJsonrpcRequest::ListTasksRequest(request) => Self::ListTasksRequest(request.params), + ServerJsonrpcRequest::CreateMessageRequest(request) => Self::CreateMessageRequest(request.params), + ServerJsonrpcRequest::ListRootsRequest(request) => Self::ListRootsRequest(request.params), + ServerJsonrpcRequest::ElicitRequest(request) => Self::ElicitRequest(request.params), + ServerJsonrpcRequest::CustomRequest(request) => Self::CustomRequest(CustomRequest { + method: request.method, + params: request.params, + }), } } } @@ -953,36 +1174,15 @@ impl TryFrom for ServerRequest { impl RequestFromServer { pub fn method(&self) -> &str { match self { - RequestFromServer::ServerRequest(request) => request.method(), - RequestFromServer::CustomRequest(request) => request["method"].as_str().unwrap(), - } - } -} - -impl From for RequestFromServer { - fn from(value: ServerRequest) -> Self { - Self::ServerRequest(value) - } -} - -impl From for RequestFromServer { - fn from(value: serde_json::Value) -> Self { - Self::CustomRequest(value) - } -} - -impl<'de> serde::Deserialize<'de> for RequestFromServer { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let server_result = ServerRequest::deserialize(&raw_value); - - match server_result { - Ok(server_request) => Ok(Self::ServerRequest(server_request)), - Err(_) => Ok(Self::CustomRequest(raw_value)), + RequestFromServer::PingRequest(_request) => PingRequest::method_value(), + RequestFromServer::GetTaskRequest(_request) => GetTaskRequest::method_value(), + RequestFromServer::GetTaskPayloadRequest(_request) => GetTaskPayloadRequest::method_value(), + RequestFromServer::CancelTaskRequest(_request) => CancelTaskRequest::method_value(), + RequestFromServer::ListTasksRequest(_request) => ListTasksRequest::method_value(), + RequestFromServer::CreateMessageRequest(_request) => CreateMessageRequest::method_value(), + RequestFromServer::ListRootsRequest(_request) => ListRootsRequest::method_value(), + RequestFromServer::ElicitRequest(_request) => ElicitRequest::method_value(), + RequestFromServer::CustomRequest(request) => request.method.as_str(), } } } @@ -992,24 +1192,106 @@ impl<'de> serde::Deserialize<'de> for RequestFromServer { //*******************************// /// "Similar to JsonrpcNotification , but with the variants restricted to server-side notifications." -#[derive(Clone, Debug)] -pub struct ServerJsonrpcNotification { - jsonrpc: ::std::string::String, - pub method: ::std::string::String, - pub notification: NotificationFromServer, +#[derive(Clone, Debug, ::serde::Deserialize, ::serde::Serialize)] +#[serde(untagged)] +pub enum ServerJsonrpcNotification { + CancelledNotification(CancelledNotification), + ProgressNotification(ProgressNotification), + ResourceListChangedNotification(ResourceListChangedNotification), + ResourceUpdatedNotification(ResourceUpdatedNotification), + PromptListChangedNotification(PromptListChangedNotification), + ToolListChangedNotification(ToolListChangedNotification), + TaskStatusNotification(TaskStatusNotification), + LoggingMessageNotification(LoggingMessageNotification), + ElicitationCompleteNotification(ElicitationCompleteNotification), + CustomNotification(JsonrpcNotification), +} + +impl From for NotificationFromServer { + fn from(notification: ServerJsonrpcNotification) -> Self { + match notification { + ServerJsonrpcNotification::CancelledNotification(notification) => { + Self::CancelledNotification(notification.params) + } + ServerJsonrpcNotification::ProgressNotification(notification) => Self::ProgressNotification(notification.params), + ServerJsonrpcNotification::ResourceListChangedNotification(notification) => { + Self::ResourceListChangedNotification(notification.params) + } + ServerJsonrpcNotification::ResourceUpdatedNotification(notification) => { + Self::ResourceUpdatedNotification(notification.params) + } + ServerJsonrpcNotification::PromptListChangedNotification(notification) => { + Self::PromptListChangedNotification(notification.params) + } + ServerJsonrpcNotification::ToolListChangedNotification(notification) => { + Self::ToolListChangedNotification(notification.params) + } + ServerJsonrpcNotification::TaskStatusNotification(notification) => { + Self::TaskStatusNotification(notification.params) + } + ServerJsonrpcNotification::LoggingMessageNotification(notification) => { + Self::LoggingMessageNotification(notification.params) + } + ServerJsonrpcNotification::ElicitationCompleteNotification(notification) => { + Self::ElicitationCompleteNotification(notification.params) + } + ServerJsonrpcNotification::CustomNotification(notification) => Self::CustomNotification(CustomNotification { + method: notification.method, + params: notification.params, + }), + } + } } +//TODO: check do we need from_message() or this impl ServerJsonrpcNotification { pub fn new(notification: NotificationFromServer) -> Self { - let method = notification.method().to_string(); - Self { - jsonrpc: JSONRPC_VERSION.to_string(), - method, - notification, + match notification { + NotificationFromServer::CancelledNotification(params) => { + Self::CancelledNotification(CancelledNotification::new(params)) + } + NotificationFromServer::ProgressNotification(params) => { + Self::ProgressNotification(ProgressNotification::new(params)) + } + NotificationFromServer::ResourceListChangedNotification(params) => { + Self::ResourceListChangedNotification(ResourceListChangedNotification::new(params)) + } + NotificationFromServer::ResourceUpdatedNotification(params) => { + Self::ResourceUpdatedNotification(ResourceUpdatedNotification::new(params)) + } + NotificationFromServer::PromptListChangedNotification(params) => { + Self::PromptListChangedNotification(PromptListChangedNotification::new(params)) + } + NotificationFromServer::ToolListChangedNotification(params) => { + Self::ToolListChangedNotification(ToolListChangedNotification::new(params)) + } + NotificationFromServer::TaskStatusNotification(params) => { + Self::TaskStatusNotification(TaskStatusNotification::new(params)) + } + NotificationFromServer::LoggingMessageNotification(params) => { + Self::LoggingMessageNotification(LoggingMessageNotification::new(params)) + } + NotificationFromServer::ElicitationCompleteNotification(params) => { + Self::ElicitationCompleteNotification(ElicitationCompleteNotification::new(params)) + } + NotificationFromServer::CustomNotification(params) => { + Self::CustomNotification(JsonrpcNotification::new(params.method, params.params)) + } } } pub fn jsonrpc(&self) -> &::std::string::String { - &self.jsonrpc + match self { + ServerJsonrpcNotification::CancelledNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ProgressNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ResourceListChangedNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ResourceUpdatedNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::PromptListChangedNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ToolListChangedNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::TaskStatusNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::LoggingMessageNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::ElicitationCompleteNotification(notification) => notification.jsonrpc(), + ServerJsonrpcNotification::CustomNotification(notification) => notification.jsonrpc(), + } } } @@ -1038,45 +1320,34 @@ impl FromStr for ServerJsonrpcNotification { /// To determine standard and custom notifications received from the MCP Server /// Custom notifications are of type serde_json::Value and can be deserialized into any custom type. -#[derive(::serde::Serialize, Clone, Debug)] +#[derive(::serde::Serialize, ::serde::Deserialize, Clone, Debug)] #[serde(untagged)] pub enum NotificationFromServer { - ServerNotification(ServerNotification), - CustomNotification(serde_json::Value), -} - -impl TryFrom for ServerNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> result::Result { - if let NotificationFromServer::ServerNotification(server_notification) = value { - Ok(server_notification) - } else { - Err(RpcError::internal_error().with_message("Not a ServerNotification".to_string())) - } - } + CancelledNotification(CancelledNotificationParams), + ProgressNotification(ProgressNotificationParams), + ResourceListChangedNotification(Option), + ResourceUpdatedNotification(ResourceUpdatedNotificationParams), + PromptListChangedNotification(Option), + ToolListChangedNotification(Option), + TaskStatusNotification(TaskStatusNotificationParams), + LoggingMessageNotification(LoggingMessageNotificationParams), + ElicitationCompleteNotification(ElicitCompleteParams), + CustomNotification(CustomNotification), } impl NotificationFromServer { pub fn method(&self) -> &str { match self { - NotificationFromServer::ServerNotification(notification) => notification.method(), - NotificationFromServer::CustomNotification(notification) => notification["method"].as_str().unwrap(), - } - } -} - -impl<'de> serde::Deserialize<'de> for NotificationFromServer { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let result = ServerNotification::deserialize(&raw_value); - - match result { - Ok(client_notification) => Ok(Self::ServerNotification(client_notification)), - Err(_) => Ok(Self::CustomNotification(raw_value)), + NotificationFromServer::CancelledNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ProgressNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ResourceListChangedNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ResourceUpdatedNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::PromptListChangedNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ToolListChangedNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::TaskStatusNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::LoggingMessageNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::ElicitationCompleteNotification(_params) => CancelledNotification::method_value(), + NotificationFromServer::CustomNotification(params) => params.method.as_str(), } } } @@ -1128,56 +1399,7 @@ impl FromStr for ServerJsonrpcResponse { //*******************************// //** ResultFromServer **// //*******************************// - -/// To determine standard and custom results from the server side -/// Custom results (CustomResult) are of type serde_json::Value and can be deserialized into any custom type. -#[allow(clippy::large_enum_variant)] -#[derive(::serde::Serialize, Clone, Debug)] -#[serde(untagged)] -pub enum ResultFromServer { - ServerResult(ServerResult), - /// **Deprecated**: Use `ServerResult::Result` with extra attributes instead. - CustomResult(serde_json::Value), -} - -impl TryFrom for ServerResult { - type Error = RpcError; - fn try_from(value: ResultFromServer) -> result::Result { - if let ResultFromServer::ServerResult(server_result) = value { - Ok(server_result) - } else { - Err(RpcError::internal_error().with_message("Not a ServerResult".to_string())) - } - } -} - -impl From for ResultFromServer { - fn from(value: ServerResult) -> Self { - Self::ServerResult(value) - } -} - -impl From for ResultFromServer { - fn from(value: serde_json::Value) -> Self { - Self::CustomResult(value) - } -} - -impl<'de> serde::Deserialize<'de> for ResultFromServer { - fn deserialize(deserializer: D) -> core::result::Result - where - D: serde::Deserializer<'de>, - { - let raw_value = Value::deserialize(deserializer)?; - - let result = ServerResult::deserialize(&raw_value); - - match result { - Ok(server_result) => Ok(Self::ServerResult(server_result)), - Err(_) => Ok(Self::CustomResult(raw_value)), - } - } -} +pub type ResultFromServer = ServerResult; //***************************// //** impl for JsonrpcErrorResponse **// @@ -1277,10 +1499,38 @@ impl FromMessage for ServerMessage { MessageFromServer::RequestFromServer(request_from_server) => { let request_id = request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new( - request_id, - request_from_server, - ))) + + let rpc_message = match request_from_server { + RequestFromServer::PingRequest(params) => { + ServerJsonrpcRequest::PingRequest(PingRequest::new(request_id, params)) + } + RequestFromServer::GetTaskRequest(params) => { + ServerJsonrpcRequest::GetTaskRequest(GetTaskRequest::new(request_id, params)) + } + RequestFromServer::GetTaskPayloadRequest(params) => { + ServerJsonrpcRequest::GetTaskPayloadRequest(GetTaskPayloadRequest::new(request_id, params)) + } + RequestFromServer::CancelTaskRequest(params) => { + ServerJsonrpcRequest::CancelTaskRequest(CancelTaskRequest::new(request_id, params)) + } + RequestFromServer::ListTasksRequest(params) => { + ServerJsonrpcRequest::ListTasksRequest(ListTasksRequest::new(request_id, params)) + } + RequestFromServer::CreateMessageRequest(params) => { + ServerJsonrpcRequest::CreateMessageRequest(CreateMessageRequest::new(request_id, params)) + } + RequestFromServer::ListRootsRequest(params) => { + ServerJsonrpcRequest::ListRootsRequest(ListRootsRequest::new(request_id, params)) + } + RequestFromServer::ElicitRequest(params) => { + ServerJsonrpcRequest::ElicitRequest(ElicitRequest::new(request_id, params)) + } + RequestFromServer::CustomRequest(params) => { + ServerJsonrpcRequest::CustomRequest(JsonrpcRequest::new(request_id, params.method, params.params)) + } + }; + + Ok(ServerMessage::Request(rpc_message)) } MessageFromServer::ResultFromServer(result_from_server) => { let request_id = @@ -1299,9 +1549,10 @@ impl FromMessage for ServerMessage { notification_from_server, ))) } - MessageFromServer::Error(jsonrpc_error_error) => { - Ok(ServerMessage::Error(JsonrpcErrorResponse::new(jsonrpc_error_error, request_id))) - } + MessageFromServer::Error(jsonrpc_error_error) => Ok(ServerMessage::Error(JsonrpcErrorResponse::new( + jsonrpc_error_error, + request_id, + ))), } } } @@ -1326,12 +1577,15 @@ pub enum MessageFromClient { impl MessageFromClient { /// Returns `true` if the message is an `InitializeRequest`. pub fn is_initialize_request(&self) -> bool { - matches!(self, Self::RequestFromClient(request) if request.is_initialize_request()) + matches!(self, Self::RequestFromClient(RequestFromClient::InitializeRequest(_))) } /// Returns `true` if the message is an `InitializedNotification` pub fn is_initialized_notification(&self) -> bool { - matches!(self, Self::NotificationFromClient(notofication) if notofication.is_initialized_notification()) + matches!( + self, + Self::NotificationFromClient(NotificationFromClient::InitializedNotification(_)) + ) } } @@ -1415,9 +1669,10 @@ impl FromMessage for ClientMessage { notification_from_client, ))) } - MessageFromClient::Error(jsonrpc_error_error) => { - Ok(ClientMessage::Error(JsonrpcErrorResponse::new(jsonrpc_error_error, request_id))) - } + MessageFromClient::Error(jsonrpc_error_error) => Ok(ClientMessage::Error(JsonrpcErrorResponse::new( + jsonrpc_error_error, + request_id, + ))), } } } @@ -1818,7 +2073,6 @@ impl FromStr for StringSchemaFormat { } } - // Helper: handle all single-select enum variants fn try_from_enum_schema(map: &serde_json::Map) -> result::Result { // All enum schemas should have type: "string" (or missing, but usually present) @@ -1948,403 +2202,41 @@ impl TryFrom<&serde_json::Map> for PrimitiveSchemaDefinition { } } -#[deprecated(since = "0.4.0", note = "This trait was renamed to RpcMessage. Use RpcMessage instead.")] -pub type RPCMessage = (); -#[deprecated(since = "0.4.0", note = "This trait was renamed to McpMessage. Use McpMessage instead.")] -pub type MCPMessage = (); +pub type CustomNotification = CustomRequest; /// BEGIN AUTO GENERATED -impl ::serde::Serialize for ClientJsonrpcRequest { +impl ::serde::Serialize for ServerJsonrpcResponse { fn serialize(&self, serializer: S) -> std::result::Result where S: ::serde::Serializer, { - let mut state = serializer.serialize_struct("JsonrpcRequest", 4)?; + let mut state = serializer.serialize_struct("JsonrpcResponse", 3)?; state.serialize_field("id", &self.id)?; state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("method", &self.method)?; - use ClientRequest::*; - match &self.request { - RequestFromClient::ClientRequest(message) => match message { - InitializeRequest(msg) => state.serialize_field("params", &msg.params)?, - PingRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ListResourcesRequest(msg) => state.serialize_field("params", &msg.params)?, - ListResourceTemplatesRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ReadResourceRequest(msg) => state.serialize_field("params", &msg.params)?, - SubscribeRequest(msg) => state.serialize_field("params", &msg.params)?, - UnsubscribeRequest(msg) => state.serialize_field("params", &msg.params)?, - ListPromptsRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - GetPromptRequest(msg) => state.serialize_field("params", &msg.params)?, - ListToolsRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - CallToolRequest(msg) => state.serialize_field("params", &msg.params)?, - GetTaskRequest(msg) => state.serialize_field("params", &msg.params)?, - GetTaskPayloadRequest(msg) => state.serialize_field("params", &msg.params)?, - CancelTaskRequest(msg) => state.serialize_field("params", &msg.params)?, - ListTasksRequest(msg) => state.serialize_field("params", &msg.params)?, - SetLevelRequest(msg) => state.serialize_field("params", &msg.params)?, - CompleteRequest(msg) => state.serialize_field("params", &msg.params)?, - }, - RequestFromClient::CustomRequest(value) => state.serialize_field("params", value)?, - } + state.serialize_field("result", &self.result)?; state.end() } } -impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcRequest { +impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcResponse { fn deserialize(deserializer: D) -> std::result::Result where D: ::serde::Deserializer<'de>, { use serde::de::{self, MapAccess, Visitor}; use std::fmt; - struct ClientJsonrpcRequestVisitor; - impl<'de> Visitor<'de> for ClientJsonrpcRequestVisitor { - type Value = ClientJsonrpcRequest; + struct ServerJsonrpcResultVisitor; + impl<'de> Visitor<'de> for ServerJsonrpcResultVisitor { + type Value = ServerJsonrpcResponse; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC request object") + formatter.write_str("a valid JSON-RPC response object") } - fn visit_map(self, mut map: M) -> std::result::Result + fn visit_map(self, mut map: M) -> std::result::Result where M: MapAccess<'de>, { let mut id: Option = None; let mut jsonrpc: Option = None; - let mut method: Option = None; - let mut params: Option = None; - while let Some(key) = map.next_key::()? { - match key.as_str() { - "id" => id = Some(map.next_value()?), - "jsonrpc" => jsonrpc = Some(map.next_value()?), - "method" => method = Some(map.next_value()?), - "params" => params = Some(map.next_value()?), - _ => { - return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"])); - } - } - } - let id = id.ok_or_else(|| de::Error::missing_field("id"))?; - let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?; - let method = method.ok_or_else(|| de::Error::missing_field("method"))?; - let params = params.unwrap_or_default(); - let req_object = json!({ "method" : method, "params" : params }); - let request = serde_json::from_value::(req_object).map_err(de::Error::custom)?; - Ok(ClientJsonrpcRequest { - id, - jsonrpc, - method, - request, - }) - } - } - deserializer.deserialize_struct( - "JsonrpcRequest", - &["id", "jsonrpc", "method", "params"], - ClientJsonrpcRequestVisitor, - ) - } -} -impl ::serde::Serialize for ServerJsonrpcRequest { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: ::serde::Serializer, - { - let mut state = serializer.serialize_struct("JsonrpcRequest", 4)?; - state.serialize_field("id", &self.id)?; - state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("method", &self.method)?; - use ServerRequest::*; - match &self.request { - RequestFromServer::ServerRequest(message) => match message { - PingRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - GetTaskRequest(msg) => state.serialize_field("params", &msg.params)?, - GetTaskPayloadRequest(msg) => state.serialize_field("params", &msg.params)?, - CancelTaskRequest(msg) => state.serialize_field("params", &msg.params)?, - ListTasksRequest(msg) => state.serialize_field("params", &msg.params)?, - CreateMessageRequest(msg) => state.serialize_field("params", &msg.params)?, - ListRootsRequest(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ElicitRequest(msg) => state.serialize_field("params", &msg.params)?, - }, - RequestFromServer::CustomRequest(value) => state.serialize_field("params", value)?, - } - state.end() - } -} -impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcRequest { - fn deserialize(deserializer: D) -> std::result::Result - where - D: ::serde::Deserializer<'de>, - { - use serde::de::{self, MapAccess, Visitor}; - use std::fmt; - struct ServerJsonrpcRequestVisitor; - impl<'de> Visitor<'de> for ServerJsonrpcRequestVisitor { - type Value = ServerJsonrpcRequest; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC request object") - } - fn visit_map(self, mut map: M) -> std::result::Result - where - M: MapAccess<'de>, - { - let mut id: Option = None; - let mut jsonrpc: Option = None; - let mut method: Option = None; - let mut params: Option = None; - while let Some(key) = map.next_key::()? { - match key.as_str() { - "id" => id = Some(map.next_value()?), - "jsonrpc" => jsonrpc = Some(map.next_value()?), - "method" => method = Some(map.next_value()?), - "params" => params = Some(map.next_value()?), - _ => { - return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"])); - } - } - } - let id = id.ok_or_else(|| de::Error::missing_field("id"))?; - let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?; - let method = method.ok_or_else(|| de::Error::missing_field("method"))?; - let params = params.unwrap_or_default(); - let req_object = json!({ "method" : method, "params" : params }); - let request = serde_json::from_value::(req_object).map_err(de::Error::custom)?; - Ok(ServerJsonrpcRequest { - id, - jsonrpc, - method, - request, - }) - } - } - deserializer.deserialize_struct( - "JsonrpcRequest", - &["id", "jsonrpc", "method", "params"], - ServerJsonrpcRequestVisitor, - ) - } -} -impl ::serde::Serialize for ClientJsonrpcNotification { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: ::serde::Serializer, - { - let mut state = serializer.serialize_struct("JsonrpcNotification", 3)?; - state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("method", &self.method)?; - use ClientNotification::*; - match &self.notification { - NotificationFromClient::ClientNotification(message) => match message { - CancelledNotification(msg) => state.serialize_field("params", &msg.params)?, - InitializedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ProgressNotification(msg) => state.serialize_field("params", &msg.params)?, - TaskStatusNotification(msg) => state.serialize_field("params", &msg.params)?, - RootsListChangedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - }, - NotificationFromClient::CustomNotification(value) => state.serialize_field("params", value)?, - } - state.end() - } -} -impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcNotification { - fn deserialize(deserializer: D) -> std::result::Result - where - D: ::serde::Deserializer<'de>, - { - use serde::de::{self, MapAccess, Visitor}; - use std::fmt; - struct ClientJsonrpcNotificationVisitor; - impl<'de> Visitor<'de> for ClientJsonrpcNotificationVisitor { - type Value = ClientJsonrpcNotification; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC notification object") - } - fn visit_map(self, mut map: M) -> std::result::Result - where - M: MapAccess<'de>, - { - let mut jsonrpc: Option = None; - let mut method: Option = None; - let mut params: Option = None; - while let Some(key) = map.next_key::()? { - match key.as_str() { - "jsonrpc" => jsonrpc = Some(map.next_value()?), - "method" => method = Some(map.next_value()?), - "params" => params = Some(map.next_value()?), - _ => { - return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"])); - } - } - } - let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?; - let method = method.ok_or_else(|| de::Error::missing_field("method"))?; - let params = params.unwrap_or_default(); - let req_object = json!({ "method" : method, "params" : params }); - let notification = - serde_json::from_value::(req_object).map_err(de::Error::custom)?; - Ok(ClientJsonrpcNotification { - jsonrpc, - method, - notification, - }) - } - } - deserializer.deserialize_struct( - "JsonrpcRequest", - &["jsonrpc", "method", "params"], - ClientJsonrpcNotificationVisitor, - ) - } -} -impl ::serde::Serialize for ServerJsonrpcNotification { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: ::serde::Serializer, - { - let mut state = serializer.serialize_struct("JsonrpcNotification", 3)?; - state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("method", &self.method)?; - use ServerNotification::*; - match &self.notification { - NotificationFromServer::ServerNotification(message) => match message { - CancelledNotification(msg) => state.serialize_field("params", &msg.params)?, - ProgressNotification(msg) => state.serialize_field("params", &msg.params)?, - ResourceListChangedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ResourceUpdatedNotification(msg) => state.serialize_field("params", &msg.params)?, - PromptListChangedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - ToolListChangedNotification(msg) => { - if let Some(params) = &msg.params { - state.serialize_field("params", params)? - } - } - TaskStatusNotification(msg) => state.serialize_field("params", &msg.params)?, - LoggingMessageNotification(msg) => state.serialize_field("params", &msg.params)?, - ElicitationCompleteNotification(msg) => state.serialize_field("params", &msg.params)?, - }, - NotificationFromServer::CustomNotification(value) => state.serialize_field("params", value)?, - } - state.end() - } -} -impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcNotification { - fn deserialize(deserializer: D) -> std::result::Result - where - D: ::serde::Deserializer<'de>, - { - use serde::de::{self, MapAccess, Visitor}; - use std::fmt; - struct ServerJsonrpcNotificationVisitor; - impl<'de> Visitor<'de> for ServerJsonrpcNotificationVisitor { - type Value = ServerJsonrpcNotification; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC notification object") - } - fn visit_map(self, mut map: M) -> std::result::Result - where - M: MapAccess<'de>, - { - let mut jsonrpc: Option = None; - let mut method: Option = None; - let mut params: Option = None; - while let Some(key) = map.next_key::()? { - match key.as_str() { - "jsonrpc" => jsonrpc = Some(map.next_value()?), - "method" => method = Some(map.next_value()?), - "params" => params = Some(map.next_value()?), - _ => { - return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"])); - } - } - } - let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?; - let method = method.ok_or_else(|| de::Error::missing_field("method"))?; - let params = params.unwrap_or_default(); - let req_object = json!({ "method" : method, "params" : params }); - let notification = - serde_json::from_value::(req_object).map_err(de::Error::custom)?; - Ok(ServerJsonrpcNotification { - jsonrpc, - method, - notification, - }) - } - } - deserializer.deserialize_struct( - "JsonrpcRequest", - &["jsonrpc", "method", "params"], - ServerJsonrpcNotificationVisitor, - ) - } -} -impl ::serde::Serialize for ServerJsonrpcResponse { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: ::serde::Serializer, - { - let mut state = serializer.serialize_struct("JsonrpcResponse", 3)?; - state.serialize_field("id", &self.id)?; - state.serialize_field("jsonrpc", &self.jsonrpc)?; - state.serialize_field("result", &self.result)?; - state.end() - } -} -impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcResponse { - fn deserialize(deserializer: D) -> std::result::Result - where - D: ::serde::Deserializer<'de>, - { - use serde::de::{self, MapAccess, Visitor}; - use std::fmt; - struct ServerJsonrpcResultVisitor; - impl<'de> Visitor<'de> for ServerJsonrpcResultVisitor { - type Value = ServerJsonrpcResponse; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid JSON-RPC response object") - } - fn visit_map(self, mut map: M) -> std::result::Result - where - M: MapAccess<'de>, - { - let mut id: Option = None; - let mut jsonrpc: Option = None; - let mut result: Option = None; + let mut result: Option = None; while let Some(key) = map.next_key::()? { match key.as_str() { "id" => id = Some(map.next_value()?), @@ -2417,417 +2309,92 @@ impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcResponse { deserializer.deserialize_struct("JsonrpcResponse", &["id", "jsonrpc", "result"], ClientJsonrpcResultVisitor) } } -impl From for RequestFromClient { - fn from(value: InitializeRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: PingRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: ListResourcesRequest) -> Self { - Self::ClientRequest(value.into()) - } +/// Enum representing SDK error codes. +#[allow(non_camel_case_types)] +pub enum SdkErrorCodes { + CONNECTION_CLOSED = -32000, + REQUEST_TIMEOUT = -32001, + RESOURCE_NOT_FOUND = -32002, + BAD_REQUEST = -32015, + SESSION_NOT_FOUND = -32016, + INVALID_REQUEST = -32600, + METHOD_NOT_FOUND = -32601, + INVALID_PARAMS = -32602, + INTERNAL_ERROR = -32603, + PARSE_ERROR = -32700, + URL_ELICITATION_REQUIRED = -32042, } -impl From for RequestFromClient { - fn from(value: ListResourceTemplatesRequest) -> Self { - Self::ClientRequest(value.into()) +impl core::fmt::Display for SdkErrorCodes { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + SdkErrorCodes::CONNECTION_CLOSED => write!(f, "Connection closed"), + SdkErrorCodes::REQUEST_TIMEOUT => write!(f, "Request timeout"), + SdkErrorCodes::INVALID_REQUEST => write!(f, "Invalid request"), + SdkErrorCodes::METHOD_NOT_FOUND => write!(f, "Method not found"), + SdkErrorCodes::INVALID_PARAMS => write!(f, "Invalid params"), + SdkErrorCodes::INTERNAL_ERROR => write!(f, "Internal error"), + SdkErrorCodes::PARSE_ERROR => write!(f, "Parse Error"), + SdkErrorCodes::RESOURCE_NOT_FOUND => write!(f, "Resource not found"), + SdkErrorCodes::BAD_REQUEST => write!(f, "Bad request"), + SdkErrorCodes::SESSION_NOT_FOUND => write!(f, "Session not found"), + SdkErrorCodes::URL_ELICITATION_REQUIRED => { + write!( + f, + "A required URL was not provided. Please supply the requested URL to continue." + ) + } + } } } -impl From for RequestFromClient { - fn from(value: ReadResourceRequest) -> Self { - Self::ClientRequest(value.into()) +impl From for i64 { + fn from(code: SdkErrorCodes) -> Self { + code as i64 } } -impl From for RequestFromClient { - fn from(value: SubscribeRequest) -> Self { - Self::ClientRequest(value.into()) - } +#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)] +pub struct SdkError { + ///The error type that occurred. + pub code: i64, + ///Additional information about the error. + pub data: ::std::option::Option<::serde_json::Value>, + ///A short description of the error. + pub message: ::std::string::String, } -impl From for RequestFromClient { - fn from(value: UnsubscribeRequest) -> Self { - Self::ClientRequest(value.into()) +impl core::fmt::Display for SdkError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "MCP error {}: {}", self.code, self.message) } } -impl From for RequestFromClient { - fn from(value: ListPromptsRequest) -> Self { - Self::ClientRequest(value.into()) +impl std::error::Error for SdkError { + fn description(&self) -> &str { + &self.message } } -impl From for RequestFromClient { - fn from(value: GetPromptRequest) -> Self { - Self::ClientRequest(value.into()) +impl SdkError { + pub fn new( + error_code: SdkErrorCodes, + message: ::std::string::String, + data: ::std::option::Option<::serde_json::Value>, + ) -> Self { + Self { + code: error_code.into(), + data, + message, + } } -} -impl From for RequestFromClient { - fn from(value: ListToolsRequest) -> Self { - Self::ClientRequest(value.into()) + pub fn connection_closed() -> Self { + Self { + code: SdkErrorCodes::CONNECTION_CLOSED.into(), + data: None, + message: SdkErrorCodes::CONNECTION_CLOSED.to_string(), + } } -} -impl From for RequestFromClient { - fn from(value: CallToolRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: GetTaskRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: GetTaskPayloadRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: CancelTaskRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: ListTasksRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: SetLevelRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for RequestFromClient { - fn from(value: CompleteRequest) -> Self { - Self::ClientRequest(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: InitializeRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: PingRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListResourcesRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListResourceTemplatesRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ReadResourceRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: SubscribeRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: UnsubscribeRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListPromptsRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetPromptRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListToolsRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CallToolRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetTaskRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetTaskPayloadRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CancelTaskRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListTasksRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: SetLevelRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CompleteRequest) -> Self { - MessageFromClient::RequestFromClient(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: CancelledNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: InitializedNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: ProgressNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: TaskStatusNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for NotificationFromClient { - fn from(value: RootsListChangedNotification) -> Self { - Self::ClientNotification(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: CancelledNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: InitializedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: ProgressNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: TaskStatusNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ClientJsonrpcNotification { - fn from(value: RootsListChangedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CancelledNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: InitializedNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ProgressNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: TaskStatusNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: RootsListChangedNotification) -> Self { - MessageFromClient::NotificationFromClient(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: Result) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: GetTaskResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: GetTaskPayloadResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: CancelTaskResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: ListTasksResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: CreateMessageResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: ListRootsResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for ResultFromClient { - fn from(value: ElicitResult) -> Self { - Self::ClientResult(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: Result) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetTaskResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: GetTaskPayloadResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CancelTaskResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListTasksResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: CreateMessageResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ListRootsResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -impl From for MessageFromClient { - fn from(value: ElicitResult) -> Self { - MessageFromClient::ResultFromClient(value.into()) - } -} -/// Enum representing SDK error codes. -#[allow(non_camel_case_types)] -pub enum SdkErrorCodes { - CONNECTION_CLOSED = -32000, - REQUEST_TIMEOUT = -32001, - RESOURCE_NOT_FOUND = -32002, - BAD_REQUEST = -32015, - SESSION_NOT_FOUND = -32016, - INVALID_REQUEST = -32600, - METHOD_NOT_FOUND = -32601, - INVALID_PARAMS = -32602, - INTERNAL_ERROR = -32603, - PARSE_ERROR = -32700, - URL_ELICITATION_REQUIRED = -32042, -} -impl core::fmt::Display for SdkErrorCodes { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - SdkErrorCodes::CONNECTION_CLOSED => write!(f, "Connection closed"), - SdkErrorCodes::REQUEST_TIMEOUT => write!(f, "Request timeout"), - SdkErrorCodes::INVALID_REQUEST => write!(f, "Invalid request"), - SdkErrorCodes::METHOD_NOT_FOUND => write!(f, "Method not found"), - SdkErrorCodes::INVALID_PARAMS => write!(f, "Invalid params"), - SdkErrorCodes::INTERNAL_ERROR => write!(f, "Internal error"), - SdkErrorCodes::PARSE_ERROR => write!(f, "Parse Error"), - SdkErrorCodes::RESOURCE_NOT_FOUND => write!(f, "Resource not found"), - SdkErrorCodes::BAD_REQUEST => write!(f, "Bad request"), - SdkErrorCodes::SESSION_NOT_FOUND => write!(f, "Session not found"), - SdkErrorCodes::URL_ELICITATION_REQUIRED => { - write!( - f, - "A required URL was not provided. Please supply the requested URL to continue." - ) - } - } - } -} -impl From for i64 { - fn from(code: SdkErrorCodes) -> Self { - code as i64 - } -} -#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)] -pub struct SdkError { - ///The error type that occurred. - pub code: i64, - ///Additional information about the error. - pub data: ::std::option::Option<::serde_json::Value>, - ///A short description of the error. - pub message: ::std::string::String, -} -impl core::fmt::Display for SdkError { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "MCP error {}: {}", self.code, self.message) - } -} -impl std::error::Error for SdkError { - fn description(&self) -> &str { - &self.message - } -} -impl SdkError { - pub fn new( - error_code: SdkErrorCodes, - message: ::std::string::String, - data: ::std::option::Option<::serde_json::Value>, - ) -> Self { - Self { - code: error_code.into(), - data, - message, - } - } - pub fn connection_closed() -> Self { - Self { - code: SdkErrorCodes::CONNECTION_CLOSED.into(), - data: None, - message: SdkErrorCodes::CONNECTION_CLOSED.to_string(), - } - } - pub fn request_timeout(timeout: u128) -> Self { - Self { - code: SdkErrorCodes::REQUEST_TIMEOUT.into(), - data: Some(json!({ "timeout" : timeout })), - message: SdkErrorCodes::REQUEST_TIMEOUT.to_string(), - } + pub fn request_timeout(timeout: u128) -> Self { + Self { + code: SdkErrorCodes::REQUEST_TIMEOUT.into(), + data: Some(json!({ "timeout" : timeout })), + message: SdkErrorCodes::REQUEST_TIMEOUT.to_string(), + } } pub fn session_not_found() -> Self { Self { @@ -3017,1528 +2584,117 @@ impl RpcError { /// ``` pub fn invalid_request() -> Self { Self { - code: RpcErrorCodes::INVALID_REQUEST.into(), - data: None, - message: "Invalid request".to_string(), - } - } - /// Creates a new `RpcError` for "Internal error". - /// - /// # Example - /// ``` - /// use rust_mcp_schema::RpcError; - /// - /// let error = RpcError::internal_error(); - /// assert_eq!(error.code, -32603); - /// ``` - pub fn internal_error() -> Self { - Self { - code: RpcErrorCodes::INTERNAL_ERROR.into(), - data: None, - message: "Internal error".to_string(), - } - } - /// Creates a new `RpcError` for "Parse error". - /// - /// # Example - /// ``` - /// use rust_mcp_schema::RpcError; - /// - /// let error = RpcError::parse_error(); - /// assert_eq!(error.code, -32700); - /// ``` - pub fn parse_error() -> Self { - Self { - code: RpcErrorCodes::PARSE_ERROR.into(), - data: None, - message: "Parse error".to_string(), - } - } - /// Sets a custom error message. - /// - /// # Example - /// ``` - /// use rust_mcp_schema::RpcError; - /// - /// let error = RpcError::invalid_request().with_message("Request format is invalid".to_string()); - /// assert_eq!(error.message, "Request format is invalid".to_string()); - /// ``` - pub fn with_message(mut self, message: String) -> Self { - self.message = message; - self - } - /// Attaches optional data to the error. - /// - /// # Example - /// ``` - /// use serde_json::json; - /// use rust_mcp_schema::RpcError; - /// - /// let error = RpcError::invalid_request().with_data(Some(json!({"reason": "Missing ID"}))); - /// assert!(error.data.is_some()); - /// ``` - pub fn with_data(mut self, data: ::std::option::Option<::serde_json::Value>) -> Self { - self.data = data; - self - } -} -impl std::error::Error for RpcError { - fn description(&self) -> &str { - &self.message - } -} -impl Display for RpcError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {err}")) - ) - } -} -impl FromStr for RpcError { - type Err = RpcError; - fn from_str(s: &str) -> std::result::Result { - serde_json::from_str(s) - .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() })))) - } -} -///Constructs a new `JsonrpcErrorResponse` using the provided arguments. -impl JsonrpcErrorResponse { - pub fn create( - id: Option, - error_code: RpcErrorCodes, - error_message: ::std::string::String, - error_data: ::std::option::Option<::serde_json::Value>, - ) -> Self { - Self::new(RpcError::new(error_code, error_message, error_data), id) - } -} -impl From for NotificationFromServer { - fn from(value: CancelledNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ProgressNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ResourceListChangedNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ResourceUpdatedNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: PromptListChangedNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ToolListChangedNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: TaskStatusNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: LoggingMessageNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for NotificationFromServer { - fn from(value: ElicitationCompleteNotification) -> Self { - Self::ServerNotification(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: CancelledNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ProgressNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ResourceListChangedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ResourceUpdatedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: PromptListChangedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ToolListChangedNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: TaskStatusNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: LoggingMessageNotification) -> Self { - Self::new(value.into()) - } -} -impl From for ServerJsonrpcNotification { - fn from(value: ElicitationCompleteNotification) -> Self { - Self::new(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CancelledNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ProgressNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ResourceListChangedNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ResourceUpdatedNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: PromptListChangedNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ToolListChangedNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: TaskStatusNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: LoggingMessageNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ElicitationCompleteNotification) -> Self { - MessageFromServer::NotificationFromServer(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: PingRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: GetTaskRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: GetTaskPayloadRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: CancelTaskRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: ListTasksRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: CreateMessageRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: ListRootsRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for RequestFromServer { - fn from(value: ElicitRequest) -> Self { - Self::ServerRequest(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: PingRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetTaskRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetTaskPayloadRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CancelTaskRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListTasksRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CreateMessageRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListRootsRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ElicitRequest) -> Self { - MessageFromServer::RequestFromServer(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: Result) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: InitializeResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListResourcesResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListResourceTemplatesResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ReadResourceResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListPromptsResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: GetPromptResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListToolsResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: CallToolResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: GetTaskResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: GetTaskPayloadResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: CancelTaskResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: ListTasksResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for ResultFromServer { - fn from(value: CompleteResult) -> Self { - Self::ServerResult(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: Result) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: InitializeResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListResourcesResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListResourceTemplatesResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ReadResourceResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListPromptsResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetPromptResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListToolsResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CallToolResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetTaskResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: GetTaskPayloadResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CancelTaskResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: ListTasksResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl From for MessageFromServer { - fn from(value: CompleteResult) -> Self { - MessageFromServer::ResultFromServer(value.into()) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: InitializeRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for InitializeRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: PingRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for PingRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListResourcesRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListResourcesRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message( - message: ListResourceTemplatesRequest, - request_id: Option, - ) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListResourceTemplatesRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ReadResourceRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ReadResourceRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: SubscribeRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for SubscribeRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: UnsubscribeRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for UnsubscribeRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListPromptsRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListPromptsRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetPromptRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetPromptRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListToolsRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListToolsRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CallToolRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CallToolRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetTaskRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetTaskRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetTaskPayloadRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetTaskPayloadRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CancelTaskRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CancelTaskRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListTasksRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListTasksRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: SetLevelRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for SetLevelRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CompleteRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CompleteRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: Result, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for Result { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetTaskResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetTaskResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: GetTaskPayloadResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetTaskPayloadResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CancelTaskResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CancelTaskResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListTasksResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListTasksResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CreateMessageResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CreateMessageResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ListRootsResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListRootsResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ElicitResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ClientMessage::Response(ClientJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ElicitResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: CancelledNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for CancelledNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: InitializedNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for InitializedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: ProgressNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ProgressNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message(message: TaskStatusNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for TaskStatusNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ClientMessage { - fn from_message( - message: RootsListChangedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for RootsListChangedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ClientMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: PingRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for PingRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetTaskRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetTaskRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetTaskPayloadRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for GetTaskPayloadRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CancelTaskRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CancelTaskRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListTasksRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListTasksRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CreateMessageRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for CreateMessageRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListRootsRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ListRootsRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ElicitRequest, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into()))) - } -} -impl ToMessage for ElicitRequest { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: Result, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for Result { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: InitializeResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for InitializeResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListResourcesResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListResourcesResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ListResourceTemplatesResult, - request_id: Option, - ) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListResourceTemplatesResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ReadResourceResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ReadResourceResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListPromptsResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListPromptsResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetPromptResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetPromptResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListToolsResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListToolsResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CallToolResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CallToolResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetTaskResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetTaskResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: GetTaskPayloadResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for GetTaskPayloadResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CancelTaskResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CancelTaskResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ListTasksResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for ListTasksResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CompleteResult, request_id: Option) -> std::result::Result { - let request_id = - request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?; - Ok(ServerMessage::Response(ServerJsonrpcResponse::new( - request_id, - message.into(), - ))) - } -} -impl ToMessage for CompleteResult { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: CancelledNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for CancelledNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: ProgressNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ProgressNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ResourceListChangedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ResourceListChangedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ResourceUpdatedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ResourceUpdatedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: PromptListChangedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for PromptListChangedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ToolListChangedNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ToolListChangedNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message(message: TaskStatusNotification, request_id: Option) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for TaskStatusNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: LoggingMessageNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for LoggingMessageNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl FromMessage for ServerMessage { - fn from_message( - message: ElicitationCompleteNotification, - request_id: Option, - ) -> std::result::Result { - if request_id.is_some() { - return Err( - RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string()) - ); - } - Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into()))) - } -} -impl ToMessage for ElicitationCompleteNotification { - fn to_message(self, request_id: Option) -> std::result::Result { - ServerMessage::from_message(self, request_id) - } -} -impl TryFrom for InitializeRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::InitializeRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a InitializeRequest".to_string())) - } - } -} -impl TryFrom for PingRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::PingRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a PingRequest".to_string())) - } - } -} -impl TryFrom for ListResourcesRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListResourcesRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListResourcesRequest".to_string())) - } - } -} -impl TryFrom for ListResourceTemplatesRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListResourceTemplatesRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListResourceTemplatesRequest".to_string())) - } - } -} -impl TryFrom for ReadResourceRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ReadResourceRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ReadResourceRequest".to_string())) - } - } -} -impl TryFrom for SubscribeRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::SubscribeRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a SubscribeRequest".to_string())) - } - } -} -impl TryFrom for UnsubscribeRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::UnsubscribeRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a UnsubscribeRequest".to_string())) - } - } -} -impl TryFrom for ListPromptsRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListPromptsRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListPromptsRequest".to_string())) - } - } -} -impl TryFrom for GetPromptRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::GetPromptRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetPromptRequest".to_string())) - } - } -} -impl TryFrom for ListToolsRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListToolsRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListToolsRequest".to_string())) + code: RpcErrorCodes::INVALID_REQUEST.into(), + data: None, + message: "Invalid request".to_string(), } } -} -impl TryFrom for CallToolRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::CallToolRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CallToolRequest".to_string())) + /// Creates a new `RpcError` for "Internal error". + /// + /// # Example + /// ``` + /// use rust_mcp_schema::RpcError; + /// + /// let error = RpcError::internal_error(); + /// assert_eq!(error.code, -32603); + /// ``` + pub fn internal_error() -> Self { + Self { + code: RpcErrorCodes::INTERNAL_ERROR.into(), + data: None, + message: "Internal error".to_string(), } } -} -impl TryFrom for GetTaskRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::GetTaskRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetTaskRequest".to_string())) + /// Creates a new `RpcError` for "Parse error". + /// + /// # Example + /// ``` + /// use rust_mcp_schema::RpcError; + /// + /// let error = RpcError::parse_error(); + /// assert_eq!(error.code, -32700); + /// ``` + pub fn parse_error() -> Self { + Self { + code: RpcErrorCodes::PARSE_ERROR.into(), + data: None, + message: "Parse error".to_string(), } } -} -impl TryFrom for GetTaskPayloadRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::GetTaskPayloadRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetTaskPayloadRequest".to_string())) - } + /// Sets a custom error message. + /// + /// # Example + /// ``` + /// use rust_mcp_schema::RpcError; + /// + /// let error = RpcError::invalid_request().with_message("Request format is invalid".to_string()); + /// assert_eq!(error.message, "Request format is invalid".to_string()); + /// ``` + pub fn with_message(mut self, message: String) -> Self { + self.message = message; + self + } + /// Attaches optional data to the error. + /// + /// # Example + /// ``` + /// use serde_json::json; + /// use rust_mcp_schema::RpcError; + /// + /// let error = RpcError::invalid_request().with_data(Some(json!({"reason": "Missing ID"}))); + /// assert!(error.data.is_some()); + /// ``` + pub fn with_data(mut self, data: ::std::option::Option<::serde_json::Value>) -> Self { + self.data = data; + self } } -impl TryFrom for CancelTaskRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::CancelTaskRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CancelTaskRequest".to_string())) - } +impl std::error::Error for RpcError { + fn description(&self) -> &str { + &self.message } } -impl TryFrom for ListTasksRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::ListTasksRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListTasksRequest".to_string())) - } +impl Display for RpcError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {err}")) + ) } } -impl TryFrom for SetLevelRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::SetLevelRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a SetLevelRequest".to_string())) - } +impl FromStr for RpcError { + type Err = RpcError; + fn from_str(s: &str) -> std::result::Result { + serde_json::from_str(s) + .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() })))) } } -impl TryFrom for CompleteRequest { - type Error = RpcError; - fn try_from(value: RequestFromClient) -> std::result::Result { - let matched_type: ClientRequest = value.try_into()?; - if let ClientRequest::CompleteRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CompleteRequest".to_string())) - } +///Constructs a new `JsonrpcErrorResponse` using the provided arguments. +impl JsonrpcErrorResponse { + pub fn create( + id: Option, + error_code: RpcErrorCodes, + error_message: ::std::string::String, + error_data: ::std::option::Option<::serde_json::Value>, + ) -> Self { + Self::new(RpcError::new(error_code, error_message, error_data), id) } } -impl TryFrom for Result { +impl TryFrom for GenericResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::Result(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a Result".to_string())) + match value { + ClientResult::GetTaskPayloadResult(result) => Ok(result.into()), + ClientResult::Result(result) => Ok(result), + _ => Err(RpcError::internal_error().with_message("Not a Result".to_string())), } } } impl TryFrom for GetTaskResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::GetTaskResult(result) = matched_type { + if let ClientResult::GetTaskResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetTaskResult".to_string())) @@ -4548,8 +2704,7 @@ impl TryFrom for GetTaskResult { impl TryFrom for GetTaskPayloadResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::GetTaskPayloadResult(result) = matched_type { + if let ClientResult::GetTaskPayloadResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetTaskPayloadResult".to_string())) @@ -4559,8 +2714,7 @@ impl TryFrom for GetTaskPayloadResult { impl TryFrom for CancelTaskResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::CancelTaskResult(result) = matched_type { + if let ClientResult::CancelTaskResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CancelTaskResult".to_string())) @@ -4570,8 +2724,7 @@ impl TryFrom for CancelTaskResult { impl TryFrom for ListTasksResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::ListTasksResult(result) = matched_type { + if let ClientResult::ListTasksResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListTasksResult".to_string())) @@ -4581,8 +2734,7 @@ impl TryFrom for ListTasksResult { impl TryFrom for CreateMessageResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::CreateMessageResult(result) = matched_type { + if let ClientResult::CreateMessageResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CreateMessageResult".to_string())) @@ -4592,8 +2744,7 @@ impl TryFrom for CreateMessageResult { impl TryFrom for ListRootsResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::ListRootsResult(result) = matched_type { + if let ClientResult::ListRootsResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListRootsResult".to_string())) @@ -4603,173 +2754,27 @@ impl TryFrom for ListRootsResult { impl TryFrom for ElicitResult { type Error = RpcError; fn try_from(value: ResultFromClient) -> std::result::Result { - let matched_type: ClientResult = value.try_into()?; - if let ClientResult::ElicitResult(result) = matched_type { + if let ClientResult::ElicitResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ElicitResult".to_string())) } } } -impl TryFrom for CancelledNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::CancelledNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CancelledNotification".to_string())) - } - } -} -impl TryFrom for InitializedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::InitializedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a InitializedNotification".to_string())) - } - } -} -impl TryFrom for ProgressNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::ProgressNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ProgressNotification".to_string())) - } - } -} -impl TryFrom for TaskStatusNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::TaskStatusNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a TaskStatusNotification".to_string())) - } - } -} -impl TryFrom for RootsListChangedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromClient) -> std::result::Result { - let matched_type: ClientNotification = value.try_into()?; - if let ClientNotification::RootsListChangedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a RootsListChangedNotification".to_string())) - } - } -} -impl TryFrom for PingRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::PingRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a PingRequest".to_string())) - } - } -} -impl TryFrom for GetTaskRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::GetTaskRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetTaskRequest".to_string())) - } - } -} -impl TryFrom for GetTaskPayloadRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::GetTaskPayloadRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a GetTaskPayloadRequest".to_string())) - } - } -} -impl TryFrom for CancelTaskRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::CancelTaskRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CancelTaskRequest".to_string())) - } - } -} -impl TryFrom for ListTasksRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::ListTasksRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListTasksRequest".to_string())) - } - } -} -impl TryFrom for CreateMessageRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::CreateMessageRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CreateMessageRequest".to_string())) - } - } -} -impl TryFrom for ListRootsRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::ListRootsRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ListRootsRequest".to_string())) - } - } -} -impl TryFrom for ElicitRequest { - type Error = RpcError; - fn try_from(value: RequestFromServer) -> std::result::Result { - let matched_type: ServerRequest = value.try_into()?; - if let ServerRequest::ElicitRequest(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ElicitRequest".to_string())) - } - } -} -impl TryFrom for Result { +impl TryFrom for GenericResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::Result(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a Result".to_string())) + match value { + ServerResult::GetTaskPayloadResult(result) => Ok(result.into()), + ServerResult::Result(result) => Ok(result), + _ => Err(RpcError::internal_error().with_message("Not a Result".to_string())), } } } impl TryFrom for InitializeResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::InitializeResult(result) = matched_type { + if let ServerResult::InitializeResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a InitializeResult".to_string())) @@ -4779,8 +2784,7 @@ impl TryFrom for InitializeResult { impl TryFrom for ListResourcesResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListResourcesResult(result) = matched_type { + if let ServerResult::ListResourcesResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListResourcesResult".to_string())) @@ -4790,8 +2794,7 @@ impl TryFrom for ListResourcesResult { impl TryFrom for ListResourceTemplatesResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListResourceTemplatesResult(result) = matched_type { + if let ServerResult::ListResourceTemplatesResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListResourceTemplatesResult".to_string())) @@ -4801,8 +2804,7 @@ impl TryFrom for ListResourceTemplatesResult { impl TryFrom for ReadResourceResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ReadResourceResult(result) = matched_type { + if let ServerResult::ReadResourceResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ReadResourceResult".to_string())) @@ -4812,8 +2814,7 @@ impl TryFrom for ReadResourceResult { impl TryFrom for ListPromptsResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListPromptsResult(result) = matched_type { + if let ServerResult::ListPromptsResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListPromptsResult".to_string())) @@ -4823,8 +2824,7 @@ impl TryFrom for ListPromptsResult { impl TryFrom for GetPromptResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::GetPromptResult(result) = matched_type { + if let ServerResult::GetPromptResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetPromptResult".to_string())) @@ -4834,8 +2834,7 @@ impl TryFrom for GetPromptResult { impl TryFrom for ListToolsResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListToolsResult(result) = matched_type { + if let ServerResult::ListToolsResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListToolsResult".to_string())) @@ -4845,8 +2844,7 @@ impl TryFrom for ListToolsResult { impl TryFrom for CallToolResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::CallToolResult(result) = matched_type { + if let ServerResult::CallToolResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CallToolResult".to_string())) @@ -4856,8 +2854,7 @@ impl TryFrom for CallToolResult { impl TryFrom for GetTaskResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::GetTaskResult(result) = matched_type { + if let ServerResult::GetTaskResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetTaskResult".to_string())) @@ -4867,8 +2864,7 @@ impl TryFrom for GetTaskResult { impl TryFrom for GetTaskPayloadResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::GetTaskPayloadResult(result) = matched_type { + if let ServerResult::GetTaskPayloadResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a GetTaskPayloadResult".to_string())) @@ -4878,8 +2874,7 @@ impl TryFrom for GetTaskPayloadResult { impl TryFrom for CancelTaskResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::CancelTaskResult(result) = matched_type { + if let ServerResult::CancelTaskResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CancelTaskResult".to_string())) @@ -4889,8 +2884,7 @@ impl TryFrom for CancelTaskResult { impl TryFrom for ListTasksResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::ListTasksResult(result) = matched_type { + if let ServerResult::ListTasksResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a ListTasksResult".to_string())) @@ -4900,113 +2894,13 @@ impl TryFrom for ListTasksResult { impl TryFrom for CompleteResult { type Error = RpcError; fn try_from(value: ResultFromServer) -> std::result::Result { - let matched_type: ServerResult = value.try_into()?; - if let ServerResult::CompleteResult(result) = matched_type { + if let ServerResult::CompleteResult(result) = value { Ok(result) } else { Err(RpcError::internal_error().with_message("Not a CompleteResult".to_string())) } } } -impl TryFrom for CancelledNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::CancelledNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a CancelledNotification".to_string())) - } - } -} -impl TryFrom for ProgressNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ProgressNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ProgressNotification".to_string())) - } - } -} -impl TryFrom for ResourceListChangedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ResourceListChangedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ResourceListChangedNotification".to_string())) - } - } -} -impl TryFrom for ResourceUpdatedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ResourceUpdatedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ResourceUpdatedNotification".to_string())) - } - } -} -impl TryFrom for PromptListChangedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::PromptListChangedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a PromptListChangedNotification".to_string())) - } - } -} -impl TryFrom for ToolListChangedNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ToolListChangedNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ToolListChangedNotification".to_string())) - } - } -} -impl TryFrom for TaskStatusNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::TaskStatusNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a TaskStatusNotification".to_string())) - } - } -} -impl TryFrom for LoggingMessageNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::LoggingMessageNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a LoggingMessageNotification".to_string())) - } - } -} -impl TryFrom for ElicitationCompleteNotification { - type Error = RpcError; - fn try_from(value: NotificationFromServer) -> std::result::Result { - let matched_type: ServerNotification = value.try_into()?; - if let ServerNotification::ElicitationCompleteNotification(result) = matched_type { - Ok(result) - } else { - Err(RpcError::internal_error().with_message("Not a ElicitationCompleteNotification".to_string())) - } - } -} impl ContentBlock { ///Create a ContentBlock::TextContent pub fn text_content(text: ::std::string::String) -> Self { @@ -5158,74 +3052,111 @@ impl CallToolResult { self } } +impl ServerRequest { + pub fn request_id(&self) -> &RequestId { + match self { + ServerRequest::PingRequest(request) => &request.id, + ServerRequest::GetTaskRequest(request) => &request.id, + ServerRequest::GetTaskPayloadRequest(request) => &request.id, + ServerRequest::CancelTaskRequest(request) => &request.id, + ServerRequest::ListTasksRequest(request) => &request.id, + ServerRequest::CreateMessageRequest(request) => &request.id, + ServerRequest::ListRootsRequest(request) => &request.id, + ServerRequest::ElicitRequest(request) => &request.id, + } + } +} +impl ClientRequest { + pub fn request_id(&self) -> &RequestId { + match self { + ClientRequest::InitializeRequest(request) => &request.id, + ClientRequest::PingRequest(request) => &request.id, + ClientRequest::ListResourcesRequest(request) => &request.id, + ClientRequest::ListResourceTemplatesRequest(request) => &request.id, + ClientRequest::ReadResourceRequest(request) => &request.id, + ClientRequest::SubscribeRequest(request) => &request.id, + ClientRequest::UnsubscribeRequest(request) => &request.id, + ClientRequest::ListPromptsRequest(request) => &request.id, + ClientRequest::GetPromptRequest(request) => &request.id, + ClientRequest::ListToolsRequest(request) => &request.id, + ClientRequest::CallToolRequest(request) => &request.id, + ClientRequest::GetTaskRequest(request) => &request.id, + ClientRequest::GetTaskPayloadRequest(request) => &request.id, + ClientRequest::CancelTaskRequest(request) => &request.id, + ClientRequest::ListTasksRequest(request) => &request.id, + ClientRequest::SetLevelRequest(request) => &request.id, + ClientRequest::CompleteRequest(request) => &request.id, + } + } +} /// END AUTO GENERATED #[cfg(test)] mod tests { - // use super::*; - // use serde_json::json; - - // #[test] - // fn test_detect_message_type() { - // // standard request - // let message = ClientJsonrpcRequest::new(RequestId::Integer(0), PingRequest::new(None).into()); - // let result = detect_message_type(&json!(message)); - // assert!(matches!(result, MessageTypes::Request)); - - // // custom request - - // let result = detect_message_type(&json!({ - // "id":0, - // "method":"add_numbers", - // "params":{}, - // "jsonrpc":"2.0" - // })); - // assert!(matches!(result, MessageTypes::Request)); - - // // standard notification - // let message = ClientJsonrpcNotification::new(RootsListChangedNotification::new(None).into()); - // let result = detect_message_type(&json!(message)); - // assert!(matches!(result, MessageTypes::Notification)); - - // // custom notification - // let result = detect_message_type(&json!({ - // "method":"notifications/email_sent", - // "jsonrpc":"2.0" - // })); - // assert!(matches!(result, MessageTypes::Notification)); - - // // standard response - // let message = ClientJsonrpcResponse::new( - // RequestId::Integer(0), - // ListRootsResult { - // meta: None, - // roots: vec![], - // } - // .into(), - // ); - // let result = detect_message_type(&json!(message)); - // assert!(matches!(result, MessageTypes::Response)); - - // //custom response - - // let result = detect_message_type(&json!({ - // "id":1, - // "jsonrpc":"2.0", - // "result":"{}", - // })); - // assert!(matches!(result, MessageTypes::Response)); - - // // error message - // let message = JsonrpcErrorResponse::create( - // RequestId::Integer(0), - // RpcErrorCodes::INVALID_PARAMS, - // "Invalid params!".to_string(), - // None, - // ); - // let result = detect_message_type(&json!(message)); - // assert!(matches!(result, MessageTypes::Error)); - - // // default - // let result = detect_message_type(&json!({})); - // assert!(matches!(result, MessageTypes::Request)); - // } + use super::*; + use serde_json::json; + + #[test] + fn test_detect_message_type() { + // standard request + let message = ClientJsonrpcRequest::new(RequestId::Integer(0), RequestFromClient::PingRequest(None)); + let result = detect_message_type(&json!(message)); + assert!(matches!(result, MessageTypes::Request)); + + // custom request + + let result = detect_message_type(&json!({ + "id":0, + "method":"add_numbers", + "params":{}, + "jsonrpc":"2.0" + })); + assert!(matches!(result, MessageTypes::Request)); + + // standard notification + let message = ClientJsonrpcNotification::new(NotificationFromClient::RootsListChangedNotification(None)); + let result = detect_message_type(&json!(message)); + assert!(matches!(result, MessageTypes::Notification)); + + // custom notification + let result = detect_message_type(&json!({ + "method":"notifications/email_sent", + "jsonrpc":"2.0" + })); + assert!(matches!(result, MessageTypes::Notification)); + + // standard response + let message = ClientJsonrpcResponse::new( + RequestId::Integer(0), + ListRootsResult { + meta: None, + roots: vec![], + } + .into(), + ); + let result = detect_message_type(&json!(message)); + assert!(matches!(result, MessageTypes::Response)); + + //custom response + + let result = detect_message_type(&json!({ + "id":1, + "jsonrpc":"2.0", + "result":"{}", + })); + assert!(matches!(result, MessageTypes::Response)); + + // error message + let message = JsonrpcErrorResponse::create( + Some(RequestId::Integer(0)), + RpcErrorCodes::INVALID_PARAMS, + "Invalid params!".to_string(), + None, + ); + let result = detect_message_type(&json!(message)); + assert!(matches!(result, MessageTypes::Error)); + + // default + let result = detect_message_type(&json!({})); + assert!(matches!(result, MessageTypes::Request)); + } } diff --git a/src/generated_schema/protocol_version.rs b/src/generated_schema/protocol_version.rs index 55e2e7f..06b2ff6 100644 --- a/src/generated_schema/protocol_version.rs +++ b/src/generated_schema/protocol_version.rs @@ -4,6 +4,7 @@ pub enum ProtocolVersion { V2024_11_05, V2025_03_26, V2025_06_18, + V2025_11_25, Draft, } impl ProtocolVersion { @@ -12,6 +13,7 @@ impl ProtocolVersion { ProtocolVersion::V2024_11_05, ProtocolVersion::V2025_03_26, ProtocolVersion::V2025_06_18, + ProtocolVersion::V2025_11_25, ]; if include_draft { versions.push(ProtocolVersion::Draft); @@ -25,10 +27,17 @@ impl Display for ProtocolVersion { ProtocolVersion::V2024_11_05 => write!(f, "2024-11-05"), ProtocolVersion::V2025_03_26 => write!(f, "2025-03-26"), ProtocolVersion::V2025_06_18 => write!(f, "2025-06-18"), - ProtocolVersion::Draft => write!(f, "DRAFT-2025-v3"), + ProtocolVersion::V2025_11_25 => write!(f, "2025-11-25"), + ProtocolVersion::Draft => write!(f, "DRAFT-2026-v1"), } } } +#[allow(clippy::from_over_into)] +impl Into for ProtocolVersion { + fn into(self) -> String { + self.to_string() + } +} #[derive(Debug)] pub struct ParseProtocolVersionError { details: String, @@ -55,7 +64,8 @@ impl TryFrom<&str> for ProtocolVersion { "2024-11-05" => Ok(ProtocolVersion::V2024_11_05), "2025-03-26" => Ok(ProtocolVersion::V2025_03_26), "2025-06-18" => Ok(ProtocolVersion::V2025_06_18), - "DRAFT-2025-v3" => Ok(ProtocolVersion::Draft), + "2025-11-25" => Ok(ProtocolVersion::V2025_11_25), + "DRAFT-2026-v1" => Ok(ProtocolVersion::Draft), "DRAFT" => Ok(ProtocolVersion::Draft), other => Err(ParseProtocolVersionError { details: other.to_string(), diff --git a/tests/v2025_11_25/test_schema_utils.rs b/tests/v2025_11_25/test_schema_utils.rs index 47c1ca2..0e2d68d 100644 --- a/tests/v2025_11_25/test_schema_utils.rs +++ b/tests/v2025_11_25/test_schema_utils.rs @@ -268,3 +268,23 @@ mod tests_schema_utils { assert!(matches!(result, PrimitiveSchemaDefinition::UntitledSingleSelectEnumSchema(_))); } } + +#[test] +fn adhoc() { + use rust_mcp_schema::mcp_2025_11_25::schema_utils::*; + use rust_mcp_schema::mcp_2025_11_25::*; + + let str = r#"{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{"sampling":{},"elicitation":{},"roots":{"listChanged":true}},"clientInfo":{"name":"inspector-client","version":"0.17.2"}}}"#; + + let msg: ClientMessage = serde_json::from_str(str).unwrap(); + assert!(matches!( + msg, + ClientMessage::Request(ClientJsonrpcRequest::InitializeRequest(_)) + )); + + let msg: ClientJsonrpcRequest = serde_json::from_str(str).unwrap(); + assert!(matches!(msg, ClientJsonrpcRequest::InitializeRequest(_))); + + let msg: ClientRequest = serde_json::from_str(str).unwrap(); + assert!(matches!(msg, ClientRequest::InitializeRequest(_))); +}