Skip to main content
Glama

ai_search_npm

Search NPM packages and documentation to find relevant packages for your project needs. Generates search URLs for accessing comprehensive package information and technical details.

Instructions

📦 NPM包搜索 - 搜索NPM包和相关文档

【重要】此工具会返回NPM搜索URL,Claude Code应该使用WebFetch工具访问该URL以获取真实搜索结果。

Input Schema

NameRequiredDescriptionDefault
queryYes包名或关键词
sizeNo返回结果数量,默认10

Input Schema (JSON Schema)

{ "properties": { "query": { "description": "包名或关键词", "type": "string" }, "size": { "default": 10, "description": "返回结果数量,默认10", "type": "number" } }, "required": [ "query" ], "type": "object" }

Implementation Reference

  • Handler function for 'ai_search_npm' tool. Normalizes input, constructs NPM search URLs (web and registry API), generates detailed tips and instructions for WebFetch usage, saves results, and returns formatted response.
    case 'ai_search_npm': { const rawQuery = normalizeString(args.query); const resolvedSize = clampNumber(args.size, 1, 100, 10); if (!rawQuery) { throw new Error('搜索关键词不能为空'); } const searchUrl = `https://www.npmjs.com/search?q=${encodeURIComponent(rawQuery)}`; const registryUrl = `https://registry.npmjs.org/-/v1/search?text=${encodeURIComponent(rawQuery)}&size=${resolvedSize}`; // NPM 搜索技巧 const tips = [ `精确包名: ${rawQuery} (使用完整包名)`, `关键词搜索: keywords:${rawQuery}`, `作者搜索: author:${rawQuery}`, `维护者: maintainer:${rawQuery}`, `作用域包: @scope/${rawQuery}`, `特定版本: ${rawQuery}@latest` ]; // 相关搜索建议 const relatedSearches = [ `${rawQuery} typescript`, `${rawQuery} cli`, `${rawQuery} plugin`, `@types/${rawQuery}` ]; // 热门类别推荐 const categories = [ 'react', 'vue', 'express', 'webpack', 'babel', 'eslint', 'testing', 'cli-tools' ]; const detailsContent = `📦 NPM 包搜索\n\n` + `**搜索关键词**: ${rawQuery}\n` + `**期望结果数**: ${resolvedSize} 个\n\n` + `---\n\n` + `🔗 **网页搜索**: ${searchUrl}\n` + `🔗 **API搜索**: ${registryUrl}\n\n` + `⚠️ **请使用 WebFetch 工具获取搜索结果**:\n` + `\`\`\`javascript\n` + `// 方式1: 网页搜索\n` + `WebFetch({\n` + ` url: "${searchUrl}",\n` + ` prompt: "提取前${resolvedSize}个包的:包名、描述、版本号、周下载量、最后更新时间"\n` + `})\n\n` + `// 方式2: API搜索 (推荐,结构化数据)\n` + `WebFetch({\n` + ` url: "${registryUrl}",\n` + ` prompt: "解析JSON数据,提取包的名称、描述、版本、作者和下载统计"\n` + `})\n` + `\`\`\`\n\n` + `---\n\n` + `💡 **NPM 搜索技巧**:\n` + tips.map(tip => `• ${tip}`).join('\n') + `\n\n📌 **相关搜索建议**:\n` + relatedSearches.map(s => `• ${s}`).join('\n') + `\n\n🏷️ **热门包分类**:\n` + categories.map(cat => `• ${cat}`).join(' ') + `\n\n📚 **直接访问包详情**: https://www.npmjs.com/package/${rawQuery}`; const filepath = await saveSearchResult('npm-search', rawQuery, detailsContent); return makeTextResponse( `📦 **NPM包搜索**\n\n` + `**关键词**: ${rawQuery}\n` + `**搜索链接**: ${searchUrl}\n\n` + `✅ 详细信息已保存至: ${filepath || '保存失败'}\n` + `💡 使用 WebFetch 工具访问搜索链接获取结果` ); }
  • Tool definition including name, description, and input schema for 'ai_search_npm' in the AI_TOOLS array.
    { name: 'ai_search_npm', description: '📦 NPM包搜索 - 搜索NPM包和相关文档\n\n【重要】此工具会返回NPM搜索URL,Claude Code应该使用WebFetch工具访问该URL以获取真实搜索结果。', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '包名或关键词' }, size: { type: 'number', description: '返回结果数量,默认10', default: 10 } }, required: ['query'] } },
  • Registration of all AI_TOOLS (including ai_search_npm) for the ListToolsRequestSchema handler.
    server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: AI_TOOLS, }));
  • General CallToolRequestSchema handler that dispatches to specific tool cases via switch statement, including 'ai_search_npm'.
    server.setRequestHandler(CallToolRequestSchema, async (request) => {
  • Helper function to save search results to a file, used by ai_search_npm handler.
    const saveSearchResult = async (toolName, query, details) => { try { const resultsDir = join(process.cwd(), '.search-results'); if (!existsSync(resultsDir)) { await mkdir(resultsDir, { recursive: true }); } const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5); const filename = `${toolName}-${timestamp}.md`; const filepath = join(resultsDir, filename); await writeFile(filepath, details, 'utf-8'); return filepath; } catch (error) { console.error('Failed to save search result:', error); return null; } };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/adminhuan/smart-search-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server