Skip to content

Commit 8865be1

Browse files
committed
fix(lib): simplify async task spawning by using runtime directly for better FFI handling
1 parent 2c513d0 commit 8865be1

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

src/lib.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -789,15 +789,11 @@ pub extern "C" fn mcp_list_tools_init() -> usize {
789789
let service_arc = client.service.clone();
790790
let runtime_handle = client.runtime.handle().clone();
791791

792-
// Use spawn_blocking which handles FFI context better on Windows
793-
let runtime_handle_clone = runtime_handle.clone();
794-
runtime_handle.spawn_blocking(move || {
795-
// Enter runtime context BEFORE block_on to set thread-local runtime for tokio::spawn()
796-
let _guard = runtime_handle_clone.enter();
797-
runtime_handle_clone.block_on(async move {
798-
let service_guard = service_arc.lock().await;
799-
if let Some(service) = service_guard.as_ref() {
800-
match service.list_tools(None).await {
792+
// Spawn async task directly on the runtime (like the official rmcp examples)
793+
runtime_handle.spawn(async move {
794+
let service_guard = service_arc.lock().await;
795+
if let Some(service) = service_guard.as_ref() {
796+
match service.list_tools(None).await {
801797
Ok(response) => {
802798
// Send each tool as a separate chunk
803799
for tool in response.tools {
@@ -817,7 +813,6 @@ pub extern "C" fn mcp_list_tools_init() -> usize {
817813
let _ = tx.send(StreamChunk::Error("Not connected. Call mcp_connect() first".to_string()));
818814
let _ = tx.send(StreamChunk::Done);
819815
}
820-
})
821816
});
822817
} else {
823818
// No client initialized
@@ -879,16 +874,12 @@ pub extern "C" fn mcp_call_tool_init(tool_name: *const c_char, arguments: *const
879874
let service_arc = client.service.clone();
880875
let runtime_handle = client.runtime.handle().clone();
881876

882-
// Use spawn_blocking which handles FFI context better on Windows
883-
let runtime_handle_clone = runtime_handle.clone();
884-
runtime_handle.spawn_blocking(move || {
885-
// Enter runtime context BEFORE block_on to set thread-local runtime for tokio::spawn()
886-
let _guard = runtime_handle_clone.enter();
887-
runtime_handle_clone.block_on(async move {
888-
let service_guard = service_arc.lock().await;
889-
if let Some(service) = service_guard.as_ref() {
890-
// Parse arguments
891-
let arguments_json: serde_json::Value = match serde_json::from_str(&arguments_str) {
877+
// Spawn async task directly on the runtime (like the official rmcp examples)
878+
runtime_handle.spawn(async move {
879+
let service_guard = service_arc.lock().await;
880+
if let Some(service) = service_guard.as_ref() {
881+
// Parse arguments
882+
let arguments_json: serde_json::Value = match serde_json::from_str(&arguments_str) {
892883
Ok(v) => v,
893884
Err(e) => {
894885
let _ = tx.send(StreamChunk::Error(format!("Invalid JSON arguments: {}", e)));
@@ -931,7 +922,6 @@ pub extern "C" fn mcp_call_tool_init(tool_name: *const c_char, arguments: *const
931922
let _ = tx.send(StreamChunk::Error("Not connected. Call mcp_connect() first".to_string()));
932923
let _ = tx.send(StreamChunk::Done);
933924
}
934-
})
935925
});
936926
} else {
937927
let _ = tx.send(StreamChunk::Error("Client not initialized".to_string()));

0 commit comments

Comments
 (0)