MCP Scenario Engine
Officialby Schimmilab
README.md
# MCP Scenario Engine
**An MCP Server for Deterministic Simulation and Scenario Planning**
[](https://github.com/YOUR_USERNAME/mcp-scenario-engine/actions)
[](https://www.python.org/downloads/)
[](LICENSE)
## Overview
The MCP Scenario Engine is a Model Context Protocol (MCP) server that provides a simulation space where AI agents can query system states, execute actions, and simulate impacts in a traceable manner.
**With complex formula support**, you can now model sophisticated systems including game theory, economics, health tracking, and scientific simulationsโall through JSON-defined rules.
### Use Cases
- ๐ฏ **Project Planning**: Resource allocation, timeline simulation
- ๐๏ธ **Infrastructure**: Capacity planning, load testing
- ๐ฌ **Scientific Modeling**: Physics, chemistry, biology simulations
- ๐ **Economics & Finance**: Market dynamics, auctions, investments
- ๐ฎ **Game Theory**: Strategic interactions, Nash equilibrium, evolutionary dynamics
- ๐๏ธ **Health & Fitness**: Body composition, nutrition, training optimization
- ๐ณ **What-If Analysis**: Timeline forking for alternative scenarios
## Core Features
### โ
State Management
- Versioned state schema (JSON Schema)
- Complete state snapshots
- Slicing and partial queries
- Time-stepped simulation
### โ
Action System
10 implemented actions:
- `step` - Advance time forward
- `set_resource` - Set resource value
- `adjust_resource` - Adjust resource by delta
- `set_metric` - Set metric value
- `adjust_metric` - Adjust metric by delta (`delta` or `amount` parameter)
- `initialize` - Batch-initialize resources, metrics, flags, entities in one call
- `set_flag` - Set boolean flag
- `add_entity` - Add/update entity
- `remove_entity` - Remove entity
- `simulate_load` - Simulate load scenario (with randomness)
### โ
World Rules (Dynamic)
- JSON-defined rules via MCP
- Automatic application on `step` actions
- Flexible conditions (comparison, and, or, not, always)
- Value sources (resource, metric, flag, metadata, time, value)
- Multiple action types (set_resource, set_metric, set_flag, set_metadata)
- **Complex formulas** with arithmetic operations:
- Addition: `{"type": "add", "values": [...]}`
- Subtraction: `{"type": "subtract", "left": ..., "right": ...}`
- Multiplication: `{"type": "multiply", "values": [...]}`
- Division: `{"type": "divide", "numerator": ..., "denominator": ...}`
- Nested operations for complex calculations
- Priority system for rule execution order
- Full rule management (CRUD operations)
### โ
Constraint Engine
- Server-side validation
- Automatic state rollback on violations
- 3+ predefined constraints:
- `NonNegativeResourceConstraint`
- `MaxResourceConstraint`
- `TimeMonotonicConstraint`
- Clear error messages with context
### โ
Determinism & Reproducibility
- Seed-based random number generation
- Same seed โ Same results
- Fully reproducible simulations
### โ
Audit & Explainability
- Complete event history
- State deltas for every change
- Constraint checks logged
- Structured logging (JSON)
### โ
Timeline Forking
- Branch simulations
- Parallel "what-if" scenarios
- Immutable original timeline
### โ
Persistence
- Save state + rules + history
- Manage multiple simulations
- Survive server restarts
- Set checkpoints and resume
- CRUD operations (Save, Load, List, Delete)
- Metadata inspection without loading
- Storage in `~/.mcp-scenario-engine/simulations/`
## What's New
### New Actions
- `adjust_metric` - Adjust a metric by delta (`delta` or `amount` parameter)
- `initialize` - Batch-initialize resources, metrics, flags, entities in one call
### New Tools
- `get_timeseries` - Query time-series snapshots with variable and time filters
- `batch_apply_actions` - Apply multiple actions in a single MCP call
### World Rules Enhancements
- `adjust_resource`, `adjust_metric`, `adjust_metadata` action types in rules
- `clamp_resource`, `clamp_metric` โ keep values within bounds
- `min`, `max`, `clamp` value types in `_compute_value`
- `priority` and `description` fields exposed in `add_world_rule`
### Engine Improvements
- `step` with `steps: N` now fires world rules N times (one per sub-step)
- Per-step timeseries snapshots for multi-step calls
- Manual action snapshots in timeseries (non-step actions that change state)
- `reset_simulation` gains `keep_rules` parameter
- `fork_timeline` gains `activate` parameter (makes fork the active simulation)
### Persistence Fixes
- Timeseries snapshots are saved and restored
- Constraints (NonNegative + Max) are saved and restored
## Installation
### Prerequisites
- Python 3.11+
- pip or uv
### Local Installation
```bash
# Clone repository
git clone https://github.com/schimmmi/mcp-scenario-engine.git
cd mcp-scenario-engine
# Install dependencies
make install
# Or manually
pip install -e ".[dev]"
```
### Docker
#### Prebuilt image (GHCR) โ no clone needed
Every release is published as a container image:
```bash
docker pull ghcr.io/schimmilab/mcp-scenario-engine:latest
```
Add it to your MCP client config (Claude Code `.mcp.json`, Claude Desktop `claude_desktop_config.json`):
```json
{
"mcpServers": {
"scenario-engine": {
"command": "docker",
"args": ["run", "-i", "--rm",
"-v", "scenario-engine-data:/root/.mcp-scenario-engine",
"ghcr.io/schimmilab/mcp-scenario-engine:latest"]
}
}
}
```
The named volume keeps saved simulations between sessions; drop the `-v` line if you don't need persistence.
Pin a version instead of `latest` (e.g. `:1.2.1`) for reproducible runs.
#### Build locally
#### Build and Run Demos
```bash
# Build Docker image
docker compose build
# Run all demo scenarios
docker compose --profile demo run demo
# Run specific demo
docker compose run demo python examples/demo_scenario_a.py
docker compose run demo python examples/demo_weight_loss.py
docker compose run demo python examples/demo_prisoners_dilemma.py
```
#### Start MCP Server
**Option 1: Standalone Server**
```bash
# Start server in background
docker compose up -d mcp-scenario-engine
# View logs
docker compose logs -f mcp-scenario-engine
# Stop server
docker compose down
```
**Option 2: Use with Claude Desktop**
Add to your `claude_desktop_config.json`:
```json
{
"mcpServers": {
"scenario-engine": {
"command": "docker",
"args": [
"compose",
"-f",
"/path/to/mcp-scenario-engine/docker-compose.yml",
"run",
"--rm",
"mcp-scenario-engine"
]
}
}
}
```
**Important Notes:**
- Replace `/path/to/mcp-scenario-engine` with actual path
- Docker must be running before starting Claude Desktop
- Simulations are saved inside the container (mount a volume to persist)
**Mount Volume for Persistence:**
```yaml
# Add to docker-compose.yml under mcp-scenario-engine service:
volumes:
- ~/.mcp-scenario-engine:/root/.mcp-scenario-engine
```
This saves simulations to your home directory instead of the container.
**๐ For complete Docker documentation, see [DOCKER.md](DOCKER.md)**
## Quick Start
### 1. Run Demo
```bash
# Both demo scenarios
make demo
# Or individually
python examples/demo_scenario_a.py
python examples/demo_scenario_b.py
```
### 2. Use as Python Library
```python
from mcp_scenario_engine import SimulationEngine
from mcp_scenario_engine.constraints import NonNegativeResourceConstraint
# Create simulation
sim = SimulationEngine(seed=42)
# Set initial state
sim.state.resources = {"budget": 10000.0, "capacity": 100.0}
# Add constraint
sim.constraint_engine.add_constraint(
NonNegativeResourceConstraint("budget")
)
# Execute actions
result = sim.apply_action(
"adjust_resource",
{"resource": "budget", "delta": -2000.0}
)
if result.success:
print(f"New budget: {sim.state.resources['budget']}")
print(f"Delta: {result.delta}")
else:
print(f"Error: {result.message}")
for v in result.constraints_violated:
print(f" - {v.constraint_id}: {v.message}")
```
### 3. Use as MCP Server
```bash
# Start server
python -m mcp_scenario_engine.server
# Configure in Claude Desktop (claude_desktop_config.json):
{
"mcpServers": {
"scenario-engine": {
"command": "python",
"args": ["-m", "mcp_scenario_engine.server"],
"cwd": "/path/to/mcp-scenario-engine"
}
}
}
```
## MCP Tools
The server provides 20 tools:
### State Management
#### `get_state`
Get current simulation state.
```json
{}
```
#### `get_schema`
Get state schema definition.
```json
{}
```
### Action Execution
#### `apply_action`
Execute an action.
```json
{
"action": "adjust_resource",
"params": {
"resource": "budget",
"delta": -500.0
}
}
```
### Simulation Control
#### `reset_simulation`
Reset simulation to initial state.
```json
{
"seed": 42,
"keep_rules": true
}
```
#### `fork_timeline`
Fork timeline for "what-if" scenarios.
```json
{
"name": "optimistic_scenario",
"activate": true
}
```
#### `get_history`
Get event history.
```json
{
"limit": 10
}
```
### World Rules (Dynamic)
#### `add_world_rule`
Add dynamic rule that automatically applies on `step` actions.
```json
{
"rule_id": "cpu_overload",
"condition": {
"type": "comparison",
"left": {"type": "resource", "name": "cpu"},
"operator": ">",
"right": {"type": "value", "value": 80}
},
"actions": [{
"type": "set_metric",
"metric": "error_rate",
"value": {"type": "increment", "amount": 0.05}
}],
"priority": 10,
"description": "Increase error rate on CPU overload"
}
```
#### `list_world_rules`
List all active rules.
```json
{}
```
#### `get_world_rule`
Get details of a specific rule.
```json
{
"rule_id": "cpu_overload"
}
```
#### `update_world_rule`
Update existing rule (partial update).
```json
{
"rule_id": "cpu_overload",
"priority": 20,
"description": "Updated description"
}
```
#### `remove_world_rule`
Remove a rule.
```json
{
"rule_id": "cpu_overload"
}
```
#### `clear_world_rules`
Remove all rules.
```json
{}
```
### Persistence
#### `save_simulation`
Save simulation persistently (State + Rules + History).
```json
{
"name": "devops_scenario_1",
"description": "High CPU scenario with 3 steps"
}
```
#### `load_simulation`
Load saved simulation (replaces current simulation).
```json
{
"name": "devops_scenario_1"
}
```
#### `list_simulations`
List all saved simulations.
```json
{}
```
#### `get_simulation_info`
Get simulation metadata without loading it.
```json
{
"name": "devops_scenario_1"
}
```
#### `delete_simulation`
Delete a saved simulation.
```json
{
"name": "devops_scenario_1"
}
```
### Extended Actions
#### `adjust_metric`
Adjust a metric by delta.
```json
{"action": "adjust_metric", "params": {"metric": "score", "delta": 5.0}}
```
#### `initialize`
Batch-initialize multiple state fields in one call.
```json
{
"action": "initialize",
"params": {
"resources": {"budget": 10000, "energy": 100},
"metrics": {"output": 0, "efficiency": 1.0},
"flags": {"active": true}
}
}
```
### Batch & Timeseries
#### `batch_apply_actions`
Apply multiple actions in a single MCP call.
```json
{
"actions": [
{"action": "initialize", "params": {"resources": {"budget": 1000}}},
{"action": "step", "params": {"steps": 5}}
],
"stop_on_failure": true
}
```
#### `get_timeseries`
Query time-series snapshots recorded at each step.
```json
{"variables": ["budget", "output"], "from_time": 0, "to_time": 10}
```
## State Schema (v1)
```json
{
"schema_version": "v1",
"simulation_id": "uuid",
"created_at": "2025-01-15T10:30:00Z",
"updated_at": "2025-01-15T10:35:00Z",
"seed": 42,
"time": 0,
"entities": {},
"metrics": {},
"resources": {},
"flags": {},
"metadata": {}
}
```
## Demo Scenarios
### Scenario A: Normal Simulation Run
Demonstrates:
- State initialization
- Multiple action types
- Resource management
- Entity lifecycle
- Metrics tracking
- Reproducibility
```bash
python examples/demo_scenario_a.py
```
**Output:**
```
============================================================
DEMO SCENARIO A: Normal Simulation Run
============================================================
1. Creating simulation with seed=42...
2. Adding constraints...
- Budget must be non-negative
- Team capacity must be non-negative
3. Executing simulation steps...
...
โ Advanced to time step 3
6. Testing Reproducibility:
โ Reproducibility verified - identical results with same seed
```
### Scenario B: Constraint Violations
Demonstrates:
- Constraint validation
- State rollback on violations
- Clear error messages
- Event history
- Timeline forking
```bash
python examples/demo_scenario_b.py
```
**Output:**
```
============================================================
DEMO SCENARIO B: Constraint Violation Handling
============================================================
5. Testing constraint violations...
Violation Attempt 1: Exceed maximum server load
โ REJECTED as expected: Action rejected due to constraint violations
Violations detected:
- max_resource_server_load: Resource 'server_load' exceeds maximum 100.0 (got 130.0)
State unchanged - Server load still: 80.00
```
### Demo: World Rules (DevOps)
Demonstrates dynamic rules:
- JSON-based rule definition
- Automatic causality
- Deterministic world model simulation
```bash
python examples/demo_devops_world.py
```
### Demo: Persistence
Demonstrates persistence features:
- Save/load simulations
- Multiple simulation management
- Continue from checkpoint
- Delete and list operations
```bash
python examples/demo_persistence.py
```
**Output:**
```
โ
Persistence Demo Complete!
๐ก Key Features:
โข Save simulations with state + rules + history
โข Load simulations and continue from checkpoint
โข List all saved simulations
โข Get metadata without loading
โข Delete simulations
โข Overwrite existing saves
```
### Demo: Weight Loss Simulation (Complex Formulas)
Demonstrates complex formula support:
- Division: `(calorie_deficit / 7700) * 7 * compliance`
- Multiplication of multiple variables
- Addition and subtraction
- Nested operations
- Real-world body composition modeling
```bash
python examples/demo_weight_loss.py
```
**Output:**
```
Week Weight Change Fat Muscle Fat%
----------------------------------------------------------------------
Start 92.47kg - 18.55kg 70.16kg 20.1%
1 88.53kg -3.94kg 18.24kg 70.29kg 20.6%
2 88.35kg -0.18kg 17.93kg 70.41kg 20.3%
...
8 87.26kg -0.18kg 16.08kg 71.18kg 18.4%
Results: -5.21kg weight, -2.47kg fat, +1.02kg muscle, body fat: 20.1% โ 18.4%
```
### Demo: Game Theory Simulations
Demonstrates game-theoretic models with complex strategic interactions:
#### Prisoner's Dilemma (Iterated Game)
```bash
python examples/demo_prisoners_dilemma.py
```
**Features:**
- Iterated prisoner's dilemma with payoff matrices
- Tit-for-Tat vs Tit-for-Tat (mutual cooperation)
- Tit-for-Tat vs Always Defect (retaliation)
- Strategic formulas calculating payoffs from action combinations
**Output:**
```
Round P1 Move P2 Move P1 Score P2 Score
----------------------------------------------------------------------
1 Cooperate Cooperate 3 3
...
10 Cooperate Cooperate 30 30
Tit-for-Tat vs Always Defect:
1 Cooperate Defect 0 5
2 Defect Defect 1 6
...
```
#### Evolutionary Game Theory (Hawk-Dove)
```bash
python examples/demo_evolutionary_game.py
```
**Features:**
- Population dynamics with Hawks and Doves
- Frequency-dependent fitness calculations
- Replicator dynamics: `hawks_new = hawks * (hawk_fitness / avg_fitness)`
- Evolutionary Stable Strategy (ESS) emergence
- Complex nested formulas for payoffs
**Output:**
```
Gen Hawks Doves H_Fit D_Fit Avg_Fit
----------------------------------------------------------------------
1 50.0 50.0 12.5 12.5 12.5
...
20 50.0 50.0 12.5 12.5 12.5
Equilibrium: 50% Hawks (matches theoretical ESS!)
```
#### Auction Theory (Vickrey Second-Price)
```bash
python examples/demo_auction_theory.py
```
**Features:**
- Sealed-bid second-price auction
- Truth-telling incentive demonstration
- Winner's surplus calculation: `valuation - payment`
- Dominant strategy analysis
- Compares truthful vs strategic bidding
**Output:**
```
Winner: Bidder 3
Payment: $95.00 (second-highest bid)
Winner's Surplus: $25.00
Truth-telling test:
โข Underbidding โ Lost auction, $25 regret
โข Overbidding โ Same surplus ($25), no benefit
```
## Claude Code Workspace
This repository includes a ready-to-use Claude Code workspace with simulation skills and templates.
### Quick Setup
1. Use this repository directory as your Claude Code workspace, **or**
2. Copy the `workspace/` folder and `.claude/` folder to your own project
### Available Skills (Slash Commands)
| Skill | Trigger | Description |
|---|---|---|
| `/sim-spieltheorie` | Game theory | Nash equilibrium, Prisoner's Dilemma, Tit-for-Tat |
| `/sim-monte-carlo` | Risk/probability | Risk estimation, uncertainty quantification via forks |
| `/sim-systemdynamik` | System dynamics | Stocks & Flows, SIR models, Lotka-Volterra |
| `/sim-markov` | State transitions | Churn, health states, project phases, credit risk |
| `/sim-agenten` | Agent-based models | Markets, opinion dynamics, emergence |
| `/sim-optimierung` | Optimization | Grid search, portfolio, resource allocation |
| `/sim-orchestrator` | Multi-agent | Orchestrator + Sub-agents for coupled simulations |
### Workspace Structure
```
workspace/
โโโ CLAUDE.md # Engine reference & project context
โโโ templates/ # Simulation templates (health, project, system)
โโโ designs/ # Pre-simulation design documents
โโโ results/ # Saved simulation results (examples included)
โโโ analysis/ # Analysis & insights
โโโ data/ # Input data & reference values
โโโ scripts/ # Helper scripts
```
### Example Results
See `workspace/results/` for three worked examples:
- `example_spieltheorie.md` โ Tit-for-Tat vs Always Defect game theory simulation
- `example_sir_epidemie.md` โ SIR epidemic model with Rโ=6
- `example_volkswirtschaft.md` โ 3-agent Keynesian economy simulation
## Testing
```bash
# Run all tests
make test
# With coverage
pytest --cov=src --cov-report=html
# Unit tests only
pytest tests/test_*.py -v
# Integration tests only
pytest tests/test_integration.py -v
```
**Test Coverage:** 80%+ (requirement met)
## Development
### Code Quality
```bash
# Linting
make lint
# Formatting
make format
# Type checking
mypy src
```
### Structure
```
mcp-scenario-engine/
โโโ src/mcp_scenario_engine/
โ โโโ __init__.py
โ โโโ models.py # Pydantic Models (State, Events)
โ โโโ constraints.py # Constraint Engine
โ โโโ actions.py # Action Implementations
โ โโโ simulation.py # Core Simulation Engine
โ โโโ dynamic_rules.py # Dynamic Rule System
โ โโโ world_rules.py # World Rule Engine
โ โโโ persistence.py # Persistence Layer
โ โโโ server.py # MCP Server (20 Tools)
โโโ tests/
โ โโโ test_simulation.py # Unit Tests
โ โโโ test_constraints.py # Constraint Tests
โ โโโ test_actions.py # Action Tests
โ โโโ test_integration.py # Integration Tests
โโโ examples/
โ โโโ demo_scenario_a.py # Demo: Normal Simulation
โ โโโ demo_scenario_b.py # Demo: Constraint Violations
โ โโโ demo_devops_world.py # Demo: World Rules
โ โโโ demo_persistence.py # Demo: Persistence
โโโ .simulations/ # Saved Simulations
โโโ Dockerfile
โโโ docker-compose.yml
โโโ pyproject.toml
โโโ Makefile
โโโ README.md
```
## Observability
### Structured Logging
All logs are output as JSON:
```json
{
"event": "action_applied",
"simulation_id": "123e4567-e89b-12d3-a456-426614174000",
"action": "adjust_resource",
"event_id": "456e7890-e89b-12d3-a456-426614174111",
"timestamp": "2025-01-15T10:30:00.123456Z"
}
```
### Event Types
- `simulation_created` - Simulation created
- `simulation_reset` - Simulation reset
- `action_applied` - Action successful
- `constraint_violated` - Constraint violation
- `timeline_forked` - Timeline forked
## Acceptance Criteria
โ
**Read state**: `get_state` returns valid schema
โ
**Execute action**: `apply_action` returns before/after/delta/event_id
โ
**Constraint enforcement**: Violations prevent state changes
โ
**Determinism**: Same seed โ Same results
โ
**Fork/Branch**: Immutable original, diverging fork
## Definition of Done
### Implementation โ
- MCP server operational (Docker + venv)
- State schema v1 documented
- 8 actions implemented
- Constraint engine with 3+ rules
- Determinism via seed
### Quality โ
- Unit tests (80%+ coverage)
- Integration tests (end-to-end)
- Linting/Formatting (ruff/black)
- Type checking (mypy)
### Observability โ
- Structured logging (JSON)
- No secrets in logs
- Clear error messages
### Documentation โ
- README with setup & examples
- Tool list & schema
- 4 demo scenarios
- Example outputs
### Demo โ
- `make demo` works
- Scenario A: Normal run
- Scenario B: Constraint handling
- Demo: World rules
- Demo: Persistence
## License
MIT
## Contact
For questions or issues, please open an issue on GitHub.
## Maintainer
Schimmi โ https://schimmilab.de
Issues und Pull Requests willkommen.
This server cannot be deployed
Maintenance
ActivitySlowing
ResponsivenessNo recent community issues