"""
DoWhy MCP Server - Main server implementation
Implements a FastMCP server for causal inference using DoWhy.
"""
import logging
from typing import Any, Dict, List, Optional, Union
from mcp.server.fastmcp import FastMCP
from .tools import (
register_modeling_tools,
register_estimation_tools,
register_sensitivity_tools,
register_discovery_tools,
register_attribution_tools,
register_root_cause_tools,
register_counterfactual_tools,
)
from .tools.dowhy_native_estimation import register_dowhy_native_tools
from .tools.gcm import register_gcm_tools
from .tools.refutation import register_refutation_tools
logger = logging.getLogger("dowhy-mcp-server")
def create_server(
name: str = "DoWhy MCP Server",
version: str = "2.0.0",
description: str = "Professional Causal Inference Tools",
mount_path: Optional[str] = None,
stateless_http: bool = False,
json_response: bool = False,
) -> FastMCP:
"""
Create a new DoWhy MCP server instance.
Args:
name: Server name
version: Server version
description: Server description
mount_path: Path to mount the server on (for HTTP transports)
stateless_http: Whether to use stateless HTTP mode
json_response: Whether to use JSON response format
Returns:
Configured FastMCP server instance
"""
# Create the server
server = FastMCP(
name=name,
stateless_http=stateless_http,
json_response=json_response,
)
# Store metadata in server instance
server._version = version
server._description = description
server._mount_path = mount_path
# Register all tools
register_modeling_tools(server)
register_estimation_tools(server) # 现在是纯 DoWhy 实现
register_dowhy_native_tools(server) # DoWhy 原生工具
register_gcm_tools(server) # DoWhy GCM 工具
register_refutation_tools(server) # DoWhy 反驳方法工具
register_sensitivity_tools(server)
register_discovery_tools(server)
register_attribution_tools(server)
register_root_cause_tools(server)
register_counterfactual_tools(server)
# Add server information resource
@server.resource("info://server")
def get_server_info() -> Dict[str, Any]:
"""Get detailed server information."""
return {
"name": server.name,
"version": server._version,
"description": server._description,
"capabilities": {
"tools": 115, # Total number of tools (38 + 73 GCM + 4 Refutation tools)
"categories": [
"Causal Modeling",
"Effect Estimation",
"GCM (Graphical Causal Models) - Expanding",
"Refutation Methods",
"Attribution Analysis",
"Root Cause Analysis",
"Counterfactual Analysis",
"Sensitivity Analysis",
"Causal Discovery"
]
},
"theoretical_foundations": [
"Structural Causal Models (Pearl)",
"Potential Outcomes Framework (Rubin)",
"Do-Calculus",
"Graphical Causal Models",
"Bootstrap Statistical Inference",
"Sensitivity Analysis Theory"
]
}
# Add documentation resource
@server.resource("docs://usage")
def get_usage_docs() -> str:
"""Get usage documentation."""
return """
# DoWhy MCP Server Usage Guide
This server provides causal inference tools through the Model Context Protocol (MCP).
## Available Tool Categories
1. **Causal Modeling** - Build and validate causal graphs and models
2. **Effect Estimation** - Estimate causal effects using various methods
3. **Attribution Analysis** - Attribute outcomes to causal factors
4. **Root Cause Analysis** - Identify root causes of events
5. **Counterfactual Analysis** - Perform "what if" analysis
6. **Sensitivity Analysis** - Test robustness of causal estimates
7. **Causal Discovery** - Discover causal relationships from data
## Example Usage
```python
# Example of using the backdoor_estimator tool
result = await session.call_tool(
"backdoor_estimator",
{
"data_path": "path/to/data.csv",
"treatment": "treatment_var",
"outcome": "outcome_var",
"confounders": ["confounder1", "confounder2"]
}
)
```
For more information, see the full documentation at https://dowhy-mcp.readthedocs.io/
"""
logger.info(f"Created DoWhy MCP server with {38} tools in 7 categories")
return server