DMCP-动态模型上下文协议
MCP的语义工具发现 -通过使工具发现查询由向量搜索驱动,解决了“工具太多”的问题。
🎯 问题
当您聚合20+MCP服务器(约300+个工具)时:
- 代币爆炸:100000多个代币只是上市工具
- LLM混淆:选择太多=工具选择不当
- 不过滤:标准MCP提前返回所有工具
✨ 解决方案
DMCP使用 语义向量搜索 按需发现工具:
User: "Create a GitHub issue for this bug"
LLM calls: search_tools(query="create GitHub issue")
→ Returns top-15 relevant tools (via semantic vector search)
→ Tools become available for use
LLM calls: github_create_issue(...)
→ Issue created!关键洞察LLM通过以下方式发现工具 询问,而不是预先加载所有内容。
✨ 特性
- 🔍 语义搜索:ToolRet训练的E5模型,用于精确的工具检索
- ⚡ 快速:搜索延迟约为50毫秒,令牌减少98%
- 🔄 连接弹性:自动重试、健康检查、重新连接
- 🐳 Docker就绪:全栈Redis VSS+嵌入服务
- 📊 可观察对象:健康端点、会话日志记录、连接状态
- ✅ 已测试:使用vitest进行56个单元测试
🏗️ 建筑
┌─────────────────────────────────────────────────────────────────────────────┐
│ VS Code / GitHub Copilot │
│ │
│ User: "search for kubernetes tools" │
│ → search_tools("kubernetes") │
│ ← Returns: 15 k8s tools (get_pods, list_deployments, ...) │
└─────────────────────────────────┬───────────────────────────────────────────┘
│ HTTP (Streamable HTTP Transport)
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ DMCP Server (port 3001) │
│ │
│ • Exposes 1 meta-tool: search_tools │
│ • Pure vector search (COSINE similarity, HNSW index) │
│ • Sends listChanged notifications when tools discovered │
│ • Connection keep-alive with health checks (30s interval) │
│ • Auto-retry on connection failures (3 attempts, exponential backoff) │
└────────────┬──────────────────────────────────────────────────┬─────────────┘
│ │
│ Query embeddings │ Tool calls (SSE)
▼ ▼
┌────────────────────────────────┐ ┌────────────────────────────┐
│ Redis Stack (port 6380) │ │ Backend MCP Servers │
│ │ │ (via Agent Gateway) │
│ • Vector Index (HNSW) │ │ │
│ • COSINE similarity │ │ • GitHub, Jira, Confluence│
│ • 400+ tools indexed │ │ • Google Workspace, Notion│
└────────────────────────────────┘ │ • Kubernetes, AWS, Azure │
▲ │ • And more... │
│ └────────────────────────────┘
┌────────────────────────────────┐
│ Infinity Embedding Service │
│ (port 5000) │
│ │
│ • ToolRet e5-large-v2 model │
│ • 1024 dimensions │
│ • OpenAI-compatible API │
└────────────────────────────────┘🚀 快速开始
先决条件
- Docker&Docker编写
- MCP服务器网关暴露您的工具(例如。, 代理网关)
1.克隆并启动
git clone https://github.com/yourusername/dmcp.git
cd dmcp
# Start everything: Redis, Embedding Service, and DMCP Server
docker compose up -d
# Wait for services to be healthy (~2-3 minutes for embedding model to load)
docker compose ps2.为您的工具建立索引
# Run the indexer to populate Redis with tools from your MCP gateway
docker compose run --rm indexer
# Verify tools are indexed
curl http://localhost:3001/health
# → {"status":"healthy","toolCount":420,...}3.配置VS代码
添加到您的 .vscode/mcp.json:
{
"servers": {
"dmcp": {
"type": "http",
"url": "http://localhost:3001/mcp"
}
}
}就是这样! 这 search_tools 元工具现在可以在VS Code/GitHub Copilot中使用。
📁 项目结构
dmcp/
├── docker-compose.yml # Full stack configuration
├── server/ # DMCP Server (TypeScript)
│ ├── src/
│ │ ├── dmcp-server.ts # Main server with connection management
│ │ ├── redis-vss.ts # Redis vector similarity search
│ │ ├── custom-embedding-provider.ts
│ │ ├── dmcp-server.test.ts # Unit tests (28 tests)
│ │ └── redis-vss.test.ts # Unit tests (28 tests)
│ ├── vitest.config.ts
│ └── package.json
├── indexer/ # Tool Indexer (TypeScript)
│ └── src/
│ └── index.ts # CLI indexer with parallel discovery
└── README.md🐳 Docker命令
# Start full stack
docker compose up -d
# View logs
docker compose logs -f dmcp-server
# Run one-shot indexing
docker compose run --rm indexer
# Start continuous sync worker
docker compose --profile worker up -d
# Rebuild after code changes
docker compose build dmcp-server && docker compose up -d dmcp-server
# Stop everything
docker compose down
# Stop and delete indexed data
docker compose down -v⚙️ 配置
环境变量
| 变量 | 默认值 | 描述 |
|---|---|---|
PORT | 3000 | 服务器端口(容器内) |
REDIS_HOST | localhost | Redis服务器主机 |
REDIS_PORT | 6379 | Redis服务器端口 |
EMBEDDING_URL | http://localhost:5000 | 嵌入服务URL |
DMCP_TOP_K | 15 | 每次搜索返回的最大工具数 |
DMCP_MIN_SCORE | 0.3 | 最小相似性阈值 |
连接弹性
| 变量 | 默认值 | 描述 |
|---|---|---|
DMCP_RETRY_ATTEMPTS | 3 | 最大连接重试次数 |
DMCP_RETRY_DELAY_MS | 1000 | 重试之间的基本延迟(指数回退) |
DMCP_HEALTH_INTERVAL_MS | 30000 | 后端连接的健康检查间隔 |
DMCP_CONNECTION_TIMEOUT_MS | 10000 | 连接超时 |
Docker编写服务
| 服务 | 容器 | 主机端口 | 描述 |
|---|---|---|---|
redis-vss | mcp-redis-vss | 6380 | 带向量搜索的redis堆栈 |
embedding-service | mcp嵌入无限 | 5000 | 无限嵌入服务 |
dmcp-server | dmcp服务器 | 3001 | dmcp MCP服务器 |
indexer | dmcp索引器 | - | 一次性索引器 |
indexer-worker | dmcp索引器工作器 | - | 连续同步(可选) |
🔍 搜索工作原理
DMCP使用 纯矢量搜索 使用ToolRet嵌入模型:
- 使用ToolRet训练的E5-large-v2嵌入查询
- Redis执行HNSW最近邻搜索
- 返回按COSINE相似性排序的Top-K结果
- 工具可通过以下方式获得
notifications/tools/list_changed
示例查询:
| 查询 | 查找 |
|---|---|
"create GitHub issue" | GitHub工具 |
"ticket management" | Jira工具 |
"check pod logs" | Kubernetes工具 |
"search emails" | 谷歌工作区 |
"query Notion database" | 概念工具 |
🏥 健康与监测
健康端点
curl http://localhost:3001/health答复:
{
"status": "healthy",
"toolCount": 440,
"activeSessions": 2,
"backendConnections": {
"total": 5,
"healthy": 5,
"details": [
{"serverId": "github", "healthy": true, "lastCheck": 1704700000000},
{"serverId": "serena", "healthy": true, "lastCheck": 1704700000000}
]
},
"config": {
"retryAttempts": 3,
"retryDelayMs": 1000,
"healthIntervalMs": 30000,
"connectionTimeoutMs": 10000
},
"uptime": 3600
}服务器日志
docker compose logs -f dmcp-server
# Example output:
# 16:38:36 [DMCP] 🚀 Server listening on http://0.0.0.0:3000
# 16:38:36 [DMCP] ✓ Found 440 indexed tools
# 16:38:52 [DMCP] POST /mcp [initialize]
# 16:38:52 [DMCP] 📡 New connection request (session #1)
# 16:39:01 [DMCP] 🔍 Search: "kubernetes pods" (limit: 15)
# 16:39:01 [DMCP] ✓ Found 12 tools in 45ms🧪 测试
cd server
# Run tests
npm test
# Run with watch mode
npm run test:watch
# Run with coverage
npm run test:coverage测试覆盖率56项测试涵盖:
- 工具名称净化和解析
- 连接重试和健康检查逻辑
- Redis矢量搜索操作
- 嵌入操作
🔧 本地开发
# Start only infrastructure
docker compose up -d redis-vss embedding-service
# Run server locally
cd server
npm install
REDIS_PORT=6380 EMBEDDING_URL=http://localhost:5000 npm run start
# Run tests
npm test➕ 添加MCP服务器
DMCP适用于任何MCP服务器。示例配置如下 gateway/config_parts/.
MCP服务器概念
MCP概念 提供搜索、阅读和创建Notion页面/数据库的工具。
- 在以下位置创建Notion集成 notion.so/my集成
- 添加到网关配置(
gateway/config_parts/70-notion.yaml):
targets:
- name: notion
type: mcp
target_config:
command: npx
args:
- -y
- "@notionhq/notion-mcp-server"
env:
OPENAPI_MCP_HEADERS: '{"Authorization": "Bearer ${NOTION_API_KEY}", "Notion-Version": "2022-06-28"}'- 设置API密钥:
export NOTION_API_KEY=ntn_... - 重新启动网关并重新索引:
docker compose run --rm indexer
其他流行的MCP服务器
| 服务器 | 包 | 文档 |
|---|---|---|
| GitHub | @modelcontextprotocol/server-github | |
| 松弛 | @modelcontextprotocol/server-slack | 松弛MCP |
| 谷歌云端硬盘 | @modelcontextprotocol/server-gdrive | GDrive MCP |
| PostgreSQL | @modelcontextprotocol/server-postgres | Postgres MCP |
📊 演出
| 度量 | 值 |
|---|---|
| 工具索引 | 440 |
| 索引时间 | ~45秒 |
| 搜索延迟 | ~50ms |
| 代币减少 | 98% |
| 嵌入模型 | ToolRet-e5-large-v2(1024 dims) |
📐 MCP规范合规性
- ✅
listChanged: true能力 - ✅
notifications/tools/list_changed通知 - ✅ 基于搜索的动态工具可用性
- ✅ 流式HTTP传输(POST/GET/DELETE)
- ✅ 使用UUID会话ID的会话管理
- ✅ SSE用于异步通知
🔬 研究基金会
实施基于 “检索模型不懂工具” (ACL 2025):
关键洞察:通用IR模型在工具检索方面表现不佳;针对特定工具的培训至关重要。
🎬 灵感
- 📺 MCP工具过载问题 油管
- 📝 Redis博客:从推理到检索
📄 许可证
麻省理工学院
