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 for local orchestration
- Defaults to
127.0.0.1 - Supports bearer-token auth through
GNN_MCP_TOKEN - Supports per-client rate limiting through
GNN_MCP_RATE_LIMIT_PER_MINUTE - Exposes only a safe tool allowlist by default; unsafe tools require explicit opt-in
- Denies resource reads by default; expose individual URIs with
GNN_MCP_SAFE_RESOURCES - Filters HTTP capability responses to the same safe tool/resource surface
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
GNN_MCP_TOKEN=local-dev-token python -m src.mcp.cli server --transport http --host 127.0.0.1 --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 website 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 stdioGNN_MCP_TOKEN=local-dev-token python -m src.mcp.cli server --transport http --host 127.0.0.1 --port 8080HTTP tool execution and resource reads are guarded separately. Tool calls are
limited to the safe HTTP allowlist, while mcp.resource.get returns an error
unless the requested URI is listed in GNN_MCP_SAFE_RESOURCES.
Rate limiting is evaluated before bearer authentication so bad-token traffic is
throttled by the same per-client limit.
python -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
- HTTP transport requires
Authorization: Bearer <GNN_MCP_TOKEN>by default. - Missing or invalid bearer tokens receive
401before JSON-RPC execution. - Unauthenticated HTTP is only available for explicit loopback development with
GNN_MCP_ALLOW_INSECURE_LOCAL=1; non-loopback clients still receive401. - Resource reads over HTTP are denied unless the exact URI is included in
GNN_MCP_SAFE_RESOURCES. - HTTP capability discovery lists only tools and resources exposed by those allowlists.
- Use stdio transport for maximum security.
- Use stdio transport for local-only access
- Keep HTTP bound to
127.0.0.1unless a separate trusted reverse proxy is used - Set
GNN_MCP_TOKENfor every HTTP run - Set
GNN_MCP_SAFE_RESOURCESonly for resource URIs that are safe to expose - 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 and live audit commands for the registered tool surface
- GNN Project Documentation
- MCP Integration Guide
- Project overview: ../../README.md
- Comprehensive docs: ../../DOCS.md
- Pipeline details (Step 22): ../../doc/pipeline/README.md