feat: dynamic tool registration — agent creates new tools at runtime#120
Open
nv78 wants to merge 1 commit intoclaude/autonomous-agent-improvementsfrom
Open
feat: dynamic tool registration — agent creates new tools at runtime#120nv78 wants to merge 1 commit intoclaude/autonomous-agent-improvementsfrom
nv78 wants to merge 1 commit intoclaude/autonomous-agent-improvementsfrom
Conversation
The agent can now call register_tool(name, description, parameters_schema,
python_code) to define a brand-new Python-backed tool mid-conversation.
The tool is immediately available for all subsequent turns.
Backend (autonomous_agent.py):
- register_tool added to _TOOL_SPECS (16th built-in tool)
- _new_session_registry(): per-conversation mutable store for dynamic tools
{specs: [...], executors: {name: callable}}
- session_registry threaded through _run → _run_anthropic/_run_openai →
_execute_tool so dynamic tools are visible on every LLM iteration
- _validate_tool_code(): AST-based safety check blocks os/sys/subprocess/
socket/eval/exec/open and other dangerous patterns; requires run() fn
- _tool_register_tool(): validates, exec()s in sandboxed namespace with
safe builtins + allowed libs (requests, pandas, numpy, bs4, json, re...)
- _tool_run_dynamic(): dispatches calls to registered executors
- register_tool runs sequentially (mutates registry); all other tools
still execute in parallel via ThreadPoolExecutor
- tool_registered SSE event emitted on successful registration
- Unknown tool error now hints the agent to use register_tool
Frontend:
- messageUtils.js: handles tool_registered SSE event → reasoning step
- ThinkingIndicator.js: emerald styling + wrench icon for tool_registered;
shows fn signature and description of newly created tools
https://claude.ai/code/session_01C9mHttiQ4ZAaBbQecVV7uu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
The agent can now build its own tools on the fly. When it encounters a task no existing tool covers, it calls
register_tool— writes the Python implementation, defines the schema, and the tool becomes available for the rest of the conversation.The loop:
thinkto plan the implementationregister_tool(name, description, parameters_schema, python_code)Architecture
Session Registry
Each conversation gets a
session_registry = {specs: [], executors: {}}. This mutable dict is threaded through every layer —_run → _run_anthropic/_run_openai → _execute_tool— so dynamic tools are injected into every subsequent LLM call within the same conversation.Safety (AST-based validation)
_validate_tool_code()walks the AST before anyexec():os,sys,subprocess,socket,shutil,ctypes,pickle, etc.eval,exec,open,compile,__import____globals__,__subclasses__, etc.)run(inputs: dict) -> strfunction to be definedAllowed libraries in the sandbox:
json,re,math,datetime,collections,itertools,urllib,requests,pandas,numpy,bs4.Execution order
register_toolalways runs sequentially (it mutatessession_registry). All other tools continue to run in parallel viaThreadPoolExecutor.Frontend
tool_registeredSSE event → new reasoning step with emerald styling + wrench iconfn name(inputs)signature and description so the user can see what was builtExample use cases
fetch_stock_pricewrapping the Yahoo Finance API, then calls it 3 times in parallelparse_invoicewith regex logic, then calls itcsv_to_markdownusing pandas, then calls itTest plan
register_toolto build a weather API wrapper, then call itimport os→ should be blocked with a validation errorrunfunction → should return errortool_registeredevent appears in the reasoning panel with emerald colorhttps://claude.ai/code/session_01C9mHttiQ4ZAaBbQecVV7uu