Dify External Knowledge MCP Server
Integrates Dify's external knowledge base API with the Model Context Protocol to enable AI agents to retrieve and query relevant information. It supports relevance scoring, metadata filtering, and flexible configuration through environment variables or command-line arguments.
README
Dify External Knowledge MCP Server
一個 Model Context Protocol (MCP) 伺服器,用於整合 Dify 外部知識庫 API,讓 AI Agent 能夠輕鬆查詢知識庫內容。
安裝
使用 npx (推薦)
npx @funtuantw/dify-external-knowledge-mcp
全域安裝
npm install -g @funtuantw/dify-external-knowledge-mcp
從源碼構建
git clone https://github.com/funtuantw/dify-external-knowledge-mcp.git
cd dify-external-knowledge-mcp
npm install
npm run build
功能特性
- 🔍 查詢 Dify 外部知識庫
- 📊 支援相關性分數閾值設定
- 🎯 支援元數據條件篩選
- 🔐 使用環境變數管理 API 憑證
- 📦 支援 npx 直接運行
- 🌐 知識庫 ID 透過環境變數配置,無需每次指定
配置
{
"mcpServers": {
"dify-knowledge": {
"command": "npx",
"args": ["@funtuantw/dify-external-knowledge-mcp"],
"env": {
"DIFY_KNOWLEDGE_ENDPOINT": "https://your-endpoint.com",
"DIFY_API_KEY": "your-api-key-here",
"DIFY_KNOWLEDGE_ID": "your-knowledge-id-here"
}
}
}
}
或者使用本地路徑(開發時):
{
"mcpServers": {
"dify-knowledge": {
"command": "node",
"args": ["/absolute/path/to/dify-external-knowledge-mcp/dist/index.js"],
"env": {
"DIFY_KNOWLEDGE_ENDPOINT": "https://your-endpoint.com",
"DIFY_API_KEY": "your-api-key-here",
"DIFY_KNOWLEDGE_ID": "your-knowledge-id-here"
}
}
}
}
使用命令列參數:
{
"mcpServers": {
"dify-knowledge": {
"command": "npx",
"args": [
"@funtuantw/dify-external-knowledge-mcp",
"--endpoint",
"https://your-endpoint.com",
"--api-key",
"your-api-key-here",
"--knowledge-id",
"your-knowledge-id-here"
]
}
}
}
使用方法
可用工具
query_dify_knowledge
查詢 Dify 外部知識庫以檢索相關資訊。
參數:
query(必需, string): 用戶的搜尋查詢或問題top_k(可選, number): 返回的最大結果數量(預設:5)score_threshold(可選, number): 最小相關性分數閾值,範圍 0-1(預設:0.5)metadata_condition(可選, object): 元數據篩選條件logical_operator(可選, string): "and" 或 "or"(預設:and)conditions(必需, array): 條件列表name(必需, string[]): 要篩選的元數據欄位名稱comparison_operator(必需, string): 比較運算符(contains, is, not contains, etc.)value(可選, string): 比較值
範例使用(通過 AI Agent):
請使用 query_dify_knowledge 工具查詢 "什麼是 Dify?"
範例輸出:
Found 2 result(s) for query: "什麼是 Dify?"
### Result 1 (Score: 0.980)
**Title**: knowledge.txt
**Content**:
Dify 是一個 LLMOps 平台,提供 GenAI 應用程序的創新引擎。
**Metadata**: {
"path": "s3://dify/knowledge.txt",
"description": "dify 知識文檔"
}
---
### Result 2 (Score: 0.660)
**Title**: introduce.txt
**Content**:
GenAI 應用程序的創新引擎
**Metadata**: {
"path": "s3://dify/introduce.txt",
"description": "dify 介紹"
}
---
開發
開發模式運行
npm run dev
構建專案
npm run build
運行構建版本
npm start
API 參考
本專案使用 Dify 外部知識庫 API。
支援的比較運算符
contains: 包含某個值not contains: 不包含某個值start with: 以某個值開頭end with: 以某個值結尾is: 等於某個值is not: 不等於某個值empty: 為空not empty: 不為空=: 等於≠: 不等於>: 大於<: 小於≥: 大於等於≤: 小於等於before: 在某個日期之前after: 在某個日期之後
錯誤處理
伺服器會處理以下錯誤情況:
- 1001: 無效的 Authorization 頭格式
- 1002: 授權失敗
- 2001: 知識庫不存在
- HTTP 403: 訪問被拒絕
- HTTP 500: 內部伺服器錯誤
專案結構
dify-external-knowledge-mcp/
├── src/
│ └── index.ts # MCP 伺服器主程式
├── dist/ # 編譯後的輸出
├── .env.example # 環境變數範例
├── .gitignore
├── package.json
├── tsconfig.json
├── plan.md # 專案計畫文檔
└── README.md
授權
MIT
貢獻
歡迎提交 Issue 和 Pull Request!
相關連結
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。