Skip to content

Commit 8967daf

Browse files
authored
fix(examples): Prevent transport from closing due to premature client drop (#324)
1 parent f100093 commit 8967daf

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

examples/simple-chat-client/src/bin/simple_chat.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async fn main() -> Result<()> {
7474
if config.mcp.is_some() {
7575
let mcp_clients = config.create_mcp_clients().await?;
7676

77-
for (name, client) in mcp_clients {
77+
for (name, client) in mcp_clients.iter() {
7878
println!("load MCP tool: {}", name);
7979
let server = client.peer().clone();
8080
let tools = get_mcp_tools(server).await?;
@@ -84,6 +84,7 @@ async fn main() -> Result<()> {
8484
tool_set.add_tool(tool);
8585
}
8686
}
87+
tool_set.set_clients(mcp_clients);
8788
}
8889

8990
// create chat session

examples/simple-chat-client/src/tool.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::{collections::HashMap, sync::Arc};
33
use anyhow::Result;
44
use async_trait::async_trait;
55
use rmcp::{
6+
RoleClient,
67
model::{CallToolRequestParam, CallToolResult, Tool as McpTool},
7-
service::ServerSink,
8+
service::{RunningService, ServerSink},
89
};
910
use serde_json::Value;
1011

@@ -70,9 +71,14 @@ impl Tool for McpToolAdapter {
7071
#[derive(Default)]
7172
pub struct ToolSet {
7273
tools: HashMap<String, Arc<dyn Tool>>,
74+
clients: HashMap<String, RunningService<RoleClient, ()>>,
7375
}
7476

7577
impl ToolSet {
78+
pub fn set_clients(&mut self, clients: HashMap<String, RunningService<RoleClient, ()>>) {
79+
self.clients = clients;
80+
}
81+
7682
pub fn add_tool<T: Tool + 'static>(&mut self, tool: T) {
7783
self.tools.insert(tool.name(), Arc::new(tool));
7884
}

0 commit comments

Comments
 (0)