Function enforce_compatible_protocol_version

Source
pub fn enforce_compatible_protocol_version(
    client_protocol_version: &str,
    server_protocol_version: &str,
) -> SdkResult<Option<String>>
Expand description

Enforces protocol version compatibility on for MCP Server , allowing the client to use a lower or equal version.

This function compares the client and server protocol versions. If the client version is higher than the server version, it returns an error indicating incompatibility. If the versions are equal, it returns Ok(None), indicating no downgrade is needed. If the client version is lower, it returns Ok(Some(client_protocol_version)), suggesting the server can use the client’s version for compatibility.

§Arguments

  • client_protocol_version - The client’s protocol version.
  • server_protocol_version - The server’s protocol version.

§Returns

  • Ok(None) if the versions are equal, indicating no downgrade is needed.
  • Ok(Some(client_protocol_version)) if the client version is lower, returning the client version to use for compatibility.
  • Err(McpSdkError::IncompatibleProtocolVersion) if the client version is higher, containing the client and server versions as strings.

§Examples

use rust_mcp_sdk::mcp_server::enforce_compatible_protocol_version;
use rust_mcp_sdk::error::McpSdkError;

// Equal versions
let result = enforce_compatible_protocol_version("2024_11_05", "2024_11_05");
assert!(matches!(result, Ok(None)));

// Client version lower (downgrade allowed)
let result = enforce_compatible_protocol_version("2024_11_05", "2025_03_26");
assert!(matches!(result, Ok(Some(ref v)) if v == "2024_11_05"));

// Client version higher (incompatible)
let result = enforce_compatible_protocol_version("2025_03_26", "2024_11_05");
assert!(matches!(
    result,
    Err(McpSdkError::IncompatibleProtocolVersion(client, server))
    if client == "2025_03_26" && server == "2024_11_05"
));