Files
AI-Trader/agent/context_injector.py
Bill b1b486dcc4 fix: inject signature and today_date into trade tool calls for concurrent simulations
Resolves issue where MCP trade tools couldn't access SIGNATURE and TODAY_DATE
during concurrent API simulations, causing "SIGNATURE environment variable is
not set" errors.

Problem:
- MCP services run as separate HTTP processes
- Multiple simulations execute concurrently via ThreadPoolExecutor
- Environment variables from executor process not accessible to MCP services

Solution:
- Add ContextInjector that implements ToolCallInterceptor
- Automatically injects signature and today_date into buy/sell tool calls
- Trade tools accept optional parameters, falling back to config/env
- BaseAgent creates interceptor and updates today_date per session

Changes:
- agent/context_injector.py: New interceptor for context injection
- agent/base_agent/base_agent.py: Create and use ContextInjector
- agent_tools/tool_trade.py: Add optional signature/today_date parameters

Benefits:
- Supports concurrent multi-model simulations
- Maintains backward compatibility with CLI mode
- AI model unaware of injected parameters
2025-11-02 20:01:32 -05:00

51 lines
1.6 KiB
Python

"""
Tool interceptor for injecting runtime context into MCP tool calls.
This interceptor automatically injects `signature` and `today_date` parameters
into buy/sell tool calls to support concurrent multi-model simulations.
"""
from typing import Any, Dict
class ContextInjector:
"""
Intercepts tool calls to inject runtime context (signature, today_date).
Usage:
interceptor = ContextInjector(signature="gpt-5", today_date="2025-10-01")
client = MultiServerMCPClient(config, tool_interceptors=[interceptor])
"""
def __init__(self, signature: str, today_date: str):
"""
Initialize context injector.
Args:
signature: Model signature to inject
today_date: Trading date to inject
"""
self.signature = signature
self.today_date = today_date
def __call__(self, tool_name: str, tool_input: Dict[str, Any]) -> Dict[str, Any]:
"""
Intercept tool call and inject context parameters.
Args:
tool_name: Name of the tool being called
tool_input: Original tool input parameters
Returns:
Modified tool input with injected context
"""
# Only inject for trade tools (buy/sell)
if tool_name in ["buy", "sell"]:
# Inject signature and today_date if not already provided
if "signature" not in tool_input:
tool_input["signature"] = self.signature
if "today_date" not in tool_input:
tool_input["today_date"] = self.today_date
return tool_input