GDPR MCP服务器
一个本地模型上下文协议(MCP)服务器,用于使用混合三元组和向量搜索来搜索GDPR文档。
目录
先决条件
- 转到1.21+ - 下载Go
- GNU 编译器套件 -SQLite(CGO)所需
- macOS: xcode-select --install - Ubuntu/Debian: sudo apt install build-essential - Fedora: sudo dnf install gcc
快速开始
步骤1:克隆和构建
# Clone the repository
git clone https://github.com/jc/gdpr-mcp.git
cd gdpr-mcp
# Build the binary
go build -o gdpr-mcp ./cmd/gdpr-mcp
# (Optional) Install to your PATH
sudo cp gdpr-mcp /usr/local/bin/第二步:获取GDPR文本
下载完整的GDPR法规文本:
# Option A: Download from EUR-Lex (official source)
curl -o gdpr.txt "https://eur-lex.europa.eu/legal-content/EN/TXT/?uri=CELEX:32016R0679"
# Option B: Create a sample file for testing
cat > gdpr.txt “在GDPR中搜索有关删除权的信息”
克劳德将使用 `gdpr_search` 工具和返回相关的GDPR部分。
______________________________________________________________________
### Ollama设置
Ollama没有原生MCP支持,但您可以使用 **打开WebUI** 或a **自定义桥接脚本**.
#### 选项A:使用Open WebUI(推荐)
[打开WebUI](https://github.com/open-webui/open-webui) 是Ollama的一个功能丰富的web界面,支持MCP。
##### 步骤1:安装Open WebUI
Using Docker (recommended)
docker run -d -p 3000:8080 \ --add-host=host.docker.internal:host-gateway \ -v open-webui:/app/backend/data \ --name open-webui \ --restart always \ ghcr.io/open-webui/open-webui:main
Or using pip
pip install open-webui open-webui serve
##### 步骤2:在Open WebUI中配置MCP
1. 打开http://localhost:3000在浏览器中
1. 首选 **设置** → **工具** → **MCP服务器**
1. 添加新的MCP服务器:
- **名字**: `gdpr`
- **命令**: `/usr/local/bin/gdpr-mcp`
- **参数**: `start`
1. 保存并重新启动Open WebUI
##### 步骤3:与Olama模型一起使用
1. 选择Ollama模型(例如。, `llama3.2`, `mistral`)
1. GDPR工具将可用于该模型
1. 问:“在GDPR中搜索数据可移植性权利”
______________________________________________________________________
#### 选项B:使用MCP CLI进行测试
您可以在没有UI的情况下直接测试MCP服务器:
##### 步骤1:安装mcp-cli
npm install -g @anthropic/mcp-cli
##### 步骤2:创建MCP配置
创建 `~/.mcp/config.json`:
{ "servers": { "gdpr": { "command": "/usr/local/bin/gdpr-mcp", "args": ["start"] } } }
##### 步骤3:测试服务器
List available tools
mcp-cli tools list --server gdpr
Call a tool
mcp-cli tools call --server gdpr --tool gdpr_search --args '{"query": "right of access"}'
______________________________________________________________________
#### 选项C:自定义Python桥
要使用Ollama进行编程访问,请创建一个桥接脚本:
#!/usr/bin/env python3 """Bridge between Ollama and GDPR MCP server."""
import json import subprocess import sys
def call_mcp_tool(tool_name: str, arguments: dict) -> str: """Call an MCP tool and return the result.""" # Start the MCP server proc = subprocess.Popen( ["/usr/local/bin/gdpr-mcp", "start"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True )
# Initialize init_req = { "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "python-bridge", "version": "1.0"} } } proc.stdin.write(json.dumps(init_req) + "\n") proc.stdin.flush() proc.stdout.readline() # Read init response
# Send initialized notification proc.stdin.write(json.dumps({"jsonrpc": "2.0", "method": "initialized"}) + "\n") proc.stdin.flush()
# Call the tool tool_req = { "jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": { "name": tool_name, "arguments": arguments } } proc.stdin.write(json.dumps(tool_req) + "\n") proc.stdin.flush()
# Read response response = json.loads(proc.stdout.readline()) proc.terminate()
if "result" in response: return response["result"]["content"][0]["text"] return json.dumps(response.get("error", {}))
def search_gdpr(query: str, limit: int = 5) -> str: """Search GDPR documents.""" return call_mcp_tool("gdpr_search", {"query": query, "limit": limit})
def get_gdpr_chunk(chunk_id: int) -> str: """Get a specific GDPR chunk by ID.""" return call_mcp_tool("gdpr_get", {"id": chunk_id})
if __name__ == "__main__": # Example usage results = search_gdpr("right to be forgotten") print(results)
将此桥与Ollama的Python库一起使用:
import ollama from gdpr_bridge import search_gdpr
Search GDPR first
gdpr_context = search_gdpr("data portability")
Use context with Ollama
response = ollama.chat( model="llama3.2", messages=[{ "role": "user", "content": f"Based on this GDPR context:\n{gdpr_context}\n\nExplain data portability rights." }] ) print(response["message"]["content"])
______________________________________________________________________
## CLI命令
|命令|描述|
|---------|-------------|
| `gdpr-mcp ingest ` |将GDPR文本导入数据库|
| `gdpr-mcp start` |启动MCP服务器(stdio模式)|
| `gdpr-mcp stop` |停止正在运行的服务器|
| `gdpr-mcp status` |检查服务器和数据库状态|
| `gdpr-mcp version` |显示版本|
| `gdpr-mcp help` |显示帮助|
## 环境变量
|变量|描述|默认值|
|----------|-------------|---------|
| `GDPR_MCP_DB` |自定义数据库路径| `~/.local/share/gdpr-mcp/gdpr.db` |
| `OPENAI_API_KEY` |用于更好嵌入的OpenAI API密钥| _(无)_ |
| `GDPR_MCP_OPENAI` |设置为 `1` 启用OpenAI| _(残疾)_ |
## 使用OpenAI嵌入(可选)
为了更好的语义搜索,使用OpenAI嵌入而不是本地存根:
Set your API key
export OPENAI_API_KEY="sk-..." export GDPR_MCP_OPENAI=1
Re-ingest with real embeddings
./gdpr-mcp ingest gdpr.txt
## MCP工具参考
### gdpr_search
使用混合搜索(三元组+向量相似性)搜索GDPR文档。
**参数:**
- `query` (字符串,必填):搜索查询
- `limit` (整数,可选):最大结果(默认值:10)
**例子:**
{"name": "gdpr_search", "arguments": {"query": "right to be forgotten", "limit": 5}}
### gdpr_get
按ID检索完整文档块。
**参数:**
- `id` (整数,必填):文档块ID
**例子:**
{"name": "gdpr_get", "arguments": {"id": 17}}
## 运作原理
1. **摄入**:GDPR文本被拆分为约1000个字符块,重叠100个字符
1. **三角指数**:块使用3个字符序列进行索引
1. **矢量嵌入**:块被转换为向量(OpenAI或本地存根)
1. **混合搜索**:查询使用这两种方法,并结合往复式排名融合
## 故障排除
### “找不到数据库”错误
首先运行摄取命令:
./gdpr-mcp ingest gdpr.txt
### “服务器已在运行”错误
停止现有服务器:
./gdpr-mcp stop
### CGO/SQLite构建错误
确保GCC已安装:
macOS
xcode-select --install
Ubuntu/Debian
sudo apt install build-essential
### Claude Desktop看不到工具
1. 验证配置文件路径是否正确
1. 使用二进制文件的绝对路径
1. 退出并重新启动克劳德桌面(Cmd+Q/Alt+F4)
1. 检查日志: `~/Library/Logs/Claude/` (macOS)
### 打开WebUI无法连接到MCP
1. 确保gdpr-mcp在PATH中或使用绝对路径
1. 检查数据库是否存在(`./gdpr-mcp status`)
1. 更改配置后重新启动Open WebUI
## 运行测试
go test ./... -v
## 项目结构
gdpr-mcp/ ├── cmd/gdpr-mcp/main.go # CLI entry point ├── internal/ │ ├── db/ # Database layer │ ├── ingest/ # Text processing │ └── server/ # MCP server ├── go.mod └── README.md
