docs: use auto lifecycle in HTTP example (#1187)
This commit is contained in:
parent
ec041b7430
commit
1afde5b4fc
1 changed files with 18 additions and 13 deletions
|
|
@ -1,14 +1,15 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use rmcp::{
|
use rmcp::{
|
||||||
ServiceExt,
|
ClientLifecycleMode, ClientServiceExt,
|
||||||
model::{CallToolRequestParams, ClientCapabilities, ClientInfo, Implementation},
|
model::{
|
||||||
|
CallToolRequestParams, ClientCapabilities, ClientInfo, Implementation, ProtocolVersion,
|
||||||
|
},
|
||||||
transport::StreamableHttpClientTransport,
|
transport::StreamableHttpClientTransport,
|
||||||
};
|
};
|
||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
// Initialize logging
|
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(
|
.with(
|
||||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||||
|
|
@ -19,25 +20,29 @@ async fn main() -> Result<()> {
|
||||||
let transport = StreamableHttpClientTransport::from_uri("http://localhost:8000/mcp");
|
let transport = StreamableHttpClientTransport::from_uri("http://localhost:8000/mcp");
|
||||||
let client_info = ClientInfo::new(
|
let client_info = ClientInfo::new(
|
||||||
ClientCapabilities::default(),
|
ClientCapabilities::default(),
|
||||||
Implementation::new("test sse client", "0.0.1"),
|
Implementation::new("streamable-http-client", "0.0.1"),
|
||||||
);
|
);
|
||||||
let client = client_info.serve(transport).await.inspect_err(|e| {
|
let client = client_info
|
||||||
tracing::error!("client error: {:?}", e);
|
.serve_with_lifecycle(
|
||||||
})?;
|
transport,
|
||||||
|
ClientLifecycleMode::Auto {
|
||||||
|
preferred_versions: vec![ProtocolVersion::V_2026_07_28],
|
||||||
|
legacy_version: Some(ProtocolVersion::V_2025_11_25),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.inspect_err(|e| {
|
||||||
|
tracing::error!("client error: {:?}", e);
|
||||||
|
})?;
|
||||||
|
|
||||||
// Initialize
|
|
||||||
let server_info = client.peer_info();
|
let server_info = client.peer_info();
|
||||||
tracing::info!("Connected to server: {server_info:#?}");
|
tracing::info!("Connected to server: {server_info:#?}");
|
||||||
|
|
||||||
// List tools
|
|
||||||
let tools = client.list_tools(Default::default()).await?;
|
let tools = client.list_tools(Default::default()).await?;
|
||||||
tracing::info!("Available tools: {tools:#?}");
|
tracing::info!("Available tools: {tools:#?}");
|
||||||
|
|
||||||
let tool_result = client
|
let tool_result = client
|
||||||
.call_tool(
|
.call_tool(CallToolRequestParams::new("increment"))
|
||||||
CallToolRequestParams::new("increment")
|
|
||||||
.with_arguments(serde_json::json!({}).as_object().cloned().unwrap()),
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
tracing::info!("Tool result: {tool_result:#?}");
|
tracing::info!("Tool result: {tool_result:#?}");
|
||||||
client.cancel().await?;
|
client.cancel().await?;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue