web.search
Search the web using Brave Search API to retrieve results for queries, with options for result count, country, safesearch, and freshness filters.
Instructions
Brave Web Search: returns results for query q
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| q | Yes | ||
| count | No | ||
| country | No | ||
| safesearch | No | ||
| freshness | No |
Implementation Reference
- src/index.ts:89-98 (handler)Handler function for the 'web.search' tool. Performs a web search using the Brave Search API with provided query and optional parameters, returns JSON stringified results.async handler(args) { const data = await braveGet('https://api.search.brave.com/res/v1/web/search', { q: args.q, count: args.count, country: args.country, safesearch: args.safesearch, freshness: args.freshness, }); return { content: [{ type: 'text', text: JSON.stringify(data) }] }; },
- src/index.ts:16-27 (schema)Input schema definition for the 'web.search' tool, validating query 'q' and optional parameters.const webSearchSchema = { type: 'object', properties: { q: { type: 'string', minLength: 1 }, count: { type: 'integer', minimum: 1, maximum: 50 }, country: { type: 'string' }, safesearch: { enum: ['off', 'moderate', 'strict'] }, freshness: { enum: ['pd', 'pw', 'pm', 'py'] }, }, required: ['q'], additionalProperties: false, } as const;
- src/index.ts:85-99 (registration)Registration of the 'web.search' tool in the toolDefs array, including name, description, schema, and handler.{ name: 'web.search', description: 'Brave Web Search: returns results for query q', inputSchema: webSearchSchema, async handler(args) { const data = await braveGet('https://api.search.brave.com/res/v1/web/search', { q: args.q, count: args.count, country: args.country, safesearch: args.safesearch, freshness: args.freshness, }); return { content: [{ type: 'text', text: JSON.stringify(data) }] }; }, },
- src/index.ts:51-74 (helper)Helper utility function 'braveGet' used by the web.search handler to make authenticated API requests to Brave Search endpoints.async function braveGet(url: string, params: Record<string, string | number | boolean | undefined>) { const apiKey = process.env.EARCH_MCP_API_KEY || process.env.BRAVE_API_KEY || process.env.BRAVE_SEARCH_API_KEY; if (!apiKey) { throw new Error('Missing API key. Set EARCH_MCP_API_KEY or BRAVE_API_KEY.'); } const headers: Record<string, string> = { Accept: 'application/json', 'Accept-Encoding': 'gzip', 'X-Subscription-Token': apiKey, }; const usp = new URLSearchParams(); for (const [k, v] of Object.entries(params)) { if (v === undefined) continue; usp.set(k, String(v)); } const endpoint = `${url}?${usp.toString()}`; const res = await fetch(endpoint, { headers }); if (!res.ok) { const body = await res.text().catch(() => ''); throw new Error(`Brave API error ${res.status}: ${body}`); } return res.json(); }