feat: initialize MCP RAG Prompts server with embedding management
- Add package.json for project configuration and dependencies. - Create src/index.ts as the entry point for the MCP server. - Implement vectorStore for managing embeddings with local and cloud providers. - Add embeddingProviders for local and cloud-based embedding services (OpenAI, Aliyun, SiliconFlow). - Define types for prompts and embeddings in types.ts. - Implement searchPersona tool for semantic search of expert personas. - Create test.ts for validating vector storage and search functionality. - Configure TypeScript with tsconfig.json for strict type checking and module resolution.
This commit is contained in:
187
README.md
Normal file
187
README.md
Normal file
@@ -0,0 +1,187 @@
|
||||
# MCP RAG Prompts Server
|
||||
|
||||
基于 MCP 协议的 RAG 提示词管理服务器,支持本地模型和云服务 API 进行语义搜索。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- 🔍 **语义搜索**:Hybrid Search(向量语义 + 关键词加权)
|
||||
- 🔄 **多提供者支持**:本地模型 / OpenAI / 阿里云百炼 / SiliconFlow
|
||||
- 🚀 **MCP 协议**:标准 MCP Server 实现,可与任何 MCP 客户端集成
|
||||
- 💾 **内存缓存**:启动时一次性向量化,搜索速度快
|
||||
|
||||
## 项目结构
|
||||
|
||||
```
|
||||
mcp-rag-prompts/
|
||||
├── package.json
|
||||
├── tsconfig.json
|
||||
├── .env.example # 环境变量配置模板
|
||||
├── data/
|
||||
│ └── prompts.json # Prompt 数据文件
|
||||
└── src/
|
||||
├── index.ts # MCP Server 入口
|
||||
├── test.ts # 测试脚本
|
||||
└── lib/
|
||||
├── types.ts # 类型定义
|
||||
├── embeddingProviders.ts # Embedding 提供者实现
|
||||
└── vectorStore.ts # 向量存储和搜索
|
||||
```
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
## Embedding 提供者配置
|
||||
|
||||
通过环境变量选择 Embedding 提供者:
|
||||
|
||||
### 方式 1:本地模型(默认)
|
||||
|
||||
无需配置,开箱即用。首次运行会自动下载模型(约 90MB)。
|
||||
|
||||
```bash
|
||||
# 默认配置,无需设置
|
||||
npm start
|
||||
|
||||
# 或显式指定
|
||||
EMBEDDING_PROVIDER=local npm start
|
||||
```
|
||||
|
||||
### 方式 2:OpenAI API
|
||||
|
||||
```bash
|
||||
# Windows PowerShell
|
||||
$env:EMBEDDING_PROVIDER="openai"
|
||||
$env:OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxx"
|
||||
npm start
|
||||
|
||||
# Linux/macOS
|
||||
EMBEDDING_PROVIDER=openai \
|
||||
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx \
|
||||
npm start
|
||||
```
|
||||
|
||||
可选配置:
|
||||
- `OPENAI_BASE_URL` - 自定义 API 地址(支持代理)
|
||||
- `OPENAI_EMBEDDING_MODEL` - 模型名称(默认 `text-embedding-3-small`)
|
||||
|
||||
### 方式 3:阿里云百炼 DashScope
|
||||
|
||||
```bash
|
||||
$env:EMBEDDING_PROVIDER="aliyun"
|
||||
$env:DASHSCOPE_API_KEY="sk-xxxxxxxxxxxxxxxx"
|
||||
npm start
|
||||
```
|
||||
|
||||
可选配置:
|
||||
- `ALIYUN_EMBEDDING_MODEL` - 模型名称(默认 `text-embedding-v3`)
|
||||
|
||||
### 方式 4:SiliconFlow 硅基流动
|
||||
|
||||
```bash
|
||||
$env:EMBEDDING_PROVIDER="siliconflow"
|
||||
$env:SILICONFLOW_API_KEY="sk-xxxxxxxxxxxxxxxx"
|
||||
npm start
|
||||
```
|
||||
|
||||
可选配置:
|
||||
- `SILICONFLOW_EMBEDDING_MODEL` - 模型名称(默认 `BAAI/bge-m3`)
|
||||
|
||||
## 运行
|
||||
|
||||
```bash
|
||||
# 启动 MCP Server
|
||||
npm start
|
||||
|
||||
# 开发模式(热重载)
|
||||
npm run dev
|
||||
|
||||
# 运行测试
|
||||
npx tsx src/test.ts
|
||||
```
|
||||
|
||||
## 本地模型网络问题
|
||||
|
||||
首次运行本地模型时需要从 HuggingFace 下载。如果遇到网络问题:
|
||||
|
||||
```bash
|
||||
# 设置代理
|
||||
$env:HTTPS_PROXY="http://127.0.0.1:7890"
|
||||
|
||||
# 或使用 HuggingFace 镜像(中国大陆)
|
||||
$env:HF_ENDPOINT="https://hf-mirror.com"
|
||||
```
|
||||
|
||||
## MCP Tool
|
||||
|
||||
### `search_expert_persona`
|
||||
|
||||
根据用户问题语义搜索最匹配的专家角色设定。
|
||||
|
||||
**参数**:
|
||||
- `query` (string): 用户的原始问题或需求描述
|
||||
|
||||
**返回示例**:
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"matchedExpert": {
|
||||
"id": "python-expert",
|
||||
"tags": ["python", "programming"],
|
||||
"description": "Python 编程专家...",
|
||||
"similarity": 0.75
|
||||
},
|
||||
"systemPrompt": "你是一位资深的 Python 编程专家..."
|
||||
}
|
||||
```
|
||||
|
||||
## 配置 MCP 客户端
|
||||
|
||||
### Claude Desktop
|
||||
|
||||
在 `claude_desktop_config.json` 中添加:
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"rag-prompts": {
|
||||
"command": "npx",
|
||||
"args": ["tsx", "C:/path/to/mcp-rag-prompts/src/index.ts"],
|
||||
"env": {
|
||||
"EMBEDDING_PROVIDER": "siliconflow",
|
||||
"SILICONFLOW_API_KEY": "sk-xxxxxxxx"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 自定义 Prompt 数据
|
||||
|
||||
编辑 `data/prompts.json` 添加你自己的专家角色:
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "unique-id",
|
||||
"tags": ["tag1", "tag2"],
|
||||
"description": "用于语义搜索的描述文本(会被向量化)",
|
||||
"content": "实际的 System Prompt 内容"
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
## 提供者对比
|
||||
|
||||
| 提供者 | 优点 | 缺点 |
|
||||
|--------|------|------|
|
||||
| **local** | 免费、离线可用、隐私安全 | 首次加载慢、需下载模型 |
|
||||
| **openai** | 效果好、稳定 | 需要付费、需要网络 |
|
||||
| **aliyun** | 中文效果好、国内访问快 | 需要付费 |
|
||||
| **siliconflow** | 性价比高、支持多种开源模型 | 需要付费 |
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
Reference in New Issue
Block a user