Skip to content

feat: introduce Cargo features to isolate client and server code #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ members = [
"examples/hello-world-mcp-server-core",
]


[workspace.dependencies]
# Workspace member crates
rust-mcp-transport = { version = "0.2.0", path = "crates/rust-mcp-transport" }
rust-mcp-sdk = { path = "crates/rust-mcp-sdk" }
rust-mcp-sdk = { path = "crates/rust-mcp-sdk", default-features = false }
rust-mcp-macros = { version = "0.2.0", path = "crates/rust-mcp-macros" }

# External crates
Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,55 @@ Here is the output :

If you are looking for a step-by-step tutorial on how to get started with `rust-mcp-sdk` , please see : [Getting Started MCP Server](https://github.com/rust-mcp-stack/rust-mcp-sdk/tree/main/doc/getting-started-mcp-server.md)

## Features

The `rust-mcp-sdk` crate provides three optional features: `server` , `client` and `macros`. By default, all features are enabled for maximum functionality. You can customize which features to include based on your project's needs.

### Available Features

- `server`: Activates MCP server capabilities in `rust-mcp-sdk`, providing modules and APIs for building and managing MCP services.
- `client`: Activates MCP client capabilities, offering modules and APIs for client development and communicating with MCP servers.
- `macros`: Provides procedural macros for simplifying the creation and manipulation of MCP Tool structures.

### Default Behavior

All features (server, client, and macros) are enabled by default. When you include rust-mcp-sdk as a dependency without specifying features, all will be included:

<!-- x-release-please-start-version -->

```toml
[dependencies]
rust-mcp-sdk = "0.2.0"
```

<!-- x-release-please-end -->

### Using Only the server Feature

If you only need the MCP Server functionality, you can disable the default features and explicitly enable the server feature. Add the following to your Cargo.toml:

<!-- x-release-please-start-version -->

```toml
[dependencies]
rust-mcp-sdk = { version = "0.2.0", default-features = false, features = ["server","macros"] }
```

<!-- x-release-please-end -->

### Using Only the client Feature

If you only need the MCP Client functionality, you can disable the default features and explicitly enable the client feature. Add the following to your Cargo.toml:

<!-- x-release-please-start-version -->

```toml
[dependencies]
rust-mcp-sdk = { version = "0.2.0", default-features = false, features = ["client"] }
```

<!-- x-release-please-end -->

### Choosing Between `mcp_server_handler` and `mcp_server_handler_core`

[rust-mcp-sdk](https://github.com/rust-mcp-stack/rust-mcp-sdk) provides two type of handler traits that you can chose from:
Expand Down
5 changes: 4 additions & 1 deletion crates/rust-mcp-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ futures = { workspace = true }
thiserror = { workspace = true }

[features]
default = ["macros"] # Default features
default = ["client", "server", "macros"] # All features enabled by default
server = [] # Server feature
client = [] # Client feature
macros = ["rust-mcp-macros"]


[lints]
workspace = true
49 changes: 49 additions & 0 deletions crates/rust-mcp-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,55 @@ Here is the output :

If you are looking for a step-by-step tutorial on how to get started with `rust-mcp-sdk` , please see : [Getting Started MCP Server](https://github.com/rust-mcp-stack/rust-mcp-sdk/tree/main/doc/getting-started-mcp-server.md)

## Features

The `rust-mcp-sdk` crate provides three optional features: `server` , `client` and `macros`. By default, all features are enabled for maximum functionality. You can customize which features to include based on your project's needs.

### Available Features

- `server`: Activates MCP server capabilities in `rust-mcp-sdk`, providing modules and APIs for building and managing MCP services.
- `client`: Activates MCP client capabilities, offering modules and APIs for client development and communicating with MCP servers.
- `macros`: Provides procedural macros for simplifying the creation and manipulation of MCP Tool structures.

### Default Behavior

All features (server, client, and macros) are enabled by default. When you include rust-mcp-sdk as a dependency without specifying features, all will be included:

<!-- x-release-please-start-version -->

```toml
[dependencies]
rust-mcp-sdk = "0.2.0"
```

<!-- x-release-please-end -->

### Using Only the server Feature

If you only need the MCP Server functionality, you can disable the default features and explicitly enable the server feature. Add the following to your Cargo.toml:

<!-- x-release-please-start-version -->

```toml
[dependencies]
rust-mcp-sdk = { version = "0.2.0", default-features = false, features = ["server","macros"] }
```

<!-- x-release-please-end -->

### Using Only the client Feature

If you only need the MCP Client functionality, you can disable the default features and explicitly enable the client feature. Add the following to your Cargo.toml:

<!-- x-release-please-start-version -->

```toml
[dependencies]
rust-mcp-sdk = { version = "0.2.0", default-features = false, features = ["client"] }
```

<!-- x-release-please-end -->

### Choosing Between `mcp_server_handler` and `mcp_server_handler_core`

[rust-mcp-sdk](https://github.com/rust-mcp-stack/rust-mcp-sdk) provides two type of handler traits that you can chose from:
Expand Down
6 changes: 6 additions & 0 deletions crates/rust-mcp-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod mcp_runtimes;
mod mcp_traits;
mod utils;

#[cfg(feature = "client")]
pub mod mcp_client {
//! Includes the runtimes and traits required to create a type-safe MCP client.
//!
Expand Down Expand Up @@ -35,6 +36,7 @@ pub mod mcp_client {
pub use super::mcp_runtimes::client_runtime::ClientRuntime;
}

#[cfg(feature = "server")]
pub mod mcp_server {
//! Includes the runtimes and traits required to create a type-safe MCP server.
//!
Expand Down Expand Up @@ -66,9 +68,13 @@ pub mod mcp_server {
pub use super::mcp_runtimes::server_runtime::ServerRuntime;
}

#[cfg(feature = "client")]
pub use mcp_traits::mcp_client::*;

#[cfg(feature = "server")]
pub use mcp_traits::mcp_server::*;

pub use rust_mcp_transport::error::*;
pub use rust_mcp_transport::*;

#[cfg(feature = "macros")]
Expand Down
4 changes: 4 additions & 0 deletions crates/rust-mcp-sdk/src/mcp_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#[cfg(feature = "client")]
pub mod mcp_client_handler;
#[cfg(feature = "client")]
pub mod mcp_client_handler_core;
#[cfg(feature = "server")]
pub mod mcp_server_handler;
#[cfg(feature = "server")]
pub mod mcp_server_handler_core;
2 changes: 2 additions & 0 deletions crates/rust-mcp-sdk/src/mcp_runtimes.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#[cfg(feature = "client")]
pub mod client_runtime;
#[cfg(feature = "server")]
pub mod server_runtime;
2 changes: 2 additions & 0 deletions crates/rust-mcp-sdk/src/mcp_traits.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "client")]
pub mod mcp_client;
pub mod mcp_handler;
#[cfg(feature = "server")]
pub mod mcp_server;
22 changes: 14 additions & 8 deletions crates/rust-mcp-sdk/src/mcp_traits/mcp_handler.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use async_trait::async_trait;
use rust_mcp_schema::{
schema_utils::{
NotificationFromClient, NotificationFromServer, RequestFromClient, RequestFromServer,
ResultFromClient, ResultFromServer,
},
RpcError,
};

#[cfg(feature = "server")]
use rust_mcp_schema::schema_utils::{NotificationFromClient, RequestFromClient, ResultFromServer};

#[cfg(feature = "client")]
use rust_mcp_schema::schema_utils::{NotificationFromServer, RequestFromServer, ResultFromClient};

use rust_mcp_schema::RpcError;

use crate::error::SdkResult;

use super::{mcp_client::McpClient, mcp_server::McpServer};
#[cfg(feature = "client")]
use super::mcp_client::McpClient;
#[cfg(feature = "server")]
use super::mcp_server::McpServer;

#[cfg(feature = "server")]
#[async_trait]
pub trait McpServerHandler: Send + Sync {
async fn on_server_started(&self, runtime: &dyn McpServer);
Expand All @@ -28,6 +33,7 @@ pub trait McpServerHandler: Send + Sync {
) -> SdkResult<()>;
}

#[cfg(feature = "client")]
#[async_trait]
pub trait McpClientHandler: Send + Sync {
async fn handle_request(
Expand Down
7 changes: 4 additions & 3 deletions examples/hello-world-mcp-server-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ license = "MIT"


[dependencies]
rust-mcp-sdk = { workspace = true }
rust-mcp-transport = { workspace = true }
rust-mcp-macros = { workspace = true }
rust-mcp-sdk = { workspace = true, default-features = false, features = [
"server",
"macros",
] }
rust-mcp-schema = { workspace = true }

tokio = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions examples/hello-world-mcp-server-core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use rust_mcp_schema::{
Implementation, InitializeResult, ServerCapabilities, ServerCapabilitiesTools,
LATEST_PROTOCOL_VERSION,
};
use rust_mcp_sdk::McpServer;
use rust_mcp_sdk::{error::SdkResult, mcp_server::server_runtime_core};
use rust_mcp_transport::{StdioTransport, TransportOptions};
use rust_mcp_sdk::{
error::SdkResult, mcp_server::server_runtime_core, McpServer, StdioTransport, TransportOptions,
};

#[tokio::main]
async fn main() -> SdkResult<()> {
Expand Down
6 changes: 4 additions & 2 deletions examples/hello-world-mcp-server-core/src/tools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use rust_mcp_macros::{mcp_tool, JsonSchema};
use rust_mcp_schema::{schema_utils::CallToolError, CallToolResult};
use rust_mcp_sdk::tool_box;
use rust_mcp_sdk::{
macros::{mcp_tool, JsonSchema},
tool_box,
};

//****************//
// SayHelloTool //
Expand Down
7 changes: 4 additions & 3 deletions examples/hello-world-mcp-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ license = "MIT"


[dependencies]
rust-mcp-sdk = { workspace = true }
rust-mcp-transport = { workspace = true }
rust-mcp-macros = { workspace = true }
rust-mcp-sdk = { workspace = true, default-features = false, features = [
"server",
"macros",
] }
rust-mcp-schema = { workspace = true }

tokio = { workspace = true }
Expand Down
4 changes: 1 addition & 3 deletions examples/hello-world-mcp-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ use rust_mcp_schema::{
use rust_mcp_sdk::{
error::SdkResult,
mcp_server::{server_runtime, ServerRuntime},
McpServer,
McpServer, StdioTransport, TransportOptions,
};

use rust_mcp_transport::{StdioTransport, TransportOptions};

#[tokio::main]
async fn main() -> SdkResult<()> {
// STEP 1: Define server details and capabilities
Expand Down
6 changes: 4 additions & 2 deletions examples/hello-world-mcp-server/src/tools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use rust_mcp_macros::{mcp_tool, JsonSchema};
use rust_mcp_schema::{schema_utils::CallToolError, CallToolResult};
use rust_mcp_sdk::tool_box;
use rust_mcp_sdk::{
macros::{mcp_tool, JsonSchema},
tool_box,
};

//****************//
// SayHelloTool //
Expand Down
6 changes: 4 additions & 2 deletions examples/simple-mcp-client-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ license = "MIT"


[dependencies]
rust-mcp-sdk = { workspace = true }
rust-mcp-transport = { workspace = true }
rust-mcp-sdk = { workspace = true, default-features = false, features = [
"client",
"macros",
] }
rust-mcp-schema = { workspace = true }

tokio = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions examples/simple-mcp-client-core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use inquiry_utils::InquiryUtils;
use rust_mcp_schema::{
ClientCapabilities, Implementation, InitializeRequestParams, JSONRPC_VERSION,
};
use rust_mcp_sdk::McpClient;
use rust_mcp_sdk::{error::SdkResult, mcp_client::client_runtime_core};
use rust_mcp_transport::{StdioTransport, TransportOptions};
use rust_mcp_sdk::{McpClient, StdioTransport, TransportOptions};
use std::sync::Arc;

const MCP_SERVER_TO_LAUNCH: &str = "@modelcontextprotocol/server-everything";
Expand Down
7 changes: 4 additions & 3 deletions examples/simple-mcp-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ license = "MIT"


[dependencies]

rust-mcp-sdk = { workspace = true }
rust-mcp-transport = { workspace = true }
rust-mcp-sdk = { workspace = true, default-features = false, features = [
"client",
"macros",
] }
rust-mcp-schema = { workspace = true }

tokio = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions examples/simple-mcp-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use rust_mcp_schema::{
};
use rust_mcp_sdk::error::SdkResult;
use rust_mcp_sdk::mcp_client::client_runtime;
use rust_mcp_sdk::McpClient;
use rust_mcp_transport::{StdioTransport, TransportOptions};
use rust_mcp_sdk::{McpClient, StdioTransport, TransportOptions};
use std::sync::Arc;

const MCP_SERVER_TO_LAUNCH: &str = "@modelcontextprotocol/server-everything";
Expand Down