Skip to content

Default Tools

Default tools included with every LLMAgent.

ReadFileTool

Bases: BaseTool

A tool for reading the contents of a local file.

Source code in src/llm_agents_from_scratch/tools/default/read_file.py
class ReadFileTool(BaseTool):
    """A tool for reading the contents of a local file."""

    @property
    def name(self) -> str:
        """Name of the read-file tool."""
        return "from_scratch__read_file"

    @property
    def description(self) -> str:
        """Description of the read-file tool."""
        return "Read the contents of a local file and return them as a string."

    @property
    def parameters_json_schema(self) -> dict[str, Any]:
        """JSON schema for tool parameters."""
        return {
            "type": "object",
            "properties": {
                "path": {
                    "type": "string",
                    "description": "Path to the file to read.",
                },
            },
            "required": ["path"],
        }

    def __call__(
        self,
        tool_call: ToolCall,
        *args: Any,
        **kwargs: Any,
    ) -> ToolCallResult:
        """Execute the read-file tool.

        Args:
            tool_call (ToolCall): The ToolCall to execute.
            *args (Any): Additional positional arguments.
            **kwargs (Any): Additional keyword arguments.

        Returns:
            ToolCallResult: The file contents, or an error result.
        """
        raw_path = tool_call.arguments.get("path")
        if not raw_path:
            return ToolCallResult(
                tool_call_id=tool_call.id_,
                content=json.dumps(
                    {"error_type": "ValueError", "message": "Missing 'path'."},
                ),
                error=True,
            )

        try:
            content = Path(raw_path).read_text(encoding="utf-8")
        except FileNotFoundError:
            return ToolCallResult(
                tool_call_id=tool_call.id_,
                content=json.dumps(
                    {
                        "error_type": "FileNotFoundError",
                        "message": f"File not found: '{raw_path}'.",
                    },
                ),
                error=True,
            )
        except OSError as e:
            return ToolCallResult(
                tool_call_id=tool_call.id_,
                content=json.dumps(
                    {"error_type": "OSError", "message": str(e)},
                ),
                error=True,
            )

        return ToolCallResult(
            tool_call_id=tool_call.id_,
            content=content,
            error=False,
        )

name property

name

Name of the read-file tool.

description property

description

Description of the read-file tool.

parameters_json_schema property

parameters_json_schema

JSON schema for tool parameters.

__call__

__call__(tool_call, *args, **kwargs)

Execute the read-file tool.

Parameters:

Name Type Description Default
tool_call ToolCall

The ToolCall to execute.

required
*args Any

Additional positional arguments.

()
**kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
ToolCallResult ToolCallResult

The file contents, or an error result.

Source code in src/llm_agents_from_scratch/tools/default/read_file.py
def __call__(
    self,
    tool_call: ToolCall,
    *args: Any,
    **kwargs: Any,
) -> ToolCallResult:
    """Execute the read-file tool.

    Args:
        tool_call (ToolCall): The ToolCall to execute.
        *args (Any): Additional positional arguments.
        **kwargs (Any): Additional keyword arguments.

    Returns:
        ToolCallResult: The file contents, or an error result.
    """
    raw_path = tool_call.arguments.get("path")
    if not raw_path:
        return ToolCallResult(
            tool_call_id=tool_call.id_,
            content=json.dumps(
                {"error_type": "ValueError", "message": "Missing 'path'."},
            ),
            error=True,
        )

    try:
        content = Path(raw_path).read_text(encoding="utf-8")
    except FileNotFoundError:
        return ToolCallResult(
            tool_call_id=tool_call.id_,
            content=json.dumps(
                {
                    "error_type": "FileNotFoundError",
                    "message": f"File not found: '{raw_path}'.",
                },
            ),
            error=True,
        )
    except OSError as e:
        return ToolCallResult(
            tool_call_id=tool_call.id_,
            content=json.dumps(
                {"error_type": "OSError", "message": str(e)},
            ),
            error=True,
        )

    return ToolCallResult(
        tool_call_id=tool_call.id_,
        content=content,
        error=False,
    )

PythonInterpreterTool

Bases: BaseTool

A tool for executing a local Python script.

Source code in src/llm_agents_from_scratch/tools/default/interpreter.py
class PythonInterpreterTool(BaseTool):
    """A tool for executing a local Python script."""

    @property
    def name(self) -> str:
        """Name of the Python interpreter tool."""
        return "from_scratch__python_interpreter"

    @property
    def description(self) -> str:
        """Description of the Python interpreter tool."""
        return "Execute a local Python script and return its stdout output."

    @property
    def parameters_json_schema(self) -> dict[str, Any]:
        """JSON schema for tool parameters."""
        return {
            "type": "object",
            "properties": {
                "path": {
                    "type": "string",
                    "description": "Path to the Python script to execute.",
                },
            },
            "required": ["path"],
        }

    def __call__(
        self,
        tool_call: ToolCall,
        *args: Any,
        **kwargs: Any,
    ) -> ToolCallResult:
        """Execute the Python interpreter tool.

        Args:
            tool_call (ToolCall): The ToolCall to execute.
            *args (Any): Additional positional arguments.
            **kwargs (Any): Additional keyword arguments.

        Returns:
            ToolCallResult: The script stdout, or an error result.
        """
        raw_path = tool_call.arguments.get("path")
        if not raw_path:
            return ToolCallResult(
                tool_call_id=tool_call.id_,
                content=json.dumps(
                    {"error_type": "ValueError", "message": "Missing 'path'."},
                ),
                error=True,
            )

        if not Path(raw_path).exists():
            return ToolCallResult(
                tool_call_id=tool_call.id_,
                content=json.dumps(
                    {
                        "error_type": "FileNotFoundError",
                        "message": f"Script not found: '{raw_path}'.",
                    },
                ),
                error=True,
            )

        result = subprocess.run(
            [sys.executable, raw_path],
            check=False,
            capture_output=True,
            text=True,
        )

        if result.returncode != 0:
            return ToolCallResult(
                tool_call_id=tool_call.id_,
                content=json.dumps(
                    {
                        "error_type": "RuntimeError",
                        "message": result.stderr,
                    },
                ),
                error=True,
            )

        return ToolCallResult(
            tool_call_id=tool_call.id_,
            content=result.stdout,
            error=False,
        )

name property

name

Name of the Python interpreter tool.

description property

description

Description of the Python interpreter tool.

parameters_json_schema property

parameters_json_schema

JSON schema for tool parameters.

__call__

__call__(tool_call, *args, **kwargs)

Execute the Python interpreter tool.

Parameters:

Name Type Description Default
tool_call ToolCall

The ToolCall to execute.

required
*args Any

Additional positional arguments.

()
**kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
ToolCallResult ToolCallResult

The script stdout, or an error result.

Source code in src/llm_agents_from_scratch/tools/default/interpreter.py
def __call__(
    self,
    tool_call: ToolCall,
    *args: Any,
    **kwargs: Any,
) -> ToolCallResult:
    """Execute the Python interpreter tool.

    Args:
        tool_call (ToolCall): The ToolCall to execute.
        *args (Any): Additional positional arguments.
        **kwargs (Any): Additional keyword arguments.

    Returns:
        ToolCallResult: The script stdout, or an error result.
    """
    raw_path = tool_call.arguments.get("path")
    if not raw_path:
        return ToolCallResult(
            tool_call_id=tool_call.id_,
            content=json.dumps(
                {"error_type": "ValueError", "message": "Missing 'path'."},
            ),
            error=True,
        )

    if not Path(raw_path).exists():
        return ToolCallResult(
            tool_call_id=tool_call.id_,
            content=json.dumps(
                {
                    "error_type": "FileNotFoundError",
                    "message": f"Script not found: '{raw_path}'.",
                },
            ),
            error=True,
        )

    result = subprocess.run(
        [sys.executable, raw_path],
        check=False,
        capture_output=True,
        text=True,
    )

    if result.returncode != 0:
        return ToolCallResult(
            tool_call_id=tool_call.id_,
            content=json.dumps(
                {
                    "error_type": "RuntimeError",
                    "message": result.stderr,
                },
            ),
            error=True,
        )

    return ToolCallResult(
        tool_call_id=tool_call.id_,
        content=result.stdout,
        error=False,
    )