Skip to content

ClassCastException: MessageDeltaEvent cannot be cast to ToolUseAggregationEvent in StreamHelper.mergeToolUseEvents when using Anthropic streaming with tools #4472

@magnusaxelqvist

Description

@magnusaxelqvist

Bug Description

When using Spring AI with Anthropic Claude and MCP tools in streaming mode, a ClassCastException occurs preventing tool execution.

Stack Trace

java.lang.ClassCastException: class org.springframework.ai.anthropic.api.AnthropicApi$MessageDeltaEvent cannot be cast to class org.springframework.ai.anthropic.api.AnthropicApi$ToolUseAggregationEvent
at org.springframework.ai.anthropic.api.StreamHelper.mergeToolUseEvents(StreamHelper.java:86)
at reactor.core.publisher.MonoReduceSeed$ReduceSeedSubscriber.onNext(MonoReduceSeed.java:116)
...

Environment

  • Spring AI: 1.0.2, 1.1.0-M1 (both affected)
  • Spring Boot: 3.5.6
  • MCP SDK: 0.13.0
  • Tools: MCP Gmail integration

Reproduction

  1. Configure Anthropic ChatClient with MCP tools
  2. Call .stream() on ChatClient with tool-enabled prompt
  3. ClassCastException occurs when tool execution is attempted

Expected Behavior

Tools should execute without ClassCastException during streaming

Actual Behavior

ClassCastException prevents all tool execution in streaming mode

Workaround

None found. Switching to .call() (non-streaming) might work but defeats the purpose.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions