法律MCP-德国法律文本搜索系统
   ](https://www.docker.com/)
一个使用向量嵌入和语义搜索搜索搜索和分析德国法律文本的综合系统,包括:
- 商店API:带有PostgreSQL、pgvector和Ollama嵌入的FastAPI后端
- MCP服务器:FastMCP服务器为AI助手提供查询法律文本的工具
- CLI工具:用于导入和查询法律文本的命令行界面
- Web剪贴器从gesetze-im-internet.de自动提取法律文本
- 解析器:德国法律XML格式的综合解析器(gii-norm.dtd)
目录
特性
存储API功能
- 🗄️ PostgreSQL+pgvector -用于语义搜索的矢量数据库
- 🤖 Olama集成 -为法律文本生成嵌入
- 🌐 网络爬取 - 自动提取法律-im-internet.de
- 📄 XML解析 -德国法律XML格式的综合解析器
- 🔍 语义搜索 -基于向量的法律文本相似性搜索
- 📊 元数据跟踪 -完整文档元数据和版本控制
- 📝 RESTful API -带自动文档的FastAPI
- 🐳 Docker支持 -通过集装箱化轻松部署
MCP服务器功能
- 🔧 FastMCP -现代MCP服务器实现
- 🤝 AI助手集成 -提供查询法律文本的工具
- 🔌 HTTP API客户端 -连接到存储API以进行数据访问
CLI工具功能
- 📋 列出命令 -查看导入的代码和可用目录
- 📥 导入命令 -导入带有进度指示的法律代码
- 🔍 查询命令 -按代码、节和子节检索文本
- 🔎 搜索命令 -具有相似性评分的语义搜索
- 📊 多种输出格式 -表视图或JSON输出
- ⚙️ 可配置的 -通过标志或环境变量自定义API URL支持
建筑
┌──────────────────────────────────────────────────┐
│ │
│ Docker Network: legal-mcp-network │
│ │
│ ┌────────────────┐ │
│ │ MCP Server │ :8001 │
│ │ (FastMCP) │ │
│ └───────┬────────┘ │
│ │ │
│ │ LEGAL_API_BASE_URL │
│ │ http://store-api:8000 │
│ │ │
│ ▼ │
│ ┌────────────────┐ │
│ │ Store API │ :8000 │
│ │ (FastAPI) │ │
│ └───────┬────────┘ │
│ │ │
│ │ DATABASE_URL │
│ │ postgresql://postgres:5432 │
│ │ OLLAMA_BASE_URL │
│ │ │
│ ▼ │
│ ┌────────────────┐ │
│ │ PostgreSQL │ :5432 │
│ │ + pgvector │ │
│ └────────────────┘ │
│ │
└──────────────────────────────────────────────────┘
│
│ External Ollama Service
│ (for embeddings)
▼
┌────────────────┐
│ Ollama API │
│ (Remote/Local)│
└────────────────┘快速开始
先决条件
- Docker和Docker Compose
- Ollama(嵌入的本地或远程端点)
- Git
⚠️ 重要提示:Olama嵌入模型 默认情况下,此项目使用嵌入模型:ryanshillington/Qwen3-Embedding-4B:latest在导入法律文本之前,您必须提取此模型(或您配置的替代方案): ``bash ollama pull ryanshillington/Qwen3-Embedding-4B:latest`您可以通过设置来使用其他模型OLLAMA_EMBEDDING_MODEL` 环境变量,但 模型必须产生2560维向量使用具有不同维度的模型会导致错误,因为数据库模式固定在2560个维度。更改为具有不同维度的模型需要修改数据库模式并重新导入所有法律文本。
1.克隆和设置
# Clone the repository
git clone
cd legal-mcp
# Copy environment file
cp .env.example .env
# Edit .env with your configuration
# Update OLLAMA_BASE_URL and OLLAMA_AUTH_TOKEN if needed2.启动所有服务
# Build and start all services
docker-compose up -d
# Check service status
docker-compose ps这将开始:
- PostgreSQL (端口5432)-具有pgvector扩展的数据库
- 商店API (端口8000)-用于法律文本的FastAPI后端
- MCP服务器 (端口8001)-用于AI助手的FastMCP服务器
3.运行数据库迁移
# Run Alembic migrations to set up the database
docker-compose exec store-api alembic upgrade head4.导入法律文本
# Import a test legal code (e.g., rag_1)
curl -X POST http://localhost:8000/legal-texts/gesetze-im-internet/rag_1
# Import German Civil Code (BGB)
curl -X POST http://localhost:8000/legal-texts/gesetze-im-internet/bgb
# Import other legal codes
curl -X POST http://localhost:8000/legal-texts/gesetze-im-internet/stgb # Criminal Code
curl -X POST http://localhost:8000/legal-texts/gesetze-im-internet/gg # Constitution5.测试API
# Check API health
curl http://localhost:8000/health
# Query legal texts by section
curl "http://localhost:8000/legal-texts/gesetze-im-internet/rag_1?section=%C2%A7%201"
# Semantic search (requires embeddings)
curl "http://localhost:8000/legal-texts/gesetze-im-internet/rag_1/search?q=Versicherung&limit=5"
# Access interactive API documentation
open http://localhost:8000/docsCLI工具
CLI提供了一个方便的命令行界面,无需编写代码即可管理法律文本。
安装
# Install in development mode (from project root)
pip install -e .
# Verify installation
legal-mcp --help先决条件
CLI要求Store API运行:
# Start all services
docker-compose up -d
# Verify Store API is running
curl http://localhost:8000/health可用命令
列出命令
列出导入的代码
# Show all imported legal codes in table format
legal-mcp list codes
# Output as JSON
legal-mcp list codes --json列出可用目录
# Show all available legal codes that can be imported
legal-mcp list catalog
# Output as JSON
legal-mcp list catalog --json导入命令
# Import a single legal code
legal-mcp import --code bgb
# Import multiple legal codes
legal-mcp import --code bgb --code stgb --code gg
# Import with JSON output
legal-mcp import --code bgb --jsonimport命令在处理时显示一个微调器,并显示每个代码的进度。
Query命令
# Query all texts for a legal code
legal-mcp query bgb
# Query specific section
legal-mcp query bgb --section "§ 1"
# Query specific sub-section
legal-mcp query bgb --section "§ 1" --sub-section "1"
# Output as JSON
legal-mcp query bgb --section "§ 1" --json搜索命令
# Semantic search in a legal code
legal-mcp search bgb "Kaufvertrag"
# Limit number of results
legal-mcp search bgb "Kaufvertrag" --limit 5
# Set similarity cutoff threshold (0-2, lower = stricter)
legal-mcp search bgb "Kaufvertrag" --cutoff 0.5
# Output as JSON
legal-mcp search bgb "Kaufvertrag" --json配置
默认API URL: http://localhost:8000
用环境变量覆盖:
export LEGAL_API_BASE_URL=http://custom-host:8000
legal-mcp list codes用命令标志覆盖:
legal-mcp list codes --api-url http://custom-host:8000输出格式
表格式(默认):
- 使用Rich库创建干净、格式化的表
- 文本截断以提高可读性
- 颜色编码输出
JSON格式:
- 包含全文内容的完整数据
- 机器可读脚本
- 使用
--json用任何命令标记
工作流示例
# 1. Check available legal codes
legal-mcp list catalog
# 2. Import desired codes
legal-mcp import --code bgb --code stgb
# 3. Verify imports
legal-mcp list codes
# 4. Query specific sections
legal-mcp query bgb --section "§ 433"
# 5. Perform semantic search
legal-mcp search bgb "Kaufvertrag" --limit 10环境配置
该应用程序使用 .env 配置文件。看 .env.example 对于模板。
所需的环境变量
# Ollama Configuration
OLLAMA_BASE_URL=https://your-ollama-endpoint.com
OLLAMA_AUTH_TOKEN=your-auth-token-here
OLLAMA_EMBEDDING_MODEL=ryanshillington/Qwen3-Embedding-4B:latest # Optional, this is the default
# PostgreSQL Configuration
POSTGRES_HOST=postgres # Use 'postgres' in Docker, 'localhost' for local dev注: 这OLLAMA_EMBEDDING_MODEL变量允许您使用不同的嵌入模型。然而, 任何替代模型都必须产生2560维向量 与数据库模式兼容。默认模型(ryanshillington/Qwen3-Embedding-4B:latest)是推荐的。
附加配置(在docker compose.yml中设置)
# Database URL (automatically constructed)
DATABASE_URL=postgresql+asyncpg://legal_mcp:legal_mcp_password@postgres:5432/legal_mcp_db
# MCP Server Configuration
LEGAL_API_BASE_URL=http://store-api:8000API文档
运行后,访问交互式API文档:
- Swagger用户界面: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
主要终点
法律文本
POST /legal-texts/gesetze-im-internet/{book}-导入带有嵌入的法律文本GET /legal-texts/gesetze-im-internet/{code}-按代码/章节查询法律文本GET /legal-texts/gesetze-im-internet/{code}/search-嵌入语义搜索
系统
GET /health-健康检查端点GET /-API信息
MCP服务器
MCP服务器为人工智能助手提供了通过模型上下文协议与法律文本数据库交互的工具。
可用工具
MCP服务器提供以下工具:
search_legal_texts-对法律文本进行语义搜索
- 参数: query, code, limit (1-20), cutoff (0-2) - 返回:具有相似性得分的匹配法律文本部分列表
get_legal_section-检索特定的法律文本部分
- 参数: code, section, sub_section (可选) - 返回:符合条件的法律文本部分列表
import_legal_code- Import a complete legal code from 互联网上的法律
- 参数: code - 返回:包含导入统计信息的成功消息
get_available_codes-获取数据库中所有可用的法律代码
- 返回:合法代码标识符列表
使用MCP服务器
MCP服务器在端口8001上运行,可由MCP兼容客户端访问:
# Check MCP server is running
curl http://localhost:8001/health
# The MCP server automatically connects to the Store API
# using LEGAL_API_BASE_URL environment variable对于AI助手,配置MCP客户端以连接到 http://localhost:8001 (或适用于您部署的适当主机/端口)。
法律文本特征
导入法律文本
系统自动:
- Scrapes legal text XML from 网站简介
- 将XML解析为结构化的法律文本部分
- 使用Ollama为每个文本部分生成嵌入
- 使用pgvector将文本及其嵌入存储在PostgreSQL中
查询法律文本
按区段标识符查询:
curl "http://localhost:8000/legal-texts/gesetze-im-internet/bgb?section=%C2%A7%201"语义搜索
使用具有向量相似性的自然语言进行搜索:
curl "http://localhost:8000/legal-texts/gesetze-im-internet/bgb/search?q=Kaufvertrag&limit=5&cutoff=0.7"参数:
q-搜索查询(必填)limit-最大结果(1-100,默认值:10)cutoff-相似性阈值(0-2,默认值:0.5)
- 较低的值=更严格的匹配 - 0.3-0.5:非常严格 - 0.6-0.7:平衡良好 - 0.8-1.0:更宽容
解析器
该系统包括一个用于gesetze-im-internet.de使用的gii-norm.dtd格式的综合解析器。
解析器功能
- 完全DTD覆盖 -gii-norm.dtd的所有主要元素
- 结构化数据 -所有结构的类型安全数据类
- 文本提取 -处理具有格式的复杂嵌套文本
- 桌架 -捕获表结构
- 脚注处理 -摘录带有参考文献的脚注
- 元数据解析 -完成元数据提取
使用解析器
from app.scrapers import GesetzteImInternetScraper
# The scraper automatically uses the XML parser
scraper = GesetzteImInternetScraper()
legal_texts = scraper.scrape('bgb')
for text in legal_texts:
print(f"Section: {text.section}")
print(f"Text: {text.text}")解析元数据
解析器提取:
- 法律缩略语(jurabk、amtabk)
- Dates(制作日期)
- 引文 (fundstelle)
- 标题(短语,语言,标题)
- 结构分类(Structural classification)
- 截面名称(enbez)
- 版本信息(标准)
发展
本地开发(无Docker)
- 安装依赖项:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -r requirements.txt
# Install CLI tool in development mode
pip install -e .- 设置本地数据库:
# Start only PostgreSQL
docker-compose up postgres -d
# Update .env to use localhost
# POSTGRES_HOST=localhost- 运行迁移:
cd store
alembic upgrade head- 启动存储API:
cd store
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000- 启动MCP服务器:
cd mcp
export LEGAL_API_BASE_URL=http://localhost:8000
python -m server.main运行测试
# Run all tests
pytest
# Run with coverage
pytest --cov=app tests/
# Run specific test file
pytest tests/test_main.py -v
# Run CLI tests specifically
pytest tests/cli/ -v贡献
我们欢迎社区的贡献!请查看我们的 贡献指南 有关以下内容的详细信息:
- 如何报告bug
- 如何建议功能
- 如何提交pull请求
- 开发设置说明
- 代码风格指南
行为准则
本项目遵守《贡献者契约》 行为准则。通过参与,您将遵守此准则。请通过项目的报告机制报告不可接受的行为。
安全
安全对我们很重要。如果您发现安全漏洞,请按照我们的 安全策略 负责披露。不要公开安全漏洞问题。
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
致谢
支持
- 问题:在GitHub上发布bug或功能请求
- 讨论:使用GitHub讨论来提问和社区聊天
______________________________________________________________________
由...制作❤️ 面向政府科技界
