Medical MCP Server
A comprehensive Model Context Protocol (MCP) server for medical document processing with advanced AI capabilities, including OCR, medical NER, local embeddings, and vector search.
README
Medical MCP Server
A comprehensive Model Context Protocol (MCP) server for medical document processing with advanced AI capabilities. This server provides document upload, OCR processing, medical Named Entity Recognition (NER), local embedding generation, and vector-based semantic search for healthcare applications.
🏥 Features
- Document Processing: Upload and process medical documents (PDF, images) with automatic text extraction
- Medical NER: Extract medical entities (medications, conditions, procedures, lab values) from text
- Local Embeddings: Generate embeddings using local HuggingFace models for privacy and control
- Vector Search: Semantic similarity search across medical documents and patient data
- OCR Processing: Extract text from medical images and scanned documents
- PDF Support: Process medical PDFs with text extraction and analysis
- MongoDB Integration: Store documents, embeddings, and medical entities with optimized indexes
- Multiple Transport Modes: HTTP and STDIO transport support
- Health Monitoring: Built-in health check endpoints and service monitoring
- Document Management: Complete CRUD operations for medical documents
📋 Available Tools
Document Management
uploadDocument- Upload and process medical documents with automatic text extraction, NER, and embedding generationsearchDocuments- Search documents using vector similarity and text search with hybrid rankinglistDocuments- List documents with filtering by patient, document type, or date range
Medical Analysis
extractMedicalEntities- Extract medical entities (medications, conditions, procedures, etc.) from textfindSimilarCases- Find similar medical cases based on symptoms, conditions, or medicationsanalyzePatientHistory- Analyze patient medical history with timeline, summary, or trend analysisgetMedicalInsights- Get medical insights and recommendations based on query and context
Embedding & Search
generateEmbeddingLocal- Generate embeddings locally using HuggingFace transformerschunkAndEmbedDocument- Split large documents into chunks and generate embeddings for eachsemanticSearchLocal- Perform semantic search using local embeddings
🚀 Quick Start
Prerequisites
- Node.js 18.0.0 or higher
- TypeScript 5.0.0 or higher
- MongoDB 4.4 or higher
- Python 3.8+ (for local embedding models)
- Tesseract OCR (for image text extraction)
MERMAID DIAGRAM: -
graph TB
subgraph "MCP Server (mcp-server)"
subgraph "MCP Protocol Layer"
Transport[Transport<br/>- Stdio mode<br/>- HTTP mode :3001]
Protocol[MCP Protocol Handler<br/>- Initialize<br/>- List tools<br/>- Call tools]
end
subgraph "Tool Registry (What Makes it MCP)"
Registry[Tool Registry<br/>THE MCP SERVER CORE]
DocTools[ Document Tools<br/>- uploadDocument<br/>- searchDocuments<br/>- listDocuments]
MedTools[ Medical Tools<br/>- extractMedicalEntities<br/>- findSimilarCases<br/>- analyzePatientHistory<br/>- getMedicalInsights]
EmbedTools[ Embedding Tools<br/>- generateEmbeddingLocal<br/>- chunkAndEmbedDocument<br/>- semanticSearchLocal]
end
subgraph "Service Layer (Tool Implementations)"
PDF[PDF Service]
OCR[OCR Service]
NER[Medical NER]
Embed[Local Embeddings]
end
subgraph "Storage"
Mongo[(MongoDB<br/>Documents & Vectors)]
end
end
%% MCP Protocol Flow
Transport -->|MCP Messages| Protocol
Protocol -->|"tools/list"| Registry
Protocol -->|"tools/call"| Registry
%% Tool Registration
Registry --> DocTools
Registry --> MedTools
Registry --> EmbedTools
%% Tool Implementation
DocTools --> PDF
DocTools --> OCR
DocTools --> NER
DocTools --> Embed
MedTools --> NER
MedTools --> Embed
EmbedTools --> Embed
%% Storage
DocTools --> Mongo
MedTools --> Mongo
EmbedTools --> Mongo
%% Styling
classDef protocol fill:#e1f5fe,stroke:#0277bd
classDef core fill:#f3e5f5,stroke:#7b1fa2,stroke-width:3px
classDef tools fill:#c8e6c9,stroke:#2e7d32
classDef service fill:#fce4ec,stroke:#c2185b
classDef storage fill:#fff9c4,stroke:#f57f17
class Transport,Protocol protocol
class Registry core
class DocTools,MedTools,EmbedTools tools
class PDF,OCR,NER,Embed service
class Mongo storage
Installation
# Clone the repository
git clone https://github.com/your-username/medical-mcp-server.git
cd medical-mcp-server
# Install dependencies
npm install
# Install Python dependencies for embeddings
pip install torch transformers sentence-transformers
# Install Tesseract OCR
# On Ubuntu/Debian:
sudo apt-get install tesseract-ocr
# On macOS:
brew install tesseract
# On Windows:
# Download from: https://github.com/UB-Mannheim/tesseract/wiki
# Build the project
npm run build
Environment Configuration
Create a .env file in the root directory:
# MongoDB Configuration (Required)
MONGODB_CONNECTION_STRING=mongodb://localhost:27017/medical_mcp
MONGODB_DATABASE_NAME=MCP
# Server Configuration
MCP_HTTP_PORT=3001
MCP_HTTP_MODE=true
# OCR Configuration
TESSERACT_PATH=/usr/bin/tesseract
OCR_LANGUAGE=eng
# Embedding Model Configuration
EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2
EMBEDDING_DEVICE=cpu
EMBEDDING_CACHE_DIR=./models
# Document Storage
DOCUMENT_UPLOAD_PATH=./uploads
MAX_DOCUMENT_SIZE=10485760
# Logging
LOG_LEVEL=info
LOG_FILE=./logs/medical-mcp.log
MongoDB Setup
# Start MongoDB
mongod --dbpath ./data/db
# Create indexes (run once)
npm run setup-indexes
Running the Server
HTTP Mode (Recommended)
npm run start:http
STDIO Mode
npm run start:stdio
Development Mode
npm run dev:http
📊 Health Monitoring
Check server health:
curl http://localhost:3001/health
Response:
{
"status": "healthy",
"server": "medical-mcp-server-with-epic",
"version": "1.0.0",
"features": [
"document-processing",
"medical-ner",
"vector-search",
"epic-fhir"
],
"services": {
"mongodb": true,
"localEmbedding": true,
"ner": true,
"ocr": true,
"pdf": true
},
"statistics": {
"documentsCount": 150,
"embeddingModel": "sentence-transformers/all-MiniLM-L6-v2",
"uptime": 3600
},
"timestamp": "2025-01-23T10:30:00.000Z"
}
🔧 Tool Usage Examples
Upload Medical Document
{
"tool": "uploadDocument",
"arguments": {
"title": "Patient Lab Results - John Doe",
"filePath": "/path/to/lab-results.pdf",
"metadata": {
"fileType": "pdf",
"patientId": "patient-123",
"documentType": "lab_report",
"tags": ["blood-work", "lipid-panel"]
}
}
}
Search Documents
{
"tool": "searchDocuments",
"arguments": {
"query": "high cholesterol treatment recommendations",
"limit": 10,
"searchType": "hybrid",
"filter": {
"documentType": "clinical_note",
"patientId": "patient-123"
}
}
}
Extract Medical Entities
{
"tool": "extractMedicalEntities",
"arguments": {
"text": "Patient presents with hypertension and diabetes. Prescribed metformin 500mg twice daily and lisinopril 10mg once daily.",
"entityTypes": ["MEDICATION", "CONDITION", "DOSAGE"]
}
}
Find Similar Cases
{
"tool": "findSimilarCases",
"arguments": {
"symptoms": ["chest pain", "shortness of breath"],
"conditions": ["hypertension"],
"patientId": "patient-123",
"limit": 5
}
}
Generate Local Embedding
{
"tool": "generateEmbeddingLocal",
"arguments": {
"text": "Patient has a history of coronary artery disease and recent myocardial infarction",
"metadata": {
"patientId": "patient-123",
"documentType": "clinical_note"
}
}
}
📚 API Reference
MCP Endpoint
- URL:
http://localhost:3001/mcp - Method: POST
- Content-Type: application/json
- Format: JSON-RPC 2.0
Health Check
- URL:
http://localhost:3001/health - Method: GET
🛠 Development
Scripts
# Build TypeScript
npm run build
# Start in HTTP mode
npm run start:http
# Start in STDIO mode
npm run start:stdio
# Development with hot reload
npm run dev:http
# Clean build artifacts
npm run clean
# Setup MongoDB indexes
npm run setup-indexes
# Run tests
npm test
# Lint code
npm run lint
Project Structure
medical-mcp-server/
├── src/
│ ├── server.ts # Main server implementation
│ ├── db/
│ │ ├── mongodb-client.ts # MongoDB connection and operations
│ │ └── setup-vector-indexes.ts # Database index setup
│ ├── services/
│ │ ├── local-embedding-service.ts # HuggingFace embedding service
│ │ ├── medical-ner-service.ts # Medical NER processing
│ │ ├── ocr-service.ts # OCR text extraction
│ │ └── pdf-service.ts # PDF processing
│ └── tools/
│ ├── document-tools.ts # Document management tools
│ ├── medical-tools.ts # Medical analysis tools
│ └── local-embedding-tools.ts # Embedding and search tools
├── dist/ # Compiled JavaScript
├── uploads/ # Document upload directory
├── models/ # Local AI model cache
├── logs/ # Server logs
├── package.json
├── tsconfig.json
└── .env # Environment configuration
🤖 AI Models & Services
Local Embedding Models
- Default:
sentence-transformers/all-MiniLM-L6-v2 - Medical Specialized:
clinical-ai/ClinicalBERT - Large Model:
sentence-transformers/all-mpnet-base-v2
Medical NER Models
- BioBERT: For biomedical text processing
- ClinicalBERT: Specialized for clinical notes
- SciSpaCy: Medical entity recognition
OCR Engine
- Tesseract: Multi-language OCR support
- Medical Enhancements: Prescription, lab report, clinical note optimizations
🔍 Troubleshooting
Common Issues
-
MongoDB Connection Failures
- Verify MongoDB is running and accessible
- Check connection string format
- Ensure database permissions
-
OCR Processing Errors
- Install Tesseract OCR engine
- Verify TESSERACT_PATH environment variable
- Check image file formats (PNG, JPG, PDF supported)
-
Embedding Model Issues
- Ensure sufficient disk space for model downloads
- Check Python dependencies (torch, transformers)
- Verify internet connection for initial model download
-
Memory Issues
- Adjust embedding batch size for large documents
- Consider using smaller embedding models
- Monitor MongoDB memory usage
-
Document Upload Failures
- Check file size limits (default 10MB)
- Verify upload directory permissions
- Ensure supported file formats
Debugging
Enable detailed logging:
# Set log level to debug
export LOG_LEVEL=debug
npm run dev:http
Check MongoDB indexes:
# Connect to MongoDB and list indexes
mongo medical_mcp
db.documents.getIndexes()
db.embeddings.getIndexes()
Monitor embedding service:
# Check embedding model status
curl http://localhost:3001/health | jq '.services.localEmbedding'
📄 Package.json
{
"name": "medical-mcp-server",
"version": "1.0.0",
"description": "Medical MCP Server with document processing, NER, and vector search capabilities",
"type": "module",
"main": "dist/server.js",
"bin": {
"medical-mcp-server": "./dist/server.js"
},
"scripts": {
"build": "tsc",
"start": "node dist/server.js",
"start:http": "MCP_HTTP_MODE=true node dist/server.js",
"start:stdio": "node dist/server.js",
"dev": "tsc --watch & nodemon dist/server.js",
"dev:http": "MCP_HTTP_MODE=true tsc --watch & nodemon dist/server.js",
"clean": "rm -rf dist",
"setup-indexes": "node dist/db/setup-vector-indexes.js",
"test": "jest",
"lint": "eslint src/**/*.ts",
"lint:fix": "eslint src/**/*.ts --fix"
},
"keywords": [
"medical",
"healthcare",
"mcp",
"ner",
"ocr",
"embeddings",
"vector-search",
"document-processing",
"mongodb",
"huggingface"
],
"author": "Your Name",
"license": "MIT",
"dependencies": {
"@modelcontextprotocol/sdk": "^0.5.0",
"cors": "^2.8.5",
"dotenv": "^17.2.0",
"express": "^4.18.2",
"mongodb": "^6.3.0",
"multer": "^1.4.5",
"pdf-parse": "^1.1.1",
"tesseract.js": "^5.0.0",
"uuid": "^9.0.1"
},
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/multer": "^1.4.11",
"@types/node": "^20.0.0",
"@types/uuid": "^9.0.8",
"eslint": "^8.57.0",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
"jest": "^29.7.0",
"@types/jest": "^29.5.0",
"nodemon": "^3.0.0",
"typescript": "^5.0.0"
},
"engines": {
"node": ">=18.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/your-username/medical-mcp-server.git"
},
"bugs": {
"url": "https://github.com/your-username/medical-mcp-server/issues"
},
"homepage": "https://github.com/your-username/medical-mcp-server#readme"
}
🐳 Docker Support
Dockerfile
FROM node:18-alpine
# Install system dependencies
RUN apk add --no-cache \
tesseract-ocr \
tesseract-ocr-data-eng \
python3 \
py3-pip \
build-base
# Install Python dependencies
RUN pip3 install torch transformers sentence-transformers
WORKDIR /app
# Copy package files
COPY package*.json ./
RUN npm ci --only=production
# Copy source code
COPY dist/ ./dist/
COPY uploads/ ./uploads/
COPY models/ ./models/
EXPOSE 3001
CMD ["npm", "start"]
Docker Compose
version: '3.8'
services:
medical-mcp-server:
build: .
ports:
- "3001:3001"
environment:
- MONGODB_CONNECTION_STRING=mongodb://mongo:27017/medical_mcp
- MCP_HTTP_MODE=true
depends_on:
- mongo
volumes:
- ./uploads:/app/uploads
- ./models:/app/models
mongo:
image: mongo:7
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db
volumes:
mongo-data:
📄 License
MIT License - see LICENSE file for details.
🤝 Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📞 Support
- MongoDB Documentation: MongoDB Docs
- HuggingFace Transformers: Transformers Documentation
- Model Context Protocol: MCP Documentation
- Issues: GitHub Issues
Note: This server processes medical documents and requires proper security measures for production deployment. Ensure compliance with HIPAA and other healthcare regulations when handling real patient data.
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。