MCP and Tool-Call Support - #981
Conversation
🦋 Changeset detectedLatest commit: ba92009 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.
Greptile Summary
This pull request adds Model Context Protocol (MCP) and custom tool support to Stagehand agents, significantly expanding the framework's capabilities beyond browser automation to include third-party service integrations. The changes introduce a flexible architecture where agents can connect to external MCP servers and execute custom tools alongside existing browser automation functions.
The implementation provides three ways to configure integrations: (1) simple string URLs that are internally converted to MCP connections, (2) pre-established connections using the built-in connectToMCPServer utility, and (3) custom Client objects from the @modelcontextprotocol/sdk. The agent configuration now accepts integrations and tools parameters in the AgentConfig interface.
Core architectural changes include:
- New MCP utilities:
lib/mcp/connection.tsprovidesconnectToMCPServerfor establishing connections, whilelib/mcp/utils.tsimplementsresolveToolsto convert MCP tools into AI SDK's ToolSet format - Agent client updates: Both OpenAI and Anthropic CUA clients now accept and execute custom tools alongside their computer use functionality
- Operator handler refactor: Complete rewrite from schema-based responses to a tool-calling architecture that combines built-in Stagehand operations (act, extract, goto, etc.) with external MCP tools
- Type system enhancements: New error handling with
MCPConnectionError, JSON Schema to Zod conversion utilities, and updated type definitions
The changes maintain backward compatibility while enabling agents to adapt to complex web environments requiring external API access, search capabilities, database interactions, and other third-party services. Examples demonstrate real-world usage with Exa AI search integration.
Confidence score: 4/5
- This PR introduces complex new functionality but appears well-architected with proper error handling and flexible API design
- Score reflects the significant architectural changes in critical files like the operator handler, though the implementation follows good patterns
- Pay close attention to
lib/handlers/operatorHandler.tsand the MCP connection utilities for potential integration issues
18 files reviewed, 3 comments
| } | ||
| case "string": { | ||
| if (schema.enum) { | ||
| return z.string().refine((val) => schema.enum!.includes(val)); |
There was a problem hiding this comment.
style: Using .refine() for enum validation instead of .enum() - this works but .enum() would be more direct for string enums
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
| clientInstance = client; | ||
| } | ||
|
|
||
| let nextCursor: string | undefined = undefined; |
There was a problem hiding this comment.
what does cursor stand for? a pointer to iterate through the listed tools?
There was a problem hiding this comment.
yeah it's for handling pagination of the tools
| options.modelName, | ||
| options.clientOptions || {}, | ||
| options.userProvidedInstructions, | ||
| options.experimental, |
There was a problem hiding this comment.
do we want to keep experimental for context pruning? or now that it's deployed it's obsolete
| }), | ||
| }, | ||
| executeOptions, | ||
| }, |
There was a problem hiding this comment.
table into separate pr once deployed
|
e2e tests failing, need to double check |
…com/browserbase/stagehand into sarif/stg-519-mcp-and-tools-support
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.0 ### Minor Changes - [#981](#981) [`8244ab2`](8244ab2) Thanks [@sameelarif](https://github.com/sameelarif)! - Added support for `stagehand.agent` to interact with MCP servers as well as custom tools to be passed in. For more information, reference the [MCP integrations documentation](https://docs.stagehand.dev/best-practices/mcp-integrations) ### Patch Changes - [#959](#959) [`09b5e1e`](09b5e1e) Thanks [@filip-michalsky](https://github.com/filip-michalsky)! - add webvoyager evals - [#1049](#1049) [`e3734b9`](e3734b9) Thanks [@miguelg719](https://github.com/miguelg719)! - Support local MCP server connections - [#1025](#1025) [`be85b19`](be85b19) Thanks [@tkattkat](https://github.com/tkattkat)! - add support for custom baseUrl within openai provider - [#1040](#1040) [`88d1565`](88d1565) Thanks [@miguelg719](https://github.com/miguelg719)! - Allow OpenAI CUA to take in an optional baseURL - [#1046](#1046) [`ab5d6ed`](ab5d6ed) Thanks [@tkattkat](https://github.com/tkattkat)! - Add support for gpt-5 in operator agent ## @browserbasehq/stagehand-evals@1.0.9 ### Patch Changes - Updated dependencies \[[`09b5e1e`](09b5e1e), [`e3734b9`](e3734b9), [`8244ab2`](8244ab2), [`be85b19`](be85b19), [`88d1565`](88d1565), [`ab5d6ed`](ab5d6ed)]: - @browserbasehq/stagehand@2.5.0 ## @browserbasehq/stagehand-examples@1.0.9 ### Patch Changes - Updated dependencies \[[`09b5e1e`](09b5e1e), [`e3734b9`](e3734b9), [`8244ab2`](8244ab2), [`be85b19`](be85b19), [`88d1565`](88d1565), [`ab5d6ed`](ab5d6ed)]: - @browserbasehq/stagehand@2.5.0 Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.0 ### Minor Changes - [#981](browserbase/stagehand#981) [`8244ab2`](browserbase/stagehand@8244ab2) Thanks [@sameelarif](https://github.com/sameelarif)! - Added support for `stagehand.agent` to interact with MCP servers as well as custom tools to be passed in. For more information, reference the [MCP integrations documentation](https://docs.stagehand.dev/best-practices/mcp-integrations) ### Patch Changes - [#959](browserbase/stagehand#959) [`09b5e1e`](browserbase/stagehand@09b5e1e) Thanks [@filip-michalsky](https://github.com/filip-michalsky)! - add webvoyager evals - [#1049](browserbase/stagehand#1049) [`e3734b9`](browserbase/stagehand@e3734b9) Thanks [@miguelg719](https://github.com/miguelg719)! - Support local MCP server connections - [#1025](browserbase/stagehand#1025) [`be85b19`](browserbase/stagehand@be85b19) Thanks [@tkattkat](https://github.com/tkattkat)! - add support for custom baseUrl within openai provider - [#1040](browserbase/stagehand#1040) [`88d1565`](browserbase/stagehand@88d1565) Thanks [@miguelg719](https://github.com/miguelg719)! - Allow OpenAI CUA to take in an optional baseURL - [#1046](browserbase/stagehand#1046) [`ab5d6ed`](browserbase/stagehand@ab5d6ed) Thanks [@tkattkat](https://github.com/tkattkat)! - Add support for gpt-5 in operator agent ## @browserbasehq/stagehand-evals@1.0.9 ### Patch Changes - Updated dependencies \[[`09b5e1e`](browserbase/stagehand@09b5e1e), [`e3734b9`](browserbase/stagehand@e3734b9), [`8244ab2`](browserbase/stagehand@8244ab2), [`be85b19`](browserbase/stagehand@be85b19), [`88d1565`](browserbase/stagehand@88d1565), [`ab5d6ed`](browserbase/stagehand@ab5d6ed)]: - @browserbasehq/stagehand@2.5.0 ## @browserbasehq/stagehand-examples@1.0.9 ### Patch Changes - Updated dependencies \[[`09b5e1e`](browserbase/stagehand@09b5e1e), [`e3734b9`](browserbase/stagehand@e3734b9), [`8244ab2`](browserbase/stagehand@8244ab2), [`be85b19`](browserbase/stagehand@be85b19), [`88d1565`](browserbase/stagehand@88d1565), [`ab5d6ed`](browserbase/stagehand@ab5d6ed)]: - @browserbasehq/stagehand@2.5.0 Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
# why
At the moment, Stagehand excels at browser automation but is limited in
being able to interact with third-party services. Many production use
cases require supporting these integrations. As the scope of browser
agents expand, it’s becoming increasingly important that agents adapt to
the web rather than the web adapting to them.
# what changed
Added support for integrations (MCPs) and custom tools to be passed in
to the Stagehand agent. Here are some examples:
```ts
const agent = stagehand.agent({
integrations: [
"https://www.example.com/mcp",
],
});
```
The above syntax will internally create the MCP connection. However, if
you prefer a self-managed connection, there are two more options
available. The first option is to use our built-in util to establish a
connection:
```ts
import { connectToMCPServer } from "@browserbasehq/stagehand"
const exampleClient = await connectToMCPServer("https://www.example.com/mcp")
const agent = stagehand.agent({
integrations: [exampleClient],
});
```
The alternative is to establish a connection to the MCP server with your
own client. Stagehand takes in a `Client` object from the
`@modelcontextprotocol/sdk` package. Please refer to their
[documentation](https://modelcontextprotocol.io/docs/getting-started/intro)
for more information.
# test plan
Internal agent evals.
---------
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.0 ### Minor Changes - [browserbase#981](browserbase#981) [`8244ab2`](browserbase@8244ab2) Thanks [@sameelarif](https://github.com/sameelarif)! - Added support for `stagehand.agent` to interact with MCP servers as well as custom tools to be passed in. For more information, reference the [MCP integrations documentation](https://docs.stagehand.dev/best-practices/mcp-integrations) ### Patch Changes - [browserbase#959](browserbase#959) [`09b5e1e`](browserbase@09b5e1e) Thanks [@filip-michalsky](https://github.com/filip-michalsky)! - add webvoyager evals - [browserbase#1049](browserbase#1049) [`e3734b9`](browserbase@e3734b9) Thanks [@miguelg719](https://github.com/miguelg719)! - Support local MCP server connections - [browserbase#1025](browserbase#1025) [`be85b19`](browserbase@be85b19) Thanks [@tkattkat](https://github.com/tkattkat)! - add support for custom baseUrl within openai provider - [browserbase#1040](browserbase#1040) [`88d1565`](browserbase@88d1565) Thanks [@miguelg719](https://github.com/miguelg719)! - Allow OpenAI CUA to take in an optional baseURL - [browserbase#1046](browserbase#1046) [`ab5d6ed`](browserbase@ab5d6ed) Thanks [@tkattkat](https://github.com/tkattkat)! - Add support for gpt-5 in operator agent ## @browserbasehq/stagehand-evals@1.0.9 ### Patch Changes - Updated dependencies \[[`09b5e1e`](browserbase@09b5e1e), [`e3734b9`](browserbase@e3734b9), [`8244ab2`](browserbase@8244ab2), [`be85b19`](browserbase@be85b19), [`88d1565`](browserbase@88d1565), [`ab5d6ed`](browserbase@ab5d6ed)]: - @browserbasehq/stagehand@2.5.0 ## @browserbasehq/stagehand-examples@1.0.9 ### Patch Changes - Updated dependencies \[[`09b5e1e`](browserbase@09b5e1e), [`e3734b9`](browserbase@e3734b9), [`8244ab2`](browserbase@8244ab2), [`be85b19`](browserbase@be85b19), [`88d1565`](browserbase@88d1565), [`ab5d6ed`](browserbase@ab5d6ed)]: - @browserbasehq/stagehand@2.5.0
why
At the moment, Stagehand excels at browser automation but is limited in being able to interact with third-party services. Many production use cases require supporting these integrations. As the scope of browser agents expand, it’s becoming increasingly important that agents adapt to the web rather than the web adapting to them.
what changed
Added support for integrations (MCPs) and custom tools to be passed in to the Stagehand agent. Here are some examples:
The above syntax will internally create the MCP connection. However, if you prefer a self-managed connection, there are two more options available. The first option is to use our built-in util to establish a connection:
The alternative is to establish a connection to the MCP server with your own client. Stagehand takes in a
Clientobject from the@modelcontextprotocol/sdkpackage. Please refer to their documentation for more information.test plan
Internal agent evals.