Skip to content

Commit 853bd11

Browse files
committed
fix(lib): use spawn_blocking for better FFI context handling on Windows
1 parent 5e23397 commit 853bd11

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/lib.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -789,11 +789,12 @@ 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-
// Spawn directly on the runtime (works with both multi-threaded and current-thread runtimes)
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 {
792+
// Use spawn_blocking which handles FFI context better on Windows
793+
runtime_handle.spawn_blocking(move || {
794+
runtime_handle.block_on(async move {
795+
let service_guard = service_arc.lock().await;
796+
if let Some(service) = service_guard.as_ref() {
797+
match service.list_tools(None).await {
797798
Ok(response) => {
798799
// Send each tool as a separate chunk
799800
for tool in response.tools {
@@ -813,6 +814,7 @@ pub extern "C" fn mcp_list_tools_init() -> usize {
813814
let _ = tx.send(StreamChunk::Error("Not connected. Call mcp_connect() first".to_string()));
814815
let _ = tx.send(StreamChunk::Done);
815816
}
817+
})
816818
});
817819
} else {
818820
// No client initialized
@@ -874,12 +876,13 @@ pub extern "C" fn mcp_call_tool_init(tool_name: *const c_char, arguments: *const
874876
let service_arc = client.service.clone();
875877
let runtime_handle = client.runtime.handle().clone();
876878

877-
// Spawn directly on the runtime (works with both multi-threaded and current-thread runtimes)
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) {
879+
// Use spawn_blocking which handles FFI context better on Windows
880+
runtime_handle.spawn_blocking(move || {
881+
runtime_handle.block_on(async move {
882+
let service_guard = service_arc.lock().await;
883+
if let Some(service) = service_guard.as_ref() {
884+
// Parse arguments
885+
let arguments_json: serde_json::Value = match serde_json::from_str(&arguments_str) {
883886
Ok(v) => v,
884887
Err(e) => {
885888
let _ = tx.send(StreamChunk::Error(format!("Invalid JSON arguments: {}", e)));
@@ -922,6 +925,7 @@ pub extern "C" fn mcp_call_tool_init(tool_name: *const c_char, arguments: *const
922925
let _ = tx.send(StreamChunk::Error("Not connected. Call mcp_connect() first".to_string()));
923926
let _ = tx.send(StreamChunk::Done);
924927
}
928+
})
925929
});
926930
} else {
927931
let _ = tx.send(StreamChunk::Error("Client not initialized".to_string()));

0 commit comments

Comments
 (0)