23 |
24 | ## Overview
25 |
26 | Hyperagent is Playwright supercharged with AI. No more brittle scripts, just powerful natural language commands.
27 | Just looking for scalable headless browsers or scraping infra? Go to [Hyperbrowser](https://app.hyperbrowser.ai/) to get started for free!
28 |
29 | ### Features
30 |
31 | - 🤖 **AI Commands**: Simple APIs like `page.ai()`, `page.extract()` and `executeTask()` for any AI automation
32 | - ⚡ **Fallback to Regular Playwright**: Use regular Playwright when AI isn't needed
33 | - 🥷 **Stealth Mode** – Avoid detection with built-in anti-bot patches
34 | - ☁️ **Cloud Ready** – Instantly scale to hundreds of sessions via [Hyperbrowser](https://app.hyperbrowser.ai/)
35 | - 🔌 **MCP Client** – Connect to tools like Composio for full workflows (e.g. writing web data to Google Sheets)
36 |
37 | ## Quick Start
38 |
39 | ### Installation
40 |
41 | ```bash
42 | # Using npm
43 | npm install @hyperbrowser/agent
44 |
45 | # Using yarn
46 | yarn add @hyperbrowser/agent
47 | ```
48 |
49 | ### CLI
50 |
51 | ```bash
52 | $ npx @hyperbrowser/agent -c "Find a route from Miami to New Orleans, and provide the detailed route information."
53 | ```
54 |
55 |
56 |
57 |
58 |
59 | The CLI supports options for debugging or using hyperbrowser instead of a local browser
60 |
61 | ```bash
62 | -d, --debug Enable debug mode
63 | -c, --command Command to run
64 | --hyperbrowser Use Hyperbrowser for the browser provider
65 | ```
66 |
67 | ### Library
68 |
69 | ```typescript
70 | import { HyperAgent } from "@hyperbrowser/agent";
71 | import { ChatOpenAI } from "@langchain/openai";
72 | import { z } from "zod";
73 |
74 | // Initialize the agent
75 | const agent = new HyperAgent({
76 | llm: new ChatOpenAI({
77 | openAIApiKey: process.env.OPENAI_API_KEY,
78 | modelName: "gpt-4o",
79 | }),
80 | });
81 |
82 | // Execute a task
83 | const result = await agent.executeTask(
84 | "Navigate to amazon.com, search for 'laptop', and extract the prices of the first 5 results"
85 | );
86 | console.log(result.output);
87 |
88 | // Use page.ai and page.extract
89 | const page = await agent.newPage();
90 | await page.goto("https://flights.google.com", { waitUntil: "load" });
91 | await page.ai("search for flights from Rio to LAX from July 16 to July 22");
92 | const res = await page.extract(
93 | "give me the flight options",
94 | z.object({
95 | flights: z.array(
96 | z.object({
97 | price: z.number(),
98 | departure: z.string(),
99 | arrival: z.string(),
100 | })
101 | ),
102 | })
103 | );
104 | console.log(res);
105 |
106 | // Clean up
107 | await agent.closeAgent();
108 | ```
109 |
110 | ## ☁️ Cloud
111 |
112 | You can scale HyperAgent with cloud headless browsers using Hyperbrowser
113 |
114 | 1. Get a free api key from [Hyperbrowser](https://app.hyperbrowser.ai/)
115 | 2. Add it to your env as `HYPERBROWSER_API_KEY`
116 | 3. Set your `browserProvider` to `"Hyperbrowser"`
117 |
118 | ```typescript
119 | const agent = new HyperAgent({
120 | browserProvider: "Hyperbrowser",
121 | });
122 |
123 | const response = await agent.executeTask(
124 | "Go to hackernews, and list me the 5 most recent article titles"
125 | );
126 |
127 | console.log(response);
128 | await agent.closeAgent();
129 | ```
130 |
131 | ## Usage Guide
132 |
133 | ### Multi-Page Management
134 |
135 | ```typescript
136 | // Create and manage multiple pages
137 | const page1 = await agent.newPage();
138 | const page2 = await agent.newPage();
139 |
140 | // Execute tasks on specific pages
141 | const page1Response = await page1.ai(
142 | "Go to google.com/travel/explore and set the starting location to New York. Then, return to me the first recommended destination that shows up. Return to me only the name of the location."
143 | );
144 | const page2Response = await page2.ai(
145 | `I want to plan a trip to ${page1Response.output}. Recommend me places to visit there.`
146 | );
147 |
148 | console.log(page2Response.output);
149 |
150 | // Get all active pages
151 | const pages = await agent.getPages();
152 | await agent.closeAgent();
153 | ```
154 |
155 | ## Customization
156 |
157 | ### Output Schema Definition
158 |
159 | HyperAgent can extract data in a specified schema. The schema can be passed in at a per-task level
160 |
161 | ```typescript
162 | import { z } from "zod";
163 |
164 | const agent = new HyperAgent();
165 | const agentResponse = await agent.executeTask(
166 | "Navigate to imdb.com, search for 'The Matrix', and extract the director, release year, and rating",
167 | {
168 | outputSchema: z.object({
169 | director: z.string().describe("The name of the movie director"),
170 | releaseYear: z.number().describe("The year the movie was released"),
171 | rating: z.string().describe("The IMDb rating of the movie"),
172 | }),
173 | }
174 | );
175 | console.log(agentResponse.output);
176 | await agent.closeAgent();
177 | ```
178 |
179 | ```bash
180 | {
181 | "director": "Lana Wachowski, Lilly Wachowski",
182 | "releaseYear": 1999,
183 | "rating": "8.7/10"
184 | }
185 | ```
186 |
187 | ### Using Different LLM Providers
188 |
189 | Hyperagent supports multiple LLM providers. A provider can be anything that extends to the Langchain `BaseChatModel` class.
190 |
191 | ```typescript
192 | // Using OpenAI
193 | const agent = new HyperAgent({
194 | llm: new ChatOpenAI({
195 | openAIApiKey: process.env.OPENAI_API_KEY,
196 | modelName: "gpt-4o",
197 | }),
198 | });
199 |
200 | // Using Anthropic's Claude
201 | const agent = new HyperAgent({
202 | llm: new ChatAnthropic({
203 | anthropicApiKey: process.env.ANTHROPIC_API_KEY,
204 | modelName: "claude-3-7-sonnet-latest",
205 | }),
206 | });
207 | ```
208 |
209 | ### MCP Support
210 |
211 | HyperAgent functions as a fully functional MCP client. For best results, we recommend using
212 | `gpt-4o` as your LLM.
213 |
214 | Here is an example which reads from wikipedia, and inserts information into a google sheet using the composio Google Sheet MCP. For the full example, see [here](https://github.com/hyperbrowserai/HyperAgent/tree/main/examples/mcp/google-sheets/most-populated-states.ts)
215 |
216 | ```typescript
217 | const agent = new HyperAgent({
218 | llm: llm,
219 | debug: true,
220 | });
221 |
222 | await agent.initializeMCPClient({
223 | servers: [
224 | {
225 | command: "npx",
226 | args: [
227 | "@composio/mcp@latest",
228 | "start",
229 | "--url",
230 | "https://mcp.composio.dev/googlesheets/...",
231 | ],
232 | env: {
233 | npm_config_yes: "true",
234 | },
235 | },
236 | ],
237 | });
238 |
239 | const response = await agent.executeTask(
240 | "Go to https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population and get the data on the top 5 most populous states from the table. Then insert that data into a google sheet. You may need to first check if there is an active connection to google sheet, and if there isn't connect to it and present me with the link to sign in. "
241 | );
242 |
243 | console.log(response);
244 | await agent.closeAgent();
245 | ```
246 |
247 | ### Custom Actions
248 |
249 | HyperAgent's capabilities can be extended with custom actions. Custom actions require 3 things:
250 |
251 | - type: Name of the action. Should be something descriptive about the action.
252 | - actionParams: A zod object describing the parameters that the action may consume.
253 | - run: A function that takes in a context, and the params for the action and produces a result based on the params.
254 |
255 | Here is an example that performs a search using Exa
256 |
257 | ```typescript
258 | const exaInstance = new Exa(process.env.EXA_API_KEY);
259 |
260 | export const RunSearchActionDefinition: AgentActionDefinition = {
261 | type: "perform_search",
262 | actionParams: z.object({
263 | search: z
264 | .string()
265 | .describe(
266 | "The search query for something you want to search about. Keep the search query concise and to-the-point."
267 | ),
268 | }).describe("Search and return the results for a given query.");,
269 | run: async function (
270 | ctx: ActionContext,
271 | params: z.infer
272 | ): Promise {
273 | const results = (await exaInstance.search(params.search, {})).results
274 | .map(
275 | (res) =>
276 | `title: ${res.title} || url: ${res.url} || relevance: ${res.score}`
277 | )
278 | .join("\n");
279 |
280 | return {
281 | success: true,
282 | message: `Succesfully performed search for query ${params.search}. Got results: \n${results}`,
283 | };
284 | },
285 | };
286 |
287 | const agent = new HyperAgent({
288 | "Search about the news for today in New York",
289 | customActions: [RunSearchActionDefinition],
290 | });
291 | ```
292 |
293 | ## Contributing
294 |
295 | We welcome contributions to Hyperagent! Here's how you can help:
296 |
297 | 1. Fork the repository
298 | 2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
299 | 3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
300 | 4. Push to the branch (`git push origin feature/AmazingFeature`)
301 | 5. Open a Pull Request
302 |
303 | ## Support
304 |
305 | - 📚 [Documentation](https://docs.hyperbrowser.ai/hyperagent/about-hyperagent)
306 | - 💬 [Discord Community](https://discord.gg/zsYzsgVRjh)
307 | - 🐛 [Issue Tracker](https://github.com/hyperbrowserai/HyperAgent/issues)
308 | - 📧 [Email Support](mailto:info@hyperbrowser.ai)
309 |
--------------------------------------------------------------------------------
/assets/flight-schedule.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hyperbrowserai/HyperAgent/138076315fc49580c6955f2de6ce231a490be394/assets/flight-schedule.gif
--------------------------------------------------------------------------------
/assets/hyperagent-banner.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hyperbrowserai/HyperAgent/138076315fc49580c6955f2de6ce231a490be394/assets/hyperagent-banner.png
--------------------------------------------------------------------------------
/cli.sh:
--------------------------------------------------------------------------------
1 | SCRIPT_DIR="$(cd "$(dirname $(realpath "$0"))" && pwd)"
2 | NODE_OPTIONS="--no-deprecation" node "$SCRIPT_DIR/dist/cli/index.js" "$@"
--------------------------------------------------------------------------------
/eslint.config.mjs:
--------------------------------------------------------------------------------
1 | import typescriptEslint from "@typescript-eslint/eslint-plugin";
2 | import tsParser from "@typescript-eslint/parser";
3 | import path from "node:path";
4 | import { fileURLToPath } from "node:url";
5 | import js from "@eslint/js";
6 | import { FlatCompat } from "@eslint/eslintrc";
7 |
8 | const __filename = fileURLToPath(import.meta.url);
9 | const __dirname = path.dirname(__filename);
10 | const compat = new FlatCompat({
11 | baseDirectory: __dirname,
12 | recommendedConfig: js.configs.recommended,
13 | allConfig: js.configs.all
14 | });
15 |
16 | export default [
17 | ...compat.extends("eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"),
18 | {
19 | plugins: {
20 | "@typescript-eslint": typescriptEslint,
21 | },
22 |
23 | languageOptions: {
24 | parser: tsParser,
25 | },
26 | },
27 | ];
--------------------------------------------------------------------------------
/examples/browser-providers/hyperbrowser.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # Hyperbrowser Provider Example
3 | *
4 | * This example demonstrates how to configure and use HyperAgent with the Hyperbrowser
5 | * provider for web browsing tasks with proxy support.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a simple web search task that:
10 | * 1. Configures HyperAgent with Hyperbrowser-specific settings
11 | * 2. Enables proxy support for enhanced privacy and reliability
12 | * 3. Searches for and extracts specific information about a movie release date
13 | *
14 | * ## Prerequisites
15 | *
16 | * 1. Node.js environment
17 | * 2. OpenAI API key set in your .env file (OPENAI_API_KEY)
18 | *
19 | * ## Running the Example
20 | *
21 | * ```bash
22 | * yarn ts-node examples/browser-providers/hyperbrowser.ts
23 | * ```
24 | */
25 |
26 | import "dotenv/config";
27 | import { HyperAgent } from "@hyperbrowser/agent";
28 | import { ChatOpenAI } from "@langchain/openai";
29 | import chalk from "chalk";
30 |
31 | async function runEval() {
32 | const llm = new ChatOpenAI({
33 | apiKey: process.env.OPENAI_API_KEY,
34 | model: "gpt-4o",
35 | });
36 |
37 | const agent = new HyperAgent({
38 | llm: llm,
39 | debug: true,
40 | browserProvider: "Hyperbrowser",
41 | hyperbrowserConfig: {
42 | hyperbrowserSessionOptions: {
43 | useProxy: true,
44 | },
45 | },
46 | });
47 | const result = await agent.executeTask(
48 | "Find the initial release date for Guardians of the Galaxy Vol. 3 the movie",
49 | {
50 | debugOnAgentOutput: (agentOutput) => {
51 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
52 | console.dir(agentOutput, { depth: null, colors: true });
53 | console.log(chalk.cyan.bold("===============") + "\n");
54 | },
55 | onStep: (step) => {
56 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
57 | console.dir(step, { depth: null, colors: true });
58 | console.log(chalk.cyan.bold("===============") + "\n");
59 | },
60 | }
61 | );
62 | await agent.closeAgent();
63 | console.log(chalk.green.bold("\nResult:"));
64 | console.log(chalk.white(result.output));
65 | return result;
66 | }
67 |
68 | (async () => {
69 | await runEval();
70 | })().catch((error) => {
71 | console.error(chalk.red("Error:"), error);
72 | process.exit(1);
73 | });
74 |
--------------------------------------------------------------------------------
/examples/custom-tool/search/exa.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # Custom Search Tool Example with Exa
3 | *
4 | * This example demonstrates how to create and use a custom search tool with HyperAgent
5 | * using the Exa search API to perform web searches and process the results.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a multi-step task that showcases custom tool integration:
10 | * 1. Defines a custom search action using the Exa API
11 | * 2. Creates a schema for the search parameters using Zod
12 | * 3. Implements a search function that returns formatted results with titles, URLs, and relevance scores
13 | * 4. Demonstrates the tool usage with a complex travel planning task for Tokyo that:
14 | * - Searches for relevant information about Tokyo attractions
15 | * - Analyzes search results and filters for relevance
16 | * - Navigates to selected URLs to extract detailed information
17 | * - Compiles recommendations based on uniqueness and frequency
18 | *
19 | * ## Prerequisites
20 | *
21 | * 1. Node.js environment
22 | * 2. OpenAI API key set in your .env file (OPENAI_API_KEY)
23 | * 3. Exa API key set in your .env file (EXA_API_KEY)
24 | *
25 | * ## Custom Tool Configuration
26 | *
27 | * The example includes:
28 | * - Custom search action definition with Zod schema validation
29 | * - Integration with Exa search API
30 | * - Formatted result output with relevance scoring
31 | *
32 | * ## Running the Example
33 | *
34 | * ```bash
35 | * yarn ts-node -r tsconfig-paths/register examples/custom-tool/search/exa.ts
36 | * ```
37 | *
38 | * ## Example Output
39 | *
40 | * The final output will include a detailed trip plan for Tokyo based on
41 | * searched and analyzed web content, with recommended places and their details.
42 | */
43 |
44 | import "dotenv/config";
45 | import HyperAgent from "@hyperbrowser/agent";
46 | import {
47 | AgentActionDefinition,
48 | ActionContext,
49 | ActionOutput,
50 | } from "@hyperbrowser/agent/types";
51 | import chalk from "chalk";
52 | import { ChatOpenAI } from "@langchain/openai";
53 | import Exa from "exa-js";
54 |
55 | import * as z from "zod";
56 |
57 | const exaInstance = new Exa(process.env.EXA_API_KEY);
58 |
59 | const searchSchema = z
60 | .object({
61 | search: z
62 | .string()
63 | .describe(
64 | "The search query for something you want to search about. Keep the search query concise and to-the-point."
65 | ),
66 | })
67 | .describe("Search and return the results for a given query.");
68 |
69 | export const RunSearchActionDefinition: AgentActionDefinition = {
70 | type: "perform_search",
71 | actionParams: searchSchema,
72 | run: async function (
73 | ctx: ActionContext,
74 | params: z.infer
75 | ): Promise {
76 | const results = (await exaInstance.search(params.search, {})).results
77 | .map(
78 | (res) =>
79 | `title: ${res.title} || url: ${res.url} || relevance: ${res.score}`
80 | )
81 | .join("\n");
82 |
83 | return {
84 | success: true,
85 | message: `Succesfully performed search for query ${params.search}. Got results: \n${results}`,
86 | };
87 | },
88 | };
89 |
90 | async function runEval() {
91 | console.log(chalk.cyan.bold("\n===== Running Custom Tool Example ====="));
92 |
93 | const llm = new ChatOpenAI({
94 | apiKey: process.env.OPENAI_API_KEY,
95 | model: "gpt-4o",
96 | });
97 |
98 | const agent = new HyperAgent({
99 | llm: llm,
100 | debug: true,
101 | customActions: [RunSearchActionDefinition],
102 | });
103 |
104 | const result = await agent.executeTask(
105 | `Make me a trip plan for Tokyo.
106 | Steps:
107 |
108 | - Peform search about the place and things to see there using the 'perform_search' tool.
109 | - Analyze part of the urls provided, filtering results for relevance, and information and collecting a subset of urls that you think warrant further examination.
110 | - For each page that you've
111 | - Navigate to that url
112 | - Extract information about trip recommendations
113 | - You must do this in order. Navigate to a single page, and then perform extraction on that page. Do not perform multiple navigations one after another.
114 | - Narrow down on places based on uniqueness, frequency of recommendation, and whatever else you feel is valuable.
115 | - Return to me a list of places you recommend, and their details (if any)`,
116 | {
117 | debugOnAgentOutput: (agentOutput) => {
118 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
119 | console.dir(agentOutput, { depth: null, colors: true });
120 | console.log(chalk.cyan.bold("===============") + "\n");
121 | },
122 | onStep: (step) => {
123 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
124 | console.dir(step, { depth: null, colors: true });
125 | console.log(chalk.cyan.bold("===============") + "\n");
126 | },
127 | }
128 | );
129 | await agent.closeAgent();
130 | console.log(chalk.green.bold("\nResult:"));
131 | console.log(chalk.white(result.output));
132 | return result;
133 | }
134 |
135 | (async () => {
136 | await runEval();
137 | })().catch((error) => {
138 | console.error(chalk.red("Error:"), error);
139 | process.exit(1);
140 | });
141 |
--------------------------------------------------------------------------------
/examples/custom-tool/wikipedia-random-article/run-custom-tool.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # Custom Wikipedia Random Article Tool Example
3 | *
4 | * This example demonstrates how to create a simple custom tool for HyperAgent
5 | * that navigates to random Wikipedia articles and extracts their content.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a straightforward task using a custom tool that:
10 | * 1. Defines a custom action to navigate to Wikipedia's random article page
11 | * 2. Retrieves the page title and URL
12 | * 3. Extracts and describes the content of the randomly selected article
13 | *
14 | * ## Prerequisites
15 | *
16 | * 1. Node.js environment
17 | * 2. OpenAI API key set in your .env file (OPENAI_API_KEY)
18 | *
19 | * ## Running the Example
20 | *
21 | * ```bash
22 | * yarn ts-node -r tsconfig-paths/register examples/custom-tool/wikipedia-random-article/run-custom-tool.ts
23 | * ```
24 | */
25 |
26 | import "dotenv/config";
27 | import { HyperAgent } from "@hyperbrowser/agent";
28 | import {
29 | AgentActionDefinition,
30 | ActionContext,
31 | ActionOutput,
32 | } from "@hyperbrowser/agent/types";
33 | import chalk from "chalk";
34 | import { ChatOpenAI } from "@langchain/openai";
35 |
36 | import * as z from "zod";
37 |
38 | export const GoToWikipediaActionDefinition: AgentActionDefinition = {
39 | type: "go_to_random_wikipedia_page",
40 | actionParams: z
41 | .object({})
42 | .describe(
43 | "Navigate to a random wikipedia page and return the title and url of the page."
44 | ),
45 | run: async function (ctx: ActionContext): Promise {
46 | await ctx.page.goto("https://en.wikipedia.org/wiki/Special:Random", {
47 | waitUntil: "domcontentloaded",
48 | });
49 |
50 | const url = ctx.page.url();
51 | const title = await ctx.page.title();
52 | return {
53 | success: true,
54 | message: `Succesfully navigated to URL: ${url} and title: ${title}`,
55 | };
56 | },
57 | };
58 |
59 | async function runEval() {
60 | console.log(chalk.cyan.bold("\n===== Running Custom Tool Example ====="));
61 |
62 | const llm = new ChatOpenAI({
63 | apiKey: process.env.OPENAI_API_KEY,
64 | model: "gpt-4o",
65 | });
66 |
67 | const agent = new HyperAgent({
68 | llm: llm,
69 | debug: true,
70 | customActions: [GoToWikipediaActionDefinition],
71 | });
72 |
73 | const result = await agent.executeTask(
74 | "Navigate to a random wikipedia page, and describe to me the contents of that page.",
75 | {
76 | debugOnAgentOutput: (agentOutput) => {
77 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
78 | console.dir(agentOutput, { depth: null, colors: true });
79 | console.log(chalk.cyan.bold("===============") + "\n");
80 | },
81 | onStep: (step) => {
82 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
83 | console.dir(step, { depth: null, colors: true });
84 | console.log(chalk.cyan.bold("===============") + "\n");
85 | },
86 | }
87 | );
88 | await agent.closeAgent();
89 | console.log(chalk.green.bold("\nResult:"));
90 | console.log(chalk.white(result.output));
91 | return result;
92 | }
93 |
94 | (async () => {
95 | await runEval();
96 | })().catch((error) => {
97 | console.error(chalk.red("Error:"), error);
98 | process.exit(1);
99 | });
100 |
--------------------------------------------------------------------------------
/examples/llms/anthropic.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # Anthropic LLM Integration Example
3 | *
4 | * This example demonstrates how to configure and use HyperAgent with Anthropic's
5 | * Claude language models for web automation tasks.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a web scraping task that:
10 | * 1. Configures HyperAgent with Anthropic's Claude 3 Sonnet model
11 | * 2. Navigates to Hacker News
12 | * 3. Searches for and extracts information about "Show HN" posts
13 | *
14 | * ## Prerequisites
15 | *
16 | * 1. Node.js environment
17 | * 2. Anthropic API key set in your .env file (ANTHROPIC_API_KEY)
18 | *
19 | * ## Running the Example
20 | *
21 | * ```bash
22 | * yarn ts-node -r tsconfig-paths/register examples/llms/anthropic.ts
23 | * ```
24 | */
25 |
26 | import "dotenv/config";
27 | import HyperAgent from "@hyperbrowser/agent";
28 |
29 | import chalk from "chalk";
30 | import { ChatAnthropic } from "@langchain/anthropic";
31 |
32 | const TASK =
33 | "Go to hackernews, and find if there's any SHOW HN post up there. If it is, then tell me the title of the post.";
34 |
35 | async function runEval() {
36 | const llm = new ChatAnthropic({
37 | apiKey: process.env.ANTHROPIC_API_KEY,
38 | model: "claude-3-7-sonnet-latest",
39 | });
40 |
41 | const agent = new HyperAgent({
42 | llm: llm,
43 | });
44 |
45 | console.log(`\n${chalk.green("Running agent with Claude Sonnet 3.7")}\n`);
46 |
47 | const result = await agent.executeTask(TASK, {
48 | debugOnAgentOutput: (agentOutput) => {
49 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
50 | console.dir(agentOutput, { depth: null, colors: true });
51 | console.log(chalk.cyan.bold("===============") + "\n");
52 | },
53 | onStep: (step) => {
54 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
55 | console.dir(step, { depth: null, colors: true });
56 | console.log(chalk.cyan.bold("===============") + "\n");
57 | },
58 | });
59 | await agent.closeAgent();
60 | console.log(chalk.green.bold("\nResult:"));
61 | console.log(chalk.white(result.output));
62 | return result;
63 | }
64 |
65 | (async () => {
66 | await runEval();
67 | })().catch((error) => {
68 | console.error(chalk.red("Error:"), error);
69 | process.exit(1);
70 | });
71 |
--------------------------------------------------------------------------------
/examples/llms/openai.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # OpenAI LLM Integration Example
3 | *
4 | * This example demonstrates how to configure and use HyperAgent with OpenAI's
5 | * language models for web automation tasks.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a web scraping task that:
10 | * 1. Configures HyperAgent with OpenAI's GPT-4 model
11 | * 2. Navigates to Hacker News
12 | * 3. Searches for and extracts information about "Show HN" posts
13 | *
14 | * ## Prerequisites
15 | *
16 | * 1. Node.js environment
17 | * 2. OpenAI API key set in your .env file (OPENAI_API_KEY)
18 | *
19 | * ## Running the Example
20 | *
21 | * ```bash
22 | * yarn ts-node -r tsconfig-paths/register examples/llms/openai.ts
23 | * ```
24 | */
25 |
26 | import "dotenv/config";
27 | import HyperAgent from "@hyperbrowser/agent";
28 |
29 | import chalk from "chalk";
30 | import { ChatOpenAI } from "@langchain/openai";
31 |
32 | const TASK =
33 | "Go to hackernews, and find if there's any SHOW HN post up there. If it is, then tell me the title of the post.";
34 |
35 | async function runEval() {
36 | const llm = new ChatOpenAI({
37 | apiKey: process.env.OPENAI_API_KEY,
38 | model: "gpt-4o",
39 | });
40 |
41 | const agent = new HyperAgent({
42 | llm: llm,
43 | debug: true,
44 | });
45 |
46 | console.log(`\n${chalk.green("Running agent with GPT-4o")}\n`);
47 |
48 | const result = await agent.executeTask(TASK, {
49 | debugOnAgentOutput: (agentOutput) => {
50 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
51 | console.dir(agentOutput, { depth: null, colors: true });
52 | console.log(chalk.cyan.bold("===============") + "\n");
53 | },
54 | onStep: (step) => {
55 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
56 | console.dir(step, { depth: null, colors: true });
57 | console.log(chalk.cyan.bold("===============") + "\n");
58 | },
59 | });
60 | await agent.closeAgent();
61 | console.log(chalk.green.bold("\nResult:"));
62 | console.log(chalk.white(result.output));
63 | return result;
64 | }
65 |
66 | (async () => {
67 | await runEval();
68 | })().catch((error) => {
69 | console.error(chalk.red("Error:"), error);
70 | process.exit(1);
71 | });
72 |
--------------------------------------------------------------------------------
/examples/mcp/google-sheets/best-buy-reviews.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # Google Sheets MCP Server Example
3 | *
4 | * This example demonstrates how to use HyperAgent with the Composio Googlesheets MCP server
5 | * to connect to Google Sheets, create a new spreadsheet, and populate it with data scraped from the web.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a multi-step task that requires web browsing and Google Sheets integration:
10 | * 1. Checks if there is an active connection to Composio Googlesheets MCP server
11 | * 2. If no connection exists, initiates a connection and waits for the user to authenticate
12 | * 3. Creates a new spreadsheet titled "BestBuy Reviews"
13 | * 4. Navigates to BestBuy to gather data on the reviews for the MacBook Air M2
14 | * 5. Adds the data to the created spreadsheet
15 | *
16 | * ## Prerequisites
17 | *
18 | * 1. Node.js environment
19 | * 2. OpenAI API key set in your .env file (OPENAI_API_KEY)
20 | * 3. Need to have a Composio account, can sign up at https://app.composio.dev
21 | * - Go to this link and get your secure MCP URL (you just need the URL part from the command): https://mcp.composio.dev/googlesheets
22 | * - You will use the url to run the script, for example:
23 | * ```
24 | * yarn ts-node tsconfig-paths/register examples/mcp/google-sheets/best-buy-reviews.ts
25 | * ```
26 | * - When running for the first time, there will be no active connection so you will need to login
27 | * with Google OAUTH at the link provided by the agent to authenticate
28 | *
29 | * ## MCP Server Configuration
30 | *
31 | * This example uses the Composio Googlesheets MCP server which provides tools for:
32 | * - `GOOGLESHEETS_CHECK_ACTIVE_CONNECTION`: Verifies if there's an active connection to Google Sheets
33 | * - `GOOGLESHEETS_INITIATE_CONNECTION`: Starts the authentication process for Google Sheets
34 | * - `GOOGLESHEETS_CREATE_GOOGLE_SHEET1`: Creates a new Google Sheet
35 | * - `GOOGLESHEETS_SHEET_FROM_JSON`: Converts JSON data to a Google Sheet format
36 | * - `GOOGLESHEETS_BATCH_UPDATE`: Updates multiple cells in a spreadsheet
37 | * - `GOOGLESHEETS_GET_SPREADSHEET_INFO`: Retrieves information about a spreadsheet
38 | * - `GOOGLESHEETS_LOOKUP_SPREADSHEET_ROW`: Looks up a specific row in a spreadsheet
39 | * - `GOOGLESHEETS_BATCH_GET`: Gets values from multiple ranges in a spreadsheet
40 | * - `GOOGLESHEETS_GET_SHEET_NAMES`: Gets the names of all sheets in a spreadsheet
41 | * - `GOOGLESHEETS_CLEAR_VALUES`: Clears values from a range in a spreadsheet
42 | * - `GOOGLESHEETS_GET_REQUIRED_PARAMETERS`: Gets required parameters for Google Sheets operations
43 | *
44 | * ## Debugging and Monitoring
45 | *
46 | * The example includes callback functions to monitor:
47 | * - Agent output: Raw output from the LLM agent
48 | * - Step execution: Each step the agent takes during the task
49 | *
50 | * ## Running the Example
51 | *
52 | * ```
53 | * yarn ts-node examples/mcp/google-sheets/best-buy-reviews.ts
54 | * ```
55 | *
56 | * ## Example Output
57 | *
58 | * The final output will include confirmation that the agent has successfully created a new Google Sheet
59 | * and populated it with information about the reviews for the MacBook Air M2.
60 | */
61 |
62 | import dotenv from "dotenv";
63 | import chalk from "chalk";
64 | import { ChatOpenAI } from "@langchain/openai";
65 | import HyperAgent from "@hyperbrowser/agent";
66 |
67 | dotenv.config();
68 |
69 | const TASK = `1. Run GOOGLESHEETS_CHECK_ACTIVE_CONNECTION to check if there is an active connection.
70 | 2. If there is an active connection, go to 4. Otherwise, go to 3.
71 | 3. Run GOOGLESHEETS_INITIATE_CONNECTION and output the the auth link to the user, then wait for the connection to be active.
72 | 4. Create a new spreadsheet titled "BestBuy Reviews".
73 | 5. Go to https://www.bestbuy.com/site/apple-macbook-air-13-inch-apple-m2-chip-built-for-apple-intelligence-16gb-memory-256gb-ssd-midnight/6602763.p?skuId=6602763 .
74 | 6. Scroll down until you see the "See All Customer Reviews" button and click on the button.
75 | 7. Once on the next page, get all the reviews from the first page.
76 | 8. Add the reviews to the "BestBuy Reviews" spreadsheet. Include these columns and data for all the columns: Review Title, Rating, Review Text, Verified Purchase, and Review Date.
77 | Make sure that the data is well formatted and the columns are all there, make sure to not cut off any of the full review text, please include it all and to get all the reviews.`;
78 |
79 | async function run(mcpUrl: string) {
80 | console.log(chalk.cyan.bold("\n===== Running Task ====="));
81 | console.log(chalk.white(`Task: ${TASK}`));
82 | console.log(chalk.cyan.bold("=======================\n"));
83 |
84 | console.log(chalk.yellow("Initializing OpenAI LLM..."));
85 | const llm = new ChatOpenAI({
86 | apiKey: process.env.OPENAI_API_KEY,
87 | model: "gpt-4o",
88 | });
89 |
90 | console.log(chalk.yellow("Creating HyperAgent..."));
91 |
92 | try {
93 | const agent = new HyperAgent({
94 | llm: llm,
95 | debug: true,
96 | });
97 | console.log(chalk.green("Agent created successfully"));
98 |
99 | console.log(
100 | chalk.yellow("Connecting to Composio Googlesheets MCP server...")
101 | );
102 | await agent.initializeMCPClient({
103 | servers: [
104 | {
105 | command: "npx",
106 | args: ["@composio/mcp@latest", "start", "--url", mcpUrl],
107 | env: {
108 | npm_config_yes: "true",
109 | },
110 | },
111 | ],
112 | });
113 | console.log(
114 | chalk.green(
115 | "Connected to Composio Googlesheets MCP server, executing task..."
116 | )
117 | );
118 |
119 | const result = await agent.executeTask(TASK, {
120 | debugOnAgentOutput: (agentOutput) => {
121 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
122 | console.dir(agentOutput, { depth: null, colors: true });
123 | console.log(chalk.cyan.bold("===============") + "\n");
124 | },
125 | onStep: (step) => {
126 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
127 | console.dir(step, { depth: null, colors: true });
128 | console.log(chalk.cyan.bold("===============") + "\n");
129 | },
130 | });
131 |
132 | await agent.closeAgent();
133 | console.log(chalk.green.bold("\nResult:"));
134 | console.log(chalk.white(result.output));
135 | return result;
136 | } catch (error) {
137 | console.error(chalk.red.bold("Error creating agent or executing task:"));
138 | console.error(
139 | chalk.red(error instanceof Error ? error.stack : String(error))
140 | );
141 | }
142 | }
143 |
144 | (async () => {
145 | try {
146 | if (process.argv.length < 3) {
147 | console.error(
148 | chalk.red("Error: Please provide your MCP URL as an argument")
149 | );
150 | process.exit(1);
151 | }
152 | await run(process.argv[2]);
153 | } catch (error) {
154 | console.error(chalk.red("Error:"), error);
155 | process.exit(1);
156 | }
157 | })();
158 |
--------------------------------------------------------------------------------
/examples/mcp/google-sheets/car-price-comparison.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # Car Price Comparison with Google Sheets MCP Server Example
3 | *
4 | * This example demonstrates how to use HyperAgent with the Composio Googlesheets MCP server
5 | * to connect to Google Sheets and populate it with car price comparison data scraped from multiple websites.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a multi-step task that requires web browsing and Google Sheets integration:
10 | * 1. Checks if there is an active connection to Composio Googlesheets MCP server
11 | * 2. If no connection exists, initiates a connection and waits for the user to authenticate
12 | * 3. Creates a new spreadsheet titled with the car name and current date
13 | * 4. Searches for the specified car (Toyota Corolla) on multiple car comparison sites:
14 | * - Carvana
15 | * - Carmax
16 | * 5. Collects the 5 cheapest listings from each site including details like price, mileage, model year, and trim
17 | * 6. Adds the collected data to the spreadsheet in a well-formatted manner
18 | *
19 | * ## Prerequisites
20 | *
21 | * 1. Node.js environment
22 | * 2. OpenAI API key set in your .env file (OPENAI_API_KEY)
23 | * 3. Need to have a Composio account, can sign up at https://app.composio.dev
24 | * - Go to this link and get your secure MCP URL (you just need the URL part from the command): https://mcp.composio.dev/googlesheets
25 | * - You will use the url to run the script, for example:
26 | * ```
27 | * yarn ts-node examples/mcp/google-sheets/car-price-comparison.ts
28 | * ```
29 | * - When running for the first time, there will be no active connection so you will need to login
30 | * with Google OAUTH at the link provided by the agent to authenticate
31 | *
32 | * ## MCP Server Configuration
33 | *
34 | * This example uses the Composio Googlesheets MCP server which provides tools for:
35 | * - `GOOGLESHEETS_CHECK_ACTIVE_CONNECTION`: Verifies if there's an active connection to Google Sheets
36 | * - `GOOGLESHEETS_INITIATE_CONNECTION`: Starts the authentication process for Google Sheets
37 | * - `GOOGLESHEETS_CREATE_GOOGLE_SHEET1`: Creates a new Google Sheet
38 | * - `GOOGLESHEETS_SHEET_FROM_JSON`: Converts JSON data to a Google Sheet format
39 | * - `GOOGLESHEETS_BATCH_UPDATE`: Updates multiple cells in a spreadsheet
40 | * - `GOOGLESHEETS_GET_SPREADSHEET_INFO`: Retrieves information about a spreadsheet
41 | * - `GOOGLESHEETS_LOOKUP_SPREADSHEET_ROW`: Looks up a specific row in a spreadsheet
42 | * - `GOOGLESHEETS_BATCH_GET`: Gets values from multiple ranges in a spreadsheet
43 | * - `GOOGLESHEETS_GET_SHEET_NAMES`: Gets the names of all sheets in a spreadsheet
44 | * - `GOOGLESHEETS_CLEAR_VALUES`: Clears values from a range in a spreadsheet
45 | * - `GOOGLESHEETS_GET_REQUIRED_PARAMETERS`: Gets required parameters for Google Sheets operations
46 | *
47 | * ## Debugging and Monitoring
48 | *
49 | * The example includes callback functions to monitor:
50 | * - Agent output: Raw output from the LLM agent
51 | * - Step execution: Each step the agent takes during the task
52 | *
53 | * ## Running the Example
54 | *
55 | * ```
56 | * yarn ts-node examples/mcp/google-sheets/car-price-comparison.ts
57 | * ```
58 | *
59 | * ## Example Output
60 | *
61 | * The final output will include confirmation that the agent has successfully created a new Google Sheet
62 | * and populated it with information about the car prices from different websites.
63 | */
64 |
65 | import dotenv from "dotenv";
66 | import chalk from "chalk";
67 | import { ChatOpenAI } from "@langchain/openai";
68 | import HyperbrowserAgent from "@hyperbrowser/agent";
69 |
70 | dotenv.config();
71 |
72 | const CAR_NAME = "Toyota Corolla";
73 |
74 | const TASK_STEPS = `
75 | Your task is to search for a certain car, namely ${CAR_NAME} and compare it's prices across multiple car price comparison sites, namely
76 | - Carvana (https://www.carvana.com/)
77 | - Carmax (https://www.carmax.com)
78 |
79 | You will search for the results for the mentioned car on each of these websites, sort the results by lowest to highest, and then add the 5 cheapest results of each website to a google sheet. As much as possible, sort results using the websites own sort, and do not try to sort results by extraction.
80 |
81 | ## Google Sheet setup:
82 | 1. Run GOOGLESHEETS_CHECK_ACTIVE_CONNECTION to check if there is an active connection.
83 | 2. If there is an active connection, go to 4. Otherwise, go to 3.
84 | 3. Run GOOGLESHEETS_INITIATE_CONNECTION and output the the auth link to the user, then wait for the connection to be active.
85 | 4. Create a new spreadsheet titled "${CAR_NAME} Comparison - {{CURRENT_DATE}}".
86 | 5. Get the results from each website, and insert the relevant data (like price, mileage, model year, model name/trim), along with the website source.
87 | 6. Add that information to the spreadsheet properly.
88 | Make sure that the data is well formatted and the columns are all there.`;
89 |
90 | async function run(mcpUrl: string) {
91 | console.log(chalk.cyan.bold("\n===== Running Task ====="));
92 | console.log(chalk.white(`Task: ${TASK_STEPS}`));
93 | console.log(chalk.cyan.bold("=======================\n"));
94 |
95 | console.log(chalk.yellow("Initializing OpenAI LLM..."));
96 | const llm = new ChatOpenAI({
97 | apiKey: process.env.OPENAI_API_KEY,
98 | model: "gpt-4o",
99 | });
100 |
101 | console.log(chalk.yellow("Creating Hyperbrowser Agent..."));
102 |
103 | try {
104 | const agent = new HyperbrowserAgent({
105 | llm: llm,
106 | debug: true,
107 | });
108 | console.log(chalk.green("Agent created successfully"));
109 |
110 | console.log(
111 | chalk.yellow("Connecting to Composio Googlesheets MCP server...")
112 | );
113 | await agent.initializeMCPClient({
114 | servers: [
115 | {
116 | command: "npx",
117 | args: ["@composio/mcp@latest", "start", "--url", mcpUrl],
118 | env: {
119 | npm_config_yes: "true",
120 | },
121 | },
122 | ],
123 | });
124 | console.log(
125 | chalk.green(
126 | "Connected to Composio Googlesheets MCP server, executing task..."
127 | )
128 | );
129 |
130 | const result = await agent.executeTask(
131 | `Your task is to look for a certain car on car comparisons website ${TASK_STEPS}`,
132 | {
133 | debugOnAgentOutput: (agentOutput) => {
134 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
135 | console.dir(agentOutput, { depth: null, colors: true });
136 | console.log(chalk.cyan.bold("===============") + "\n");
137 | },
138 | onStep: (step) => {
139 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
140 | console.dir(step, { depth: null, colors: true });
141 | console.log(chalk.cyan.bold("===============") + "\n");
142 | },
143 | }
144 | );
145 |
146 | await agent.closeAgent();
147 | console.log(chalk.green.bold("\nResult:"));
148 | console.log(chalk.white(result.output));
149 | return result;
150 | } catch (error) {
151 | console.error(chalk.red.bold("Error creating agent or executing task:"));
152 | console.error(
153 | chalk.red(error instanceof Error ? error.stack : String(error))
154 | );
155 | }
156 | }
157 |
158 | (async () => {
159 | try {
160 | if (process.argv.length < 3) {
161 | console.error(
162 | chalk.red("Error: Please provide your MCP URL as an argument")
163 | );
164 | process.exit(1);
165 | }
166 | await run(process.argv[2]);
167 | } catch (error) {
168 | console.error(chalk.red("Error:"), error);
169 | process.exit(1);
170 | }
171 | })();
172 |
--------------------------------------------------------------------------------
/examples/mcp/google-sheets/most-populated-states.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # Google Sheets MCP Server Example
3 | *
4 | * This example demonstrates how to use HyperAgent with the Composio Googlesheets MCP server
5 | * to connect to Google Sheets, create a new spreadsheet, and populate it with data scraped from the web.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a multi-step task that requires web browsing and Google Sheets integration:
10 | * 1. Checks if there is an active connection to Composio Googlesheets MCP server
11 | * 2. If no connection exists, initiates a connection and waits for the user to authenticate
12 | * 3. Creates a new spreadsheet titled "Most Populated States"
13 | * 4. Navigates to Wikipedia to gather data on the 5 most populous US states
14 | * 5. Adds the data to the created spreadsheet
15 | *
16 | * ## Prerequisites
17 | *
18 | * 1. Node.js environment
19 | * 2. OpenAI API key set in your .env file (OPENAI_API_KEY)
20 | * 3. Need to have a Composio account, can sign up at https://app.composio.dev
21 | * - Go to this link and get your secure MCP URL (you just need the URL part from the command): https://mcp.composio.dev/googlesheets
22 | * - You will use the url to run the script, for example:
23 | * ```
24 | * yarn ts-node tsconfig-paths/register examples/mcp/google-sheets/most-populated-states.ts
25 | * ```
26 | * - When running for the first time, there will be no active connection so you will need to login
27 | * with Google OAUTH at the link provided by the agent to authenticate
28 | *
29 | * ## MCP Server Configuration
30 | *
31 | * This example uses the Composio Googlesheets MCP server which provides tools for:
32 | * - `GOOGLESHEETS_CHECK_ACTIVE_CONNECTION`: Verifies if there's an active connection to Google Sheets
33 | * - `GOOGLESHEETS_INITIATE_CONNECTION`: Starts the authentication process for Google Sheets
34 | * - `GOOGLESHEETS_CREATE_GOOGLE_SHEET1`: Creates a new Google Sheet
35 | * - `GOOGLESHEETS_SHEET_FROM_JSON`: Converts JSON data to a Google Sheet format
36 | * - `GOOGLESHEETS_BATCH_UPDATE`: Updates multiple cells in a spreadsheet
37 | * - `GOOGLESHEETS_GET_SPREADSHEET_INFO`: Retrieves information about a spreadsheet
38 | * - `GOOGLESHEETS_LOOKUP_SPREADSHEET_ROW`: Looks up a specific row in a spreadsheet
39 | * - `GOOGLESHEETS_BATCH_GET`: Gets values from multiple ranges in a spreadsheet
40 | * - `GOOGLESHEETS_GET_SHEET_NAMES`: Gets the names of all sheets in a spreadsheet
41 | * - `GOOGLESHEETS_CLEAR_VALUES`: Clears values from a range in a spreadsheet
42 | * - `GOOGLESHEETS_GET_REQUIRED_PARAMETERS`: Gets required parameters for Google Sheets operations
43 | *
44 | * ## Debugging and Monitoring
45 | *
46 | * The example includes callback functions to monitor:
47 | * - Agent output: Raw output from the LLM agent
48 | * - Step execution: Each step the agent takes during the task
49 | *
50 | * ## Running the Example
51 | *
52 | * ```
53 | * yarn ts-node examples/mcp/google-sheets/most-populated-states.ts
54 | * ```
55 | *
56 | * ## Example Output
57 | *
58 | * The final output will include confirmation that the agent has successfully created a new Google Sheet
59 | * and populated it with information about the top 5 most populous US states.
60 | */
61 |
62 | import dotenv from "dotenv";
63 | import chalk from "chalk";
64 | import { ChatOpenAI } from "@langchain/openai";
65 | import HyperAgent from "@hyperbrowser/agent";
66 |
67 | dotenv.config();
68 |
69 | const TASK = `1. Run GOOGLESHEETS_CHECK_ACTIVE_CONNECTION to check if there is an active connection.
70 | 2. If there is an active connection, go to 4. Otherwise, go to 3.
71 | 3. Run GOOGLESHEETS_INITIATE_CONNECTION and output the the auth link to the user, then wait for the connection to be active.
72 | 4. Create a new spreadsheet titled "Most Populated States".
73 | 5. Go to https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population and get the data on the top 5 most populous states from the table.
74 | 6. Add that information to the spreadsheet properly.
75 | Make sure that the data is well formatted and the columns are all there.`;
76 |
77 | async function run(mcpUrl: string) {
78 | console.log(chalk.cyan.bold("\n===== Running Task ====="));
79 | console.log(chalk.white(`Task: ${TASK}`));
80 | console.log(chalk.cyan.bold("=======================\n"));
81 |
82 | console.log(chalk.yellow("Initializing OpenAI LLM..."));
83 | const llm = new ChatOpenAI({
84 | apiKey: process.env.OPENAI_API_KEY,
85 | model: "gpt-4o",
86 | });
87 |
88 | console.log(chalk.yellow("Creating HyperAgent..."));
89 |
90 | try {
91 | const agent = new HyperAgent({
92 | llm: llm,
93 | debug: true,
94 | });
95 | console.log(chalk.green("Agent created successfully"));
96 |
97 | console.log(
98 | chalk.yellow("Connecting to Composio Googlesheets MCP server...")
99 | );
100 | await agent.initializeMCPClient({
101 | servers: [
102 | {
103 | command: "npx",
104 | args: ["@composio/mcp@latest", "start", "--url", mcpUrl],
105 | env: {
106 | npm_config_yes: "true",
107 | },
108 | },
109 | ],
110 | });
111 | console.log(
112 | chalk.green(
113 | "Connected to Composio Googlesheets MCP server, executing task..."
114 | )
115 | );
116 |
117 | const result = await agent.executeTask(TASK, {
118 | debugOnAgentOutput: (agentOutput) => {
119 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
120 | console.dir(agentOutput, { depth: null, colors: true });
121 | console.log(chalk.cyan.bold("===============") + "\n");
122 | },
123 | onStep: (step) => {
124 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
125 | console.dir(step, { depth: null, colors: true });
126 | console.log(chalk.cyan.bold("===============") + "\n");
127 | },
128 | });
129 |
130 | await agent.closeAgent();
131 | console.log(chalk.green.bold("\nResult:"));
132 | console.log(chalk.white(result.output));
133 | return result;
134 | } catch (error) {
135 | console.error(chalk.red.bold("Error creating agent or executing task:"));
136 | console.error(
137 | chalk.red(error instanceof Error ? error.stack : String(error))
138 | );
139 | }
140 | }
141 |
142 | (async () => {
143 | try {
144 | if (process.argv.length < 3) {
145 | console.error(
146 | chalk.red("Error: Please provide your MCP URL as an argument")
147 | );
148 | process.exit(1);
149 | }
150 | await run(process.argv[2]);
151 | } catch (error) {
152 | console.error(chalk.red("Error:"), error);
153 | process.exit(1);
154 | }
155 | })();
156 |
--------------------------------------------------------------------------------
/examples/mcp/notion/create-shopping-list.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # Notion MCP Server Example
3 | *
4 | * This example demonstrates how to use HyperAgent with the Composio Notion MCP server
5 | * to connect to Notion, create a new page, and populate it with ingredients for a recipe scraped from allrecipes.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a multi-step task that requires web browsing and Notion MCP:
10 | * 1. Checks if there is an active connection to Composio Notion MCP server
11 | * 2. If no connection exists, initiates a connection and waits for the user to authenticate
12 | * 3. Creates a new notion page titled "{{RECIPE}} ingredients"
13 | * 4. Navigates to allrecipes and finds a recipe matching the criterias
14 | * 5. Adds the data to the created spreadsheet
15 | *
16 | * ## Prerequisites
17 | *
18 | * 1. Node.js environment
19 | * 2. OpenAI API key set in your .env file (OPENAI_API_KEY)
20 | * 3. Need to have a Composio account, can sign up at https://app.composio.dev
21 | * - Go to this link and get your secure MCP URL (you just need the URL part from the command): https://mcp.composio.dev/notion
22 | * - You will use the url to run the script, for example:
23 | * ```
24 | * yarn ts-node examples/mcp/notion/create-shoppping-list.ts
25 | * ```
26 | * - When running for the first time, there will be no active connection so you will need to login
27 | * with Notion OAUTH at the link provided by the agent to authenticate
28 | *
29 | * ## MCP Server Configuration
30 | *
31 | * This example uses the Composio Notion MCP server which provides tools for a number of use cases. We will be using: :
32 | * - `NOTION_CHECK_ACTIVE_CONNECTION`: Verifies if there's an active connection to Notion
33 | * - `NOTION_INITIATE_CONNECTION`: Starts the authentication process for Notion
34 | * - `NOTION_ADD_PAGE_CONTENT`: Adds a single content block to a Notion page
35 | * - `NOTION_CREATE_PAGE`: Creates a new page in Notion
36 | *
37 | * ## Debugging and Monitoring
38 | *
39 | * The example includes callback functions to monitor:
40 | * - Agent output: Raw output from the LLM agent
41 | * - Step execution: Each step the agent takes during the task
42 | *
43 | * ## Running the Example
44 | *
45 | * ```
46 | * yarn ts-node examples/mcp/notion/create-shoppping-list.ts
47 | * ```
48 | *
49 | * ## Example Output
50 | *
51 | * The final output will include confirmation that the agent has successfully created a new Notion Page
52 | * and populated it with the ingredients for a recipe.
53 | */
54 |
55 | import dotenv from "dotenv";
56 | import chalk from "chalk";
57 | import { ChatOpenAI } from "@langchain/openai";
58 | import HyperbrowserAgent from "@hyperbrowser/agent";
59 |
60 | dotenv.config();
61 |
62 | const TASK = `
63 | Go to allrecipes and find a suitable recipe for Salsa verde with more than 100 ratings. Then insert each ingredient into a notion page. Don't get the trivial ingredients like salt, water, or pepper.
64 |
65 |
66 | ## Steps to insert into a notion page:
67 |
68 | 1. Run NOTION_CHECK_ACTIVE_CONNECTION to check if there is an active connection.
69 | 2. If there is an active connection, go to 4. Otherwise, go to 3.
70 | 3. Run NOTION_INITIATE_CONNECTION and output the the auth link to the user, then wait for the connection to be active.
71 | 4. Create a new notion page title - {{RECIPE}} Ingredients
72 | 5. Go to allrecipes, find a suitable recipe for {{RECIPE}}, and get it's ingredients
73 | 6. For each ingredient, call NOTION_ADD_PAGE_CONTENT to insert a single ingredient
74 |
75 | Make sure that the data is well formatted and the columns are all there.`;
76 |
77 | async function run(mcpUrl: string) {
78 | console.log(chalk.cyan.bold("\n===== Running Task ====="));
79 | console.log(chalk.white(`Task: ${TASK}`));
80 | console.log(chalk.cyan.bold("=======================\n"));
81 |
82 | console.log(chalk.yellow("Initializing OpenAI LLM..."));
83 | const llm = new ChatOpenAI({
84 | apiKey: process.env.OPENAI_API_KEY,
85 | model: "gpt-4o",
86 | });
87 |
88 | console.log(chalk.yellow("Creating Hyperbrowser Agent..."));
89 |
90 | try {
91 | const agent = new HyperbrowserAgent({
92 | llm: llm,
93 | debug: true,
94 | });
95 | console.log(chalk.green("Agent created successfully"));
96 |
97 | console.log(
98 | chalk.yellow("Connecting to Composio Notion MCP server...")
99 | );
100 | await agent.initializeMCPClient({
101 | servers: [
102 | {
103 | command: "npx",
104 | args: ["@composio/mcp@latest", "start", "--url", mcpUrl],
105 | env: {
106 | npm_config_yes: "true",
107 | },
108 | },
109 | ],
110 | });
111 | console.log(
112 | chalk.green(
113 | "Connected to Composio Notion MCP server, executing task..."
114 | )
115 | );
116 |
117 | const result = await agent.executeTask(TASK, {
118 | debugOnAgentOutput: (agentOutput) => {
119 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
120 | console.dir(agentOutput, { depth: null, colors: true });
121 | console.log(chalk.cyan.bold("===============") + "\n");
122 | },
123 | onStep: (step) => {
124 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
125 | console.dir(step, { depth: null, colors: true });
126 | console.log(chalk.cyan.bold("===============") + "\n");
127 | },
128 | });
129 |
130 | await agent.closeAgent();
131 | console.log(chalk.green.bold("\nResult:"));
132 | console.log(chalk.white(result.output));
133 | return result;
134 | } catch (error) {
135 | console.error(chalk.red.bold("Error creating agent or executing task:"));
136 | console.error(
137 | chalk.red(error instanceof Error ? error.stack : String(error))
138 | );
139 | }
140 | }
141 |
142 | (async () => {
143 | try {
144 | if (process.argv.length < 3) {
145 | console.error(
146 | chalk.red("Error: Please provide your MCP URL as an argument")
147 | );
148 | process.exit(1);
149 | }
150 | await run(process.argv[2]);
151 | } catch (error) {
152 | console.error(chalk.red("Error:"), error);
153 | process.exit(1);
154 | }
155 | })();
156 |
--------------------------------------------------------------------------------
/examples/mcp/weather/get-weather-alert.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * # Weather MCP Server Example
3 | *
4 | * This example demonstrates how to use HyperAgent with a MCP (Model Context Protocol) server
5 | * to browse the web, extract information, and use that information to query a separate API service.
6 | *
7 | * ## What This Example Does
8 | *
9 | * The agent performs a multi-step task that requires web browsing and data extraction:
10 | * 1. Navigates to a Wikipedia page listing US states by population
11 | * 2. Identifies the most populated state
12 | * 3. Uses the custom weather MCP server to find weather alerts for that state
13 | *
14 | * ## Prerequisites
15 | *
16 | * - Node.js environment
17 | * - OpenAI API key set in your .env file (OPENAI_API_KEY)
18 | *
19 | * ## MCP Server Configuration
20 | *
21 | * This example uses a custom MCP server (weather-server.js) that provides tools for:
22 | * - `get-alerts`: Fetches weather alerts for a specific state from the National Weather Service API
23 | * - `get-forecast`: Retrieves weather forecasts for specific coordinates
24 | *
25 | *
26 | * ## Debugging and Monitoring
27 | *
28 | * The example includes callback functions to monitor:
29 | * - Agent output: Raw output from the LLM agent
30 | * - Step execution: Each step the agent takes during the task
31 | *
32 | * ## Running the Example
33 | *
34 | * ```
35 | * yarn ts-node examples/mcp/weather/get-weather-alert.ts
36 | * ```
37 | *
38 | * ## Example Output
39 | *
40 | * The final output will include the most populated US state and a list of current weather alerts for that state.
41 | */
42 |
43 | import dotenv from "dotenv";
44 | import chalk from "chalk";
45 | import path from "path";
46 | import { ChatOpenAI } from "@langchain/openai";
47 | import HyperAgent from "@hyperbrowser/agent";
48 |
49 | dotenv.config();
50 |
51 | const TASK = `Go to https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population and find the most populated state.
52 | Then list 3 weather alerts for that state.`;
53 |
54 | async function run() {
55 | console.log(chalk.cyan.bold("\n===== Running Task ====="));
56 | console.log(chalk.white(`Task: ${TASK}`));
57 | console.log(chalk.cyan.bold("=======================\n"));
58 |
59 | const llm = new ChatOpenAI({
60 | apiKey: process.env.OPENAI_API_KEY,
61 | model: "gpt-4o",
62 | });
63 |
64 | const mcpServerPath = path.join(__dirname, "/servers/weather-server.js");
65 |
66 | console.log(chalk.yellow("Creating Hyperbrowser Agent..."));
67 |
68 | try {
69 | const agent = new HyperAgent({
70 | llm: llm,
71 | debug: true,
72 | });
73 |
74 | await agent.initializeMCPClient({
75 | servers: [
76 | {
77 | command: "node",
78 | args: [mcpServerPath],
79 | },
80 | ],
81 | });
82 |
83 | const result = await agent.executeTask(TASK, {
84 | debugOnAgentOutput: (agentOutput) => {
85 | console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
86 | console.dir(agentOutput, { depth: null, colors: true });
87 | console.log(chalk.cyan.bold("===============") + "\n");
88 | },
89 | onStep: (step) => {
90 | console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
91 | console.dir(step, { depth: null, colors: true });
92 | console.log(chalk.cyan.bold("===============") + "\n");
93 | },
94 | });
95 |
96 | await agent.closeAgent();
97 | console.log(chalk.green.bold("\nResult:"));
98 | console.log(chalk.white(result.output));
99 | return result;
100 | } catch (error) {
101 | console.error(chalk.red("Error creating agent or executing task:"));
102 | console.error(
103 | chalk.red(error instanceof Error ? error.stack : String(error))
104 | );
105 | }
106 | }
107 |
108 | (async () => {
109 | try {
110 | await run();
111 | } catch (error) {
112 | console.error(chalk.red("Error:"), error);
113 | process.exit(1);
114 | }
115 | })();
116 |
--------------------------------------------------------------------------------
/examples/mcp/weather/servers/weather-server.js:
--------------------------------------------------------------------------------
1 | const { McpServer } = require("@modelcontextprotocol/sdk/server/mcp.js");
2 | const { StdioServerTransport } = require("@modelcontextprotocol/sdk/server/stdio.js");
3 | const { z } = require("zod");
4 |
5 | const NWS_API_BASE = "https://api.weather.gov";
6 | const USER_AGENT = "weather-app/1.0";
7 |
8 | // Helper function for making NWS API requests
9 | /**
10 | * @param {string} url
11 | * @returns {Promise