search
Perform a web search and receive up to 10 results using Google's search engine.
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:158-205 (handler)Handler logic for the 'search' tool. Validates args, calls Google Custom Search API, maps results to SearchResult objects, and returns JSON-formatted results.
if (request.params.name === 'search') { if (!isValidSearchArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'Invalid search arguments' ); } const { query, num = 5 } = request.params.arguments; try { const response = await this.axiosInstance.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) { if (axios.isAxiosError(error)) { return { content: [ { type: 'text', text: `Search API error: ${ error.response?.data?.error?.message ?? error.message }`, }, ], isError: true, }; } throw error; } - src/index.ts:53-57 (schema)Type definition for search result items returned from the API.
interface SearchResult { title: string; link: string; snippet: string; } - src/index.ts:65-71 (schema)Type guard that validates search tool input arguments.
const isValidSearchArgs = ( args: any ): args is { query: string; num?: number } => typeof args === 'object' && args !== null && typeof args.query === 'string' && (args.num === undefined || typeof args.num === 'number'); - src/index.ts:120-139 (registration)Registration of the 'search' tool with its name, description, and JSON Schema input definition.
{ name: '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'], }, },