LogoLogo
SupportDashboard
  • Community
  • Welcome to Hyperbrowser
  • Get Started
    • Quickstart
      • AI Agents
        • Browser Use
        • Claude Computer Use
        • OpenAI CUA
      • Web Scraping
        • Scrape
        • Crawl
        • Extract
      • Browser Automation
        • Puppeteer
        • Playwright
        • Selenium
  • Agents
    • Browser Use
    • Claude Computer Use
    • OpenAI CUA
  • HyperAgent
    • About HyperAgent
      • HyperAgent SDK
      • HyperAgent Types
  • Quickstart
  • Multi-Page actions
  • Custom Actions
  • MCP Support
    • Tutorial
  • Examples
    • Custom Actions
    • LLM support
    • Cloud Support
      • Setting Up
      • Proxies
      • Profiles
    • MCP Examples
      • Google Sheets
      • Weather
        • Weather Server
    • Output to Schema
  • Web Scraping
    • Scrape
    • Crawl
    • Extract
  • Sessions
    • Overview
      • Session Parameters
    • Advanced Privacy & Anti-Detection
      • Stealth Mode
      • Proxies
      • Static IPs
      • CAPTCHA Solving
      • Ad Blocking
    • Profiles
    • Recordings
    • Live View
    • Extensions
    • Downloads
  • Guides
    • Model Context Protocol
    • Scraping
    • AI Function Calling
    • Extract Information with an LLM
    • Using Hyperbrowser Session
    • CAPTCHA Solving
  • Integrations
    • ⛓️LangChain
    • 🦙LlamaIndex
  • reference
    • Pricing
    • SDKs
      • Node
        • Sessions
        • Profiles
        • Scrape
        • Crawl
        • Extensions
      • Python
        • Sessions
        • Profiles
        • Scrape
        • Crawl
        • Extensions
    • API Reference
      • Sessions
      • Scrape
      • Crawl
      • Extract
      • Agents
        • Browser Use
        • Claude Computer Use
        • OpenAI CUA
      • Profiles
      • Extensions
Powered by GitBook
On this page
Export as PDF
  1. Examples
  2. MCP Examples

Weather

Get the weather

PreviousGoogle SheetsNextWeather Server

Last updated 1 month ago

Here is an example that connects to a local weather MCP server ()

The example searches for the state in the US with the biggest population, and then queries the weather MCP server to get information about weather related alerts in that ___location.

Code

import dotenv from "dotenv";
import chalk from "chalk";
import path from "path";
import { ChatOpenAI } from "@langchain/openai";
import HyperAgent from "@hyperbrowser/agent";

dotenv.config();

const TASK = `Search bing for the most populated state in the USA currently. Then list 3 weather alerts for that state.`;

async function run() {
  console.log(chalk.cyan.bold("\n===== Running Task ====="));
  console.log(chalk.white(`Task: ${TASK}`));
  console.log(chalk.cyan.bold("=======================\n"));

  const llm = new ChatOpenAI({
    apiKey: process.env.OPENAI_API_KEY,
    model: "gpt-4o",
  });

  const mcpServerPath = path.join(__dirname, "/servers/weather-server.js");

  console.log(chalk.yellow("Creating Hyperbrowser Agent..."));

  try {
    const agent = new HyperAgent({
      llm: llm,
      debug: true,
    });

    await agent.initializeMCPClient({
      servers: [
        {
          command: "node",
          args: [mcpServerPath],
        },
      ],
    });

    const result = await agent.executeTask(TASK, {
      debugOnAgentOutput: (agentOutput) => {
        console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
        console.dir(agentOutput, { depth: null, colors: true });
        console.log(chalk.cyan.bold("===============") + "\n");
      },
      onStep: (step) => {
        console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
        console.dir(step, { depth: null, colors: true });
        console.log(chalk.cyan.bold("===============") + "\n");
      },
    });

    await agent.closeAgent();
    console.log(chalk.green.bold("\nResult:"));
    console.log(chalk.white(result.output));
    return result;
  } catch (error) {
    console.error(chalk.red("Error creating agent or executing task:"));
    console.error(
      chalk.red(error instanceof Error ? error.stack : String(error))
    );
  }
}

(async () => {
  try {
    await run();
  } catch (error) {
    console.error(chalk.red("Error:"), error);
    process.exit(1);
  }
})();
code here