CodeDocs Vault

04 - Agent Tools

Tool System Architecture

Registration and Dispatch (agent/core/tools.py)

Tools are registered via two mechanisms:

  1. Built-in tools: Python async functions wrapped in ToolSpec dataclasses (line 116). Each has a name, description, parameters (JSON Schema), and handler callable.

  2. MCP tools: Discovered dynamically from the HF MCP server via fastmcp.Client. Registered with handler=None and dispatched through the MCP client.

The ToolRouter (line 126) is the central dispatch hub:

# Simplified (tools.py:234-270)
async def call_tool(self, name, args, session=None, tool_call_id=None):
    spec = self._tool_map[name]
    if spec.handler:
        # Built-in: inspect signature to pass optional session/tool_call_id
        sig = inspect.signature(spec.handler)
        kwargs = {"args": args}
        if "session" in sig.parameters:
            kwargs["session"] = session
        if "tool_call_id" in sig.parameters:
            kwargs["tool_call_id"] = tool_call_id
        return await spec.handler(**kwargs)
    else:
        # MCP: delegate to MCP client
        result = await self.mcp_client.call_tool(name, args)
        return convert_mcp_content_to_string(result), True

Handler Convention

All tool handlers return tuple[str, bool] -- (output text, success flag). The agent loop adds the output as a tool result message, and the success flag determines display styling.

Tool Creation Order (tools.py:282)

The order tools are listed in matters -- it affects their position in the LLM's tool list:

  1. Sandbox/local tools (prepended): sandbox_create, bash, read, write, edit
  2. research (sub-agent)
  3. explore_hf_docs, fetch_hf_docs
  4. hf_papers
  5. hf_inspect_dataset
  6. plan_tool
  7. hf_jobs
  8. hf_repo_files, hf_repo_git
  9. github_find_examples, github_list_repos, github_read_file
  10. find_hf_api (registered async, from OpenAPI spec)
  11. MCP tools from HF MCP server

Blocked MCP Tools (tools.py:65)

Some MCP tools are blocked to avoid conflicts with built-in implementations:

NOT_ALLOWED_TOOL_NAMES = {"hf_jobs", "hf_doc_search", "hf_doc_fetch", "hf_whoami"}

Complete Tool Catalog

1. Sandbox / Code Execution

sandbox_create (agent/tools/sandbox_tool.py:203)

bash (sandbox_tool.py:237 or local_tools.py:315)

read (sandbox_tool.py:237 or local_tools.py:352)

write (sandbox_tool.py:237 or local_tools.py:371)

edit (sandbox_tool.py:237 or local_tools.py:394)

The Sandbox Server (Embedded in sandbox_client.py:100-456)

A complete FastAPI application stored as a string literal and uploaded to the HF Space. Endpoints: /api/bash, /api/read, /api/write, /api/edit, /api/exists, /api/kill, /api/health. Runs on ghcr.io/astral-sh/uv:python3.12-bookworm-slim with development tools pre-installed.


2. Research Sub-Agent

research (agent/tools/research_tool.py)

The research tool has its own system prompt (research_tool.py:43-169) focused on literature-first methodology:

"You are a research specialist agent. Your job is to thoroughly investigate a specific question..."


3. HuggingFace Documentation

explore_hf_docs (agent/tools/docs_tools.py:879)

fetch_hf_docs (docs_tools.py:957)

find_hf_api (docs_tools.py:786, registered dynamically)


4. Academic Papers

hf_papers (agent/tools/papers_tool.py)


5. Training Jobs

hf_jobs (agent/tools/jobs_tool.py)


6. Datasets

hf_inspect_dataset (agent/tools/dataset_tools.py)


7. Repository Management

hf_repo_files (agent/tools/hf_repo_files_tool.py)

hf_repo_git (agent/tools/hf_repo_git_tool.py)


8. GitHub Integration

github_find_examples (agent/tools/github_find_examples.py)

github_read_file (agent/tools/github_read_file.py)

github_list_repos (agent/tools/github_list_repos.py)


9. Planning

plan_tool (agent/tools/plan_tool.py)


Tool Integration Summary

External Service Tools Using It
HuggingFace Hub API hf_jobs, hf_repo_files, hf_repo_git, sandbox_create
HuggingFace Dataset Server hf_inspect_dataset
HuggingFace Papers API hf_papers
HuggingFace Docs (raw MD) explore_hf_docs, fetch_hf_docs
HuggingFace OpenAPI spec find_hf_api
HuggingFace MCP Server Various MCP tools (dynamic)
Semantic Scholar API hf_papers (citations, snippets, recommendations)
arXiv / ar5iv HTML hf_papers (paper reading)
GitHub REST API github_find_examples, github_read_file, github_list_repos
Gradio API explore_hf_docs (Gradio-specific search)
HF Spaces (sandbox) bash, read, write, edit

Cross-Cutting Patterns

Async Wrapping

All sync HuggingFace Hub API calls are universally wrapped with asyncio.to_thread() via local _async_call() helper functions.

Error Handling Convention

All tools use try/except with specific exception types (HfHubHTTPError, RepositoryNotFoundError, httpx.HTTPStatusError) and return formatted error strings with success=False.

Sub-Agent LLM Usage

Only the research tool uses LLM calls internally. All other tools are pure API/computation tools.

Tool Output Limits