-
Notifications
You must be signed in to change notification settings - Fork 1.2k
properly convert custom / mcp tools to anthropic cua format #1103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: ac46fb6 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Overview
Summary
This PR fixes a critical integration issue with custom AI SDK tools and MCP (Model Context Protocol) integrations when using Anthropic's Computer Use API (CUA). The core problem was that Zod schemas used by AI SDK tools weren't being properly converted to the JSON Schema format expected by Anthropic's API, resulting in empty parameters `{}` being passed to tools instead of the actual parameter values.The changes enhance the AnthropicCUAClient class in three key ways:
-
Schema Conversion: Imports and uses
zodToJsonSchemato properly convert Zod schemas to JSON Schema format before sending tool definitions to the Anthropic API. This ensures that tool parameters are correctly structured and populated. -
Action Tracking: Adds tracking for custom tool executions in the actions array by creating
custom_toolaction entries when custom tools are invoked. This improves observability and debugging capabilities. -
Warning Suppression: Silences "Unknown tool name" warnings for registered custom tools, cleaning up the logging output and reducing noise for legitimate tool usage.
These changes integrate seamlessly with the existing agent architecture, building on the AnthropicCUAClient's existing tool handling infrastructure while fixing the schema conversion gap that prevented proper parameter passing to custom tools.
Important Files Changed
Changed Files
| Filename | Score | Overview |
|---|---|---|
| lib/agent/AnthropicCUAClient.ts | 4/5 | Fixes custom tool integration by adding Zod to JSON Schema conversion, custom tool action tracking, and warning suppression |
Confidence score: 4/5
- This PR addresses a specific integration bug with clear, targeted fixes that are unlikely to cause regressions
- Score reflects well-contained changes that solve a legitimate problem without introducing complex new logic paths
- Pay close attention to the schema conversion logic to ensure zodToJsonSchema handles all edge cases properly
Sequence Diagram
sequenceDiagram
participant User
participant AnthropicCUAClient
participant AnthropicAPI as "Anthropic API"
participant CustomTool as "Custom/MCP Tool"
participant Logger
User->>AnthropicCUAClient: "execute(instruction, tools)"
AnthropicCUAClient->>Logger: "log: Starting execution"
loop For each step (until completion or maxSteps)
AnthropicCUAClient->>AnthropicCUAClient: "createInitialInputItems(instruction)"
AnthropicCUAClient->>AnthropicCUAClient: "convertCustomToolsToJsonSchema()"
Note over AnthropicCUAClient: Uses zodToJsonSchema to convert Zod schemas
AnthropicCUAClient->>AnthropicAPI: "messages.create(with custom tools)"
AnthropicAPI-->>AnthropicCUAClient: "response with tool_use blocks"
AnthropicCUAClient->>Logger: "log: Processing content blocks"
loop For each tool_use block
alt Computer tool
AnthropicCUAClient->>AnthropicCUAClient: "convertToolUseToAction()"
AnthropicCUAClient->>AnthropicCUAClient: "captureScreenshot()"
else Custom tool
AnthropicCUAClient->>CustomTool: "execute(input)"
CustomTool-->>AnthropicCUAClient: "tool result"
AnthropicCUAClient->>AnthropicCUAClient: "create custom_tool action"
Note over AnthropicCUAClient: Now tracks custom tools in actions array
end
AnthropicCUAClient->>Logger: "log: Tool execution result"
end
AnthropicCUAClient->>AnthropicCUAClient: "takeAction(toolUseItems)"
AnthropicCUAClient->>AnthropicCUAClient: "updateInputItemsForNextStep()"
end
AnthropicCUAClient-->>User: "AgentResult with actions and usage"
1 file reviewed, 1 comment
Why
Custom AI SDK tools and MCP integrations weren't working properly with
Anthropic CUA - parameters were empty {} and tools weren't tracked.
What Changed
- Convert Zod schemas to JSON Schema before sending to Anthropic (using
zodToJsonSchema)
- Track custom tool calls in the actions array
- Silence "Unknown tool name" warnings for custom tools
Test Plan
Tested with examples file.
Parameters passed correctly ({"city":"San Francisco"} instead of {})
Custom tools execute and appear in actions array
No warnings
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @browserbasehq/stagehand@2.5.1 ### Patch Changes - [#1082](#1082) [`8c0fd01`](8c0fd01) Thanks [@tkattkat](https://github.com/tkattkat)! - Pass stagehand object to agent instead of stagehand page - [#1104](#1104) [`a1ad06c`](a1ad06c) Thanks [@miguelg719](https://github.com/miguelg719)! - Fix logging for stagehand agent - [#1066](#1066) [`9daa584`](9daa584) Thanks [@tkattkat](https://github.com/tkattkat)! - Add playwright arguments to agent execute response - [#1077](#1077) [`7f38b3a`](7f38b3a) Thanks [@tkattkat](https://github.com/tkattkat)! - adds support for stagehand agent in the api - [#1032](#1032) [`bf2d0e7`](bf2d0e7) Thanks [@miguelg719](https://github.com/miguelg719)! - Fix for zod peer dependency support - [#1014](#1014) [`6966201`](6966201) Thanks [@tkattkat](https://github.com/tkattkat)! - Replace operator handler with base of new agent - [#1089](#1089) [`536f366`](536f366) Thanks [@miguelg719](https://github.com/miguelg719)! - Fixed info logs on api session create - [#1103](#1103) [`889cb6c`](889cb6c) Thanks [@tkattkat](https://github.com/tkattkat)! - patch custom tool support in anthropic cua client - [#1056](#1056) [`6a002b2`](6a002b2) Thanks [@chrisreadsf](https://github.com/chrisreadsf)! - remove need for duplicate project id if already passed to Stagehand - [#1090](#1090) [`8ff5c5a`](8ff5c5a) Thanks [@miguelg719](https://github.com/miguelg719)! - Improve failed act error logs - [#1014](#1014) [`6966201`](6966201) Thanks [@tkattkat](https://github.com/tkattkat)! - replace operator agent with scaffold for new stagehand agent - [#1107](#1107) [`3ccf335`](3ccf335) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fix: url extraction not working inside an array - [#1102](#1102) [`a99aa48`](a99aa48) Thanks [@miguelg719](https://github.com/miguelg719)! - Add current page and date context to agent - [#1110](#1110) [`dda52f1`](dda52f1) Thanks [@miguelg719](https://github.com/miguelg719)! - Add support for new Gemini Computer Use models ## @browserbasehq/stagehand-evals@1.1.0 ### Minor Changes - [#1057](#1057) [`b7be89e`](b7be89e) Thanks [@filip-michalsky](https://github.com/filip-michalsky)! - added web voyager ground truth (optional), added web bench, and subset of OSWorld evals which run on a browser ### Patch Changes - [#1072](#1072) [`dc2d420`](dc2d420) Thanks [@filip-michalsky](https://github.com/filip-michalsky)! - improve evals screenshot service - add img hashing diff to add screenshots and change to screenshot intercepts from the agent - Updated dependencies \[[`8c0fd01`](8c0fd01), [`a1ad06c`](a1ad06c), [`9daa584`](9daa584), [`7f38b3a`](7f38b3a), [`bf2d0e7`](bf2d0e7), [`6966201`](6966201), [`536f366`](536f366), [`889cb6c`](889cb6c), [`6a002b2`](6a002b2), [`8ff5c5a`](8ff5c5a), [`6966201`](6966201), [`3ccf335`](3ccf335), [`a99aa48`](a99aa48), [`dda52f1`](dda52f1)]: - @browserbasehq/stagehand@2.5.1 ## @browserbasehq/stagehand-examples@1.0.10 ### Patch Changes - Updated dependencies \[[`8c0fd01`](8c0fd01), [`a1ad06c`](a1ad06c), [`9daa584`](9daa584), [`7f38b3a`](7f38b3a), [`bf2d0e7`](bf2d0e7), [`6966201`](6966201), [`536f366`](536f366), [`889cb6c`](889cb6c), [`6a002b2`](6a002b2), [`8ff5c5a`](8ff5c5a), [`6966201`](6966201), [`3ccf335`](3ccf335), [`a99aa48`](a99aa48), [`dda52f1`](dda52f1)]: - @browserbasehq/stagehand@2.5.1 Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Why
Custom AI SDK tools and MCP integrations weren't working properly with Anthropic CUA - parameters were empty {} and tools weren't tracked.
What Changed
Test Plan
Tested with examples file.
Parameters passed correctly ({"city":"San Francisco"} instead of {})
Custom tools execute and appear in actions array
No warnings