Mistral OCR MCP Server
Extracts text and images from PDFs and image files using the Mistral OCR API, with a security sandbox for file writes.
README
Mistral OCR MCP Server
A Model Context Protocol (MCP) server that provides tools for extracting text and images from PDF and image files using the Mistral OCR API.
Features
- Simple Text Extraction: Extract markdown content from documents without handling images
- Full Extraction with Images: Extract markdown and save embedded images to disk with proper relative links
- Security Sandbox: Restricts file writes to a configured allowed directory
- Zero-Install Deployment: Run with
uvxwithout prior installation - Supported Formats: PDF (
.pdf), PNG (.png), JPEG (.jpg,.jpeg), WebP (.webp), GIF (.gif)
Client Configuration
Claude Desktop
Add this to your claude_desktop_config.json:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"mistral-ocr": {
"command": "uvx",
"args": ["mistral-ocr-mcp"],
"env": {
"MISTRAL_API_KEY": "your-api-key-here",
"MISTRAL_OCR_ALLOWED_DIR": "/absolute/path/to/allowed/directory"
}
}
}
}
OpenCode
Add this to the mcp section of your configuration file:
{
"mcp": {
"mistral-ocr": {
"type": "local",
"command": ["uvx", "mistral-ocr-mcp"],
"enabled": true,
"environment": {
"MISTRAL_API_KEY": "your-api-key-here",
"MISTRAL_OCR_ALLOWED_DIR": "/absolute/path/to/allowed/directory"
}
}
}
}
Codex
If you use the Codex CLI, you can add the server with:
codex mcp add mistral-ocr -- uvx mistral-ocr-mcp
Make sure the environment variables MISTRAL_API_KEY and MISTRAL_OCR_ALLOWED_DIR are set in your shell environment.
Configuration
Required Environment Variables
| Variable | Description | Example |
|---|---|---|
MISTRAL_API_KEY |
Your Mistral API key (never logged) | sk-abc123... |
MISTRAL_OCR_ALLOWED_DIR |
Absolute path to allowed write directory | /Users/username/workdir |
Security Sandbox
The server enforces a write directory sandbox to prevent unauthorized file writes:
extract_markdown: No write restrictions (read-only operation)extract_markdown_with_images: Theoutput_dirparameter must be withinMISTRAL_OCR_ALLOWED_DIR
Validation Examples:
MISTRAL_OCR_ALLOWED_DIR |
output_dir |
Result |
|---|---|---|
/Users/username/workdir |
/Users/username/workdir/project/output |
✅ Allowed |
/Users/username/workdir |
/Users/username/workdir |
✅ Allowed (exact match) |
/Users/username/workdir |
/Users/username/documents |
❌ Rejected |
/Users/username/workdir |
/Users/username/workdir/../documents |
❌ Rejected (resolves outside) |
Security Notes:
- All paths are canonicalized (symlinks resolved,
..eliminated) before validation - Image filenames are sanitized to prevent path traversal attacks
Tool Reference
Tool 1: extract_markdown
Extract markdown content from a document without saving images.
Arguments:
{
"file_path": "/absolute/path/to/document.pdf"
}
| Parameter | Type | Required | Description |
|---|---|---|---|
file_path |
string |
Yes | Absolute path to input file (PDF or image) |
Returns:
"# Document Title\n\nExtracted markdown content from all pages..."
Returns a single string containing concatenated markdown from all pages.
Example:
{
"tool": "extract_markdown",
"arguments": {
"file_path": "/Users/username/documents/report.pdf"
}
}
Tool 2: extract_markdown_with_images
Extract markdown content and save embedded images to disk.
Arguments:
{
"file_path": "/absolute/path/to/document.pdf",
"output_dir": "/absolute/path/to/output/parent"
}
| Parameter | Type | Required | Description |
|---|---|---|---|
file_path |
string |
Yes | Absolute path to input file (PDF or image) |
output_dir |
string |
Yes | Absolute path to output parent directory (must exist and be writable, must be within MISTRAL_OCR_ALLOWED_DIR) |
Returns:
{
"output_directory": "/absolute/path/to/output/parent/document",
"markdown_file": "/absolute/path/to/output/parent/document/content.md",
"images": ["img_abc123.png", "img_def456.jpeg"]
}
| Field | Type | Description |
|---|---|---|
output_directory |
string |
Absolute path to created subdirectory |
markdown_file |
string |
Absolute path to content.md file |
images |
array[string] |
List of saved image filenames (not full paths) |
Behavior:
- Creates a subdirectory named after the input file stem (e.g.,
reportforreport.pdf) - If the subdirectory already exists, appends a timestamp:
report_20260102_143022 - Saves all extracted images as
<sanitized_id>.<ext>(e.g.,img_abc123.png) - Saves markdown to
content.mdwith relative image links (e.g.,)
Example:
{
"tool": "extract_markdown_with_images",
"arguments": {
"file_path": "/Users/username/documents/quarterly-report.pdf",
"output_dir": "/Users/username/workdir/extracted"
}
}
Output Structure:
/Users/username/workdir/extracted/
quarterly-report/
content.md # Markdown with relative image links
img_abc123.png # First extracted image
img_def456.jpeg # Second extracted image
Example Client Usage
Here's a minimal Python example using the MCP SDK to call the tools:
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def extract_document():
server_params = StdioServerParameters(
command="mistral-ocr-mcp",
env={
"MISTRAL_API_KEY": "your-api-key",
"MISTRAL_OCR_ALLOWED_DIR": "/Users/username/workdir"
}
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# Simple extraction
result = await session.call_tool(
"extract_markdown",
arguments={"file_path": "/path/to/document.pdf"}
)
print(result.content[0].text)
# Extraction with images
result = await session.call_tool(
"extract_markdown_with_images",
arguments={
"file_path": "/path/to/document.pdf",
"output_dir": "/Users/username/workdir/output"
}
)
print(result.content[0].text)
asyncio.run(extract_document())
Troubleshooting
| Error | Cause | Solution |
|---|---|---|
Missing required environment variable: MISTRAL_API_KEY |
MISTRAL_API_KEY not set |
Set the environment variable before running the server |
Missing required environment variable: MISTRAL_OCR_ALLOWED_DIR |
MISTRAL_OCR_ALLOWED_DIR not set |
Set the environment variable to an absolute path |
MISTRAL_OCR_ALLOWED_DIR must be an absolute path |
Relative path provided (e.g., ~/documents) |
Use an absolute path (e.g., /Users/username/documents) |
MISTRAL_OCR_ALLOWED_DIR does not exist |
Directory does not exist on filesystem | Create the directory first: mkdir -p /path/to/dir |
MISTRAL_OCR_ALLOWED_DIR is not a directory |
Path points to a file, not a directory | Ensure the path is a directory |
validate file_path: must be an absolute path: {path} |
Relative path provided for input file | Use an absolute path (e.g., /Users/username/file.pdf) |
validate file_path: resolve failed, path does not exist: {path} |
Input file does not exist | Check the file path and ensure the file exists |
validate file_path: unsupported file type '{suffix}'. Supported types: ... |
File extension not supported | Use .pdf, .png, .jpg, .jpeg, .webp, or .gif |
validate output_dir: resolve failed, path does not exist: {path} |
Output directory does not exist | Create the directory first: mkdir -p {path} |
validate output_dir: path is not a directory: {path} |
Path points to a file, not a directory | Ensure the path is a directory |
validate output_dir: writability check failed, directory not writable: {path} |
Output directory exists but is not writable | Check directory permissions: chmod u+w {path} |
output_dir must be within the allowed directory |
output_dir is outside MISTRAL_OCR_ALLOWED_DIR |
Use a path within the allowed directory |
Mistral OCR request failed (status=401): {message} |
Invalid API key | Check your MISTRAL_API_KEY |
Mistral OCR request failed (status=429): {message} |
Rate limit exceeded | Wait and retry, or check your API quota |
Development
Setup
Clone the repository and install with development dependencies:
git clone https://github.com/ORDIS-Co-Ltd/mistral-ocr-mcp
cd mistral-ocr-mcp
pip install -e '.[dev]'
Run the server locally:
MISTRAL_API_KEY="your-key" \
MISTRAL_OCR_ALLOWED_DIR="/path/to/allowed/dir" \
python -m mistral_ocr_mcp
Run Tests
pytest
Project Structure
mistral-ocr-mcp/
├── src/
│ └── mistral_ocr_mcp/
│ ├── __init__.py
│ ├── __main__.py # Entry point
│ ├── server.py # MCP server and tool definitions
│ ├── config.py # Configuration loading and validation
│ ├── extraction.py # OCR orchestration logic
│ ├── mistral_client.py # Mistral API client
│ ├── images.py # Image parsing and saving
│ ├── markdown_rewrite.py # Markdown link rewriting
│ └── path_sandbox.py # Path validation and sandbox enforcement
├── tests/ # Unit tests
├── pyproject.toml # Package configuration
└── README.md # This file
License
MIT
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
Links
- GitHub Repository: https://github.com/ORDIS-Co-Ltd/mistral-ocr-mcp
- MCP Specification: https://modelcontextprotocol.io
- Mistral AI: https://mistral.ai
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。