模型上下文协议 (MCP) 服务器
模型上下文协议的简单服务器实现,为多个 AI 模型提供者提供统一的 API。
特征
为多个 AI 提供商(Anthropic、OpenAI)提供统一的 API
支持聊天完成和遗留完成
工具调用支持
上下文/系统消息处理
基于环境的配置
MongoDB 数据库用于持久性和状态管理
工具执行历史和分析
Related MCP server: Model Control Plane (MCP) Server
安装
# Clone the repository
git clone <repository-url>
cd testmcp
# Install dependencies
npm install
# Run the interactive setup
npm run setup安装脚本将指导您配置必要的 API 密钥:
ANTHROPIC_API_KEY- 适用于 Claude 模型OPENAI_API_KEY- 用于 GPT 模型和 DALL-E 图像生成STABILITY_API_KEY- 用于稳定扩散图像生成GOOGLE_CSE_API_KEY和GOOGLE_CSE_ID- 用于网络搜索功能BING_SEARCH_API_KEY- 用于后备网络搜索
如果愿意,您也可以手动编辑.env文件。
MongoDB 设置
MCP 服务器使用 MongoDB 进行数据持久化。您可以通过多种方式设置 MongoDB:
选项 1:自动设置(推荐)
运行 MongoDB 安装脚本,它将指导您完成整个过程:
# Run the MongoDB setup script
npm run setup-mongodb该脚本将:
检查 Docker 是否可用
使用 Docker Compose 启动 MongoDB(如果可用)
在 .env 文件中配置连接
验证 MongoDB 连接
选项 2:手动 Docker 设置
开始使用 MongoDB 最简单的方法是使用包含的 Docker Compose 配置:
# Start MongoDB and Mongo Express in Docker
docker compose up -d
# Update your .env file with the connection string
echo "MONGODB_URI=mongodb://mcpuser:mcppassword@localhost:27017/mcp-server" >> .envMongoDB 将在mongodb://mcpuser:mcppassword@localhost:27017/mcp-server可用
Mongo Express(Web 管理员)将在http://localhost:8081上线
选项 3:本地 MongoDB 安装
如果您希望在本地安装 MongoDB:
启动 MongoDB 服务
使用以下命令更新您的
.env文件:MONGODB_URI=mongodb://localhost:27017/mcp-server
选项 4:MongoDB Atlas(云)
对于生产用途,建议使用 MongoDB Atlas:
创建新集群
设置数据库用户并将您的 IP 地址列入白名单
获取连接字符串并更新
.env文件:MONGODB_URI=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/mcp-server?retryWrites=true&w=majority
数据库迁移
要将现有数据迁移到 MongoDB:
# Run the migration script
npm run migrate-mongodb该脚本将:
将工具定义迁移到 MongoDB
将配置(如 API 密钥)迁移到 MongoDB
导入所有备份数据(如果可用)
用法
启动服务器
# Interactive startup (checks for API keys)
npm start
# Development mode with auto-reload
npm run dev
# Quick start (skips environment checks)
npm run quick-start
# Start server with PM2 process manager
npm run pm2:start
# Start server with PM2 in production mode
npm run pm2:start:prod服务器将在http://localhost:3000 (或您在 .env 中指定的端口)运行。
启动选项
标准启动(
npm start):检查是否配置了 API 密钥
如果未找到密钥,则提示设置
建议首次使用者
开发模式(
npm run dev):使用 nodemon 在代码更改时自动重新加载
仍执行环境检查
最适合开发
快速启动(
npm run quick-start):绕过所有环境检查
立即启动服务器
当您知道您的配置正确时很有用
PM2 生产模式(
npm run pm2:start:prod):使用 PM2 进程管理器运行服务器
如果服务器崩溃则自动重启
针对生产环境进行了优化
绕过环境检查
使用 PM2 进程管理器
该服务器可以与 PM2(Node.js 应用程序的生产流程管理器)一起运行。PM2 提供以下功能:
进程管理(崩溃时重启)
日志管理
性能监控
负载平衡(针对多个实例)
PM2 命令
# Start the server with PM2
npm run pm2:start
# Start in production mode
npm run pm2:start:prod
# View logs
npm run pm2:logs
# Monitor performance
npm run pm2:monit
# Restart the server
npm run pm2:restart
# Stop the server
npm run pm2:stop
# Remove the server from PM2
npm run pm2:deletePM2 配置存储在ecosystem.config.js中。您可以修改此文件以进行以下更改:
进程名称
环境变量
内存限制
部署配置
实例数量(用于负载平衡)
API 端点
POST /mcp/:provider
通过统一的API向AI模型发出请求。
URL 参数:
provider:要使用的 AI 提供者(anthropic或openai)
请求正文:
{
"messages": [
{ "role": "user", "content": "Your prompt here" }
],
"model": "claude-3-opus-20240229", // Optional, provider-specific model name
"tools": [...], // Optional, tools for function calling
"context": "System message or context" // Optional
}或(旧格式):
{
"prompt": "Your prompt here",
"model": "claude-3-opus-20240229", // Optional
"context": "System message or context" // Optional
}**响应:**返回来自提供商 API 的原始响应。
GET /tools/available
获取包含所有可用工具详细信息的综合列表。
查询参数:
format- 响应格式:json(默认)、yaml、table或htmlcategory- 按类别过滤工具(可选)enabled- 按启用状态过滤:true(默认)或falsesearch- 按名称、描述或标签搜索工具provider-按提供商过滤工具(例如openai、google)limit- 返回的最大工具数量(用于分页)offset- 分页偏移量(默认值:0)
响应(JSON格式):
{
"success": true,
"count": 10,
"metadata": {
"categories": ["web", "image", "utility"],
"providers": ["openai", "anthropic", "internal"],
"totalCount": 24,
"offset": 0,
"limit": 10
},
"tools": [
{
"name": "web_search",
"description": "Search the web for information",
"category": "web",
"version": "1.0.0",
"provider": "google",
"enabled": true,
"parameters": {
"query": {
"type": "string",
"description": "The search query",
"required": true
},
"limit": {
"type": "number",
"description": "Maximum number of results",
"required": false,
"default": 5
}
},
"usage": {
"endpoint": "/tools/web/search",
"method": "POST",
"parameters": { /* same as above */ }
},
"metadata": {
"createdAt": "2023-10-15T12:00:00Z",
"updatedAt": "2024-04-20T09:30:00Z",
"usageCount": 1245
}
}
// ... more tools
]
}GET /health
如果服务器正在运行,则健康检查端点返回状态 200。
数据管理
数据库备份
您可以创建和管理数据库备份:
# Create a full backup
npm run backup-mongodb
# Create a backup with execution history
npm run backup-mongodb -- --with-executions
# List existing backups
npm run backup-mongodb -- --list测试数据库连接
要验证您的 MongoDB 设置:
# Run the database test script
npm run test-mongodb示例客户端
命令行客户端
src/client.js中包含一个测试客户端。运行方法如下:
node src/client.jsWeb 客户端
服务器运行时,http://localhost:3000处有一个简单的 Web 界面。您可以使用它直接从浏览器测试 API。
可用工具
MCP 服务器提供了一个工具发现端点,允许用户和 AI 代理以编程方式列出所有可用的工具:
工具发现
GET /tools/available - 列出所有可用工具及其详细信息。
支持多种格式:JSON、YAML、HTML 和 ASCII 表
提供按类别、提供商和搜索词进行过滤的功能
包括每个工具的详细元数据和使用示例
使用示例:
# Get all tools in JSON format
curl http://localhost:3000/tools/available
# Get tools in a specific category
curl http://localhost:3000/tools/available?category=web
# Search for image-related tools
curl http://localhost:3000/tools/available?search=image
# Get a formatted HTML page of all tools
curl http://localhost:3000/tools/available?format=html > tools.html网络搜索工具
该服务器包括内置的网络搜索和检索工具:
网页搜索(
/tools/web/search)在网络上搜索有关给定查询的信息
参数:
query(必需)、limit(可选)需要:
GOOGLE_CSE_API_KEY和GOOGLE_CSE_ID环境变量如果 Google 搜索失败,则返回
BING_SEARCH_API_KEY
网页内容(
/tools/web/content)从特定 URL 检索并提取内容
参数:
url(必需)、useCache(可选)
Web 批处理(
/tools/web/batch)并行从多个 URL 检索内容
参数:
urls(必需数组)、useCache(可选)
图像生成工具
该服务器还包括图像生成、编辑和变化工具:
生成图像(
/tools/image/generate)根据文本提示生成图像
参数:
prompt(必填):图片的详细描述provider(可选):openai或stability(默认为openai)options(可选):特定于提供商的选项
编辑图像(
/tools/image/edit)使用文本提示编辑现有图像
参数:
imagePath(必需):要编辑的图像的路径prompt(必需):要进行的编辑的描述maskPath(可选):遮罩图像的路径
创建图像变体(
/tools/image/variation)创建现有图像的变体
参数:
imagePath(必需):用于创建变体的图像路径
**注意:**要使用这些工具,您需要在
.env文件中设置 API 密钥:
对于 OpenAI 图像:
OPENAI_API_KEY对于稳定性 AI 图像:
STABILITY_API_KEY对于网络搜索:
GOOGLE_CSE_API_KEY和GOOGLE_CSE_ID
工具与 AI 模型的集成
MCP 服务器会自动处理 AI 模型的工具调用和执行。当模型决定使用某个工具时,服务器会执行以下操作:
使用提供的参数执行请求的工具
返回工具对模型的响应
然后,模型可以将工具的响应纳入其最终答案中
人工智能模型的工具发现
AI 模型可以使用/tools/available端点来发现可用的工具以及如何使用它们。这在以下情况下尤其有用:
运行时动态工具发现
人工智能代理的自我文档
使人工智能系统能够适应可用的能力
AI 模型的系统提示示例:
You have access to external tools through the MCP server.
Before using any tools, you should check what tools are available by calling:
GET /tools/available
This will return a list of all available tools with their parameters and usage instructions.
You can then use these tools by following the provided usage patterns.工具使用示例
请参阅/examples目录中演示工具用法的示例代码。
添加新的提供程序或工具
添加新的AI提供商
要添加新的 AI 提供程序:
将提供商的 SDK 添加到项目
在
server.js中创建新的处理函数在主路由处理程序中添加新案例
添加新工具
要向服务器添加新工具:
在
/src/tools目录中创建一个新的工具实现将工具定义添加到
tool-definitions.js更新
server.js中的工具执行函数添加新的 API 端点以供直接使用工具(如果需要)
执照
国际学习中心
This server cannot be installed
Resources
Looking for Admin?
Admins can modify the Dockerfile, update the server description, and track usage metrics. If you are the server author, to access the admin panel.