google_search
Execute web searches to find information, answer questions, and retrieve relevant results from the internet.
Instructions
Perform a web search query
Input Schema
TableJSON 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 main handler function that performs the Google Custom Search API query, processes results into title/link/snippet format, and returns them 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)Defines the tool schema for 'google_search', including name, description, and input schema with query (required) and optional num results.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:182-187 (registration)Registers the CallToolRequestSchema handler which dispatches tool calls, specifically checking for 'google_search' and invoking the handleSearch function.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); }
- src/index.ts:178-180 (registration)Registers the ListToolsRequestSchema handler that returns tool definitions, including the 'google_search' schema via getToolDefinitions().this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: this.getToolDefinitions(), }));