google_search
Perform a web search with a query and control result count (1-10). Leverages Google Custom Search API for web data retrieval.
Instructions
Perform a web search query
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| num | No | Number of results (1-10) |
Implementation Reference
- src/index.ts:71-101 (handler)The core handler function for google_search. Calls Google Custom Search API with the query and num parameters, returns results as JSON text content.
private async handleSearch(query: string, num = 10) { try { const response = await this.client.get('', { params: { q: query, num: Math.min(num, 10), }, }); const results: SearchResult[] = response.data.items.map((item: any) => ({ title: item.title, link: item.link, snippet: item.snippet, })); return { content: [{ type: 'text', text: JSON.stringify(results, null, 2), }], }; } catch (error: unknown) { return { content: [{ type: 'text', text: `Search API error: ${error instanceof Error ? error.message : String(error)}`, }], isError: true, }; } } - src/index.ts:135-154 (schema)The input schema definition for the google_search tool. Defines 'query' (required string) and 'num' (optional number, 1-10).
const searchToolSchema = { name: 'google_search', description: 'Perform a web search query', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query', }, num: { type: 'number', description: 'Number of results (1-10)', minimum: 1, maximum: 10, }, }, required: ['query'], }, }; - src/index.ts:177-200 (registration)Registration of the google_search tool via the MCP CallToolRequestSchema handler. Dispatches to handleSearch() when tool name matches 'google_search'. Also lists the tool via ListToolsRequestSchema.
private setupToolHandlers() { this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: this.getToolDefinitions(), })); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { // Handle search tool if (request.params.name === 'google_search') { const {query, num = 10} = request.params.arguments as { query: string; num?: number }; return await this.handleSearch(query, num); } // Handle read_webpage tool if (request.params.name === 'read_webpage') { const {url} = request.params.arguments as { url: string }; return await this.handleReadWebpage(url); } // If the tool name is not recognized, throw an error throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}` ); }); - src/index.ts:24-24 (helper)The SearchResult interface used to type the data returned by the google_search tool.
interface SearchResult { - src/index.ts:53-59 (helper)Axios client configured to call the Google Custom Search API, used by the google_search handler.
this.client = axios.create({ baseURL: 'https://www.googleapis.com/customsearch/v1', params: { key: API_KEY, cx: SEARCH_ENGINE_ID, }, });