refactor: fix error introduced by merge, and reorganize feature (#185)

This commit is contained in:
4t145 2025-05-18 14:15:41 +08:00 • committed by GitHub
commit 5e4e77efa5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 67 additions and 55 deletions

View file

@ -65,22 +65,26 @@ server = ["transport-async-rw", "dep:schemars"]
macros = ["dep:rmcp-macros", "dep:paste"] macros = ["dep:rmcp-macros", "dep:paste"]
# reqwest http client # reqwest http client
reqwest = ["dep:reqwest"] __reqwest = ["dep:reqwest"]
reqwest = ["__reqwest", "reqwest?/rustls-tls"]
reqwest-tls-no-provider = ["__reqwest", "reqwest?/rustls-tls-no-provider"]
axum = ["dep:axum"]
# SSE client # SSE client
client-side-sse = ["dep:sse-stream"] client-side-sse = ["dep:sse-stream"]
__transport-sse = ["reqwest", "client-side-sse", "dep:url", "transport-worker"] transport-sse-client = ["client-side-sse", "transport-worker"]
transport-worker = ["dep:tokio-stream"] transport-worker = ["dep:tokio-stream"]
transport-sse = ["__transport-sse", "reqwest?/rustls-tls"]
transport-sse-tls-no-provider = ["__transport-sse", "reqwest?/rustls-tls-no-provider"]
# Streamable HTTP client # Streamable HTTP client
__transport-streamable-http-client = ["reqwest", "client-side-sse", "transport-worker", "dep:url"] transport-streamable-http-client = [
transport-streamable-http-client = ["__transport-streamable-http-client", "reqwest?/rustls-tls"] "client-side-sse",
transport-streamable-http-client-tls-no-provider = ["__transport-streamable-http-client", "reqwest?/rustls-tls-no-provider"] "transport-worker",
]
transport-async-rw = ["tokio/io-util", "tokio-util/codec"] transport-async-rw = ["tokio/io-util", "tokio-util/codec"]
@ -88,14 +92,14 @@ transport-io = ["transport-async-rw", "tokio/io-std"]
transport-child-process = ["transport-async-rw", "tokio/process"] transport-child-process = ["transport-async-rw", "tokio/process"]
transport-sse-server = [ transport-sse-server = [
"transport-async-rw", "transport-async-rw",
"dep:axum", "axum",
"dep:rand", "dep:rand",
"dep:tokio-stream", "dep:tokio-stream",
"uuid", "uuid",
] ]
transport-streamable-http-server = [ transport-streamable-http-server = [
"transport-streamable-http-server-session", "transport-streamable-http-server-session",
"dep:axum", "axum",
"uuid", "uuid",
] ]
transport-streamable-http-server-session = [ transport-streamable-http-server-session = [
@ -104,9 +108,7 @@ transport-streamable-http-server-session = [
] ]
# transport-ws = ["transport-io", "dep:tokio-tungstenite"] # transport-ws = ["transport-io", "dep:tokio-tungstenite"]
tower = ["dep:tower-service"] tower = ["dep:tower-service"]
__auth = ["dep:oauth2", "dep:reqwest", "dep:url"] auth = ["dep:oauth2", "__reqwest", "dep:url"]
auth = ["__auth", "reqwest?/rustls-tls"]
auth-tls-no-provider = ["auth", "reqwest?/rustls-tls-no-provider"]
schemars = ["dep:schemars"] schemars = ["dep:schemars"]
[dev-dependencies] [dev-dependencies]
@ -127,12 +129,26 @@ path = "tests/test_tool_macros.rs"
[[test]] [[test]]
name = "test_with_python" name = "test_with_python"
required-features = ["server", "client", "transport-sse-server", "transport-sse", "transport-child-process"] required-features = [
"server",
"client",
"transport-sse-server",
"transport-sse-client",
"transport-child-process",
]
path = "tests/test_with_python.rs" path = "tests/test_with_python.rs"
[[test]] [[test]]
name = "test_with_js" name = "test_with_js"
required-features = ["server", "client", "transport-sse-server", "transport-child-process", "transport-streamable-http-server", "transport-streamable-http-client"] required-features = [
"server",
"client",
"transport-sse-server",
"transport-child-process",
"transport-streamable-http-server",
"transport-streamable-http-client",
"__reqwest",
]
path = "tests/test_with_js.rs" path = "tests/test_with_js.rs"
[[test]] [[test]]
@ -154,4 +170,3 @@ path = "tests/test_message_protocol.rs"
name = "test_message_schema" name = "test_message_schema"
required-features = ["server", "client", "schemars"] required-features = ["server", "client", "schemars"]
path = "tests/test_message_schema.rs" path = "tests/test_message_schema.rs"

View file

@ -93,11 +93,11 @@ pub mod io;
#[cfg_attr(docsrs, doc(cfg(feature = "transport-io")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-io")))]
pub use io::stdio; pub use io::stdio;
#[cfg(feature = "__transport-sse")] #[cfg(feature = "transport-sse-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "__transport-sse")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-sse-client")))]
pub mod sse_client; pub mod sse_client;
#[cfg(feature = "__transport-sse")] #[cfg(feature = "transport-sse-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "__transport-sse")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-sse-client")))]
pub use sse_client::SseClientTransport; pub use sse_client::SseClientTransport;
#[cfg(feature = "transport-sse-server")] #[cfg(feature = "transport-sse-server")]
@ -107,15 +107,15 @@ pub mod sse_server;
#[cfg_attr(docsrs, doc(cfg(feature = "transport-sse-server")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-sse-server")))]
pub use sse_server::SseServer; pub use sse_server::SseServer;
#[cfg(feature = "__auth")] #[cfg(feature = "auth")]
#[cfg_attr(docsrs, doc(cfg(feature = "__auth")))] #[cfg_attr(docsrs, doc(cfg(feature = "auth")))]
pub mod auth; pub mod auth;
#[cfg(feature = "__auth")] #[cfg(feature = "auth")]
#[cfg_attr(docsrs, doc(cfg(feature = "__auth")))] #[cfg_attr(docsrs, doc(cfg(feature = "auth")))]
pub use auth::{AuthError, AuthorizationManager, AuthorizationSession, AuthorizedHttpClient}; pub use auth::{AuthError, AuthorizationManager, AuthorizationSession, AuthorizedHttpClient};
// #[cfg(feature = "transport-ws")] // #[cfg(feature = "transport-ws")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport-ws")))] // #[cfg_attr(docsrs, doc(cfg(feature = "transport-ws")))]
// pub mod ws; // pub mod ws;
#[cfg(feature = "transport-streamable-http-server-session")] #[cfg(feature = "transport-streamable-http-server-session")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport-streamable-http-server-session")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-streamable-http-server-session")))]
@ -124,11 +124,11 @@ pub mod streamable_http_server;
#[cfg_attr(docsrs, doc(cfg(feature = "transport-streamable-http-server")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-streamable-http-server")))]
pub use streamable_http_server::axum::StreamableHttpServer; pub use streamable_http_server::axum::StreamableHttpServer;
#[cfg(feature = "__transport-streamable-http-client")] #[cfg(feature = "transport-streamable-http-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "__transport-streamable-http-client")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-streamable-http-client")))]
pub mod streamable_http_client; pub mod streamable_http_client;
#[cfg(feature = "__transport-streamable-http-client")] #[cfg(feature = "transport-streamable-http-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "__transport-streamable-http-client")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-streamable-http-client")))]
pub use streamable_http_client::StreamableHttpClientTransport; pub use streamable_http_client::StreamableHttpClientTransport;
/// Common use codes /// Common use codes

View file

@ -6,7 +6,7 @@ pub mod axum;
pub mod http_header; pub mod http_header;
#[cfg(feature = "reqwest")] #[cfg(feature = "__reqwest")]
#[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))] #[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
mod reqwest; mod reqwest;

View file

@ -1,7 +1,7 @@
#[cfg(feature = "__transport-streamable-http-client")] #[cfg(feature = "transport-streamable-http-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "__transport-streamable-http-client")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-streamable-http-client")))]
mod streamable_http_client; mod streamable_http_client;
#[cfg(feature = "__transport-sse")] #[cfg(feature = "transport-sse-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "__transport-sse")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-sse-client")))]
mod sse_client; mod sse_client;

View file

@ -1,7 +1,7 @@
#[cfg(feature = "__transport-streamable-http-client")] #[cfg(feature = "transport-streamable-http-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "__transport-streamable-http-client")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-streamable-http-client")))]
mod streamable_http_client; mod streamable_http_client;
#[cfg(feature = "__transport-sse")] #[cfg(feature = "transport-sse-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "__transport-sse")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-sse-client")))]
mod sse_client; mod sse_client;

View file

@ -26,16 +26,10 @@ pub enum SseTransportError<E: std::error::Error + Send + Sync + 'static> {
Client(E), Client(E),
#[error("unexpected end of stream")] #[error("unexpected end of stream")]
UnexpectedEndOfStream, UnexpectedEndOfStream,
#[error("Url error: {0}")]
Url(#[from] url::ParseError),
#[error("Unexpected content type: {0:?}")] #[error("Unexpected content type: {0:?}")]
UnexpectedContentType(Option<HeaderValue>), UnexpectedContentType(Option<HeaderValue>),
#[error("Tokio join error: {0}")] #[cfg(feature = "auth")]
TokioJoinError(#[from] tokio::task::JoinError), #[cfg_attr(docsrs, doc(cfg(feature = "auth")))]
#[error("Transport terminated")]
TransportTerminated,
#[cfg(feature = "__auth")]
#[cfg_attr(docsrs, doc(cfg(feature = "__auth")))]
#[error("Auth error: {0}")] #[error("Auth error: {0}")]
Auth(#[from] crate::transport::auth::AuthError), Auth(#[from] crate::transport::auth::AuthError),
} }

View file

@ -42,8 +42,8 @@ pub enum StreamableHttpError<E: std::error::Error + Send + Sync + 'static> {
Deserialize(#[from] serde_json::Error), Deserialize(#[from] serde_json::Error),
#[error("Transport channel closed")] #[error("Transport channel closed")]
TransportChannelClosed, TransportChannelClosed,
#[cfg(feature = "__auth")] #[cfg(feature = "auth")]
#[cfg_attr(docsrs, doc(cfg(feature = "__auth")))] #[cfg_attr(docsrs, doc(cfg(feature = "auth")))]
#[error("Auth error: {0}")] #[error("Auth error: {0}")]
Auth(#[from] crate::transport::auth::AuthError), Auth(#[from] crate::transport::auth::AuthError),
} }

View file

@ -19,7 +19,7 @@ Enable the auth feature in Cargo.toml:
```toml ```toml
[dependencies] [dependencies]
rmcp = { version = "0.1", features = ["auth", "transport-sse"] } rmcp = { version = "0.1", features = ["auth", "transport-sse-client"] }
``` ```
### 2. Use OAuthState ### 2. Use OAuthState

View file

@ -9,7 +9,8 @@ publish = false
[dependencies] [dependencies]
rmcp = { path = "../../crates/rmcp", features = [ rmcp = { path = "../../crates/rmcp", features = [
"client", "client",
"transport-sse", "transport-sse-client",
"reqwest",
"transport-streamable-http-client", "transport-streamable-http-client",
"transport-child-process", "transport-child-process",
"tower", "tower",

View file

@ -17,7 +17,7 @@ tokio = { version = "1", features = ["full"] }
rmcp = { path = "../../crates/rmcp", features = [ rmcp = { path = "../../crates/rmcp", features = [
"client", "client",
"transport-child-process", "transport-child-process",
"transport-sse", "transport-sse-client",
] } ] }
anyhow = "1.0" anyhow = "1.0"
serde_json = "1" serde_json = "1"

View file

@ -7,15 +7,16 @@ edition = "2021"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
reqwest = { version = "0.11", features = ["json"] } reqwest = { version = "0.12", features = ["json"] }
anyhow = "1.0" anyhow = "1.0"
thiserror = "1.0" thiserror = "1.0"
async-trait = "0.1" async-trait = "0.1"
futures = "0.3" futures = "0.3"
toml = "0.8" toml = "0.8"
rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", features = [ rmcp = { workspace = true, features = [
"client", "client",
"transport-child-process", "transport-child-process",
"transport-sse", "transport-sse-client",
"reqwest"
], no-default-features = true } ], no-default-features = true }
clap = { version = "4.0", features = ["derive"] } clap = { version = "4.0", features = ["derive"] }

View file

@ -45,7 +45,8 @@ impl McpServerTransportConfig {
pub async fn start(&self) -> Result<RunningService<RoleClient, ()>> { pub async fn start(&self) -> Result<RunningService<RoleClient, ()>> {
let client = match self { let client = match self {
McpServerTransportConfig::Sse { url } => { McpServerTransportConfig::Sse { url } => {
let transport = rmcp::transport::sse::SseTransport::start(url).await?; let transport =
rmcp::transport::sse_client::SseClientTransport::start(url.to_owned()).await?;
().serve(transport).await? ().serve(transport).await?
} }
McpServerTransportConfig::Stdio { McpServerTransportConfig::Stdio {