feat(anthropic): support defer_loading tool search expansion#109
Open
juslintek wants to merge 2 commits intojwadow:mainfrom
Open
feat(anthropic): support defer_loading tool search expansion#109juslintek wants to merge 2 commits intojwadow:mainfrom
juslintek wants to merge 2 commits intojwadow:mainfrom
Conversation
…ol_result content blocks Claude Code v2.1.69+ sends tool_reference blocks inside tool_result content when using the deferred tool search feature (ENABLE_TOOL_SEARCH=true). The Anthropic API also returns server_tool_use and tool_search_tool_result blocks for server-side tool search invocations. Without these models, Pydantic validation rejects requests containing these block types with 422 errors. Changes: - Add ToolReferenceContentBlock model (type='tool_reference') - Add ServerToolUseContentBlock model (type='server_tool_use') - Add ToolSearchResultContentBlock model (type='tool_search_tool_result') - Add ToolReferenceContentBlock to ToolResultContentBlock.content union - Add all three to ContentBlock union - Add model_config extra=allow to ToolResultContentBlock for cache_control Related: jwadow#90, jwadow#96, jwadow#82 (different approaches to the same 422 issue)
When Claude Code sends tools with defer_loading=true (ENABLE_TOOL_SEARCH), the gateway now: 1. Separates deferred tools from active tools 2. Only sends active tools to Kiro API (reducing token usage) 3. Scans messages for tool_reference blocks 4. Expands referenced deferred tools into the active set This enables Claude Code's MCP Tool Search feature to work through the gateway, where the model requests tools on-demand instead of sending all MCP tools in every request. Also skips Anthropic built-in server tools (no input_schema) that the Kiro API cannot handle. Depends on: jwadow#108 (tool search content block models)
|
Thanks for the PR! 🎉 Before merge, we need a one-time CLA confirmation. Full CLA text: Please reply once with: You need to write once, all further messages from me can be ignored. |
Author
|
I have read the CLA and I accept its terms |
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
Enables Claude Code's MCP Tool Search feature to work through the gateway by handling
defer_loadingtools and expandingtool_referenceblocks.Why
When Claude Code has
ENABLE_TOOL_SEARCH=true, it sends MCP tools withdefer_loading: trueinstead of including them all in every request. The model then requests specific tools on-demand viatool_referenceblocks. Without this, the gateway either sends all deferred tools (wasting tokens) or drops them entirely.How it works
convert_anthropic_tools()readsdefer_loadingfrom each tool, stores it onUnifiedTool._defer_loadinganthropic_to_kiro()separates deferred vs active toolstool_referenceblocks (both top-level and insidetool_resultcontent)Also skips Anthropic built-in server tools (no
input_schema) that the Kiro API cannot handle.Changes
converters_core.py: Add_defer_loading: boolfield toUnifiedTooldataclassconverters_anthropic.py:convert_anthropic_tools(): Readdefer_loading, skip server tools withoutinput_schemaanthropic_to_kiro(): Defer/expand logic withtool_referencescanningDependencies
Depends on #108 (tool search content block models)
Testing
All 1413 existing tests pass. Verified locally with live Claude Code traffic — logs show: