When an agent uses MCPToolBox for dynamic tool discovery, the tools are not created during load_component() they are discovered lazily at execution time via get_tools_async(). This call runs through the portal, where the _GLOBAL_ENABLED_MCP_WITHOUT_AUTH ContextVar has never been set, causing _validate_auth to raise.
Following a similar pattern to _ACTIVE_SPAN_STACK for _GLOBAL_ENABLED_MCP_WITHOUT_AUTH would resolve this.
Failure sequence
- Application calls enable_mcp_without_auth() [main async context]
- Application calls loader.load_component(agent) [main async context]
- MCPToolBox created, stores transport reference [no tool discovery yet]
- Agent conversation executes
- _collect_tools() → toolbox.get_tools_async()
- AsyncRuntime.call_async() → to_thread.run_sync() [worker thread, context copied]
- portal.call() [portal thread, SEPARATE context]
- get_server_tools_from_mcp_server() → MCPTool()
- _validate_auth() → _is_mcp_without_auth_enabled() [returns False — flag not set here]
- ValueError raised
Error output
ValueError: Using MCP servers without proper authentication is highly discouraged.
If you still want to use it, please call `enable_mcp_without_auth` before instantiating the MCPToolBox.
When an agent uses
MCPToolBoxfor dynamic tool discovery, the tools are not created duringload_component()they are discovered lazily at execution time viaget_tools_async(). This call runs through the portal, where the_GLOBAL_ENABLED_MCP_WITHOUT_AUTHContextVar has never been set, causing_validate_authto raise.Following a similar pattern to _ACTIVE_SPAN_STACK for _GLOBAL_ENABLED_MCP_WITHOUT_AUTH would resolve this.
Failure sequence
Error output