Skip to content

Tool Models

Kubiya SDK defines a set of data models that represent the structure and configuration of tools. These models provide a structured way to define and work with tools programmatically.

Core Models

Tool

The Tool model is the primary representation of a tool in Kubiya SDK.

Python
from kubiya_sdk.tools.models import Tool, Arg, FileSpec

# Create a tool definition
tool = Tool(
    name="example-tool",
    description="An example tool",
    type="docker",
    content="python /app/main.py",
    image="python:3.12-slim",
    args=[
        Arg(
            name="input_text",
            description="Text to process",
            required=True
        ),
        Arg(
            name="operation",
            description="Operation to perform",
            required=False,
            default="uppercase"
        )
    ],
    env=["LOG_LEVEL"],
    secrets=["API_KEY"],
    with_files=[
        FileSpec(
            destination="/app/main.py",
            content="""
                import sys
                text = sys.argv[1]
                operation = sys.argv[2]

                if operation == "uppercase":
                    result = text.upper()
                elif operation == "lowercase":
                    result = text.lower()
                else:
                    result = text

                print(result)
            """
        )
    ]
)

Arg

The Arg model represents a single argument or parameter for a tool.

Python
from kubiya_sdk.tools.models import Arg

# Define a tool argument
arg = Arg(
    name="input_data",
    description="Input data to process",
    required=True,
    default=None,
    type="string"
)

FileSpec

The FileSpec model represents a file to be included in the tool's Docker container.

Python
from kubiya_sdk.tools.models import FileSpec

# Define a file to include in the container
file_spec = FileSpec(
    destination="/app/script.py",
    content="print('Hello, World!')",
    source=None,  # Optional local file path
    executable=True
)

Creating Tools from Models

You can create tools directly from these models, which is useful for dynamic tool generation or when loading tools from external sources.

Python
from kubiya_sdk.tools.registry import tool_registry
from kubiya_sdk.tools.models import Tool, Arg

# Create a tool definition
tool = Tool(
    name="dynamic-tool",
    description="Dynamically created tool",
    type="docker",
    image="python:3.12-slim",
    content="python -c \"import sys; print(sys.argv[1].upper())\"",
    args=[
        Arg(
            name="text",
            description="Text to uppercase",
            required=True
        )
    ]
)

# Register the tool
tool_registry.register_tool(tool)

# Use the tool
from kubiya_sdk.execution import execute_tool
result = execute_tool("dynamic-tool", {"text": "hello world"})
# Returns: "HELLO WORLD"

Loading Tools from JSON

You can also create tools from JSON, which is useful for sharing tools or loading them from configuration files.

Python
from kubiya_sdk.tools.models import Tool

json_data = """
{
    "name": "json-tool",
    "description": "Tool loaded from JSON",
    "type": "docker",
    "image": "python:3.12-slim",
    "content": "echo 'Processing: $INPUT'",
    "args": [
        {
            "name": "input",
            "description": "Input to process",
            "required": true
        }
    ]
}
"""

# Create a tool from JSON
tool = Tool.from_json(json_data)

# Register the tool
from kubiya_sdk.tools.registry import tool_registry
tool_registry.register_tool(tool)

Loading Tools from URLs

Tools can also be loaded from remote URLs, enabling tool sharing and distribution.

Python
from kubiya_sdk.tools.models import Tool

# Create a tool from a URL
tool = Tool.from_url("https://example.com/tools/my-tool.json")

# Register the tool
from kubiya_sdk.tools.registry import tool_registry
tool_registry.register_tool(tool)