AsyncMcpToolCallbackProvider block()/blockFirst()/blockLast() are blocking, which is not supported
spring-ai version: 1.1.2 java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-4 at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:87) ~[reactor-core-3.7.11.jar:3.7.11] at reactor.core.publisher.Mono.block(Mono.java:1779) ~[reactor-core-3.7.11.jar:3.7.11] at org.springframework.ai.mcp.AsyncMcpToolCallbackProvider.getToolCallbacks(AsyncMcpToolCallbackProvider.java:155) ~[spring-ai-mcp-1.1.0.jar:1.1.0] at org.springframework.ai.chat.client.DefaultChatClientUtils.toChatClientRequest(DefaultChatClientUtils.java:134) ~[spring-ai-client-chat-1.1.0.jar:1.1.0] at org.springframework.ai.chat.client.DefaultChatClient$DefaultChatClientRequestSpec.stream(DefaultChatClient.java:1022) ~[spring-ai-client-chat-1.1.0.jar:1.1.0]
spring-ai version: 1.1.0-M3 Everything is ok.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-azure-openai</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client-webflux</artifactId>
</dependency>
spring:
ai:
mcp:
client:
type: ASYNC
streamable-http:
connections:
toolbox:
url: http://127.0.0.1:5000
@Bean
ChatClient chatClient(
ChatClient.Builder chatClientBuilder,
ToolCallbackProvider[] toolCallbackProviders,
ChatMemory chatMemory,
ChatProperties chatProperties
) {
return chatClientBuilder
.defaultToolCallbacks(toolCallbackProviders)
.defaultSystem(chatProperties.getSystemPrompt())
.defaultAdvisors(
MessageChatMemoryAdvisor.builder(chatMemory).build(),
new SystemPromptFirstSortingAdvisor()
)
.build();
}
public Flux<ChatResponse> chat(ChatRequest request, String conversationId) {
return chatClient.prompt()
.user(request.prompt())
.advisors(a -> a.param(ChatMemory.CONVERSATION_ID, conversationId))
.stream()
.chatResponse();
}
强制将包含阻塞调用的 ChatClient 启动流程转移到阻塞线程池。
eg: ```
return Flux.defer(() ->
chatClient.prompt()
.user(message)
.advisors(new SimpleLoggerAdvisor())
.stream()
.content()
)
.subscribeOn(Schedulers.boundedElastic());
}
Workaround: resolve tools on bean creation and pass tool callbacks instead of providers:
NOTE: spring ai 2.0.0-M7
@Bean
ChatClient chatClient(
ChatClient.Builder chatClientBuilder,
ToolCallbackProvider[] toolCallbackProviders,
List<Advisor> advisors,
ChatProperties chatProperties
) {
List<ToolCallback> toolCallbacks = Arrays.stream(toolCallbackProviders)
.flatMap(toolCallbackProvider -> Arrays.stream(toolCallbackProvider.getToolCallbacks()))
.toList();
return chatClientBuilder
.defaultTools(customizer -> customizer.callbacks(toolCallbacks))
.defaultSystem(chatProperties.getSystemPrompt())
.defaultAdvisors(advisors)
.build();
}
Hi maintainers, I'd like to work on this issue.
I reviewed the reported path and the current discussion. AsyncMcpToolCallbackProvider.getToolCallbacks() resolves asynchronous tool discovery through a synchronous ToolCallbackProvider boundary, so invoking it from a Reactor non-blocking thread can trigger the blocking guard.
Before implementing, could you confirm the preferred direction: keep ToolCallbackProvider synchronous with safe eager/cached resolution or controlled offloading, or introduce a reactive provider path?
I plan to add a focused regression test that invokes callback discovery from a Reactor non-blocking thread, preserve existing synchronous callers, and cover discovery errors and refresh behavior. I favor the smallest source-compatible fix unless a reactive API is already planned.
I reproduced this against current main.
The WebFlux path is:
ChatClient.stream() -> DefaultChatClientUtils.toChatClientRequest() -> ToolCallbackProvider.getToolCallbacks() -> AsyncMcpToolCallbackProvider.getToolCallbacks() -> McpAsyncClient.listTools().block()
Because AsyncMcpToolCallbackProvider implements the synchronous
ToolCallbackProvider contract, moving .block() to another scheduler would
still make the event-loop request wait, while returning an empty cache could
silently omit tools.
Would maintainers prefer:
- a reactive tool-provider contract integrated into the reactive ChatClient request path, or
- eager/off-request-thread discovery with a defined cache refresh strategy for MCP tool-change events?
asyncToolCallbacks(...) also currently calls getToolCallbacks(), so it
blocks before returning its Flux. I can prepare a focused regression test and
PR once the preferred direction is confirmed.
Thanks @MuhammadMaazA for the reproduction — the full WebFlux path and the
asyncToolCallbacks() blocking note are a great addition, and they match the
constraint I found: AsyncMcpToolCallbackProvider has to satisfy the synchronous
ToolCallbackProvider contract, so any fix has to decide where the async discovery
actually happens.
Since we're both waiting on the same maintainer question (reactive provider path vs eager/off-request-thread discovery with cache refresh), I'd suggest we coordinate so we don't end up with two overlapping PRs: once the maintainers confirm a direction, happy to split the work — e.g. I take the regression-test coverage (invocation from a non-blocking thread, discovery errors, refresh behavior) and you take the implementation, or the other way around. If you'd prefer to lead, I'm happy to collaborate on review instead.
Maintainers: one confirmation comment on the preferred direction is all we need and either of us can move quickly. Thanks!