Legal Contract Review Agent
An AI-powered MCP server for analyzing Japanese legal contracts and identifying risks through a RAG-enhanced workflow. It enables clients to search legal knowledge, analyze clause risks, and generate automated contract review reports.
README
ContractGuard
Japanese contract risk analysis built as an AI engineering case study — LangGraph workflow + pgvector RAG + multi-modal ingestion + recoverable streaming UX.
⚠️ Not a legal service. This repository has never been operated commercially — Japan Attorney Act §72 (弁護士法第72条) reserves paid legal advice for licensed attorneys. The codebase is published as an open-source technical artifact only. Outputs are not legal opinions.
Status
Production-ready open-source reference implementation. The full stack — frontend, backend, OCR, payment, email, Postgres, Redis, error tracking — is wired with real integrations and ready to deploy. It has simply never been launched, by design (Attorney Act §72).
A synthetic Japanese contract sits in docs/samples/ so the local flow can be exercised end-to-end immediately after clone.
Architecture
flowchart LR
U[React/Vite UI<br/>text, PDF, image upload] --> API[FastAPI routers]
API --> Q[Quote + PII + OCR budget guards]
Q --> PAY[KOMOJU checkout<br/>reference implementation]
PAY --> JOB[Persistent analysis job]
JOB --> SSE[Recoverable SSE stream<br/>status + events + after_seq]
JOB --> LG[LangGraph pipeline]
LG --> P[parse_contract]
P --> A[clause-by-clause risk analysis]
A --> T[tool call: analyze_clause_risk]
T --> RAG[(PostgreSQL pgvector<br/>331 Japanese legal articles)]
A --> S[tool call: generate_suggestion<br/>medium/high risks only]
S --> REP[report generation + translation]
REP --> CACHE[(Redis 72h report cache)]
REP --> DB[(PostgreSQL orders/reports/costs)]
Tech Stack
| Layer | Stack |
|---|---|
| Frontend | React, Vite, TypeScript, i18next (9 languages) |
| Backend | FastAPI, SQLAlchemy async, Alembic, APScheduler |
| AI workflow | LangGraph + OpenAI tool calling, MCP server |
| RAG | PostgreSQL pgvector, 331 public e-Gov Japanese statutes |
| OCR | Google Cloud Vision (DOCUMENT_TEXT_DETECTION) |
| Storage | PostgreSQL (orders / reports / events), Redis (72h cache + rate limiting) |
| Payment | KOMOJU checkout |
| Resend | |
| Observability | Sentry + PostHog |
| Infra | Docker Compose (local), Fly.io + Vercel (deployment reference) |
Quick Start (local)
Local run only requires an OpenAI API key.
cp .env.example .env
# Edit .env: set OPENAI_API_KEY
docker compose up --build
Then open http://localhost:5173 and upload docs/samples/sample-contract-ja.txt.
In this minimal mode:
- ✅ Plain-text contracts and text-based PDFs (selectable text) work end-to-end.
- ❌ Image / scanned-PDF OCR is disabled. To enable it, add
GOOGLE_APPLICATION_CREDENTIALS_JSONandGOOGLE_VISION_PROJECT_ID. - KOMOJU / Resend auto-bypass in dev — no real payment, no real email.
Production Setup
The repository is shaped to deploy to production by setting APP_ENV=production and supplying credentials for each external service:
| Service | Required env vars |
|---|---|
| OpenAI | OPENAI_API_KEY |
| Google Cloud Vision (OCR) | GOOGLE_APPLICATION_CREDENTIALS_JSON, GOOGLE_VISION_PROJECT_ID |
| KOMOJU (payment) | KOMOJU_SECRET_KEY, KOMOJU_PUBLISHABLE_KEY, KOMOJU_WEBHOOK_SECRET |
| Resend (email) | RESEND_API_KEY |
| Sentry | SENTRY_DSN, VITE_SENTRY_DSN |
| PostHog | POSTHOG_API_KEY, VITE_POSTHOG_KEY |
| Database / Cache | DATABASE_URL (managed Postgres + pgvector), REDIS_URL (managed Redis) |
| App | FRONTEND_URL (non-localhost), ADMIN_API_TOKEN |
When APP_ENV=production, the app refuses to boot if any of the above is missing or FRONTEND_URL still points at localhost. Strict-validation logic lives in backend/config.py (validate_runtime()).
fly.toml and vercel.json describe the deployment topology used during development. The service is not currently hosted.
Flow
- Upload a contract (text, PDF, or image). The upload route runs text extraction, PII checks, token estimation, non-contract detection, and OCR budget guards.
- Checkout reference path creates an order. Empty KOMOJU credentials trigger a local bypass in dev.
/review/:orderIdstarts or resumes the persistent analysis job and streams progress events that survive page refresh.- LangGraph parses clauses, analyzes each clause with RAG-grounded tool calls, and generates suggestions only where the risk warrants it.
/report/:orderIdshows the saved report, clause excerpts, risk filters, and PDF export — retained for 72 hours.
User contract text is deleted after analysis. The vector store contains only public e-Gov statutes; user contracts are never embedded.
Demo

Repository Map
backend/agent/graph.py— LangGraph pipeline.backend/agent/tools.py— RAG-grounded tool calls.backend/services/analysis_executor.py— persistent analysis job + event sourcing.backend/rag/store.py— pgvector storage and search.backend/config.py— runtime configuration and strict validation.frontend/src/pages/ReviewPage.tsx— recoverable analysis progress UI.frontend/src/pages/ReportPage.tsx— report UI with risk filters and PDF export.tests/— backend pytest suites.scripts/smoke_local_flow.sh— end-to-end local smoke test.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。