search_stackoverflow
Search Stack Overflow for programming questions and answers to find code examples and solutions. Input a query to retrieve relevant results.
Instructions
Search Stack Overflow for programming questions and answers
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| limit | No | Maximum number of results (default: 5) |
Implementation Reference
- src/index.ts:63-106 (handler)The core handler function that implements the search_stackoverflow tool logic: queries Stack Overflow API, extracts and formats results using Cheerio, applies caching, and handles errors.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 for the search_stackoverflow tool, defining 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:321-339 (registration)Registration of the search_stackoverflow tool in the ListTools response, including name, description, and input schema.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 (handler)Dispatch handler in CallToolRequestSchema that extracts arguments, calls the searchStackOverflow function, and formats the MCP 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 } ] }; }