Legal Analysis Tools
Relevant source files
The following files were used as context for generating this wiki page:
Legal Analysis Tools comprise a specialized suite of services within the SecondLayer platform designed to provide automated, AI-driven evaluation of legal documents, court precedents, and legislative texts. These tools leverage the Model Context Protocol (MCP) to bridge raw legal data from sources like ZakonOnline and Verkhovna Rada with sophisticated reasoning engines.
The primary purpose of these tools is to assist legal professionals in identifying risks, validating citations, and generating structured legal advice. The system is organized into a modular architecture involving specialized handlers for document parsing, pattern recognition, and "hallucination guarding" to ensure technical accuracy and trust in AI-generated responses.
Core Tool Architecture
The legal analysis suite operates through a unified gateway that routes requests to specific tool handlers. The system identifies user intent to select the appropriate analysis pipeline, ranging from quick research to deep due diligence.
The flow above illustrates the standard "Legal Pipeline" where raw queries are classified, enriched with retrieved sources, analyzed for success/risk patterns, and finally validated against original source texts.
Sources: lexconfig/pipeline_contracts.txt, mcp_backend/src/api/mcp-query-api.ts:50-80
Core Primitives
The system defines several "primitives" that serve as the building blocks for complex legal operations:
| Tool Name | Category | Description |
|---|---|---|
classify_intent | Routing | Determines service type (e.g., legal_research, due_diligence) and reasoning depth. |
retrieve_legal_sources | Retrieval | Fetches raw cases, laws, and guidance from external APIs. |
analyze_legal_patterns | Analysis | Extracts successful arguments and risk factors from legal reasoning. |
validate_response | Trust | A "Hallucination Guard" that verifies AI claims against citations. |
check_precedent_status | Verification | Checks if a court case is active, modified, or overturned. |
Sources: mcp_backend/src/api/mcp-query-api.ts:258-310, lexconfig/services_tooling_map.txt
Document Analysis & Due Diligence
The Document Analysis module focuses on structural and semantic evaluation of user-provided files (PDF, DOCX, HTML). This includes summarizing long texts, comparing versions, and performing risk-based Due Diligence (DD).
Due Diligence Workflow
The Due Diligence service automates the review of document batches to identify legal and financial risks. It assigns risk scores based on findings and generates formatted reports with direct anchors to the source text.
The Due Diligence process integrates pattern recognition to flag issues such as "insufficient evidence" or "missing payment terms."
Sources: mcp_backend/src/api/tests/due-diligence-tools.test.ts:39-80, mcp_backend/src/services/due-diligence-service.ts
Risk Scoring Matrix
Risks are classified into levels that contribute to an overall numerical score for the document:
| Risk Level | Score Points | Description |
|---|---|---|
| Critical | 25 | Fundamental legal violations or terminal risks. |
| High | 15 | Significant financial or compliance issues. |
| Medium | 8 | Moderate operational risks or missing non-critical terms. |
| Low | 3 | Minor administrative or formal discrepancies. |
Sources: mcp_backend/src/api/tests/due-diligence-tools.test.ts:182-200
Trust & Validation (Hallucination Guard)
A critical component of the legal analysis tools is the HallucinationGuard and CitationValidator. These services ensure that the AI does not invent legal norms or misapply court precedents.
Validation Logic
When a legal advice response is generated, it passes through the validate_response tool, which performs three primary checks:
- Citation Verification: Checks if cited laws (e.g., Art. 625 of the Civil Code) actually exist and are relevant.
- Claim Mapping: Ensures every legal claim in the text has a corresponding source in the retrieved context.
- Precedent Status: Uses the
check_precedent_statustool to verify if a cited court decision is still valid law.
Implementation Details
Intent Classification
The classify_intent tool uses a budget-aware model selection (quick, standard, or deep) to route queries. It identifies keywords to determine if a request belongs to the document_vault, due_diligence, or legal_research services.
// mcp_backend/src/api/mcp-query-api.ts:50-70
const looksLikeVault = /vault|сховищ|хранилищ/i.test(query);
const looksLikeDD = /due\s*diligence|dd\b|перевірк|провер|m&a/i.test(query);
const service = looksLikeWorkflow
? 'workflow_automation'
: looksLikeVault ? 'document_vault'
: looksLikeDD ? 'due_diligence'
: 'legal_research';
Sources: mcp_backend/src/api/mcp-query-api.ts:50-70
Pattern Recognition
The analyze_legal_patterns tool leverages vector embeddings to match user queries against a LegalPatternStore. It returns success_arguments (factors that led to favorable rulings) and risk_factors (factors leading to rejections).
Sources: mcp_backend/src/api/mcp-query-api.ts:145-160
Conclusion
The Legal Analysis Tools provide a structured, multi-stage pipeline that transforms raw legal text into validated, risk-aware insights. By combining intent classification, multi-source retrieval (RADA and ZakonOnline), and a dedicated trust layer, the system minimizes AI hallucinations while maximizing the utility of automated legal research and document review.