### Detecting InitializeRequest Message on MCP Server (Rust) Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/README.md This Rust example illustrates how an MCP server can detect and process an `InitializeRequest` message without relying on `schema_utils`. It deserializes the JSON payload into a `JsonrpcMessage`, verifies it's a `Request` type with the method "initialize", and then extracts and prints the `InitializeRequestParams`. ```Rust fn handle_message(message_payload: &str) -> std::result::Result<(), AppError> { // Deserialize json string into JsonrpcMessage let message: JsonrpcMessage = serde_json::from_str(message_payload)?; // Check it's a Request type of message if let JsonrpcMessage::Request(client_message) = message { // Check method to detect is a "initialize" request if client_message.method == "initialize" { // 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(); // print out InitializeRequestParams param values println!( "client_info : {:?} \ncapabilities: {:?} \nprotocol_version: {:?}", params.client_info, params.capabilities, params.protocol_version ); } } } Ok(()) } ``` -------------------------------- ### Create InitializeResult Response on MCP Server (Rust) Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/README.md This Rust code snippet illustrates the construction of an InitializeResult object, populating its various fields like capabilities, instructions, and server_info. It then demonstrates how to convert this result into a ServerMessage and serialize it into a JSON string suitable for sending back to an MCP client. ```Rust // 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 a ServerMessage (a message intended to be sent from the server) let message: ServerMessage = initial_result.to_message(request_id).unwrap(); // Serialize the MCP message into a valid JSON string for sending to the client let json_payload = message.to_string(); println!("{}", json_payload); ``` ```JSON { "id": 15, "jsonrpc": "2.0", "result": { "capabilities": { "prompts": { "listChanged": true }, "resources": { "listChanged": true, "subscribe": true }, "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" } } } ``` -------------------------------- ### Run Tests for All Schema Versions Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/development.md Executes a shell script to run tests against all available schema versions. This ensures comprehensive testing and compatibility across different schema iterations, verifying backward and forward compatibility. ```sh ./scripts/run_test.sh ``` -------------------------------- ### Run Latest Schema Tests using Alias Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/development.md An alias command to execute the test suite for the latest version of the schema. It provides a convenient shortcut for running tests, simplifying the testing workflow. ```sh cargo run_test ``` -------------------------------- ### Format Rust Code with `cargo fmt` Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/development.md This command formats Rust source code according to the default `rustfmt` style. It ensures consistent code style across the project by automatically applying formatting rules. ```sh cargo fmt ``` -------------------------------- ### Lint Rust Code with Clippy Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/development.md This command runs Clippy, a Rust linter, to check for common mistakes and idiomatic issues in the code. It helps maintain code quality and identify potential bugs or non-idiomatic patterns. ```sh cargo clippy ``` -------------------------------- ### Enable Draft MCP Schema Version in Cargo.toml Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/README.md This TOML snippet shows how to enable the `draft` version of the `rust-mcp-schema` crate in your `Cargo.toml`. Similar to enabling a specific dated version, it disables default features and activates the `draft` feature, providing access to the latest development schema. ```TOML #Cargo.toml rust-mcp-schema = { version: 0.7.1 , default-features = false, features=["draft"] } ``` -------------------------------- ### Run Latest Schema Tests with `cargo nextest` Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/development.md Executes the test suite for the latest version of the schema using `cargo-nextest`. This command is used to verify the functionality and correctness of the current schema implementation. ```sh cargo nextest run ``` -------------------------------- ### Detect InitializeRequest Message on MCP Server (Rust) Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/README.md This Rust code demonstrates how an MCP server can deserialize an incoming JSON message into a ClientMessage enum. It then shows how to pattern match to identify if the message is a Request and specifically an InitializeRequest, allowing for further processing. ```Rust 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); } } } } ``` -------------------------------- ### Enable Specific MCP Schema Version in Cargo.toml Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/README.md This TOML snippet demonstrates how to configure your `Cargo.toml` file to enable a specific version of the `rust-mcp-schema` crate. It sets `default-features` to `false` and explicitly enables the `2024_11_05` feature, allowing access to that particular schema version. ```TOML # Cargo.toml rust-mcp-schema = { version: 0.7.1 , default-features = false, features=["2024_11_05"] } ``` -------------------------------- ### Detecting InitializeResult Response in MCP Client (Rust) Source: https://github.com/rust-mcp-stack/rust-mcp-schema/blob/main/README.md This Rust code snippet demonstrates how to handle an `InitializeResult` response message received by an MCP client. It deserializes the incoming payload, checks if it's a `ServerMessage::Response` containing a `ServerResult::InitializeResult`, and then processes it by calling `handle_initialize_result`. ```Rust 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. 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); } } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.