-
Notifications
You must be signed in to change notification settings - Fork 306
feat: adding agent tool for mcp server #130
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { z } from "zod"; | ||
| import type { Tool, ToolSchema, ToolResult } from "./tool.js"; | ||
| import type { Context } from "../context.js"; | ||
| import type { ToolActionResult } from "../types/types.js"; | ||
|
|
||
| /** | ||
| * Stagehand Agent | ||
| * Docs: https://docs.stagehand.dev/basics/agent | ||
| * | ||
| * This tool uses Gemini Computer Use to autonomously complete web-based tasks. | ||
| * The agent will navigate, interact, and complete the task described in the prompt. | ||
| */ | ||
|
|
||
| const AgentInputSchema = z.object({ | ||
| prompt: z.string().describe( | ||
| `The task prompt describing what you want the agent to accomplish. | ||
| Be clear and specific about the goal. For example: | ||
| 'Go to Hacker News and find the most controversial post from today, then summarize the top 3 comments'. | ||
| The agent will autonomously navigate and interact with web pages to complete this task.`, | ||
| ), | ||
| }); | ||
|
|
||
| type AgentInput = z.infer<typeof AgentInputSchema>; | ||
|
|
||
| const agentSchema: ToolSchema<typeof AgentInputSchema> = { | ||
| name: "browserbase_stagehand_agent", | ||
| description: `Execute a task autonomously using Gemini Computer Use agent. The agent will navigate and interact with web pages to complete the given task.`, | ||
| inputSchema: AgentInputSchema, | ||
| }; | ||
|
|
||
| async function handleAgent( | ||
| context: Context, | ||
| params: AgentInput, | ||
| ): Promise<ToolResult> { | ||
| const action = async (): Promise<ToolActionResult> => { | ||
| try { | ||
| const stagehand = await context.getStagehand(); | ||
|
|
||
| // You need to provide GOOGLE_GENERATIVE_AI_API_KEY | ||
| const agent = stagehand.agent({ | ||
| cua: true, | ||
| model: { | ||
| modelName: "google/gemini-2.5-computer-use-preview-10-2025", | ||
| apiKey: | ||
| process.env.GOOGLE_GENERATIVE_AI_API_KEY || | ||
| process.env.GOOGLE_API_KEY || | ||
| process.env.GEMINI_API_KEY, | ||
| }, | ||
| }); | ||
|
|
||
| // Execute the task | ||
| const result = await agent.execute({ | ||
| instruction: params.prompt, | ||
| maxSteps: 20, | ||
| }); | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: "text", | ||
| text: `Agent execution completed:\n${JSON.stringify(result, null, 2)}`, | ||
| }, | ||
| ], | ||
| }; | ||
| } catch (error) { | ||
| const errorMsg = error instanceof Error ? error.message : String(error); | ||
| throw new Error(`Failed to execute agent task: ${errorMsg}`); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| action, | ||
| waitForNetwork: false, | ||
| }; | ||
| } | ||
|
|
||
| const agentTool: Tool<typeof AgentInputSchema> = { | ||
| capability: "core", | ||
| schema: agentSchema, | ||
| handle: handleAgent, | ||
| }; | ||
|
|
||
| export default agentTool; | ||
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
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.
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.
logic: missing validation - if all env vars are undefined, the agent will be created with
undefinedand fail at runtime when executingvalidate the resolved value is not
undefinedbefore passing to agent constructorPrompt To Fix With AI