search
Search the web using Google to find information, websites, and answers to queries without requiring an API key.
Instructions
Search the web using Google (no API key required)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| limit | No | Maximum number of results to return (default: 5) |
Implementation Reference
- src/index.ts:121-152 (handler)The performSearch method implements the core logic of the 'search' tool: performs a Google search using axios, parses the HTML with cheerio, extracts up to the specified number of results including title, URL, and description.private async performSearch(query: string, limit: number): Promise<SearchResult[]> { const response = await axios.get('https://www.google.com/search', { params: { q: query }, headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' } }); const $ = cheerio.load(response.data); const results: SearchResult[] = []; $('div.g').each((i, element) => { if (i >= limit) return false; const titleElement = $(element).find('h3'); const linkElement = $(element).find('a'); const snippetElement = $(element).find('.VwiC3b'); if (titleElement.length && linkElement.length) { const url = linkElement.attr('href'); if (url && url.startsWith('http')) { results.push({ title: titleElement.text(), url: url, description: snippetElement.text() || '', }); } } }); return results; }
- src/index.ts:13-17 (schema)Defines the TypeScript interface for a single search result returned by the tool.interface SearchResult { title: string; url: string; description: string; }
- src/index.ts:56-71 (schema)JSON schema defining the input parameters for the 'search' tool: query (required string) and optional limit (number 1-10).inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query', }, limit: { type: 'number', description: 'Maximum number of results to return (default: 5)', minimum: 1, maximum: 10, }, }, required: ['query'], },
- src/index.ts:51-74 (registration)Registers the 'search' tool in the MCP server by handling ListToolsRequestSchema and providing the tool's metadata, description, and input schema.this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: 'search', description: 'Search the web using Google (no API key required)', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query', }, limit: { type: 'number', description: 'Maximum number of results to return (default: 5)', minimum: 1, maximum: 10, }, }, required: ['query'], }, }, ], }));
- src/index.ts:19-23 (helper)Type guard function to validate the arguments passed to the 'search' tool.const isValidSearchArgs = (args: any): args is { query: string; limit?: number } => typeof args === 'object' && args !== null && typeof args.query === 'string' && (args.limit === undefined || typeof args.limit === 'number');