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 rmcp::{
|
||||
ServiceExt,
|
||||
model::{CallToolRequestParams, ClientCapabilities, ClientInfo, Implementation},
|
||||
ClientLifecycleMode, ClientServiceExt,
|
||||
model::{
|
||||
CallToolRequestParams, ClientCapabilities, ClientInfo, Implementation, ProtocolVersion,
|
||||
},
|
||||
transport::StreamableHttpClientTransport,
|
||||
};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Initialize logging
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
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 client_info = ClientInfo::new(
|
||||
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
|
||||
.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();
|
||||
tracing::info!("Connected to server: {server_info:#?}");
|
||||
|
||||
// List tools
|
||||
let tools = client.list_tools(Default::default()).await?;
|
||||
tracing::info!("Available tools: {tools:#?}");
|
||||
|
||||
let tool_result = client
|
||||
.call_tool(
|
||||
CallToolRequestParams::new("increment")
|
||||
.with_arguments(serde_json::json!({}).as_object().cloned().unwrap()),
|
||||
)
|
||||
.call_tool(CallToolRequestParams::new("increment"))
|
||||
.await?;
|
||||
tracing::info!("Tool result: {tool_result:#?}");
|
||||
client.cancel().await?;
|
||||
|
|
|
|||
Loading…
Reference in a new issue