Skip to content

feat(tools): implement deferred tool discovery with tool_search#9

Merged
Jonnytoshen merged 8 commits into
mainfrom
feat/dynamic-tools
Jun 1, 2026
Merged

feat(tools): implement deferred tool discovery with tool_search#9
Jonnytoshen merged 8 commits into
mainfrom
feat/dynamic-tools

Conversation

@Jonnytoshen

Copy link
Copy Markdown
Owner

Summary

Implements a deferred tool discovery mechanism so the agent only loads MCP tool schemas on demand, keeping the system prompt lean while still making all tools accessible via tool_search.

Changes

  • ToolDefinition interface — added shouldDefer and searchHint metadata fields
  • MCP registration — all MCP tools are now marked shouldDefer: true with auto-generated searchHint from their server name and description
  • ToolRegistry — tracks which deferred tools have been discovered via a discoveredTools Set
  • searchTools() — exact-name lookup for deferred tools; adds discovered tools to the active set
  • getActiveTools() / getDeferredToolSummary() — control prompt visibility and generate the "available but deferred" hint injected into the system prompt
  • countTokenEstimate() — debug/monitoring utility that estimates prompt token impact split between active and deferred tools
  • ToolSearch tool — new built-in tool_search tool; the agent calls it to hydrate a deferred tool's full schema before invoking it
  • CLI (src/index.ts) — wires the new APIs into the startup system prompt and tool dashboard table (shouldDefer column + token stats)

How It Works

Startup
  └── getDeferredToolSummary() → appended to SYSTEM prompt
        "以下工具可用,但需要先通过 tool_search 搜索获取完整定义: ..."

Agent needs an MCP tool
  └── calls tool_search("mcp__github__list_issues")
        └── searchTools() marks tool as discovered
              └── next turn: tool appears in toAISDKFormat() output
                    └── agent calls the real tool

Commits

# Hash Message
1 e0fe4b6 feat(tools): add deferred metadata fields to tool definition
2 c039f19 feat(tools): mark MCP tools as deferred with search hints
3 8d21962 feat(tools): add discovered tool tracking in registry
4 7d424a7 feat(tools): add exact-name lookup API for deferred tools
5 03f73bb feat(tools): add active and deferred tool summary APIs
6 6fd8e6d feat(tools): add token estimate stats for tool schemas
7 195866a feat(tools): add tool_search for deferred tool schema loading
8 fe96037 feat(cli): wire dynamic tool discovery into startup prompt and tool dashboard

Files Changed

File +/-
src/tools/tool-registry.ts +107
src/tools/ToolSearch.ts +35 (new)
src/index.ts +19 / -9

Checklist

  • Lint passes (pnpm lint)
  • No breaking changes to existing tool registration API
  • All MCP tools automatically deferred — no per-tool configuration needed
  • Backward compatible: non-deferred tools behave identically to before

Copilot AI review requested due to automatic review settings June 1, 2026 01:42
@Jonnytoshen Jonnytoshen merged commit dbd64e9 into main Jun 1, 2026
1 check failed
@Jonnytoshen Jonnytoshen deleted the feat/dynamic-tools branch June 1, 2026 01:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants