brave_local_search_code_mode
Extract specific data from Brave local search results by executing custom JavaScript code on the raw API response, reducing context window usage for targeted local business lookups.
Instructions
Performs a local search using Brave APIs, and then runs a custom JavaScript code string against the RAW API RESPONSE in a secure QuickJS sandbox. This reduces context window usage by only returning the output of your script. Use this for local/business lookups when you only need specific fields from large local payloads. Your script should read the 'DATA' global variable (a JSON string payload) and use console.log() to print the desired output.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Local search query (e.g. 'pizza near Central Park') | |
| count | No | Number of results (1-20, default 5) | |
| code | Yes | JavaScript code to execute against the 'DATA' variable. | |
| language | No | Language of the code. Only 'javascript' is supported. | javascript |
Implementation Reference
- src/tools/handlers.ts:101-139 (handler)The handler for 'brave_local_search_code_mode' which fetches local search data and processes it using a user-provided JavaScript script in a sandbox.
export async function braveLocalSearchCodeModeHandler(args: unknown) { if (!isBraveLocalSearchCodeModeArgs(args)) { throw new Error("Invalid arguments for brave_local_search_code_mode"); } const { query, count = 5, code, language = "javascript" } = args; if (language.toLowerCase() !== "javascript") { return { content: [{ type: "text", text: "Unsupported language. Only 'javascript' is supported." }], isError: true, }; } console.error(`Fetching local search for code mode: "${query}"`); const rawDataStr = await performLocalSearchRaw(query, count); const beforeSizeKB = (Buffer.byteLength(rawDataStr, "utf8") / 1024).toFixed(1); console.error("Executing local search code mode sandbox..."); const { stdout, error, executionTimeMs } = await runInSandbox(rawDataStr, code); if (error) { return { content: [{ type: "text", text: `Sandboxed Execution Failed:\n${error}` }], isError: true, }; } const finalOutput = stdout.trim() || "[No output from script]"; const afterSizeKB = (Buffer.byteLength(finalOutput, "utf8") / 1024).toFixed(1); const reductionPct = (100 - (Number(afterSizeKB) / Number(beforeSizeKB)) * 100).toFixed(1); const header = `[code-mode: ${beforeSizeKB}KB -> ${afterSizeKB}KB (${reductionPct}% reduction) in ${executionTimeMs}ms]\n\n`; return { content: [{ type: "text", text: header + finalOutput }], isError: false, }; } - src/tools/definitions.ts:143-151 (schema)Tool definition and type guard for 'brave_local_search_code_mode'.
export const BRAVE_LOCAL_SEARCH_CODE_MODE_TOOL: Tool = { name: "brave_local_search_code_mode", description: "Performs a local search using Brave APIs, and then runs a custom JavaScript code string against the RAW API RESPONSE in a secure QuickJS sandbox. " + "This reduces context window usage by only returning the output of your script. " + "Use this for local/business lookups when you only need specific fields from large local payloads. " + "Your script should read the 'DATA' global variable (a JSON string payload) and use console.log() to print the desired output.", inputSchema: { type: "object",