Skip to content

Custom tools do not work with binary agent server builds #1531

@xingyaoww

Description

@xingyaoww

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:

  1. PyInstaller creates a frozen executable with all Python modules bundled inside
  2. The frozen Python interpreter can only import modules that were bundled at build time
  3. External Python files copied into the Docker image are not accessible to the binary's internal importer
  4. importlib.import_module() will raise ModuleNotFoundError for 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 source and source-minimal targets
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpriority:lowFor bugs, affects only non-mainstream cases, or is annoying but with a clear workaround.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions