You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] MCP Client does not propagate contextvars #56
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
This issue was fixed in strands-agents#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
)
Activity