Skip to content

Propagate ThreadLocal context to streaming tools - #7005

Open
luyu0425 wants to merge 1 commit into
spring-projects:mainfrom
luyu0425:fix/streaming-tool-context-propagation
Open

luyu0425 wants to merge 1 commit into
spring-projects:mainfrom
luyu0425:fix/streaming-tool-context-propagation

Conversation

@luyu0425

Copy link
Copy Markdown

Summary

Preserve registered ThreadLocal context across the boundedElastic thread
hop used for streaming tool execution.

Streaming tool calls execute blocking tool callbacks on a boundedElastic
worker. As a result, thread-local state from the subscribing thread, such as
servlet request attributes, is otherwise unavailable during tool execution.

The change captures registered Micrometer ThreadLocalAccessor state at
subscription time, carries it through Reactor Context, and restores it around
tool execution.

It also:

  • clears missing thread-local state to prevent stale worker context from leaking between requests
  • excludes Observation state so the existing Spring AI tracing hierarchy is preserved
  • registers RequestAttributesThreadLocalAccessor only for servlet web applications
  • manages that accessor for the lifetime of the application context

Tests cover subscription-time capture, recursive tool-call rounds, scope
cleanup, stale worker state, observation exclusion, and servlet
auto-configuration lifecycle.

Fixes #3877

Testing

  • ./mvnw -pl spring-ai-client-chat -am test
  • ./mvnw -pl auto-configurations/models/chat/client/spring-ai-autoconfigure-model-chat-client -am test
  • ./mvnw clean package

Preserve registered ThreadLocal state across the boundedElastic
thread hop used for streaming tool execution.

Fixes spring-projects#3877

Signed-off-by: luyu0425 <2072649770@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auth (and others) context lost on MCP Tool call when using streaming chat model

2 participants