spring-projects / spring-projects/spring-ai

Streaming with tools not working with Anthropic ChatClient

Open
#3,767 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

When using a streaming Anthropic ChatClient with MCP tools, the chat response stops just before the tool call returns any results.

For example, with the filesystem MCP tools configured:

User: Can you list the files on my Desktop

Response: Certainly! I'd be happy to list the files on your Desktop. To do this, we'll need to use the "spring_ai_mcp_client_filesystem_list_directory" function. However, before we proceed, we need to make sure we have the correct path for your Desktop.

Let's first check the allowed directories to ensure we have access to the Desktop folder:

And it stops there.

I have tried with OpenAI and it works correctly.

Here is a code Snippet that reproduces the problem:

@RestController
public class ChatController {

    private final ChatClient chatClient;    

    public ChatController(
        final ChatClient.Builder builder,
        final ToolCallbackProvider toolCallbackProvider)
    {
        this.chatClient = builder
            .defaultToolCallbacks(toolCallbackProvider)
            .build();
    }

    @GetMapping("/stream")
    public Flux<String> chatWithStream(
        @RequestParam(required = false) String message)
    {
        return chatClient.prompt().user(message).stream().content();
    }

    @GetMapping("/")
    public String chat(
        @RequestParam(required = false) String message)
    {
        return chatClient.prompt().user(message).call().content();
    }
}

I have two methods; a streaming and a non streaming version. The non streaming version works but the streaming one does not for Anthropic.

I also attach a project that shows the issue:
anthropic_bug.zip

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the attached reproduction project and the ChatController /stream and / entry points. Compare Anthropic streaming with the working non-streaming path and the OpenAI streaming path, focusing on what happens when an MCP tool returns results. Done means the Anthropic stream continues after the tool call and produces the complete response.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.