Skip to content

[Bug]: MCP tool context params (data_dir) lost in thread pool executor #6853

Description

@Hundao

Describe the Bug

MCP tools that depend on auto-injected context params (data_dir, etc.) fail with "Missing required argument: data_dir" when called during agent execution. Tools like save_data, serve_file_to_user, and append_data are affected. The agent falls back to writing files to /tmp instead of the session data directory.

To Reproduce

  1. Run any agent that uses save_data or serve_file_to_user via quickstart
  2. Observe the error in logs:
MCP tool 'save_data' execution failed: MCP tool 'save_data' failed: 1 validation error for call[save_data]
data_dir
  Missing required argument [type=missing_argument, ...]

Expected Behavior

data_dir should be auto-injected by the framework via execution context. Tools should write to the session-scoped data directory (~/.hive/agents/{agent}/sessions/{session}/data/).

Root Cause

tool_result_handler.py offloads tool executor calls to a thread pool via loop.run_in_executor(). Python's contextvars do not propagate to worker threads by default. The execution context (set by GraphExecutor via set_execution_context(data_dir=...)) is lost when the tool executor runs in the thread pool.

Call chain:

  1. GraphExecutor.execute() sets _execution_context with data_dir (contextvar)
  2. tool_result_handler.execute_tool() calls loop.run_in_executor(None, tool_executor, tool_use)
  3. Worker thread: _execution_context.get() returns None (contextvar not propagated)
  4. MCP executor builds merged_inputs without data_dir
  5. client_ref.call_tool() fails: "Missing required argument: data_dir"

Note: workspace_id, agent_id, session_id are unaffected because they use _session_context (a regular dict attribute), not contextvars.

Fix

Wrap the executor call in contextvars.copy_context().run() so the current context propagates into the worker thread:

ctx = contextvars.copy_context()
result = await loop.run_in_executor(None, ctx.run, tool_executor, tool_use)

File: core/framework/graph/event_loop/tool_result_handler.py

Logs

2026-03-29 22:15:01 [ERROR] MCP tool 'save_data' execution failed: MCP tool 'save_data' failed: 1 validation error for call[save_data]
data_dir
  Missing required argument [type=missing_argument, input_value={'filename': 'risk_assess...'], input_type=dict]

Additional Context

  • All 4 tool executor definitions in tool_registry.py are synchronous (def, not async def), so ctx.run() works correctly.
  • Fix verified by running vulnerability_assessment agent end-to-end: save_data and serve_file_to_user work correctly after the fix.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions