trackmcp
Back to directory
pickleton89

mutation-clinical-trial-matching-mcp

View on GitHub

MCP server to query clinicaltrial.gov to identify mutation specific trials

0 stars PythonAI & Machine Learning Updated Jul 21, 2025
agentic-codingai-assistantclaude-desktopclinicaltrialsgovgenetic-mutationllm-toolsmcp-serveroncologyprecision-medicine

Documentation

Mutation Clinical Trial Matching MCP

License: MIT
Python 3.11+
Version 0.2.1
Tests
Code Style: Ruff
Code Deduplication
Architecture

A high-performance unified Model Context Protocol (MCP) server that enables Claude Desktop to search for clinical trial matches on clinicaltrials.gov based on genetic mutations.

Status

Production Ready - This project has completed a major architectural transformation, achieving a unified codebase with 60% code reduction while maintaining 100% backward compatibility:

βœ… Repository Quality Excellence: 99.6% improvement in code quality with modern Python standards (Python 3.11+ compatibility)

βœ… Professional Type Safety: 69% reduction in type diagnostics with comprehensive typing standards

βœ… Unified Architecture: Single server supporting both sync and async modes with runtime selection

βœ… Code Deduplication: 60% reduction (~1,000 lines) through comprehensive 4-phase consolidation

βœ… Legacy Cleanup: Professional codebase structure with 3,435 lines of deprecated code removed

βœ… Zero Breaking Changes: Complete backward compatibility with automatic migration guidance via compatibility layer

βœ… Enterprise Features: Circuit breakers, metrics, retry logic, distributed caching, and monitoring

βœ… High Performance: Async architecture with 80% performance improvement and concurrent processing

βœ… API Resilience: Robust error handling with 403 Forbidden error resolution via unified HTTP client

βœ… Comprehensive Testing: Complete test suite with 114 tests covering unified components

βœ… Modern Tooling: Uses `uv` for dependency management and follows Python best practices

βœ… Production Monitoring: Prometheus metrics, cache analytics, and health monitoring dashboards

The server is actively used and maintained, with the unified architecture documented in the changelog.

AI-Collaborative Development

This project was developed through human-AI collaboration, combining domain expertise with LLM-directed implementation:

  • 🧠 Domain Direction: 20+ years cancer research experience guided architecture and feature requirements
  • πŸ€– AI Implementation: Code generation, API design, and performance optimization through systematic LLM direction
  • πŸ”„ Quality Assurance: Iterative refinement ensuring professional standards and production reliability
  • πŸ“ˆ Development Approach: Demonstrates how domain experts can effectively leverage AI tools to build bioinformatics platforms

Methodology: This AI-collaborative approach combines biological expertise with AI capabilities to accelerate development while maintaining code quality and reliability standards.

Overview

This project follows the Agentic Coding principles to create a system that integrates Claude Desktop with the clinicaltrials.gov API. The server allows for natural language queries about genetic mutations and returns summarized information about relevant clinical trials.

mermaid
flowchart LR
    Claude[Claude Desktop] |MCP Protocol| Server[Unified MCP Server]
    
    subgraph Detection[Runtime Mode Detection]
        Auto[Auto-Detect Event Loop]
        Env[MCP_ASYNC_MODE]
        Config[Configuration Override]
    end
    
    subgraph Cache[Distributed Cache]
        Redis[(Redis)]
        Memory[In-Memory]
    end
    
    subgraph Flow[Unified PocketFlow]
        QueryNode[Unified Query Node] -->|trials_data| SummarizeNode[Unified Summarize Node]
    end
    
    subgraph Services[Service Abstraction Layer]
        HttpClient[Unified HTTP Client]
        TrialsService[Clinical Trials Service]
        LLMService[LLM Service]
    end
    
    subgraph Monitoring[Enterprise Features]
        Metrics[Prometheus Metrics]
        Circuit[Circuit Breaker]
        Analytics[Cache Analytics]
    end
    
    Server -->|mode selection| Detection
    Detection -->|sync/async| Flow
    Server -->|mutation| Flow
    Flow -->|service calls| Services
    Services |cache| Cache
    Services -->|concurrent/sequential requests| API[Clinicaltrials.gov API]
    API -->|trial data| Services
    Flow -->|summary| Server
    Server -->|metrics| Monitoring
    Server -->|formatted response| Claude

Each node in the flow follows the Unified PocketFlow Node pattern with `prep`, `exec`, and `post` methods that automatically handle both sync and async execution modes.

πŸš€ Unified Architecture & Code Deduplication Achievement

This project has completed a comprehensive 4-phase code deduplication effort, transforming from a duplicated codebase into a unified, maintainable architecture:

Code Deduplication Results

MetricAchievement
Code Reduction60% reduction (~1,000 lines eliminated)
Legacy Cleanup3,435 lines removed - Professional codebase structure
Code Quality99.6% improvement - 1,695 of 1,702 linting errors fixed
Type Safety69% reduction in type diagnostics (48 β†’ 15)
Components Unified4 major consolidations (Servers, Nodes, Services, HTTP)
Breaking ChangesZero - Complete backward compatibility with compatibility layer
Performance Gain30-40% memory reduction, 20-30% faster startup
Test Coverage114 tests covering all unified components

Before vs After Consolidation

ComponentBeforeAfterReduction
Servers`primary.py` + `sync_server.py``main.py`70%
Nodes`nodes.py` + `async_nodes.py``unified_nodes.py`85%
Services`query.py` + `async_query.py``service.py`95%
LLM Client`call_llm.py` + `async_call_llm.py``llm_service.py`95%

Key Architectural Improvements

βœ… Runtime Mode Selection: Automatic detection or explicit configuration via `MCP_ASYNC_MODE`

βœ… Single Point of Truth: Unified business logic across sync/async execution

βœ… Auto-Detection: Intelligent mode selection based on execution context

βœ… Service Abstraction: Unified HTTP client and service layer

βœ… Configuration System: Centralized configuration with environment overrides

βœ… Backward Compatibility Layer: Complete `utils/node.py` compatibility module for legacy imports

βœ… Migration Support: Deprecation warnings with clear migration guidance

Project Structure

This project is organized according to the Agentic Coding paradigm:

1. Requirements (Human-led):

    2. Flow Design (Collaborative):

      3. Utilities (Collaborative):

        4. Node Design (AI-led):

          5. Implementation (AI-led):

            Architecture Components

            Unified MCP Server (`servers/main.py`)

            The main unified server implementing the Model Context Protocol with runtime mode selection:

            • Unified Architecture: Single implementation supporting both sync and async modes
            • Runtime Mode Selection: Automatic detection via event loop or explicit `MCP_ASYNC_MODE` configuration
            • Enterprise Tools: Health monitoring, metrics collection, cache management (mode-dependent)
            • Auto-scaling: Circuit breakers and retry logic for robust API communication
            • Cache Warming: Automatically pre-loads common mutations for instant responses (async mode)
            • API Resilience: Handles 403 Forbidden errors with unified HTTP client fallback mechanisms
            • Backward Compatibility: Legacy servers redirect with deprecation warnings

            Unified Service Layer

            Clinical Trials Service (`clinicaltrials/service.py`): Unified API client with mode-aware processing

            • Dual Mode Support: Same interface for both sync (`query_trials`) and async (`aquery_trials`) calls
            • Circuit Breaker Integration: Automatic failure detection and recovery
            • Distributed Caching: Redis-backed caching with in-memory fallback
            • Metrics Collection: Detailed performance and usage analytics
            • API Compatibility: Uses unified HTTP client for reliable clinicaltrials.gov API access

            LLM Service (`utils/llm_service.py`): Unified LLM interaction client

            • Mode-Aware Processing: Supports both sync and async LLM calls
            • Retry Logic: Built-in retry mechanisms with exponential backoff
            • Error Handling: Comprehensive error handling with structured logging

            Unified Nodes (`clinicaltrials/unified_nodes.py`)

            PocketFlow nodes with automatic sync/async execution:

            • QueryTrialsNode: Unified node with mode detection for API requests
            • SummarizeTrialsNode: Unified LLM-powered summarization with retry logic
            • BatchQueryTrialsNode: Batch processing with concurrency control (async) or sequential processing (sync)
            • Auto-Detection: Nodes automatically determine execution mode at runtime

            Unified Foundation Layer

            • Unified HTTP Client (`utils/http_client.py`): Single HTTP client supporting both sync and async with connection pooling
            • Unified Node Framework (`utils/unified_node.py`): Base classes with automatic mode detection
            • Shared Utilities (`utils/shared.py`): Common validation, error handling, and metrics functions
            • Cache Strategies (`utils/cache_strategies.py`): Smart cache warming and invalidation (async mode)
            • Configuration System (`servers/config.py`): Centralized configuration with environment overrides
            • Legacy Compatibility (`servers/legacy_compat.py`): Backward compatibility layer with migration guidance

            Unified Node Pattern Implementation

            This project implements the enhanced PocketFlow Node pattern with unified sync/async execution, providing a modular, maintainable approach to building AI workflows:

            Unified Core Node Classes (`utils/unified_node.py`)

            • UnifiedNode: Base class supporting both sync and async execution with automatic mode detection
            • UnifiedBatchNode: Extension for batch processing with concurrency control (async) or sequential processing (sync)
            • UnifiedFlow: Orchestrates execution with intelligent mode selection

            Unified Implementation Nodes (`clinicaltrials/unified_nodes.py`)

            1. QueryTrialsNode (Unified):

            python
            # Single implementation supporting both modes
               def prep(self, shared): return shared["mutation"]
               
               def exec(self, mutation): 
                   return self.trials_service.query_trials(mutation)  # Sync version
               
               async def aexec(self, mutation): 
                   return await self.trials_service.aquery_trials(mutation)  # Async version
               
               def post(self, shared, mutation, result):
                   shared["trials_data"] = result
                   shared["studies"] = result.get("studies", [])
                   return self.get_next_node_id(result)

            2. SummarizeTrialsNode (Unified):

            python
            # Unified summarization with mode detection
               def prep(self, shared): return shared["studies"]
               
               def exec(self, studies): 
                   return self.llm_service.call_llm(prompt)  # Sync version
               
               async def aexec(self, studies): 
                   return await self.llm_service.acall_llm(prompt)  # Async version
               
               def post(self, shared, studies, summary):
                   shared["summary"] = summary
                   return None  # End of flow

            Unified Flow Execution

            The unified MCP server creates and runs flows with automatic mode detection:

            python
            # Create unified nodes (mode determined at runtime)
            query_node = QueryTrialsNode(async_mode=server.async_mode)
            summarize_node = SummarizeTrialsNode(async_mode=server.async_mode)
            
            # Use PocketFlow chaining syntax
            query_node >> summarize_node
            
            # Create unified flow
            flow = UnifiedFlow(start_node=query_node, async_mode=server.async_mode)
            
            # Run flow with shared context (automatically sync or async)
            shared = {"mutation": mutation}
            if server.async_mode:
                result = await flow.aexecute(shared)
            else:
                result = flow.execute(shared)

            Key Advantages of Unified Pattern

            βœ… Single Implementation: One codebase supports both sync and async execution

            βœ… Auto-Detection: Nodes automatically determine optimal execution mode

            βœ… Runtime Selection: Mode can be selected at server startup or runtime

            βœ… Preserved Interface: Same `prep`, `exec`, `post` pattern maintained

            βœ… Performance Optimization: Mode-specific optimizations (timeouts, concurrency, batch limits)

            βœ… Backward Compatibility: Legacy node patterns continue working with deprecation warnings

            This unified pattern eliminates code duplication while preserving the modular, testable nature of the original PocketFlow design. For more details, see the design document.

            Usage

            1. Install dependencies with uv:

            bash
            uv sync

            2. Configure Claude Desktop to use the unified server:

            json
            {
                 "mcpServers": {
                   "mutation-clinical-trials-mcp": {
                     "command": "uv",
                     "args": ["run", "python", "servers/main.py"],
                     "description": "Unified clinical trials matching server with runtime mode selection"
                   }
                 }
               }

            3. Optional: Configure execution mode via environment variables:

            json
            {
                 "mcpServers": {
                   "mutation-clinical-trials-mcp": {
                     "command": "uv",
                     "args": ["run", "python", "servers/main.py"],
                     "env": {
                       "MCP_ASYNC_MODE": "true"
                     },
                     "description": "Unified server in explicit async mode"
                   }
                 }
               }

            3. Start Claude Desktop and ask questions like:

              4. Use enterprise monitoring tools:


                Integrating with Claude Desktop

                You can configure this project as a Claude Desktop MCP tool. Use path placeholders in your configuration, and substitute them with your actual paths:

                json
                "mutation-clinical-trials-mcp": {
                  "command": "{PATH_TO_VENV}/bin/python",
                  "args": [
                    "{PATH_TO_PROJECT}/servers/main.py"
                  ],
                  "description": "Unified clinical trials matching server with automatic mode selection."
                }

                Legacy Compatibility (Still Supported)

                json
                "mutation-clinical-trials-mcp-legacy": {
                  "command": "{PATH_TO_VENV}/bin/python",
                  "args": [
                    "{PATH_TO_PROJECT}/servers/primary.py"
                  ],
                  "description": "Legacy async server (redirects to unified server with deprecation warnings)."
                }

                Path Variables:

                • `{PATH_TO_VENV}`: Full path to your virtual environment directory.
                • `{PATH_TO_PROJECT}`: Full path to the directory containing your project files.

                Installation Instructions:

                1. Clone the repository to your local machine.

                2. Install uv if you don't have it already:

                bash
                curl -LsSf https://astral.sh/uv/install.sh | sh    # macOS/Linux
                   # or
                   iwr -useb https://astral.sh/uv/install.ps1 | iex    # Windows PowerShell

                3. Create a virtual environment and install dependencies in one step:

                bash
                uv sync

                4. Activate the virtual environment when needed:

                bash
                source .venv/bin/activate    # macOS/Linux
                   .venv\Scripts\activate       # Windows

                5. Determine the full path to your virtual environment and project directory.

                6. Update your configuration with these specific paths.

                Examples:

                • On macOS/Linux:
                json
                "command": "/Users/username/projects/mutation_trial_matcher/.venv/bin/python"
                • On Windows:
                json
                "command": "C:\\Users\\username\\projects\\mutation_trial_matcher\\.venv\\Scripts\\python.exe"

                Path Finding Tips:

                • To find the exact path to your Python interpreter in the virtual environment, run:
                  • `which python` (macOS/Linux)
                  • `where python` (Windows, after activating the venv)
                • For the project path, use the full path to the directory containing `servers/primary.py`.

                Future Improvements

                For a comprehensive list of planned enhancements and future work, please see the future_work.md document.

                Dependencies

                This project relies on the following key dependencies:

                • Python 3.11+ - Base runtime environment (lowered from 3.13+ for broader compatibility)
                • FastMCP (`fastmcp>=2.10.2`) - High-performance async MCP framework
                • PocketFlow (`pocketflow>=0.0.1`) - Framework for building modular AI workflows with the Node pattern
                • Requests (`requests==2.31.0`) - HTTP library for clinicaltrials.gov API calls (dev dependency for legacy test compatibility)
                • HTTPX (`httpx>=0.28.1`) - Async HTTP client for direct Anthropic API calls
                • Redis (`redis>=6.2.0`) - Optional distributed caching backend
                • Python-dotenv (`python-dotenv==1.1.0`) - Environment variable management

                Enterprise Features:

                • Prometheus metrics collection and monitoring
                • Circuit breaker patterns for fault tolerance
                • Distributed caching with Redis backend
                • Cache warming strategies for performance optimization

                All dependencies can be installed using `uv sync` as described in the installation instructions.

                Troubleshooting

                If Claude Desktop disconnects from the MCP server:

                • Check logs at: `~/Library/Logs/Claude/mcp-server-mutation-clinical-trials-mcp.log`
                • Restart Claude Desktop
                • Verify the server is running correctly with `uv run python servers/main.py`
                • Check for deprecation warnings if using legacy servers (`servers/primary.py` or `servers/legacy/sync_server.py`)

                Redis Connection Warnings:

                • Redis connection errors are expected if Redis is not installed - the server uses in-memory caching as fallback
                • To eliminate warnings: `brew install redis && brew services start redis`
                • The server works perfectly without Redis, just with reduced caching performance

                Cache Warming on Startup:

                • Server automatically queries 15 common mutations on startup for performance optimization
                • This is normal behavior and improves response times for frequent queries
                • To disable: comment out `asyncio.run(startup_tasks())` in `servers/primary.py`

                Development History

                This project evolved through multiple phases of AI-collaborative development:

                Phase 1 (2024-04-30): Initial prototype using synchronous architecture

                Phase 2 (2024-12): Enhanced with comprehensive testing and documentation

                Phase 3 (2025-01): Major refactoring for improved organization and maintainability

                Phase 4 (2025-01): Full async migration with enterprise features and 80% performance improvement

                Phase 5 (2025-07): API resilience improvements and 403 error resolution

                Phase 6 (2025-07): Code Deduplication Project - Comprehensive 4-phase unification effort

                Phase 7 (2025-07): Repository Quality Excellence - Professional code standards and legacy cleanup

                Recent Achievements (July 2025)

                Code Deduplication Project:

                Phase 1: Foundation Layer - Unified HTTP client and shared utilities

                Phase 2: Service Layer Consolidation - Unified LLM and Clinical Trials services

                Phase 3: Node Layer Unification - Enhanced UnifiedNode framework

                Phase 4: Server Consolidation - Complete unified architecture

                Repository Quality Excellence:

                • Code Quality: 99.6% improvement (1,695 of 1,702 linting errors fixed)
                • Type Safety: 69% reduction in type diagnostics (48 β†’ 15)
                • Professional Cleanup: 3,435 lines of deprecated code removed
                • Compatibility Layer: Complete backward compatibility with `utils/node.py` compatibility module
                • Python Compatibility: Lowered requirement from Python 3.13+ to 3.11+ for broader adoption

                Results: 60% code reduction (~1,000 lines eliminated), zero breaking changes, unified sync/async architecture, production-ready code quality

                Current Version (v0.2.1): Production-ready unified server with enterprise features, automatic mode selection, professional type safety, and comprehensive backward compatibility. Developed through collaboration with Claude Code, leveraging 20+ years of cancer research domain expertise to guide AI implementation and architectural transformation.

                Contributing

                We welcome contributions to improve the Mutation Clinical Trial Matching MCP! Here's how you can get involved:

                Development Setup

                1. Clone the repository:

                bash
                git clone https://github.com/pickleton89/mutation-clinical-trial-matching-mcp.git
                   cd mutation-clinical-trial-matching-mcp

                2. Install dependencies:

                bash
                uv sync

                3. Run tests:

                bash
                uv run python -m unittest discover tests/

                Contribution Guidelines

                • Follow the PocketFlow Node pattern for new features
                • Add comprehensive tests for any new functionality
                • Update documentation including relevant docstrings and README sections
                • Follow Python best practices and maintain type hints
                • Run linting and type checking before submitting PRs

                Areas for Contribution

                • Performance optimizations for large-scale clinical trial searches
                • Additional mutation formats and standardization
                • Enhanced summarization capabilities with more detailed filtering
                • Integration with other clinical databases beyond ClinicalTrials.gov
                • UI/UX improvements for the Claude Desktop integration

                Reporting Issues

                Please use the GitHub Issues page to report bugs or request features.

                License

                This project is licensed under the MIT License - see the LICENSE file for details.

                Acknowledgements

                This project was built using the PocketFlow-Template-Python as a starting point. Special thanks to the original contributors of that project for providing the foundation and structure that made this implementation possible.

                The project follows the Agentic Coding methodology as outlined in the original template.


                ⚠️ Disclaimer

                This project is a prototype and is intended for research and demonstration purposes only. It should not be used to make medical decisions or as a substitute for professional medical advice, diagnosis, or treatment. Due to the limitations of large language models (LLMs), the information provided by this tool may be incomplete, inaccurate, or outdated. Users should exercise caution and consult qualified healthcare professionals before making any decisions based on the outputs of this system.


                Frequently asked questions

                What is mutation-clinical-trial-matching-mcp?

                mutation-clinical-trial-matching-mcp is MCP server to query clinicaltrial.gov to identify mutation specific trials

                How do I install mutation-clinical-trial-matching-mcp?

                Open the GitHub repository and follow its README. Most MCP servers are added to your client's MCP config, then called by your agent.

                Is mutation-clinical-trial-matching-mcp open source?

                Yes β€” it is hosted on GitHub at https://github.com/pickleton89/mutation-clinical-trial-matching-mcp.

                Related MCP tools

                Run your own MCP server? See who uses it and what to fix.

                Measure it with TrackMCP