Introducing a new way to review your PRs.

Read the announcement

greydgl / pentestgpt

Automated Penetration Testing Agentic Framework Powered by Large Language Models

View on GitHub

13 documents · Updated Nov 16, 2026

docs / unified-agent-tools

Unified Agent Tools

Overview

unified_agent/tools.py defines the shared tool registration layer used by both supported agent backends. A caller creates a ToolRegistry, decorates sync or async Python functions with ToolRegistry.tool(), and then passes either that registry or an explicit pkg.mod:REGISTRY string into the agent setup. unified_agent/agent.py prepares the run by calling build_tool_server_spec() when UnifiedAgent.tools is set, placing the resulting ToolServerSpec into RunOptions.tool_server so each backend can launch the same stdio MCP server for the task.

ToolRegistry is intentionally strict because the same model-visible tool names must work in both Claude Code and Codex. Registry and tool names must match ^[a-z][a-z0-9_]*$; hyphens are rejected because Codex rewrites them to underscores while Claude permission strings do not. During registration, ToolRegistry.tool() builds the fully qualified name mcp__<server_name>__<tool_name> and rejects names over 64 bytes, duplicate tool names, and tools with no description. If no explicit description is supplied, the first line of the function docstring becomes the MCP description. Registered entries are stored as ToolEntry(fn, name, description) and exposed via ToolRegistry.tools.

The registry must be importable by a subprocess. registry_spec() records the module where the registry was constructed, imports that module, and finds the registry as a top-level attribute to produce pkg.mod:ATTR; registries created in __main__ or not bound at module top level raise ToolRegistryError. resolve_registry() performs the inverse for both direct callers and the server process: it parses a pkg.mod:ATTR string, imports the module, retrieves the attribute, and verifies it is a ToolRegistry.

build_tool_server_spec() in unified_agent/tools.py is the bridge from registration to backend runtime. It resolves the registry, rejects empty registries, computes a PYTHONPATH containing the registry module root and the repository root that contains unified_agent, appends any caller-supplied PYTHONPATH, and returns a ToolServerSpec with server_name, command [sys.executable, "-m", "unified_agent.tool_server", spec], and the explicit environment. The explicit environment matters because unified_agent/…/codex.py does not inherit the parent environment for MCP server processes, while unified_agent/…/claude_code.py passes the same spec for parity.

unified_agent/tool_server.py is the runtime process launched by that spec. Its CLI entry point is python -m unified_agent.tool_server pkg.mod:REGISTRY; main() validates that exactly one registry spec was passed, resolves it with resolve_registry(), logs the number of tools and server name to stderr, and then runs a FastMCP server over stdio. build_server() creates FastMCP(registry.server_name) and registers each ToolEntry with server.add_tool(entry.fn, name=entry.name, description=entry.description). Stdout is reserved for MCP JSON-RPC traffic, so operational messages are printed to stderr.

Backend connection is configuration-only after ToolServerSpec is built. unified_agent/…/codex.py turns the spec into -c mcp_servers.<server> overrides for command, args, env, startup timeout, tool timeout, required startup, and automatic default tool approval. unified_agent/…/claude_code.py adds a stdio MCP server entry with the same command, args, and env, and appends mcp__<server_name>__* to allowed tools so Claude Code may call every tool registered on that server. Claude Code also inspects init messages and raises ToolServerError if the named MCP server reports a bad connection status.

Coupling

  • If ToolRegistry name validation or fully qualified name construction changes in unified_agent/tools.py, verify both unified_agent/…/codex.py and unified_agent/…/claude_code.py: Codex config keys use spec.server_name, while Claude permissions allow mcp__<server_name>__*, so any naming drift can make registered tools unreachable or unapproved in one backend.
  • If build_tool_server_spec() changes its command, arguments, or environment shape, update both backend launch paths. Codex serializes ToolServerSpec.env into a TOML inline table, and Claude Code passes the same env dict into its mcp_servers option; missing PYTHONPATH entries break subprocess imports before the model can call a tool.
  • If a registry is constructed dynamically rather than as a top-level importable module attribute, pass an explicit pkg.mod:ATTR string or refactor it into a module. registry_spec() in unified_agent/tools.py cannot discover registries defined in __main__, and unified_agent/tool_server.py can only serve registries that resolve_registry() can import.

Risks

  • unified_agent/tools.py depends on sys._getframe(1) to remember the defining module for ToolRegistry. If a helper factory creates and returns a registry from a different module, registry_spec() may search the helper module instead of the module where the caller stores the registry, causing build_tool_server_spec() to fail before the task starts.
  • unified_agent/tool_server.py reserves stdout for FastMCP stdio protocol. If diagnostic output is added with plain print() instead of file=sys.stderr, it can corrupt JSON-RPC traffic and make both backends fail to connect to the tool server.