-
Notifications
You must be signed in to change notification settings - Fork 101
Description
Problem
When the agent server is bundled into a binary executable using PyInstaller (the binary target in the Dockerfile), custom tools cannot be dynamically imported at runtime.
Root Cause
The current implementation in PR #1383 uses importlib.import_module() to dynamically load custom tool modules:
if request.tool_module_qualnames:
import importlib
for tool_name, module_qualname in request.tool_module_qualnames.items():
importlib.import_module(module_qualname)This works in source mode where a real Python interpreter is available, but fails in binary mode because:
- PyInstaller creates a frozen executable with all Python modules bundled inside
- The frozen Python interpreter can only import modules that were bundled at build time
- External Python files copied into the Docker image are not accessible to the binary's internal importer
importlib.import_module()will raiseModuleNotFoundErrorfor any module not bundled in the binary
Context
This was identified during review of PR #1383 (feat: Add support for custom tools with remote agent server). The PR works correctly for source-mode deployments but not for binary-mode deployments.
See: #1383 (comment)
Potential Solutions
1. Use Source Mode for Custom Tools (Simplest)
- The Dockerfile already has
sourceandsource-minimaltargets - Users needing custom tools should use source mode instead of binary mode
- Pros: Works with existing implementation, no code changes needed
- Cons: Larger image size, slower startup
2. HTTP-based Tool Execution (Most Flexible)
- Custom tools run as separate HTTP services
- Agent server calls tools via HTTP instead of importing them
- Similar to MCP (Model Context Protocol) approach
- Pros: Language-agnostic, works with binary, clean separation
- Cons: More complex setup, network overhead
3. Build Custom Binary with Tools Included
- Modify the build process to include custom tools at build time
- Users provide their tools, and a custom binary is built that includes them
- Pros: Single binary with everything included
- Cons: Requires rebuild for each tool set
4. Dynamic Code Execution
- Send tool code as a string instead of module qualnames
- Server executes the code using
exec()to define the tool class - Pros: Works with binary, no filesystem needed
- Cons: Security concerns, harder to debug
Recommendation
For now, document that custom tools require source mode deployment. The binary mode should be used for standard deployments without custom tools.
Longer term, consider implementing HTTP-based tool execution for maximum flexibility.
Related
- PR feat: Add support for custom tools with remote agent server #1383: feat: Add support for custom tools with remote agent server
- Issue Document and implement custom tool support for remote agent server #1381: Support custom tools with remote agent server