Skip to content

[bug]: agent dynamic tool discovery fails with enable_mcp_without_auth() #115

@gotsysdba

Description

@gotsysdba

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

  1. Application calls enable_mcp_without_auth() [main async context]
  2. Application calls loader.load_component(agent) [main async context]
  3. MCPToolBox created, stores transport reference [no tool discovery yet]
  4. Agent conversation executes
  5. _collect_tools() → toolbox.get_tools_async()
  6. AsyncRuntime.call_async() → to_thread.run_sync() [worker thread, context copied]
  7. portal.call() [portal thread, SEPARATE context]
  8. get_server_tools_from_mcp_server() → MCPTool()
  9. _validate_auth() → _is_mcp_without_auth_enabled() [returns False — flag not set here]
  10. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions