NPMJS模型上下文协议(MCP)服务器(v1.1.5)
1.项目目的
这个项目的主要目标是创建一个轻量级的、基于TypeScript的模型上下文协议(MCP)服务器。该服务器致力于以简单、标准化的JSON格式提供有关npmjs包的结构化信息,如版本详细信息、下载统计信息、发布日期、描述和许可证。这种格式适合大型语言模型(LLM)和人工智能驱动的开发工具使用。
该服务器旨在通过抽象与多个npmjs API端点的直接交互来简化对npm包元数据的访问。
2.设置说明
- 克隆存储库:
git clone https://github.com/yiannis-spyridakis/npmjs-mcp-server.git
cd npmjs-mcp-server- 安装依赖项:
npm install3.运行服务器
此MCP服务器设计为由MCP客户端(如LLM代理或开发工具)作为子进程运行。它使用模型上下文协议通过标准输入(stdin)和标准输出(stdout)与客户端通信。
开发环境
要直接运行服务器进行开发,并在文件更改时自动重新启动:
npm run dev或者,对于不观察变化的单次运行:
npm run watch(注: npm run watch 用途 nodemon 和 ts-node 如所述 package.json 为了发展。 npm run dev 用途 ts-node 直接。)
服务器将启动并通过标准输入/输出监听MCP请求。
生产大楼
要将TypeScript代码编译为JavaScript用于生产:
npm run build这将创建一个 dist 包含编译文件的目录。
生产环境
要在生产环境中运行编译后的服务器,请执行以下操作:
npm start此命令执行 node dist/index.jsMCP客户端负责将此命令作为子进程启动。
4.可用的MCP工具
此服务器提供可以使用MCP客户端调用的工具。
工具: get_npm_package_summary
- 说明: 提供基本的软件包详细信息:名称、最新版本、描述、最新版的发布日期、许可证、主页和存储库URL(如果可用)。
- 输入架构:
{
"type": "object",
"properties": {
"packageName": {
"type": "string",
"description": "The name of the npm package (e.g., 'express', 'react')"
}
},
"required": ["packageName"]
}工具: get_npm_package_versions
- 说明: 列出所有可用的软件包版本及其各自的发布日期。
- 输入架构:
{
"type": "object",
"properties": {
"packageName": {
"type": "string",
"description": "The name of the npm package"
}
},
"required": ["packageName"]
}工具: get_npm_package_downloads
- 说明: 提供下载统计信息。可以获取特定时段或所有默认时段(
last-day,last-week,last-month)如果period省略。 - 输入架构:
{
"type": "object",
"properties": {
"packageName": {
"type": "string",
"description": "The name of the npm package"
},
"period": {
"type": "string",
"description": "Optional: 'last-day', 'last-week', 'last-month'. If omitted, all are fetched.",
"enum": ["last-day", "last-week", "last-month"]
}
},
"required": ["packageName"]
}工具: get_npm_package_details
- 说明: 提供一套全面的信息,包括摘要细节、维护者和关键字。
- 输入架构:
{
"type": "object",
"properties": {
"packageName": {
"type": "string",
"description": "The name of the npm package"
}
},
"required": ["packageName"]
}5.可用的MCP提示
除了工具,此服务器还提供提示,MCP客户端可以使用这些提示根据提供的变量生成特定的用户请求,从而简化常见的交互。
提示: get_summary_prompt
- 说明: 生成一个请求,以获取指定npm包的快速摘要。
- 输入架构:
{
"packageName": {
"type": "string",
"description": "The name of the npm package"
}
}提示: get_details_prompt
- 说明: 生成对指定npm包的完整详细信息的请求,包括维护者和存储库URL。
- 输入架构:
{
"packageName": {
"type": "string",
"description": "The name of the npm package"
}
}提示: find_homepage_prompt
- 说明: 生成一个请求,以查找指定npm包的官方主页。
- 输入架构:
{
"packageName": {
"type": "string",
"description": "The name of the npm package"
}
}提示: list_versions_prompt
- 说明: 生成一个请求,列出指定npm包的所有可用版本及其发布日期。
- 输入架构:
{
"packageName": {
"type": "string",
"description": "The name of the npm package"
}
}提示: get_version_date_prompt
- 说明: 生成一个请求,以查找指定npm包的特定版本的发布日期。
- 输入架构:
{
"packageName": {
"type": "string",
"description": "The name of the npm package"
},
"version": {
"type": "string",
"description": "The specific version string (e.g., '16.8.0')"
}
}提示: get_downloads_prompt
- 说明: 生成对特定时间段内指定npm包的下载计数的请求。
- 输入架构:
{
"packageName": {
"type": "string",
"description": "The name of the npm package"
},
"timePeriod": {
"type": "string",
"enum": ["last-day", "last-week", "last-month"],
"description": "The time period for download counts"
}
}提示: get_all_downloads_prompt
- 说明: 生成对指定npm包在最后一天、一周和一个月的下载计数的请求。
- 输入架构:
{
"packageName": {
"type": "string",
"description": "The name of the npm package"
}
}提示: audit_project_prompt
- 说明: 生成一个请求,以审核指定项目目录中的依赖关系是否存在安全漏洞。
- 输入架构:
{
"projectPath": {
"type": "string",
"description": "The path to the project directory (e.g., '.', '../my-app')"
}
}提示: simulate_audit_fix_prompt
- 说明: 生成模拟运行的请求
npm audit fix在指定的项目目录中。 - 输入架构:
{
"projectPath": {
"type": "string",
"description": "The path to the project directory (e.g., '.', '../my-app')"
}
}6.工具使用和响应示例
以下示例说明了如何调用工具(概念上的、实际的客户端使用情况可能会有所不同)以及预期的 data 成功的MCP的一部分 CallToolResponseMCP SDK处理完整的响应包络(版本、时间戳等)。这 data MCP响应中的字段将包含 content 数组,其中第一个元素是类型为的对象 text 及其 text 属性包含下面显示的结果的JSON字符串。
示例:呼叫 get_npm_package_summary
工具调用参数:
{
"packageName": "express"
}预期的JSON字符串 data.content[0].text:
{
"name": "express",
"latestVersion": "4.19.2",
"description": "Fast, unopinionated, minimalist web framework for node.",
"publishDateLatest": "2024-03-25T14:30:36.103Z",
"license": "MIT",
"homepage": "http://expressjs.com/",
"repository": "https://github.com/expressjs/express",
"source": "https://registry.npmjs.org/express"
}_(注:版本和日期仅为示例,将反映查询时的实际数据)_
示例:呼叫 get_npm_package_versions
工具调用参数:
{
"packageName": "express"
}预期的JSON字符串 data.content[0].text:
{
"versions": {
"1.0.0": "2010-12-29T19:38:25.450Z",
"1.0.1": "2010-12-29T19:38:25.450Z",
"4.19.2": "2024-03-25T14:30:36.103Z"
// ... potentially many more versions
},
"source": "https://registry.npmjs.org/express"
}示例:呼叫 get_npm_package_downloads (所有默认时段)
工具调用参数:
{
"packageName": "express"
}预期的JSON字符串 data.content[0].text:
{
"downloads": {
"last-day": 7895822,
"last-week": 37439130,
"last-month": 162348160
},
"package": "express",
"source": "https://api.npmjs.org/downloads/point"
}_(注:下载计数仅为示例,将反映查询时的实际数据)_
示例:呼叫 get_npm_package_details
工具调用参数:
{
"packageName": "express"
}预期的JSON字符串 data.content[0].text:
{
"name": "express",
"latestVersion": "4.19.2",
"description": "Fast, unopinionated, minimalist web framework for node.",
"publishDateLatest": "2024-03-25T14:30:36.103Z",
"license": "MIT",
"homepage": "https://expressjs.com/",
"repository": "https://github.com/expressjs/express",
"maintainers": [
{ "name": "dougwilson", "email": "doug@somethingdoug.com" },
{ "name": "wesleytodd", "email": "wes@wesleytodd.com" }
// ... other maintainers
],
"keywords": [
"express",
"framework",
"sinatra",
"web",
"rest",
"restful",
"router"
],
"source": "https://registry.npmjs.org/express"
}_(注:版本、日期、维护者和关键字是示例,将反映查询时的实际数据)_
错误处理
如果工具调用失败(例如,找不到包、参数无效),MCP服务器将返回标准的MCP错误响应。这 result.error 此响应中的对象将包含 message 详细说明问题。
MCP错误响应示例(概念结构):
{
"version": "0.2.0", // SDK version
"id": "response-id-string",
"type": "CallToolResponse",
"timestamp": "YYYY-MM-DDTHH:mm:ss.sssZ",
"result": {
"error": {
"type": "ToolError", // Or similar error type from SDK
"message": "Package 'nonexistent-pkg' not found on npmjs."
// Potentially other fields like 'toolName'
}
}
}如果一个必要的论点像 packageName 如果缺少,工具处理程序将抛出错误,导致类似的MCP错误响应。
7.提示用法示例
以下示例显示了如何通过提供参数来使用可用提示。服务器将返回 GetPromptResponse 包含所生成的用户消息。
示例:使用 get_summary_prompt
即时呼叫参数:
{ "packageName": "react" }生成的用户消息(messages[0].content.text):
Get a quick summary of the 'react' npm package.示例:使用 get_version_date_prompt
即时呼叫参数:
{ "packageName": "lodash", "version": "4.17.21" }生成的用户消息(messages[0].content.text):
What was the publish date of version 4.17.21 for 'lodash'?示例:使用 get_downloads_prompt
即时呼叫参数:
{ "packageName": "axios", "timePeriod": "last-week" }生成的用户消息(messages[0].content.text):
How many times was 'axios' downloaded in the last-week?示例:使用 audit_project_prompt
即时呼叫参数:
{ "projectPath": "../my-frontend-app" }生成的用户消息(messages[0].content.text):
Audit the dependencies in the project at '../my-frontend-app' for security vulnerabilities.示例:使用 simulate_audit_fix_prompt
即时呼叫参数:
{ "projectPath": "." }生成的用户消息(messages[0].content.text):
Simulate running 'npm audit fix' on the project at '.' and show me what would change.