google_search
Retrieve structured data from Google search results to extract information for research, analysis, or content creation. Supports country-specific searches for localized data.
Instructions
Retrieve structured data from Google search results
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| country | No | Country code for localized results (e.g., US, GB) | US |
| query | Yes | The search query to perform |
Implementation Reference
- src/tools/getGoogleSearch.ts:20-85 (handler)The asynchronous handler function that performs the Google search. It constructs a Google search URL, sends a scraping request to the Olostep API using the provided parser, parses the JSON response, and returns the structured search results or an error message.handler: async ({ query, country }: { query: string; country?: string }, apiKey: string, orbitKey?: string) => { try { const headers = new Headers({ 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` }); const searchUrl = new URL('https://www.google.com/search'); searchUrl.searchParams.append('q', query); if (country) searchUrl.searchParams.append('gl', country); const payload = { formats: ["parser_extract"], parser_extract: { parser_id: "@olostep/google-search" }, url_to_scrape: searchUrl.toString(), wait_before_scraping: 0, ...(orbitKey && { force_connection_id: orbitKey }) }; const response = await fetch(OLOSTEP_SCRAPE_API_URL, { method: 'POST', headers: headers, body: JSON.stringify(payload) }); if (!response.ok) { const errorDetails = await response.json(); return { isError: true, content: [{ type: "text", text: `Olostep API Error: ${response.status} ${response.statusText}. Details: ${JSON.stringify(errorDetails)}` }] }; } const data = await response.json() as GoogleSearchResponse; if (data.result?.json_content) { const parsedContent = JSON.parse(data.result.json_content); return { content: [{ type: "text", text: JSON.stringify(parsedContent, null, 2) }] }; } else { return { isError: true, content: [{ type: "text", text: "Error: No search results found in Olostep API response." }] }; } } catch (error: unknown) { return { isError: true, content: [{ type: "text", text: `Error: Failed to perform Google search. ${error instanceof Error ? error.message : String(error)}` }] }; } }
- src/tools/getGoogleSearch.ts:16-19 (schema)Zod schema defining the input parameters for the tool: required 'query' string and optional 'country' string (defaults to 'US').schema: { query: z.string().describe("The search query to perform"), country: z.string().optional().default("US").describe("Country code for localized results (e.g., US, GB)") },
- src/index.ts:54-65 (registration)MCP server registration of the 'google_search' tool, providing name, description, schema, and a wrapper handler that calls the tool's handler and ensures content types are 'text'.server.tool( getGoogleSearch.name, getGoogleSearch.description, getGoogleSearch.schema, async (params) => { const result = await getGoogleSearch.handler(params, OLOSTEP_API_KEY, ORBIT_KEY); return { ...result, content: result.content.map(item => ({ ...item, type: item.type as "text" })) }; } );