web.rich
Retrieve detailed web data through Brave Search by enabling callback flow and fetching rich results for specific queries.
Instructions
Brave Rich Search via callback flow. First enables rich callback, then fetches rich data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| q | Yes |
Implementation Reference
- src/index.ts:126-139 (handler)Handler function that executes the web.rich tool: performs initial web search with rich callback enabled, extracts callback key, fetches rich results, and returns JSON.async handler(args) { const first = await braveGet('https://api.search.brave.com/res/v1/web/search', { q: args.q, enable_rich_callback: 1, }); const callbackKey = (first as any)?.rich?.hint?.callback_key; if (!callbackKey) { return { content: [{ type: 'text', text: 'No rich results available for this query.' }] }; } const rich = await braveGet('https://api.search.brave.com/res/v1/web/rich', { callback_key: callbackKey, }); return { content: [{ type: 'text', text: JSON.stringify({ hint: (first as any)?.rich?.hint, rich }) }] }; },
- src/index.ts:43-48 (schema)Input schema for the web.rich tool, defining a required 'q' query string.const richCallbackSchema = { type: 'object', properties: { q: { type: 'string', minLength: 1 } }, required: ['q'], additionalProperties: false, } as const;
- src/index.ts:122-141 (registration)The web.rich tool is registered as part of the toolDefs array, which populates toolList for listing and toolMap for dispatching calls.{ name: 'web.rich', description: 'Brave Rich Search via callback flow. First enables rich callback, then fetches rich data', inputSchema: richCallbackSchema, async handler(args) { const first = await braveGet('https://api.search.brave.com/res/v1/web/search', { q: args.q, enable_rich_callback: 1, }); const callbackKey = (first as any)?.rich?.hint?.callback_key; if (!callbackKey) { return { content: [{ type: 'text', text: 'No rich results available for this query.' }] }; } const rich = await braveGet('https://api.search.brave.com/res/v1/web/rich', { callback_key: callbackKey, }); return { content: [{ type: 'text', text: JSON.stringify({ hint: (first as any)?.rich?.hint, rich }) }] }; }, }, ];
- src/index.ts:51-74 (helper)Helper utility braveGet used by web.rich (and other tools) 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(); }
- src/index.ts:158-172 (registration)MCP server request handler for tool calls, which dispatches to the specific tool handler via toolMap (including web.rich).server.setRequestHandler(CallToolRequestSchema, async (request) => { const name = request.params.name; const args = request.params.arguments ?? {}; const tool = toolMap.get(name); if (!tool) { return { content: [{ type: 'text', text: `Unknown tool: ${name}` }] } as unknown as typeof CallToolResultSchema._type; } try { const result = await tool.handler(args); return result as unknown as typeof CallToolResultSchema._type; } catch (err: any) { const message = err?.message ?? String(err); return { content: [{ type: 'text', text: `Error: ${message}` }] } as unknown as typeof CallToolResultSchema._type; } });