web_search
Search the internet using Google Custom Search API to find relevant web pages and summaries based on your query, with options to specify language and result limits.
Instructions
在互联网上搜索信息,返回相关的网页链接和摘要
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | No | 搜索语言(如:zh-CN, en-US) | zh-CN |
| maxResults | No | 最大返回结果数量(默认10) | |
| query | Yes | 搜索查询关键词 |
Implementation Reference
- src/index.ts:179-205 (handler)Primary handler function for the 'web_search' tool. Parses input arguments, performs web search (real or mock), formats results as markdown text, and returns as MCP content.
private async handleWebSearch(args: any) { const { query, maxResults = 10, language = 'zh-CN' } = args; if (!this.searchApiKey || !this.searchEngineId) { // 如果没有配置API密钥,使用模拟数据 return this.getMockSearchResults(query, maxResults); } try { const results = await this.performWebSearch(query, maxResults, language); const formattedResults = results.map((result, index) => `${index + 1}. **${result.title}**\n URL: ${result.url}\n 摘要: ${result.snippet}\n` ).join('\n'); return { content: [ { type: 'text', text: `搜索 "${query}" 的结果:\n\n${formattedResults}`, }, ], }; } catch (error) { throw new Error(`搜索失败: ${error instanceof Error ? error.message : String(error)}`); } } - src/index.ts:288-310 (helper)Core helper function that performs the actual web search using Google Custom Search API via axios, mapping results to SearchResult interface.
private async performWebSearch(query: string, maxResults: number, language: string): Promise<SearchResult[]> { const url = `https://www.googleapis.com/customsearch/v1`; const params = { key: this.searchApiKey, cx: this.searchEngineId, q: query, num: Math.min(maxResults, 10), lr: `lang_${language}`, }; const response = await axios.get(url, { params, timeout: this.requestTimeout, }); const items = response.data.items || []; return items.map((item: any, index: number) => ({ title: item.title, url: item.link, snippet: item.snippet, rank: index + 1, })); } - src/index.ts:77-96 (schema)Input schema definition for the web_search tool, specifying query (required), maxResults, and language parameters.
inputSchema: { type: 'object', properties: { query: { type: 'string', description: '搜索查询关键词', }, maxResults: { type: 'number', description: '最大返回结果数量(默认10)', default: 10, }, language: { type: 'string', description: '搜索语言(如:zh-CN, en-US)', default: 'zh-CN', }, }, required: ['query'], }, - src/index.ts:74-97 (registration)Registration of the web_search tool in the ListToolsRequestHandler, including name, description, and schema.
{ name: 'web_search', description: '在互联网上搜索信息,返回相关的网页链接和摘要', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '搜索查询关键词', }, maxResults: { type: 'number', description: '最大返回结果数量(默认10)', default: 10, }, language: { type: 'string', description: '搜索语言(如:zh-CN, en-US)', default: 'zh-CN', }, }, required: ['query'], }, }, - src/index.ts:156-157 (registration)Dispatch/registration of web_search handler in the CallToolRequestHandler switch statement.
case 'web_search': return await this.handleWebSearch(args);