generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 603
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Checks
- I have updated to the lastest minor and patch version of Strands
- I have checked the documentation and this is not expected behavior
- I have searched ./issues and there are no duplicates of my issue
Strands Version
1.21.1
Python Version
3.10
Operating System
MacOS 26.2
Installation Method
pip
Steps to Reproduce
# Factory function runs in background thread
mcp_client = MCPClient(lambda: streamablehttp_client(MCP_URL))
# Set Context Var in Current thread
context_var.set("some-value")
# The factory runs in BACKGROUND thread, so current contextvars do not propagate
# This makes then inaccessible to transports or auth logic that requires them
with mcp_client:
# Make request requiring context var
Expected Behavior
Contextvar propagated to background thread and is accessible by transports.
Actual Behavior
New thread is created without propagating current contextvars
Additional Context
In MCPClient.start, the background thread is started with
self._background_thread = threading.Thread(target=self._background_task, args=[], daemon=True)
This issue was fixed in #1146 for tool invocations by propagating copying contextvars in the ThreadPoolExecutor.
Possible Solution
This can be fixed by copying context vars in the start method when starting the background thread.
# Copy the current context
ctx = contextvars.copy_context()
# Create the thread with the context
self._background_thread = threading.Thread(
target=ctx.run,
args=(self._background_task,),
daemon=True
)
Related Issues
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working