search_pypi
Search the Python Package Index (PyPI) for Python packages to retrieve relevant packages based on a specified query, aiding in code research and development.
Instructions
Search PyPI for Python packages
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query |
Input Schema (JSON Schema)
{
"properties": {
"query": {
"description": "Search query",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/index.ts:241-269 (handler)The core handler function implementing the search_pypi tool logic: queries PyPI JSON API for the exact package matching the query, formats key info with caching and 404 handling.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:399-410 (schema)Input schema and metadata for the search_pypi tool as registered in ListToolsRequestHandler.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)Registration of the search_pypi tool call handler in the switch statement of CallToolRequestSchema handler.case 'search_pypi': { const { query } = request.params.arguments as { query: string }; const results = await this.searchPyPI(query); return { content: [ { type: 'text', text: results } ] }; }