Agentic
โฑ 8 min read
๐ examples/11_web_tools.mjs
Lesson 32: Web tools: search, scrape, and the headless browser
web search, scrape, and the headless browser
1. Why This Feature Exists
Two sessions, one network story:
2. What the Example Demonstrates
A โ web search + web scrape
one session carries
.withWebSearch(...) and .withWebScrape(...). Each mode is read from an env var, validated by the SDK's resolve*Mode helpers, and the components' EVENTS.WEB_SEARCH.* / EVENTS.WEB_SCRAPE.* lifecycle is subscribed end-to-end. The scrape targets a REAL local HTTP server (loopback, no external egress).B โ browser
a second session carries
.withBrowser(...); navigating the read leg fetches the same local server (action must be omitted โ an explicit action: "navigate" switches to the Playwright/CDP interactive leg, which needs obscura serve). Both sessions opt into the inject date context: a Today's date: โฆ line (and optional instructions) is rendered into the capability's preload block so the model can answer "today"-style questions. Env knobs: EXAMPLE_WEB_SEARCH_MODE (default tavily), EXAMPLE_WEB_SCRAPE_MODE (default curl), EXAMPLE_BROWSER_MODE (default curl). Tavily modes need TAVILY_API_KEY; a missing key surfaces as web_search:error and a tool error โ never a crash. Run: node examples/11_web_tools.mjs Needs: PROVIDER + provider credentials in secret.env; a search key for the default search mode. @version 0.27.0imported from 'merci-sdk'
EVENTS, defaultDisplay, initializeClient, loadCredentials, log, resolveBrowserMode, resolveScrapeMode, resolveSearchMode
3. How the Core Call Works
The entry point, condensed. The full runnable file is linked below.
import {
EVENTS, defaultDisplay, initializeClient, loadCredentials, log,
resolveBrowserMode, resolveScrapeMode, resolveSearchMode,
} from 'merci-sdk';
import { startLocalApiServer } from './_shared/real-tools.mjs';
import { reportUsage, resolveExample, runMain, trackUsage, warnEmptyAnswer, wireClient } from './_shared/example-utils.mjs';
const SEARCH_MODE = process.env.EXAMPLE_WEB_SEARCH_MODE ?? 'tavily';
const SCRAPE_MODE = process.env.EXAMPLE_WEB_SCRAPE_MODE ?? 'curl';
const BROWSER_MODE = process.env.EXAMPLE_BROWSER_MODE ?? 'curl';
/** Opt-in context injection: the capability preload block gets a date line. */
const INJECT = { date: { format: 'iso' }, time: false, timezone: 'UTC' };
async function main() {
const { provider, defaultModel } = resolveExample();
const { client } = await initializeClient({ provider, options: loadCredentials({ provider }) });
wireClient({ client });
const usage = trackUsage({ client });
const searchLeg = resolveSearchMode({ sessionProvider: provider, mode: SEARCH_MODE });
const scrapeLeg = resolveScrapeMode({ sessionProvider: provider, mode: SCRAPE_MODE });
const browserLeg = resolveBrowserMode({ _sessionProvider: provider, mode: BROWSER_MODE });
log.header({ t: `Example 11 โ Web tools (${provider})` });
log.option({ name: 'web.search', value: `${SEARCH_MODE} โ ${searchLeg}`, source: 'EXAMPLE_WEB_SEARCH_MODE / resolveSearchMode' });
log.option({ name: 'web.scrape', value: `${SCRAPE_MODE} โ ${scrapeLeg}`, source: 'EXAMPLE_WEB_SCRAPE_MODE / resolveScrapeMode' });
log.option({ name: 'browser', value: `${BROWSER_MODE} โ ${browserLeg}`, source: 'EXAMPLE_BROWSER_MODE / resolveBrowserMode' });
log.option({ name: 'inject', value: JSON.stringify(INJECT), source: 'opt-in date context rendered into the preload block' });
const web = {
search: { start: 0, ok: 0, err: 0 },
scrape: { start: 0, ok: 0, err: 0 },
browser: { start: 0, ok: 0, err: 0 },
};
client.on(EVENTS.WEB_SEARCH.SEARCH_START, (/** @type {any} */ e) => { web.search.start++; log.event({ type: EVENTS.WEB_SEARCH.SEARCH_START, payload: { query: e.query, mode: e.mode } }); });
client.on(EVENTS.WEB_SEARCH.SEARCH_SUCCESS, (/** @type {any} */ e) => { web.search.ok++; log.event({ type: EVENTS.WEB_SEARCH.SEARCH_SUCCESS, payload: { query: e.query, sources: e.resultCount, ms: e.durationMs } }); });
client.on(EVENTS.WEB_SEARCH.SEARCH_ERROR, (/** @type {any} */ e) => { web.search.err++; log.warn({ m: `${EVENTS.WEB_SEARCH.SEARCH_ERROR} โ ${e.error}` }); });
client.on(EVENTS.WEB_SCRAPE.SCRAPE_START, (/** @type {any} */ e) => { web.scrape.start++; log.event({ type: EVENTS.WEB_SCRAPE.SCRAPE_START, payload: { url: e.url, mode: e.mode } }); });
client.on(EVENTS.WEB_SCRAPE.SCRAPE_SUCCESS, (/** @type {any} */ e) => { web.scrape.ok++; log.event({ type: EVENTS.WEB_SCRAPE.SCRAPE_SUCCESS, payload: { url: e.url, chars: e.chars, truncated: e.truncated, ms: e.durationMs } }); });
client.on(EVENTS.WEB_SCRAPE.SCRAPE_ERROR, (/** @type {any} */ e) => { web.scrape.err++; log.warn({ m: `${EVENTS.WEB_SCRAPE.SCRAPE_ERROR} โ ${e.error}` }); });
client.on(EVENTS.BROWSER.NAVIGATE_START, (/** @type {any} */ e) => { web.browser.start++; log.event({ type: EVENTS.BROWSER.NAVIGATE_START, payload: { url: e.url, mode: e.mode } }); });4. Behavior the Code Fixes
web search, scrape, and the headless browser
5. Run the Example
Execute the runnable example from the repository root:
node examples/11_web_tools.mjs
What it does: web search, scrape, and the headless browser