tools.read_tool_code module
Read Python tool source code from the tools/ directory.
- async tools.read_tool_code.run(filename, max_lines=None, start_line=None, end_line=None, ctx=None, **_kwargs)[source]
Read the source of a Python tool file from the
tools/directory.Entry point for the
read_tool_codetool, used to inspect or debug existing tool implementations. It returns the full file or a bounded slice, after a privilege check and several safety checks that keep reads confined totools/and to.pyfiles.It first calls
_check_read_tool_code(), short-circuiting with that helper’s error JSON when the caller lacksREAD_TOOL_CODE. It then rejects subdirectory components and path traversal (resolving againstTOOLS_ROOTand requiring the result to stay inside it), checks existence and file-ness viafilepath.exists/filepath.is_fileoffloaded withasyncio.to_thread, and requires a.pyextension. The file is read asynchronously withaiofiles; astart_line/end_linewindow or amax_lineshead is applied, and the result is serialized withjsonutil(imported asjson). Errors (permission, non-UTF-8, anything else) are logged via the moduleloggerand returned as JSON rather than raised. This module-levelrunis discovered and dispatched by the tool runtime viatool_loader.py(getattr(module, "run")); no other in-repo callers.- Parameters:
filename (
str) – Bare filename (no path) of a.pyfile undertools/to read.max_lines (
int) – When set and no line range is given, return only the first this-many lines.start_line (
int) – 1-indexed first line of a range to return (withend_line).end_line (
int) – 1-indexed last line of a range to return (withstart_line).ctx (
ToolContext|None) – Tool context used for the privilege check.
- Returns:
A JSON document with the file
content,total_linesandreturned_lineson success, or a{"success": False, "error": ...}payload for an authorization failure, an invalid/missing/non-Python file, an out-of-range line selection, or a read error.- Return type: