GenAI-exchange is a cutting-edge full-stack application that revolutionizes legal document analysis using Model Context Protocol (MCP) architecture. This intelligent system simplifies the understanding of legal documents for both lawyers and non-lawyers through AI-powered analysis.
- π PDF Document Processing: Upload and analyze legal PDFs with intelligent OCR extraction
- π€ AI-Powered Q&A: Get instant answers about document content using Gemini AI via MCP
- π Smart Note-Taking: Interactive annotation system with page-specific notes
- π OCR Text Extraction: Advanced text extraction using Google Cloud Document AI
- βοΈ Legal Precedent Analysis: Find relevant case law and legal precedents by jurisdiction
- π MCP Architecture: Modern, extensible server using Model Context Protocol for tool integration
- π Secure Cloud Storage: Google Cloud Storage integration for document management
GenAI-exchange/
βββ backend/ # MCP Server & AI Backend
β βββ mcp_app.py # Main MCP server with FastAPI
β βββ app.py # Legacy Flask server (commented out)
β βββ config.yaml # Configuration file (empty)
β βββ requirements-versioned.txt # Python dependencies with versions
β βββ test_precedent.py # Testing script for precedent functionality
β βββ Class/ # Core AI modules
β β βββ chat.py # Gemini AI chat integration
β β βββ OCR.py # Google Document AI OCR processing
β β βββ Precedent.py # Legal precedent analysis
β βββ cmd_commands/ # Command utilities
β β βββ MCP_docker.ipynb # Docker setup notebook
β βββ testings/ # Prototyping & testing notebooks
β β βββ bragdocOCR.ipynb
β β βββ chatTest.ipynb
β β βββ documentai-async-v1.0.0.ipynb
β β βββ documentai-sync-v1.0.0.ipynb
β β βββ precedent.ipynb
β β βββ s1.json
β βββ pdf/ # Sample PDFs for testing
β βββ uploads/ # Local PDF upload directory
βββ frontend/ # Next.js Application
β βββ src/
β β βββ app/ # Next.js app router
β β β βββ page.tsx # Main application page
β β β βββ layout.tsx # Root layout
β β β βββ action.ts # Server actions
β β β βββ globals.css # Global styles
β β βββ components/ # React components
β β β βββ docunote-layout.tsx # Main app layout
β β β βββ pdf-viewer.tsx # PDF viewing component
β β β βββ qna-chat.tsx # Q&A chat interface
β β β βββ notes-tab.tsx # Notes management
β β β βββ ocr-view.tsx # OCR text display
β β β βββ type.ts # TypeScript interfaces
β β β βββ ui/ # Reusable UI components
β β βββ lib/
β β β βββ mcp-client.ts # MCP client implementation
β β β βββ utils.ts # Utility functions
β β βββ hooks/ # React hooks
β βββ package.json # Node.js dependencies & scripts
β βββ tsconfig.json # TypeScript configuration
β βββ next.config.ts # Next.js configuration
β βββ tailwind.config.js # Tailwind CSS configuration
β βββ public/ # Static assets
βββ LICENSE # Project license
βββ README.md # This documentation
This application is built using the Model Context Protocol (MCP), providing a standardized way to integrate AI tools and capabilities.
The MCP server (mcp_app.py
) runs on FastAPI and provides the following tools:
Tool Name | Description | Parameters | Returns |
---|---|---|---|
upload_pdf |
Upload PDF to Google Cloud Storage | filename (str), file_data (base64) |
GCS URI or local path |
pdf_qa |
Ask questions about PDF content | question (str), gsUri (str, optional) |
AI-generated answer |
extract_text_from_pdf |
OCR text extraction from PDF | gcs_uri (str) |
Structured text data with pages |
find_legal_precedents |
Find relevant case law | clause (str), location (str, default="US") |
Legal precedents analysis |
- Main Server:
http://localhost:8080
- MCP Endpoint:
http://localhost:8080/mcp/
- Health Check:
http://localhost:8080/health
- Root:
http://localhost:8080/
(status information)
- FastMCP Framework: Modern MCP server implementation
- Streamable HTTP Transport: Real-time communication with frontend
- CORS Configuration: Cross-origin support for frontend integration
- Session Management: MCP session handling with headers
- Error Handling: Comprehensive error handling and logging
- Project ID:
sodium-coil-470706-f4
- Location:
us
(US region) - Processor ID:
18d898182b219656
- Capabilities: PDF text extraction, form field detection, confidence scoring
- Model:
gemini-2.5-flash-lite
- Location:
us-central1
- Features: Legal document Q&A, precedent analysis, plain-language summaries
- Bucket:
legal-doc-bucket1
- Purpose: Secure PDF storage and URI generation for AI processing
Built with Next.js 15 using the app router and modern React patterns:
docunote-layout.tsx
: Main application layout with sidebar and tab navigationpdf-viewer.tsx
: Interactive PDF viewer with annotation capabilitiesqna-chat.tsx
: AI-powered chat interface for document questionsnotes-tab.tsx
: Note management with page-specific annotationsocr-view.tsx
: OCR text display and search functionality
The frontend uses a sophisticated MCP client (mcp-client.ts
) that provides:
// Key MCP Client Features
- Real-time connection management
- Streamable HTTP transport
- Automatic reconnection handling
- Tool calling abstraction
- TypeScript type safety
- Responsive Design: Mobile-first approach with Tailwind CSS
- Glass Morphism: Modern UI with backdrop blur effects
- Real-time Status: MCP connection status indicators
- Drag & Drop: Intuitive PDF upload experience
- Progressive Enhancement: Works offline for basic PDF viewing
{
"key_dependencies": {
"@modelcontextprotocol/sdk": "^1.18.1", // MCP client
"next": "15.5.2", // Next.js framework
"react": "19.1.0", // React 19
"react-pdf": "^10.1.0", // PDF rendering
"pdfjs-dist": "^5.3.93", // PDF.js core
"@radix-ui/*": "^1.x.x", // UI components
"tailwindcss": "^4", // Styling
"framer-motion": "^12.23.12" // Animations
}
}
graph LR
A[User selects PDF] --> B[Frontend validation]
B --> C[Convert to base64]
C --> D[MCP upload_pdf tool]
D --> E[GCS storage]
E --> F[Return gs:// URI]
graph LR
A[GCS URI] --> B[Document AI OCR]
B --> C[Gemini AI processing]
C --> D[Structured responses]
D --> E[Frontend display]
graph LR
A[User question] --> B[MCP pdf_qa tool]
B --> C[Gemini AI analysis]
C --> D[Context-aware answer]
D --> E[Chat interface]
- Node.js: v18+ (for frontend)
- Python: v3.10+ (for backend)
- PowerShell: v5.1+ (Windows)
- Git: For version control
- Google Cloud Project: Active project with billing enabled
- APIs Enabled:
- Document AI API
- Vertex AI API
- Cloud Storage API
- Service Account: JSON key file with appropriate permissions
- GCS Bucket: For PDF storage (
legal-doc-bucket1
)
Your Service Account needs these IAM roles:
Storage Object Admin
(for GCS)AI Platform User
(for Vertex AI)Document AI API User
(for Document AI)
-
Navigate to backend directory:
cd backend
-
Create Python virtual environment:
python -m venv .venv .venv\Scripts\Activate.ps1
-
Install dependencies:
pip install --upgrade pip pip install -r requirements-versioned.txt
-
Alternative: Install specific packages:
pip install fastapi uvicorn fastmcp google-cloud-storage google-cloud-documentai google-genai
-
Configure environment variables:
# Set Google Cloud credentials $env:GOOGLE_APPLICATION_CREDENTIALS = "C:\path\to\service-account-key.json" # Set project configuration $env:PROJECT_ID = "sodium-coil-470706-f4" $env:BUCKET_NAME = "legal-doc-bucket1" $env:MCP_NAME = "LegalDemystifierMCP" $env:PORT = "8080"
-
Run the MCP server:
python mcp_app.py
The server will start at:
http://localhost:8080
-
Navigate to frontend directory:
cd frontend
-
Install dependencies:
npm install # or yarn install # or pnpm install
-
Configure environment: Create
.env.local
file:NEXT_PUBLIC_MCP_SERVER_URL=http://localhost:8080/mcp/
-
Run development server:
npm run dev # or yarn dev # or pnpm dev
Frontend will be available at:
http://localhost:3000
-
Test MCP server health:
curl http://localhost:8080/health
-
Test MCP endpoint:
curl http://localhost:8080/mcp/
-
Upload test PDF via frontend:
- Open
http://localhost:3000
- Upload a PDF document
- Verify MCP connection status shows "Connected"
- Open
Purpose: Upload PDF files to Google Cloud Storage
Parameters:
filename
(string, required): Name of the PDF filefile_data
(string, required): Base64-encoded PDF databucket_name
(string, optional): GCS bucket name
Returns:
{
"message": "File uploaded to GCS",
"gcs_uri": "gs://bucket-name/filename.pdf"
}
Example Usage:
const result = await mcpClient.callTool('upload_pdf', {
filename: 'contract.pdf',
file_data: base64Data
});
Purpose: Ask questions about PDF content using AI
Parameters:
question
(string, required): The question to askgsUri
(string, optional): GCS URI of the PDF
Returns:
{
"answer": "AI-generated response based on PDF content"
}
Example Usage:
const result = await mcpClient.callTool('pdf_qa', {
question: "What are the key terms of this contract?",
gsUri: "gs://bucket/contract.pdf"
});
Purpose: Extract text from PDF using Google Document AI
Parameters:
gcs_uri
(string, required): GCS URI of the PDF
Returns:
{
"success": true,
"full_text": "Complete extracted text...",
"pages": [
{
"page_number": 1,
"text": "Page 1 content...",
"confidence": 0.98
}
],
"form_fields": [...],
"confidence_score": 0.95,
"total_pages": 5,
"total_characters": 12500
}
Purpose: Find relevant legal precedents for clauses
Parameters:
clause
(string, required): Legal clause text to analyzelocation
(string, optional): Jurisdiction (default: "US")
Returns:
{
"success": true,
"clause": "Original clause text",
"location": "California",
"precedents": "Formatted precedent analysis..."
}
Supported Jurisdictions:
US
(Federal)California
,New York
,Texas
(US States)India
,UK
,EU
(International)
The frontend automatically manages MCP connections:
// Connection status monitoring
const [connectionStatus, setConnectionStatus] = useState<'checking' | 'connected' | 'error'>('checking');
// Automatic reconnection
useEffect(() => {
const interval = setInterval(async () => {
const isHealthy = await MCPService.checkHealth();
setConnectionStatus(isHealthy ? 'connected' : 'error');
}, 30000);
return () => clearInterval(interval);
}, []);
Notes are automatically saved to localStorage:
// Auto-save notes
useEffect(() => {
if (pdfFile && notes.length > 0) {
localStorage.setItem(`docunote-notes-${pdfFile.name}`, JSON.stringify(notes));
}
}, [notes, pdfFile]);
Interactive PDF viewer with features:
- Page Navigation: Arrow keys, jump to page
- Text Selection: Right-click for AI analysis
- Annotation Support: Click to add notes
- Responsive Rendering: Optimized for all screen sizes
- File Type Validation: Only PDF files accepted
- Size Limits: Configurable upload limits
- CORS Protection: Restricted origins in production
- Input Sanitization: XSS protection for user inputs
- Turbopack: Next.js development with Turbopack for faster builds
- Streaming: Real-time MCP communication
- Lazy Loading: Components loaded on demand
- Caching: Intelligent PDF and response caching
- Compression: Optimized asset delivery
- Horizontal Scaling: Multiple MCP server instances
- Load Balancing: Distribute PDF processing load
- Cloud Storage: Unlimited PDF storage capacity
- CDN Ready: Static asset optimization
# Start MCP server
python mcp_app.py
# Test precedent functionality
python test_precedent.py
# Install requirements
pip install -r requirements-versioned.txt
# Health check
curl http://localhost:8080/health
# Development server
npm run dev
# Production build
npm run build
# Start production server
npm start
# Lint code
npm run lint
# Install dependencies
npm install
Create Dockerfile
in backend directory:
FROM python:3.11-slim
WORKDIR /app
COPY requirements-versioned.txt .
RUN pip install -r requirements-versioned.txt
COPY . .
EXPOSE 8080
CMD ["python", "mcp_app.py"]
Create Dockerfile
in frontend directory:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
Create docker-compose.yml
:
version: '3.8'
services:
backend:
build: ./backend
ports:
- "8080:8080"
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/app/credentials.json
- PROJECT_ID=sodium-coil-470706-f4
- BUCKET_NAME=legal-doc-bucket1
volumes:
- ./backend/credentials.json:/app/credentials.json
frontend:
build: ./frontend
ports:
- "3000:3000"
environment:
- NEXT_PUBLIC_MCP_SERVER_URL=http://backend:8080/mcp/
depends_on:
- backend
# Google Cloud Configuration
GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
PROJECT_ID=sodium-coil-470706-f4
BUCKET_NAME=legal-doc-bucket1
# MCP Server Configuration
MCP_NAME=LegalDemystifierMCP
PORT=8080
FRONTEND_ORIGIN=http://localhost:3000
# AI Configuration
VERTEX_AI_LOCATION=us-central1
DOCUMENT_AI_LOCATION=us
DOCUMENT_AI_PROCESSOR_ID=18d898182b219656
# MCP Client Configuration
NEXT_PUBLIC_MCP_SERVER_URL=http://localhost:8080/mcp/
# Application Configuration
NEXT_PUBLIC_APP_NAME=DocuNote
NEXT_PUBLIC_VERSION=1.0.0
# Test MCP tools
python test_precedent.py
# Test Google Cloud connections
python -c "from Class.OCR import process_pdf_with_document_ai; print('OCR module ready')"
python -c "from Class.chat import automated_chat; print('Chat module ready')"
python -c "from Class.Precedent import find_precedents; print('Precedent module ready')"
# Test MCP server
curl -X POST http://localhost:8080/mcp/ \
-H "Content-Type: application/json" \
-d '{"method": "tools/list"}'
# Component testing
npm test
# E2E testing with Playwright
npm run test:e2e
# Type checking
npm run type-check
# Linting
npm run lint
# Build verification
npm run build
- Upload Flow: Test PDF upload through MCP
- OCR Processing: Verify Document AI integration
- Q&A System: Test Gemini AI responses
- Precedent Analysis: Validate legal research
- Real-time Features: Test MCP connection handling
Backend Deployment:
# Build and deploy MCP server
gcloud run deploy genai-exchange-backend \
--source ./backend \
--platform managed \
--region us-central1 \
--allow-unauthenticated
Frontend Deployment on Vercel:
# Deploy Next.js app
vercel --prod
- Backend: Google Cloud Run, AWS Lambda, Railway, Render
- Frontend: Vercel, Netlify, AWS Amplify, Firebase Hosting
# Remove development CORS
ALLOWED_ORIGINS=https://your-frontend-domain.com
# Use environment secrets
GOOGLE_APPLICATION_CREDENTIALS=/secrets/service-account.json
# Enable HTTPS only
SECURE_COOKIES=true
HTTPS_ONLY=true
Endpoint | Method | Purpose | Authentication |
---|---|---|---|
/ |
GET | Server status | None |
/health |
GET | Health check | None |
/mcp/ |
POST | MCP protocol endpoint | MCP headers |
Content-Type: application/json
MCP-Session-Id: unique-session-id
MCP-Protocol-Version: 1.0
{
"error": "Error description",
"code": "ERROR_CODE",
"details": {
"tool": "tool_name",
"parameter": "invalid_value"
}
}
Backend Logs:
# MCP server logs
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("mcp_app")
Frontend Logs:
// MCP client debugging
console.log('π MCP Client connected:', connected);
console.log('π Calling tool:', toolName, parameters);
# Backend health
curl http://localhost:8080/health
# MCP endpoint health
curl -X POST http://localhost:8080/mcp/ \
-H "Content-Type: application/json" \
-d '{"method": "ping"}'
- PDF Upload Time: Target < 5 seconds
- OCR Processing: Target < 30 seconds
- Q&A Response: Target < 10 seconds
- MCP Connection: Target < 1 second
- Fork the repository
- Create feature branch:
git checkout -b feature/amazing-feature
- Setup development environment:
# Backend setup cd backend python -m venv .venv .venv\Scripts\Activate.ps1 pip install -r requirements-versioned.txt # Frontend setup cd ../frontend npm install
- Make changes and test
- Commit changes:
git commit -m 'Add amazing feature'
- Push to branch:
git push origin feature/amazing-feature
- Open Pull Request
- Python: Follow PEP 8, use type hints
- TypeScript: Strict mode enabled, proper interfaces
- Testing: Unit tests for all MCP tools
- Documentation: Update README for new features
# Backend tools
pip install black isort mypy pytest
# Frontend tools
npm install -D @types/node typescript eslint prettier
# Check server status
curl http://localhost:8080/health
# Verify MCP endpoint
curl http://localhost:8080/mcp/
# Check CORS settings
# Ensure frontend origin is in allowed_origins
# Verify credentials
$env:GOOGLE_APPLICATION_CREDENTIALS
gcloud auth application-default print-access-token
# Test API access
python -c "from google.cloud import storage; print(storage.Client().list_buckets())"
# Check file permissions
ls -la uploads/
# Verify GCS bucket access
gsutil ls gs://legal-doc-bucket1/
# Test Document AI
python -c "from Class.OCR import process_pdf_with_document_ai; print('OCR ready')"
# Clear cache
npm run clean
rm -rf .next node_modules
npm install
# Check TypeScript
npm run type-check
# Verify environment variables
echo $NEXT_PUBLIC_MCP_SERVER_URL
Enable detailed logging:
# Backend debug mode
$env:LOG_LEVEL = "DEBUG"
python mcp_app.py
# Frontend debug mode
$env:NODE_ENV = "development"
npm run dev
- π Analytics Dashboard: Document processing metrics
- π User Authentication: Multi-user support with OAuth
- π Document Comparison: Compare multiple legal documents
- π¨ Custom Templates: Legal document templates
- π Multi-language Support: International legal systems
- π± Mobile App: React Native mobile application
- π Advanced Search: Full-text search across documents
- π Usage Analytics: Track user interactions and performance
- WebSocket Support: Real-time collaborative features
- Microservices: Split MCP tools into separate services
- Kubernetes: Container orchestration for scalability
- GraphQL: Advanced API query capabilities
- Edge Computing: CDN-based processing for global scale
This project is licensed under the MIT License - see the LICENSE file for details.
- Google Cloud Platform: Document AI, Vertex AI, Cloud Storage
- Model Context Protocol: Modern AI tool integration framework
- Next.js Team: Amazing React framework
- FastAPI: High-performance Python API framework
- Open Source Community: Various libraries and tools used
π Ready to revolutionize legal document analysis? Get started with GenAI-exchange today!
For support, issues, or feature requests, please visit our GitHub repository.
Built with β€οΈ using MCP, Next.js, and Google Cloud AI