This directory contains the comprehensive Model Context Protocol (MCP) implementation for the GeneralizedNotationNotation (GNN) project. The MCP server exposes all GNN functionalities as standardized tools that can be accessed by MCP-compatible clients.
The GNN MCP implementation provides:
- Core MCP Server: JSON-RPC 2.0 compliant server with tool and resource management
- Multiple Transport Layers: stdio and HTTP transport support
- Comprehensive Tool Ecosystem: Tools from all GNN modules (gnn, type_checker, export, visualization, etc.)
- Meta-Tools: Server introspection and diagnostic capabilities
- CLI Interface: Command-line access to all MCP functionality
- Extensible Architecture: Easy addition of new tools and resources
graph TD
Client[MCP Client] --> Transport{Transport Layer}
Transport -->|stdio| Stdio[Stdio Server]
Transport -->|http| HTTP[HTTP Server]
Stdio & HTTP --> Core[Core MCP Server]
Core --> Reg[Tool Registry]
Core --> Res[Resource Manager]
Core --> Meta[Meta Tools]
Reg --> GNN[GNN Tools]
Reg --> Viz[Viz Tools]
Reg --> Export[Export Tools]
Reg --> Others[...]
The central MCP server implementation that:
- Manages tool and resource registration
- Handles JSON-RPC 2.0 requests
- Provides module discovery and loading
- Implements error handling and logging
- Tracks performance metrics
Key Features:
- Dynamic module loading
- Tool and resource registration
- Performance tracking
- Error handling with custom MCP error codes
- Server status monitoring
- Reads JSON-RPC requests from stdin
- Writes responses to stdout
- Multi-threaded architecture for concurrent processing
- Ideal for local process communication
- HTTP-based JSON-RPC server
- Supports both GET and POST requests
- Configurable host and port
- Suitable for network-based access
Comprehensive CLI for MCP operations:
# List all capabilities
python -m src.mcp.cli list
# Execute a tool
python -m src.mcp.cli execute get_gnn_files --params '{"target_dir": "doc"}'
# Get server status
python -m src.mcp.cli status
# Start server
python -m src.mcp.cli server --transport stdio
python -m src.mcp.cli server --transport http --host 0.0.0.0 --port 8080Server introspection and diagnostic tools:
get_mcp_server_capabilities: Full server capabilitiesget_mcp_server_status: Operational status and metricsget_mcp_server_auth_status: Authentication configurationget_mcp_server_encryption_status: Encryption statusget_mcp_module_info: Detailed module informationget_mcp_tool_categories: Tools organized by categoryget_mcp_performance_metrics: Performance statistics
- GNN file discovery and parsing
- Model structure analysis
- Parameter extraction and validation
- GNN syntax validation
- Resource estimation
- Type consistency checking
- Multi-format export (JSON, XML, GraphML, GEXF, Pickle)
- Network graph export
- Structured data preservation
- Graph visualization
- Matrix visualization
- Ontology relationship diagrams
- PyMDP code generation
- RxInfer.jl model translation
- Template-based code generation
- Script execution
- Result capture and reporting
- Multi-backend support
- AI-powered model analysis
- Enhancement suggestions
- Natural language explanations
- HTML site generation
- Report aggregation
- Interactive elements
- Audio generation and sonification
- Model sonification
- Real-time audio processing
- Pipeline step discovery
- Execution monitoring
- Configuration management
- System diagnostics
- File operations
- Environment validation
python -m src.mcp.cli server --transport stdiopython -m src.mcp.cli server --transport http --host 0.0.0.0 --port 8080python -m src.mcp.cli list --format humanpython -m src.mcp.cli execute get_gnn_files --params '{"target_dir": "doc", "recursive": true}'python -m src.mcp.cli status --format json{
"jsonrpc": "2.0",
"id": 1,
"method": "mcp.capabilities",
"params": {}
}{
"jsonrpc": "2.0",
"id": 2,
"method": "mcp.tool.execute",
"params": {
"name": "get_gnn_files",
"params": {
"target_dir": "doc",
"recursive": true
}
}
}{
"jsonrpc": "2.0",
"id": 3,
"method": "get_gnn_files",
"params": {
"target_dir": "doc",
"recursive": true
}
}The MCP implementation provides comprehensive error handling:
-32700: Parse error-32600: Invalid Request-32601: Method not found-32602: Invalid params-32603: Internal error
-32000: MCP-specific errors-32001: Tool execution errors-32002: Resource retrieval errors-32003: Module loading errors
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32001,
"message": "Tool execution failed",
"data": {
"tool": "get_gnn_files",
"details": "Target directory not found"
}
}
}The MCP server tracks various performance metrics:
- Request count and error rates
- Average execution times per tool
- Module loading statistics
- Server uptime and activity
Access performance data via:
python -m src.mcp.cli execute get_mcp_performance_metrics- stdio: Local process only, high security
- HTTP: Network accessible, consider HTTPS for production
- No built-in authentication (relies on transport security)
- Implement authentication for network deployments
- Use stdio transport for maximum security
- Use stdio transport for local-only access
- Configure HTTPS for HTTP transport if needed
- Implement authentication for untrusted networks
- Monitor access logs and performance metrics
- Create or update the module's
mcp.pyfile - Implement tool functions with proper error handling
- Register tools using
mcp_instance.register_tool() - Add comprehensive documentation and schemas
def register_tools(mcp_instance):
mcp_instance.register_tool(
name="my_tool",
func=my_tool_function,
schema={
"type": "object",
"properties": {
"param1": {"type": "string"}
},
"required": ["param1"]
},
description="Description of my tool",
module="my_module",
category="My Category",
version="1.0.0"
)Use the CLI to test tools:
# Test tool execution
python -m src.mcp.cli execute my_tool --params '{"param1": "value"}'
# Test tool info
python -m src.mcp.cli info my_toolConfigure Claude Desktop to use the GNN MCP server:
{
"mcpServers": {
"gnn": {
"command": "python",
"args": ["-m", "src.mcp.cli", "server", "--transport", "stdio"]
}
}
}The server is compatible with any JSON-RPC 2.0 MCP client. Use the HTTP transport for network-based clients or stdio for local integration.
-
Module Import Errors
- Ensure all dependencies are installed
- Check Python path configuration
- Verify module structure
-
Tool Execution Failures
- Check tool parameters and schemas
- Review error messages and logs
- Validate input data
-
Server Connection Issues
- Verify transport configuration
- Check firewall settings for HTTP transport
- Ensure proper permissions
Enable verbose logging:
python -m src.mcp.cli --verbose listCheck log files in the output directory:
ls -la output/logs/When contributing to the MCP implementation:
- Follow the established patterns for tool registration
- Add comprehensive error handling
- Include proper documentation and schemas
- Test with both stdio and HTTP transports
- Update this README for new features
- Model Context Protocol Specification
- JSON-RPC 2.0 Specification
- Comprehensive MCP Documentation — Full implementation guide with 133+ tools registered
- GNN Project Documentation
- MCP Integration Guide
- Project overview: ../../README.md
- Comprehensive docs: ../../DOCS.md
- Pipeline details (Step 22): ../../doc/pipeline/README.md