Content
# Dify Knowledge MCP Server
This is a server based on the Model Context Protocol (MCP) for accessing the Dify knowledge base. It allows AI assistants (such as Cursor) to directly query your Dify knowledge base content.
<a href="https://glama.ai/mcp/servers/@liiklin/dify-knowledge-mcp-server">
<img width="380" height="200" src="https://glama.ai/mcp/servers/@liiklin/dify-knowledge-mcp-server/badge" alt="Dify Knowledge Server MCP server" />
</a>
## Features
- 🔍 Query Dify knowledge base content
- 📊 Support multiple search methods (semantic search, full-text search, hybrid search)
- 🎯 Support re-ranking results
- ⚙️ Flexible configuration options
- 🛠️ Standard MCP protocol support
## Security Warning
⚠️ **Important: Protect Your API Key**
- Never hard-code API keys in your code
- Do not commit `.env` files containing sensitive information to version control
- Use `.env` files or environment variables to store sensitive configurations
- Regularly update your API keys
## Quick Start
### 1. Install Dependencies
```bash
npm install
```
### 2. Configure API Information
You need to provide two important pieces of information:
- **Dataset ID**: Your Dify knowledge base ID
- **API Key**: Your Dify API key
### 3. Configure API Key
#### Method 1: Using `.env` File (Recommended)
```bash
# Copy configuration template
cp env.example .env
# Edit `.env` file and fill in your actual configuration
# DIFY_API_URL=https://api.dify.ai/v1/datasets/YOUR_DATASET_ID/documents/query
# DIFY_API_KEY=your_actual_api_key
```
#### Method 2: Environment Variables
```bash
# Linux/Mac
export DIFY_API_URL="https://api.dify.ai/v1/datasets/YOUR_DATASET_ID/documents/query"
export DIFY_API_KEY="your_actual_api_key"
```
```cmd
# Windows
set DIFY_API_URL=https://api.dify.ai/v1/datasets/YOUR_DATASET_ID/documents/query
set DIFY_API_KEY=your_actual_api_key
```
#### Method 3: Command Line Arguments
```bash
node index.js --api-url="https://api.dify.ai/v1/datasets/YOUR_DATASET_ID/documents/query" --api-key="your_actual_api_key"
```
### 4. Run the Server
```bash
node index.js
```
## Using in Cursor
### 1. Configure MCP Server
In Cursor, you need to configure the MCP server. Create or edit your MCP configuration file:
```json
{
"mcpServers": {
"dify-knowledge": {
"command": "node",
"args": [
"/path/to/your/dify-knowledge-mcp-server/index.js"
],
"env": {
"DIFY_API_URL": "https://api.dify.ai/v1/datasets/YOUR_DATASET_ID/documents/query",
"DIFY_API_KEY": "your_actual_api_key"
}
}
}
}
```
**Note:** Replace placeholders with your actual values:
- `YOUR_DATASET_ID`: Your Dify knowledge base ID
- `your_actual_api_key`: Your Dify API key
- `/path/to/your/dify-knowledge-mcp-server/index.js`: Your project path
### 2. Available Tool Functions
After configuration, Cursor can use the following tools:
#### `query_dify_knowledge`
Query Dify knowledge base content
**Parameters:**
- `query` (required): Search query
- `top_k` (optional): Number of results to return, default is 3
- `search_method` (optional): Search method, available values:
- `semantic_search`: Semantic search
- `full_text_search`: Full-text search
- `hybrid_search`: Hybrid search (default)
- `reranking_enable` (optional): Enable re-ranking, default is true
**Example:**
```
Please query related content about "product introduction"
```
#### `get_dify_config`
Check current Dify API configuration status
## Usage Examples
### Conversation Example in Cursor
```
User: Please help me query the content about "user manual" in the knowledge base
AI Assistant: I'll query the Dify knowledge base for you about "user manual"...
[Call `query_dify_knowledge` tool]
Found 3 relevant knowledge entries:
1. (Score: 0.95)
User manual chapter 1 introduces the basic functions and usage methods of the product...
2. (Score: 0.87)
User manual chapter 2 details the configuration steps for advanced functions...
3. (Score: 0.82)
User manual FAQ section provides solutions to problems...
```
## Configuration Instructions
### API URL Format
```
https://api.dify.ai/v1/datasets/{dataset_id}/documents/query
```
### Supported Search Methods
- **semantic_search**: Semantic search
- **full_text_search**: Traditional full-text search
- **hybrid_search**: Hybrid mode combining semantic and full-text search
### Re-ranking
Enabling re-ranking can improve the relevance of search results but may increase response time.
## Troubleshooting
### Common Issues
1. **"Please configure DIFY_API_URL and DIFY_API_KEY"**
- Ensure API URL and API Key are correctly configured
- Check if Dataset ID is correct
2. **"Failed to retrieve knowledge from Dify"**
- Check if API Key is valid
- Confirm if Dataset ID exists
- Verify network connection
3. **Connection timeout**
- Check network connection
- Confirm if Dify API service is accessible
### Test Configuration
Run the following command to test configuration:
```bash
# Test configuration status
node index.js
```
If you see the "Ready to query knowledge base!" message, it means the configuration is correct.
### Clean Up Sensitive Information
If you have exposed your API key in a configuration file before, please:
1. **Update Cursor configuration file** (usually in `~/.cursor/mcp.json` or similar path)
2. **Delete or replace exposed API key**
3. **Check version control history** to ensure no sensitive information is committed
4. **Use new configuration method** via `.env` file or environment variables
## Development Information
- **Protocol**: Model Context Protocol (MCP)
- **Transport Method**: Standard I/O
- **API Version**: Dify API v1
- **Node.js Version**: Supports ES module versions
## License
MIT License
Connection Info
You Might Also Like
markitdown
Python tool for converting files and office documents to Markdown.
markitdown
MarkItDown-MCP is a lightweight server for converting URIs to Markdown.
Filesystem
Node.js MCP Server for filesystem operations with dynamic access control.
TrendRadar
TrendRadar: Your hotspot assistant for real news in just 30 seconds.
mempalace
The highest-scoring AI memory system ever benchmarked. And it's free.
mempalace
The highest-scoring AI memory system ever benchmarked. And it's free.