Metadata-Version: 2.5
Name: dify-kb-mcp
Version: 0.1.0
Summary: MCP server that exposes Dify knowledge base retrieval as agent tools
Project-URL: Homepage, https://open.workbuddy.cn
Author: B2B增长工坊
License: MIT
License-File: LICENSE
Keywords: dify,knowledge-base,llm,mcp,rag
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.10
Requires-Dist: httpx>=0.27
Requires-Dist: mcp>=1.2.0
Description-Content-Type: text/markdown

# dify-kb-mcp

MCP server that exposes [Dify](https://dify.ai) knowledge base retrieval as agent tools.

Two tools, both read-only:

| Tool | What it does |
|---|---|
| `search_knowledge` | Semantic search across one or more datasets, returns the best passages with scores |
| `list_knowledge_bases` | Lists datasets with document counts and retrieval settings |

## Install

```bash
uvx dify-kb-mcp
# or
pip install dify-kb-mcp
```

## Configure

| Variable | Required | Default | Notes |
|---|---|---|---|
| `DIFY_API_BASE` | no | `http://localhost:5001/v1` | Your Dify API base, e.g. `https://dify.example.com/v1` |
| `DIFY_DATASET_TOKEN` | **yes** | – | Dataset API key from Dify → 知识库 → API |
| `DIFY_RERANK` | no | `0` | `1` enables reranking when the dataset has it configured |
| `DIFY_RERANK_PROVIDER` | no | `huggingface_tei` | Rerank provider name |
| `DIFY_RERANK_MODEL` | no | `bge-reranker-base` | Rerank model name |
| `DIFY_MAX_RESULT_CHARS` | no | `3000` | Result truncation limit |

## MCP client config

```json
{
  "mcpServers": {
    "dify-kb": {
      "command": "uvx",
      "args": ["dify-kb-mcp"],
      "env": {
        "DIFY_API_BASE": "https://dify.example.com/v1",
        "DIFY_DATASET_TOKEN": "dataset-xxxxxxxx"
      }
    }
  }
}
```

## Behaviour notes

- Only datasets with at least one document are searched.
- Each dataset's own retrieval settings (search method, score threshold) are respected; only reranking is overridden by `DIFY_RERANK`.
- If reranking is requested but the rerank service is down, the request is retried once without reranking instead of failing.
- Errors (bad token, unreachable host) are returned as readable text rather than raised, so the agent can explain them to the user.

## License

MIT
