A MCP server for managing structured Markdown documents
Project description
Document MCP
Document MCP exists to complement and diversify the predominantly STEM-oriented toolsets (e.g. Claude Code, bash/grep agents) by giving writers, researchers, and knowledge-managers first-class, local-first control over large-scale Markdown documents with built-in safety features that prevent content loss.
๐ Quick Start
๐ก Just want to use the MCP server with Claude Code? See the Package Installation Guide for simple setup instructions with universal path finding.
Installation
PyPI Installation (Recommended):
# Simple one-command install
pip install document-mcp
# Verify installation
document-mcp --version
Development Setup:
# Install uv package manager (if not already installed)
# macOS/Linux: curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows: powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Clone and install with development dependencies
git clone https://github.com/clchinkc/document-mcp.git
cd document-mcp
uv sync
# Install with development dependencies (includes testing tools)
uv pip install ".[dev]"
Dependencies Information:
- Core Runtime: The package includes all necessary dependencies for normal operation
- Development Dependencies: Additional tools for testing and development are available via
pip install ".[dev]":pytest,pytest-asyncio,pytest-cov- Testing frameworkruff,mypy- Code quality and type checkingmemory-profiler,psutil- Performance monitoringtwine- Package validation and publishing
Alternative: Traditional virtual environment setup
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -e ".[dev]"
### Configuration
```bash
# Create .env file with your API key
echo "OPENAI_API_KEY=your_openai_api_key_here" > .env
# OR for Google Gemini:
echo "GEMINI_API_KEY=your_gemini_api_key_here" > .env
# Verify your setup
python3 src/agents/simple_agent/main.py --check-config
Quick Test
# Start MCP server (in one terminal)
# If installed via PyPI:
document-mcp stdio
# If using development setup:
python3 -m document_mcp.doc_tool_server stdio
# Test basic functionality (in another terminal)
python3 -c "from document_mcp import __version__; print(f'Document MCP v{__version__} ready')"
๐ Complete Manual Testing Guide - Step-by-step workflows for creative writing, editing, and document management
๐ ๏ธ Development
Modern Toolchain
This project uses modern Python development tools for enhanced performance and developer experience:
uv: Ultra-fast Python package manager and dependency resolver (10-100x faster than pip)ruff: Lightning-fast Python linter and formatter (replaces black, isort, flake8, pydocstyle, autoflake)mypy: Static type checking for enhanced code qualitypytest: Comprehensive testing framework with async support
Testing Strategy
# Run all tests with uv (recommended)
uv run pytest
# Run with traditional python3 (for compatibility)
python3 -m pytest
# Run by test tier
uv run pytest tests/unit/ # Unit tests (fastest, no external deps)
uv run pytest tests/integration/ # Integration tests (real MCP, mocked LLM)
uv run pytest tests/e2e/ # E2E tests (requires API keys, 600s timeout)
# Run with coverage
uv run pytest --cov=document_mcp --cov-report=html
# Code quality checks
uv run ruff check # Lint code
uv run ruff check --fix # Auto-fix linting issues
uv run ruff format # Format code
uv run mypy document_mcp/ # Type checking
# Quality checks script (uses uv and ruff internally)
python3 scripts/quality.py full
Running the System
# Start MCP server (stdio transport)
uv run python -m document_mcp.doc_tool_server stdio
# Alternative: python3 -m document_mcp.doc_tool_server stdio
# Test agents
uv run python src/agents/simple_agent/main.py --query "list all documents"
uv run python src/agents/react_agent/main.py --query "create a book with multiple chapters"
# Interactive mode
uv run python src/agents/simple_agent/main.py --interactive
uv run python src/agents/react_agent/main.py --interactive
# Optimize agent prompts
uv run python -m prompt_optimizer simple # Optimize specific agent
uv run python -m prompt_optimizer all # Optimize all agents
uv run python -m prompt_optimizer simple # Development use within repo only
# Development infrastructure testing
python3 scripts/development/metrics/test_production.py # Test production metrics
python3 scripts/development/telemetry/scripts/test.py # Test telemetry infrastructure
scripts/development/telemetry/scripts/start.sh # Start development telemetry
Environment Configuration
Create a .env file with your API key according to .env.example, and fill in the required values.
Running the System
๐ What is Document MCP?
Document MCP provides a structured way to manage large documents composed of multiple chapters. Think of it as a file system specifically designed for books, research papers, documentation, or any content that benefits from being split into manageable sections.
Recent Updates (v0.0.3)
- ๐ Pagination System: Industry-standard pagination replaces character truncation for complete data access
- ๐ Enhanced Testing: Comprehensive 4-tier testing with 352 tests (100% pass rate)
- ๐งน Code Quality: Improved documentation and development comments
- โก Performance: Optimized E2E test timeouts for reliable API integration
- ๐ง Tool Enhancement: Updated content access tools with pagination support
Key Features
- ๐ก๏ธ Built-in Safety Features: Write-safety system, automatic micro-snapshots, and comprehensive version control prevent content loss.
- ๐ Document Structure: Organize content as directories with chapter files.
- ๐ง 26 MCP Tools: Comprehensive document manipulation API organized in 6 functional categories with tools for atomic paragraph operations, pagination-based content access, semantic search, fine-grain summaries, and more.
- ๐ค AI Agents:
- Simple Agent: Stateless, single-turn execution for discrete operations.
- ReAct Agent: Stateful, multi-turn agent for complex workflows.
- Planner Agent: Strategic planning with execution for complex task decomposition.
- ๐ Prompt Optimizer: Automated prompt optimization with performance benchmarking and real LLM evaluation.
- ๐ Observability: Structured logging with OpenTelemetry and Prometheus metrics.
- โ Robust Testing: 3-tier testing strategy (unit, integration, E2E, evaluation).
- ๐ Version Control Friendly: Plain Markdown files work great with Git.
Document Organization
.documents_storage/
โโโ my_novel/ # A document
โ โโโ 01-prologue.md # Chapters ordered by filename
โ โโโ 02-chapter-one.md
โ โโโ 03-chapter-two.md
โโโ research_paper/ # Another document
โโโ 00-abstract.md
โโโ 01-introduction.md
โโโ 02-methodology.md
๐ก๏ธ Safety Features
Document MCP includes comprehensive safety features designed to prevent content loss and enable safe writing workflows:
Write-Safety System
- Content Freshness Checking: Automatically validates content modification status before writes
- Conflict Detection: Warns about potential overwrites if content has been modified externally
- Safe Write Operations: All write operations include safety information and warnings
Automatic Version Control
- Micro-Snapshots: Automatic snapshots created before destructive operations
- Named Snapshots: Create checkpoints with messages using
snapshot_document - Version Restoration: Restore to any previous version using
restore_snapshot - Intelligent Diffs: Compare versions with prose-aware diff algorithms
Modification History
- Complete Tracking: All document changes tracked with timestamps and operation details
- Audit Trail: Full history accessible via
get_modification_history - Pattern Analysis: Understand content change patterns over time
๐๏ธ Project Structure
document-mcp/
โโโ document_mcp/ # Core MCP server package
โ โโโ doc_tool_server.py # Main server with modular tool registrations
โ โโโ tools/ # Modular tool architecture (26 tools)
โ โ โโโ __init__.py # Tool registration system
โ โ โโโ document_tools.py # Document management and summaries (6 tools)
โ โ โโโ chapter_tools.py # Chapter operations (5 tools)
โ โ โโโ paragraph_tools.py # Paragraph editing (7 tools)
โ โ โโโ content_tools.py # Unified content access with pagination (5 tools)
โ โ โโโ safety_tools.py # Version control (3 tools)
โ โโโ logger_config.py # Structured logging with OpenTelemetry
โ โโโ metrics_config.py # Prometheus metrics and monitoring
โโโ src/agents/ # AI agent implementations
โ โโโ simple_agent/ # Stateless single-turn agent package
โ โ โโโ main.py # Agent execution logic
โ โ โโโ prompts.py # System prompts
โ โโโ react_agent/ # Stateful multi-turn ReAct agent
โ โ โโโ main.py
โ โโโ shared/ # Shared agent utilities
โ โโโ cli.py # Common CLI functionality
โ โโโ config.py # Enhanced Pydantic Settings
โ โโโ error_handling.py
โโโ prompt_optimizer/ # Automated prompt optimization tool
โ โโโ core.py # Main PromptOptimizer class
โ โโโ evaluation.py # Performance evaluation system
โ โโโ cli.py # Command-line interface
โโโ tests/ # 3-tier testing strategy
โโโ unit/ # Isolated component tests (mocked)
โโโ integration/ # Agent-server tests (real MCP, mocked LLM)
โโโ e2e/ # Full system tests (real APIs)
โโโ evaluation/ # Performance benchmarking and prompt evaluation
๐ค Agent Examples and Tutorials
Agent Architecture Overview
This project provides two distinct agent implementations for document management using the Model Context Protocol (MCP). Both agents interact with the same document management tools but use different architectural approaches and execution patterns.
Quick Reference
| Feature | Simple Agent | ReAct Agent |
|---|---|---|
| Architecture | Single-step execution | Multi-step reasoning loop with intelligent termination |
| Chat History | No memory between queries | Full history maintained across steps |
| Context Retention | Each query is independent | Builds context from all previous steps |
| Best For | Simple queries, direct operations | Complex multi-step tasks |
| Output | Structured JSON response | Step-by-step execution log |
| Error Handling | Basic timeout handling | Advanced retry & circuit breaker |
| Performance | Fast for simple tasks | Optimized for complex workflows |
| Complexity | Simple, straightforward | Advanced with caching & optimization |
| Termination Logic | N/A (single step) | Intelligent completion detection: task completion, step limits, error recovery, timeout management |
| Conversational Flow | Independent query processing | Multi-round conversation support |
| State Management | Clean isolation between queries | Persistent context with proper cleanup |
Getting Started with the Agents
Choose between three agent implementations:
- Simple Agent: Single-step operations, structured JSON output, fast performance
- ReAct Agent: Multi-step workflows, contextual reasoning, production reliability
- Planner Agent: Strategic planning with execution, complex task decomposition
Agent Selection:
- Use Simple Agent for: direct operations, JSON output, prototyping
- Use ReAct Agent for: complex workflows, multi-step planning, production environments, reasoning transparency
- Use Planner Agent for: strategic planning, complex task decomposition, hierarchical execution
๐ Prompt Optimization
The system includes an automated prompt optimizer that uses real performance benchmarks to improve agent efficiency:
# Optimize specific agent
python3 -m prompt_optimizer simple
python3 -m prompt_optimizer react
python3 -m prompt_optimizer planner
# Optimize all agents
python3 -m prompt_optimizer all
Key Features:
- Safe Optimization: Conservative changes that preserve all existing functionality
- Performance-Based: Uses real execution metrics to evaluate improvements
- Comprehensive Testing: Validates changes against 352 tests (unit + integration + E2E + evaluation)
- Automatic Backup: Safe rollback if optimization fails or breaks functionality
- Multi-Agent Support: Works with Simple, ReAct, and Planner agents
Practical Examples - Step by Step
Once you have both the MCP server and an agent running, try these structured examples:
Example 1: Create Your First Document
๐ค User: Create a new document called 'Getting Started Guide'
๐ค Agent: โ
Successfully created the new document named 'Getting Started Guide'.
Example 2: Add Content with Chapters
๐ค User: Add a chapter named '01-introduction.md' to 'Getting Started Guide' with content '# Introduction\nWelcome to Document MCP!'
๐ค Agent: โ
Chapter '01-introduction.md' created successfully in document 'Getting Started Guide'.
Example 3: View Your Documents
๐ค User: List all my documents
๐ค Agent: โ
Found 1 document: 'Getting Started Guide' with 1 chapter, 4 words total.
Example 4: Analyze Document Statistics
๐ค User: Get statistics for 'Getting Started Guide'
๐ค Agent: โ
Document 'Getting Started Guide' contains 1 chapter, 4 words, and 1 paragraph.
Example 5: Read Full Content with Pagination
๐ค User: Read the full document 'Getting Started Guide'
๐ค Agent: โ
Retrieved page 1 of document 'Getting Started Guide' (2,450 characters, 1 of 1 pages).
Content: # Introduction
Welcome to Document MCP!
# For larger documents with navigation:
๐ค User: Read page 2 of document 'Large Guide' with 25KB page size
๐ค Agent: โ
Retrieved page 2 of document 'Large Guide' (25,000 characters, 2 of 5 pages).
Navigation: Previous page available, Next page available (page 3)
# Progressive loading pattern:
๐ค User: Read all pages of 'Large Guide' progressively
๐ค Agent: โ
Processing all 5 pages progressively... Complete! Total: 125,000 characters processed.
Advanced Examples
Search and Replace Operations:
๐ค User: Find "Welcome" in document 'Getting Started Guide'
๐ค Agent: โ
Found 1 paragraph containing "Welcome" in chapter '01-introduction.md'.
๐ค User: Replace "Welcome" with "Hello" in document 'Getting Started Guide'
๐ค Agent: โ
Replaced 1 occurrence across 1 chapter in document 'Getting Started Guide'.
Complex Multi-Step Workflows (ReAct Agent):
๐ค User: Create a research paper structure with an abstract, introduction, methodology, and conclusion
๐ค ReAct Agent:
Thought: I need to create a document and then add multiple chapters for a research paper structure.
Action: create_document with name 'Research Paper'
Observation: Document created successfully
Thought: Now I need to add the abstract chapter...
Action: add_chapter with name '00-abstract.md'...
[Continues with step-by-step execution]
Try It Now - Interactive Walkthrough
Once you have the MCP server running, you can immediately test all features:
Quick Configuration Check:
# Verify your setup is working
python src/agents/simple_agent/main.py --check-config
Test the Complete Workflow:
# Start with simple operations
python src/agents/simple_agent/main.py --query "Create a new document called 'Test Document'"
python src/agents/simple_agent/main.py --query "Add a chapter named '01-intro.md' with content 'Hello World!'"
python src/agents/simple_agent/main.py --query "List all my documents"
python src/agents/simple_agent/main.py --query "Read the full document 'Test Document'"
# Try complex multi-step workflows
python src/agents/react_agent/main.py --query "Create a book outline with 3 chapters"
Interactive Mode for Extended Testing:
# Simple agent for straightforward tasks
python src/agents/simple_agent/main.py --interactive
# ReAct agent for complex reasoning
python src/agents/react_agent/main.py --interactive
This immediate hands-on approach lets you:
- โ Verify your configuration works correctly
- ๐ See real responses from both agent types
- ๐ง Experience the ReAct agent's reasoning process
- ๐ Build confidence before diving deeper
Automatic LLM Detection
The system automatically detects which LLM to use based on your .env configuration:
- OpenAI (Priority 1): If
OPENAI_API_KEYis set, uses OpenAI models (default:gpt-4.1-mini) - Gemini (Priority 2): If
GEMINI_API_KEYis set, uses Gemini models (default:gemini-2.5-flash)
When an agent starts, it will display which model it's using:
Using OpenAI model: gpt-4.1-mini
or
Using Gemini model: gemini-2.5-flash
Configuration
Both agents share the same configuration system and support the same command-line interface:
# Check configuration (both agents)
python src/agents/simple_agent/main.py --check-config
python src/agents/react_agent/main.py --check-config
# Single query mode (both agents)
python src/agents/simple_agent/main.py --query "list all documents"
python src/agents/react_agent/main.py --query "create a book with multiple chapters"
# Interactive mode (both agents)
python src/agents/simple_agent/main.py --interactive
python src/agents/react_agent/main.py --interactive
๐ง Troubleshooting
Setup Verification Checklist
Run through this checklist if you're having issues:
- โ
.envfile exists with valid API key - โ
Virtual environment activated (
source .venv/bin/activate) - โ
Package and dependencies installed (
pip install -e ".[dev]") - โ MCP server running on localhost:3001
- โ
Configuration check passes (
--check-config)
Getting Help
If you're experiencing issues, follow this support hierarchy:
- ๐ Check the Manual Testing Guide - Comprehensive troubleshooting for common issues
- ๐ง Run diagnostics:
python src/agents/simple_agent/main.py --check-config - ๐งช Test basic functionality:
python src/agents/simple_agent/main.py --query "list documents" - ๐ Search existing issues: GitHub Issues
- ๐ฌ Open a new issue: Include output from
--check-configand system details
Common Solutions:
- API key issues โ See Configuration
- MCP server connection โ Check if
document-mcp stdiois running - Model loading failures โ Try different model in
.env - Timeout issues โ See Performance troubleshooting
Testing Strategy
# Run all tests
pytest
# Run by test tier
python3 -m pytest tests/unit/ # Unit tests (fastest, no external deps)
python3 -m pytest tests/integration/ # Integration tests (real MCP, mocked LLM)
python3 -m pytest tests/e2e/ # E2E tests (requires API keys)
# Run with coverage
python3 -m pytest --cov=document_mcp --cov-report=html
# Quality checks
python3 scripts/quality.py full
๐ ๏ธ Development Setup
Prerequisites
- Python 3.8+
- Git
Local Development
# Clone the repository
git clone https://github.com/document-mcp/document-mcp.git
cd document-mcp
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install package in editable mode with development dependencies
pip install -e ".[dev]"
Running Tests
# Use the pytest runner
python scripts/run_pytest.py
# Or run pytest directly
python -m pytest tests/ -v
Code Quality Management
This project maintains high code quality standards through automated tools and scripts. The quality system is managed through a dedicated script:
# Quick quality check (linting and type checking only)
python scripts/quality.py check
# Apply automatic fixes and format code
python scripts/quality.py fix # Remove unused imports, fix issues
python scripts/quality.py format # Black formatting + isort
# Run specific quality tools
python scripts/quality.py lint # flake8 linting
python scripts/quality.py typecheck # mypy type checking
# Complete quality pass (recommended before commits)
python scripts/quality.py full # fix + format + check
# Get detailed output for debugging
python scripts/quality.py check --verbose
Quality Tools Configured
- Black: Code formatting (88 character line length)
- isort: Import sorting and organization
- flake8: Linting and style checking (configured in
.flake8) - mypy: Static type checking (configured in
pyproject.toml) - autoflake: Automated cleanup of unused imports/variables
- pytest: Comprehensive test suite execution
Quality Standards
- Line Length: 88 characters (Black standard)
- Import Style: Black-compatible with isort
- Type Hints: Encouraged for public APIs
- Complexity: Maximum cyclomatic complexity of 10
- Test Coverage: Comprehensive 4-tier testing (352 tests with 100% pass rate)
Recommended Workflow
# During development
python scripts/quality.py format # Format as you code
# Before committing
python scripts/quality.py full # Complete quality pass
python scripts/run_pytest.py # Run tests
# Quick check
python scripts/quality.py check # Verify quality standards
The quality management system provides comprehensive automation for maintaining code standards throughout development.
Test Coverage
The system provides enterprise-grade reliability with 352 comprehensive tests covering:
Core Testing Areas:
- Document Operations: Full CRUD operations and pagination system (181 unit tests)
- Agent Architecture: Complete testing of both Simple and ReAct agent implementations (155 integration tests)
- MCP Protocol: End-to-end server-client communication validation (6 E2E tests)
- Performance Benchmarking: Real API testing and prompt optimization (4 evaluation tests)
- Monitoring & Metrics: OpenTelemetry and Prometheus integration (6 metrics tests)
- Quality Assurance: Centralized fixtures and comprehensive cleanup validation
Test Results: 100% success rate (352/352 tests passing) with execution time under 6 minutes
The test suite spans unit, integration, and end-to-end categories, ensuring production-ready reliability with proper resource management and state isolation.
๐ Documentation
End User Guides
- ๐ฆ Package Installation Guide - Install and use the MCP server with Claude Code (universal setup)
- ๐ Manual Testing & User Workflows - Complete guide for creative writing, editing workflows, and troubleshooting
- ๐ Quick Start Examples - Get up and running in minutes
Developer Guides
- ๐ค Agent Architecture Guide - Choose the right agent for your workflow
- ๐๏ธ MCP Design Patterns Guide - Production-ready patterns for context management, partial hydration, and security best practices
- API Reference - Complete MCP tools documentation
- Testing Strategy - 3-tier testing architecture and best practices
๐ค Contributing
I welcome any contribution!
๐ Related Resources
- Pydantic AI Documentation: Learn more about Pydantic AI
- MCP Specification: Model Context Protocol details
- Model Context Protocol (MCP): Official MCP repository
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Built with Model Context Protocol (MCP)
- Powered by Pydantic AI
- Agents support both OpenAI and Google Gemini
โญ Star this repo if you find it useful!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file document_mcp-0.0.4.tar.gz.
File metadata
- Download URL: document_mcp-0.0.4.tar.gz
- Upload date:
- Size: 81.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22bdb23e2164c7eb5d510193e2a2fbffbcb442e3b1062fd77310b7cb7381d5c6
|
|
| MD5 |
ee0ed372abdd92aa99be5920e0a7cc2f
|
|
| BLAKE2b-256 |
b36d69fba9ab0a54aa765dd98457b8bc9d3fca8a6396f5edcec48ef2f73974d6
|
File details
Details for the file document_mcp-0.0.4-py3-none-any.whl.
File metadata
- Download URL: document_mcp-0.0.4-py3-none-any.whl
- Upload date:
- Size: 82.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbd0acb4dd811c68e40d0468215cca42a9528d3d05fce3fab944cf7c7b3b2156
|
|
| MD5 |
54e665b2948fc2449e3aaf3e8eabf00f
|
|
| BLAKE2b-256 |
38acf0e4e49d9e1e6aa9ad0699a66328e867dc501246dbffcaa804d40350f2de
|