search_stackoverflow
Retrieve programming-related answers and solutions from Stack Overflow by entering a search query. Specify the maximum number of results to streamline your code research.
Instructions
Search Stack Overflow for programming questions and answers
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Maximum number of results (default: 5) | |
| query | Yes | Search query |
Implementation Reference
- src/index.ts:63-106 (handler)Core handler function that performs Stack Overflow search using Stack Exchange API, caches results, parses with cheerio, and formats output.private async searchStackOverflow(query: string, limit: number = 5): Promise<string> { const cacheKey = `stackoverflow:${query}:${limit}`; const cached = cache.get<string>(cacheKey); if (cached) return cached; try { const response = await this.axiosInstance.get( `https://api.stackexchange.com/2.3/search/advanced`, { params: { q: query, site: 'stackoverflow', pagesize: limit, order: 'desc', sort: 'votes', filter: 'withbody' } } ); const results = response.data.items.map((item: any) => { const $ = cheerio.load(item.body); return { title: item.title, link: item.link, score: item.score, answer_count: item.answer_count, excerpt: $.text().substring(0, 200) + '...' }; }); const formatted = results.map((r: any, i: number) => `${i + 1}. ${r.title}\n Score: ${r.score} | Answers: ${r.answer_count}\n ${r.link}\n ${r.excerpt}\n` ).join('\n'); cache.set(cacheKey, formatted); return formatted; } catch (error) { throw new McpError( ErrorCode.InternalError, `Stack Overflow API error: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/index.ts:323-338 (schema)Input schema definition for the search_stackoverflow tool, specifying 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 (default: 5)', minimum: 1, maximum: 10 } }, required: ['query'] }
- src/index.ts:320-339 (registration)Tool registration in the list of tools returned by ListToolsRequestSchema handler.{ name: 'search_stackoverflow', description: 'Search Stack Overflow for programming questions and answers', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query' }, limit: { type: 'number', description: 'Maximum number of results (default: 5)', minimum: 1, maximum: 10 } }, required: ['query'] } },
- src/index.ts:437-448 (registration)Dispatch case in CallToolRequestSchema handler that invokes the searchStackOverflow function and returns formatted text response.case 'search_stackoverflow': { const { query, limit } = request.params.arguments as { query: string; limit?: number }; const results = await this.searchStackOverflow(query, limit); return { content: [ { type: 'text', text: results } ] }; }