web_search
Perform Bing-powered web or news searches with customizable filters. Retrieve up to 20 results, apply time filters for news, and access both general web and news-specific data efficiently.
Instructions
Perform web or news search using Bing search engine. Supports both general web search and news search modes.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| maxResults | No | Maximum number of results | |
| query | Yes | Search query, e.g., Node.js tutorial, tech news, political updates, etc. | |
| searchType | Yes | Search type: web (general web search), news (news search) - required | |
| timeFilter | No | Time filter (only valid for news search): past 1 hour, 24 hours, 7 days, 30 days | past_24_hours |
Implementation Reference
- src/mcp/server.js:173-211 (handler)The primary handler function for the 'web_search' tool. Validates input parameters and delegates to Bing web or news search implementations in searchService.async function handleWebSearch(args) { const { query, searchType, maxResults = 10, timeFilter = 'past_24_hours' } = args; if (!query || typeof query !== 'string') { throw new Error('Query parameter is required and must be a string'); } if (!searchType || !['web', 'news'].includes(searchType)) { throw new Error('searchType is required and must be either "web" or "news"'); } if (maxResults < 1 || maxResults > 20) { throw new Error('maxResults must be between 1 and 20'); } if (searchType === 'news' && !['past_hour', 'past_24_hours', 'past_7_days', 'past_30_days'].includes(timeFilter)) { throw new Error('timeFilter must be a valid time filter option'); } const searchService = (await import('../services/searchService.js')).default; let results; if (searchType === 'news') { results = await searchService.searchBingNews(query, maxResults, timeFilter); } else { results = await searchService.searchBing(query, maxResults); } return { tool: 'web_search', searchType, query, maxResults, timeFilter: searchType === 'news' ? timeFilter : undefined, results: results.results, totalResults: results.totalResults, timestamp: results.timestamp }; }
- src/mcp/server.js:9-39 (schema)Input schema definition for the web_search tool, including required parameters query and searchType, optional maxResults and timeFilter.name: 'web_search', description: 'Perform web or news search using Bing search engine. Supports both general web search and news search modes.', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query, e.g., Node.js tutorial, tech news, political updates, etc.' }, searchType: { type: 'string', enum: ['web', 'news'], description: 'Search type: web (general web search), news (news search) - required' }, maxResults: { type: 'number', description: 'Maximum number of results', default: 10, minimum: 1, maximum: 20 }, timeFilter: { type: 'string', enum: ['past_hour', 'past_24_hours', 'past_7_days', 'past_30_days'], description: 'Time filter (only valid for news search): past 1 hour, 24 hours, 7 days, 30 days', default: 'past_24_hours' } }, required: ['query', 'searchType'] } },
- src/mcp/server.js:117-121 (registration)Registration of ListToolsRequestSchema handler, which provides the tool list including web_search via generateTools().server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: generateTools() }; });
- src/mcp/server.js:129-132 (registration)Tool dispatcher in CallToolRequestSchema handler, routes 'web_search' calls to handleWebSearch.switch (name) { case 'web_search': result = await handleWebSearch(args); break;
- Helper function implementing general web search on Bing using Puppeteer browser automation.async searchBing(query, maxResults = 10) { try { const browser = await this.initBrowser(); const page = await browser.newPage(); await page.setUserAgent(this.getRandomUserAgent()); await page.setViewport({ width: 1920, height: 1080 }); const searchUrl = `https://www.bing.com/search?q=${encodeURIComponent(query)}&count=${maxResults}`; await page.goto(searchUrl, { waitUntil: 'networkidle2' }); await page.waitForSelector('#b_results', { timeout: 10000 }); const rawResults = await page.evaluate(() => { const searchResults = []; const resultElements = document.querySelectorAll('#b_results .b_algo'); resultElements.forEach((element, index) => { if (index >= 10) return; const titleElement = element.querySelector('h2 a'); const snippetElement = element.querySelector('.b_caption p'); if (titleElement) { searchResults.push({ title: titleElement.textContent.trim(), url: titleElement.href, snippet: snippetElement ? snippetElement.textContent.trim() : '', rank: index + 1 }); } }); return searchResults; }); // Clean URLs in Node.js const results = rawResults.map(item => ({ ...item, url: this.cleanUrl(item.url) })); await page.close(); logger.info(`Bing search completed for query: "${query}", found ${results.length} results`); return { engine: 'Bing', query, results, totalResults: results.length, timestamp: new Date().toISOString() }; } catch (error) { logger.error('Bing search error:', error); throw new Error(`Bing search failed: ${error.message}`); } }