# rust-mcp-schema

> MCP Client

Rust implementation of the Model Context Protocol (MCP) schema. Type-safe data structures for model context exchange.

## Overview

- **Category:** Developer Tools
- **Language:** Rust
- **Stars:** 75
- **Forks:** 4
- **Owner:** rust-mcp-stack
- **GitHub:** https://github.com/rust-mcp-stack/rust-mcp-schema
- **Created:** 2025-02-08T18:21:21+00:00
- **Updated:** 2025-07-01T18:08:58+00:00
- **Source:** https://model-context-protocol.com/clients/rust-mcp-schema

## Setup

## Setup

To use the `rust-mcp-schema` crate in your Rust project, follow these steps:

**1. Prerequisites:**

*   Ensure you have Rust and Cargo installed. You can download them from [https://www.rust-lang.org/](https://www.rust-lang.org/).

**2. Installation:**

Add the following to your `Cargo.toml` file:

```toml
[dependencies]
rust-mcp-schema = "0.7.1" # Replace with the desired version
```

This will include the latest version of the MCP Protocol schema by default.

**3. Switching Schema Versions (Optional):**

If you need to use a specific schema version, disable the default features and enable the feature corresponding to the desired version.  You can enable multiple schema versions concurrently if needed.

*   **Example: Enable the `2024_11_05` schema version:**

    ```toml
    [dependencies]
    rust-mcp-schema = { version = "0.7.1", default-features = false, features = ["2024_11_05"] }
    ```

*   **Example: Enable the `draft` schema version:**

    ```toml
    [dependencies]
    rust-mcp-schema = { version = "0.7.1", default-features = false, features = ["draft"] }
    ```

**4. Using the Crate:**

In your Rust code, import the crate and use the desired schema version.  If you are using a specific version, access it through its module name.

```rust
use rust_mcp_schema; // For the default (latest) version

// For a specific version (e.g., 2024_11_05):
#[cfg(feature = "2024_11_05")]
use rust_mcp_schema::mcp_2024_11_05 as mcp;

// Example usage (assuming you've enabled the 2024_11_05 feature):
#[cfg(feature = "2024_11_05")]
fn main() {
    // Access types from the specific schema version
    let _ = mcp::InitializeRequest {
        client_info: None,
        capabilities: None,
        protocol_version: None,
        _meta: None,
    };

    println!("Using the 2024_11_05 schema.");
}

#[cfg(not(feature = "2024_11_05"))]
fn main() {
    println!("Using the default schema.");
}
```

**5. Enabling `schema_utils` (Optional):**

The `schema_utils` module is enabled by default. If you want to explicitly enable it (or re-enable it after disabling default features), ensure the `schema_utils` feature is included:

```toml
[dependencies]
rust-mcp-schema = { version = "0.7.1", features = ["schema_utils"] }
```

Then, you can access it in your code:

```rust
use rust_mcp_schema::schema_utils;
```

**Note:** There are no specific environment variables or configuration files required for this crate. The schema version is controlled through Cargo features.

## Tools

## Available Tools

- **Type-safe MCP Protocol Implementation:** Provides a type-safe Rust implementation of the Model Context Protocol (MCP) specification, ensuring data integrity and reducing runtime errors.

- **Auto-Generated Schemas:** Schemas are automatically generated and synchronized with the official MCP schema specifications, guaranteeing up-to-date alignment with the latest protocol definitions.

- **Comprehensive Schema Versions:** Includes all official released versions of the MCP schema, such as `2025_06_18`, `2025_03_26`, `2024_11_05`, and a `draft` version for early adoption, allowing developers to work with different protocol versions as needed.

  *Example:* To enable a specific schema version, add it as a feature in your `Cargo.toml`:

  ```toml
  # Cargo.toml
  rust-mcp-schema = { version: "0.7.1", default-features = false, features=["2024_11_05"] }
  ```

- **`schema_utils` Module:** A complimentary schema utility module that enhances productivity and ensures development integrity by providing strongly-typed objects and implementations without modifying the originally generated schema. It divides the unified `JsonrpcMessage` type into `ClientMessage` and `ServerMessage` for improved type safety.

  *Example:* Using `schema_utils` to detect an `InitializeRequest` message on an MCP server:

  ```rs
  use rust_mcp_schema::schema_utils::{ClientMessage, RequestFromClient};
  use rust_mcp_schema::ClientRequest;

  pub fn handle_message(message_payload: &str)->Result<Error> {
      let message = ClientMessage::from_str(&message_payload)?;

      if let ClientMessage::Request(message_object) = message {
          if let RequestFromClient::ClientRequest(client_request) = message_object.request {
              if let ClientRequest::InitializeRequest(initialize_request) = client_request {
                  handle_initialize_request(initialize_request);
              }
          }
      }
  }
  ```
