Tool Mode
Tool Mode development guide
Tool Mode Development Guide
This guide demonstrates how to develop an MCP (Model Context Protocol) based scmcphub API using Tool Mode. Tool Mode allows you to create custom tools that can be integrated into the scmcphub ecosystem for single-cell analysis workflows.
Overview
Tool Mode enables you to:
- Create custom analysis tools that work with AnnData objects
- Integrate external libraries and algorithms
- Provide standardized interfaces for data processing
- Build reusable components for the scmcphub community
What We’ll Build
We’ll create a simple demo that demonstrates the basic structure of an MCP tool. This demo will:
- Query data from
adata.uns
(unstructured data storage in AnnData) - List available keys in the unstructured data
- Follow scmcphub conventions and best practices
Prerequisites
Before starting, ensure you have:
- Python 3.8 or higher
- Basic understanding of AnnData objects
- Familiarity with Pydantic for data validation
Project Setup
1. Install UV Package Manager
UV is a fast Python package installer and resolver. It’s recommended for scmcphub development:
2. Initialize the Project
Create a new MCP project using UV:
This creates a basic Python package structure with the following layout:
3. Create Required Files
We need to create three main files for our MCP tool:
Implementation Details
1. Schema Definition (schema.py
)
The schema file defines the input parameters for your tools using Pydantic models. This ensures type safety and provides automatic validation:
Key Points:
Field(...)
indicates this is a required parameter- The
description
provides documentation for the tool - Pydantic automatically validates the input type
2. Server Implementation (server.py
)
The server file contains the core logic of your MCP tools. Here’s a detailed breakdown:
Important Concepts:
- FastMCP: The framework that provides the MCP functionality
- ToolError: Custom exception for tool-specific errors
- AdataInfo: Standard parameter for specifying which AnnData object to use
- get_ads(): Utility function to access the AnnData manager
- add_op_log(): Function to log operations for tracking
3. CLI Implementation (cli.py
)
The CLI file provides a command-line interface for your MCP tools:
Benefits of using MCPCLI:
- Standardized CLI interface across all scmcphub tools
- Built-in help and documentation
- Consistent command-line argument handling
- Integration with the broader scmcphub ecosystem
4. Package Configuration (pyproject.toml
)
Update the project configuration to include all necessary dependencies:
Running the Demo
1. Install in Development Mode
Install your package in development mode to enable live code changes:
2. Test the CLI
Verify that the CLI is working correctly:
This should display help information about available commands.
3. Start the MCP Server
Run the MCP server in tool mode:
This starts the server and makes your tools available for use.
Understanding the Architecture
MCP Tool Lifecycle
- Registration: Tools are registered using the
@mcp.tool()
decorator - Validation: Input parameters are validated using Pydantic schemas
- Execution: The tool function is called with validated parameters
- Response: Results are returned in a structured format
- Logging: Operations are logged for debugging and tracking
Error Handling
The implementation includes comprehensive error handling:
- ToolError: For expected errors (e.g., missing keys)
- Exception: For unexpected errors with proper logging
- Validation: Automatic parameter validation through Pydantic
Integration Points
Your MCP tools integrate with:
- AnnData Manager: For accessing and managing data objects
- Shared Schemas: For consistent parameter definitions
- Logging System: For operation tracking and debugging
- CLI Framework: For command-line access
Best Practices
1. Schema Design
- Use descriptive field names and descriptions
- Provide default values when appropriate
- Include type hints for better IDE support
2. Error Handling
- Use specific error messages
- Log errors appropriately
- Handle both expected and unexpected errors
3. Documentation
- Write clear docstrings for all tools
- Include examples in documentation
- Document parameter types and constraints
4. Testing
- Write unit tests for all tools
- Test error conditions
- Use the provided test fixtures
Next Steps
This simple demo shows the basic structure. For more complex tools, you can:
-
Add More Sophisticated Parameter Validation
- Custom validators for complex data types
- Conditional parameter requirements
- Cross-parameter validation
-
Implement Multiple Tools in the Same Module
- Group related functionality
- Share common utilities
- Maintain consistent interfaces
-
Add Plotting Tools Using Shared Utilities
- Integrate with matplotlib/seaborn
- Use shared plotting configurations
- Support interactive visualizations
-
Integrate with External Libraries and APIs
- Bioinformatics tools (scanpy, anndata)
- Statistical packages (scipy, statsmodels)
- Machine learning frameworks (scikit-learn, tensorflow)
-
Add Comprehensive Testing and Documentation
- Unit tests with pytest
- Integration tests
- API documentation
- User guides
Extending the Pattern
The pattern shown here follows the scmcphub conventions and can be extended to build more complex MCP modules for single-cell analysis workflows. Key extension points include:
- Data Processing Pipelines: Chain multiple tools together
- Custom Algorithms: Implement novel analysis methods
- Visualization Tools: Create custom plotting functions
- Integration Tools: Connect with external databases and APIs
This foundation provides a solid base for building sophisticated single-cell analysis tools that integrate seamlessly with the scmcphub ecosystem.