Writing Tools
Learn how to implement executable Python functions (Tools) within your Genbase Kit.
Tools are the core executable logic units within a Genbase Kit. They are Python functions that Agents can invoke to perform tasks beyond simple LLM interaction, such as interacting with databases, calling external APIs, or manipulating files in the Module's Workspace.
Defining Tools
- Location: Place your Python code within the
tools/
directory at the root of your Kit. - Organization: You can organize your code into multiple
.py
files (modules). Antools/__init__.py
file is common for simple or central tools. - Function Definition: Write standard Python functions. They can be synchronous (
def
) or asynchronous (async def
). - Parameters & Return Values:
- Functions receive parameters as standard Python keyword arguments.
- Use type hints for parameters and return types. These hints, along with the docstring, are used by Genbase to generate the schema that the LLM uses to call your tool correctly.
- Return values should be JSON-serializable (or serializable via
cloudpickle
, which handles more complex Python objects) as they are passed back from the Docker container to the Agent.
- Docstrings: Write clear docstrings for your functions. The first line/paragraph is used as the tool's
description
inkit.yaml
(unless explicitly overridden there). Use standard docstring formats (like Google or NumPy style) to document parameters (:param name: description
) so Genbase can potentially extract parameter descriptions for the schema.
Declaring Tools in kit.yaml
For an tool function to be usable, it must be declared in your kit.yaml
file, either within a profiles
section or the provide.tools
section.
name
: The identifier the Agent/LLM will use. Keep it descriptive and concise.path
: Locates the function.filename:function_name
: For functions in specific modules (e.g.,db_tools.py
).function_name
: For functions defined directly intools/__init__.py
.
description
: Essential for the LLM to understand what the tool does and when to use it. Be clear and specific.
Execution Environment
Remember that Kit Tools run inside isolated Docker containers. Your code needs to operate within this context:
- Dependencies: List all required Python packages in your Kit's root
requirements.txt
. They will bepip install
ed into the container image. - Environment Variables: Access configuration (API keys, URLs, etc.) using
os.getenv("YOUR_ENV_VAR_NAME")
. These variables are injected from the Module's specific environment settings. - Workspace Access: The Module's Git repository (its Workspace) is mounted read-write at
/repo
. Use standard Python file operations (pathlib
,open()
) relative to this path (e.g.,Path("/repo/data/output.json")
). - Tool Code Access: The Kit's
tools/
directory is mounted read-only at/tools
. You can import helper functions or modules from within this directory using relative imports if structured correctly as a Python package (using__init__.py
files). - Networking: Containers run in Docker's default bridge network. They can typically access the host machine and external internet resources. If an Tool needs to expose a service, define a Port in
kit.yaml
. The mapped host port will be available as an environment variable likePORT_{YOUR_PORT_NAME}
inside the container.
Example Tool (tools/file_utils.py
)
Corresponding kit.yaml
declaration:
Best Practices
- Keep tools focused on a single task.
- Handle errors gracefully and return informative success/failure messages or exceptions.
- Use type hints and clear docstrings.
- Be mindful of the containerized execution environment (paths, env vars).
- List all external dependencies in
requirements.txt
.