search_pypi
Search PyPI for Python packages to find code examples and documentation, helping developers locate programming resources.
Instructions
Search PyPI for Python packages
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query |
Implementation Reference
- src/index.ts:241-269 (handler)The core handler function for the 'search_pypi' tool. It queries the PyPI JSON API for the exact package name provided in the query parameter, caches the result, and formats package information. Handles 404 by returning a 'not found' message.private async searchPyPI(query: string, limit: number = 5): Promise<string> { const cacheKey = `pypi:${query}:${limit}`; const cached = cache.get<string>(cacheKey); if (cached) return cached; try { const response = await this.axiosInstance.get( `https://pypi.org/pypi/${encodeURIComponent(query)}/json` ); const pkg = response.data.info; const result = `Package: ${pkg.name} (v${pkg.version})\n` + `Description: ${pkg.summary || 'No description'}\n` + `Author: ${pkg.author || 'Unknown'}\n` + `Homepage: ${pkg.home_page || pkg.project_url || 'N/A'}\n` + `PyPI: https://pypi.org/project/${pkg.name}/\n`; cache.set(cacheKey, result); return result; } catch (error) { if (axios.isAxiosError(error) && error.response?.status === 404) { return `No package found for "${query}"`; } throw new McpError( ErrorCode.InternalError, `PyPI API error: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/index.ts:401-410 (schema)The input schema defining the expected parameters for the search_pypi tool: a required 'query' string.inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query' } }, required: ['query'] }
- src/index.ts:398-411 (registration)Registration of the 'search_pypi' tool in the ListToolsRequestSchema handler, including name, description, and input schema.{ name: 'search_pypi', description: 'Search PyPI for Python packages', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query' } }, required: ['query'] } },
- src/index.ts:493-504 (registration)The dispatch logic in CallToolRequestSchema handler that routes 'search_pypi' calls to the searchPyPI method and formats the response.case 'search_pypi': { const { query } = request.params.arguments as { query: string }; const results = await this.searchPyPI(query); return { content: [ { type: 'text', text: results } ] }; }