#
tokens: 48713/50000 59/78 files (page 1/3)
lines: off (toggle) GitHub
raw markdown copy
This is page 1 of 3. Use http://codebase.md/angrysky56/mcts-mcp-server?page={x} to view the full context.

# Directory Structure

```
├── .env.example
├── .gitignore
├── archive
│   ├── ANALYSIS_TOOLS.md
│   ├── First-Run.md
│   ├── fixed_tools.py
│   ├── gemini_adapter_old.py
│   ├── gemini_adapter.py
│   ├── GEMINI_SETUP.md
│   ├── QUICK_START_FIXED.md
│   ├── QUICK_START.md
│   ├── README.md
│   ├── run_test.py
│   ├── SERVER_FIX_SUMMARY.md
│   ├── setup_analysis_venv.sh
│   ├── setup_analysis.sh
│   ├── SETUP_SUMMARY.md
│   ├── test_adapter.py
│   ├── test_fixed_server.py
│   ├── test_gemini_setup.py
│   ├── test_mcp_init.py
│   ├── test_minimal.py
│   ├── test_new_adapters.py
│   ├── test_ollama.py
│   ├── test_rate_limiting.py
│   ├── test_server_debug.py
│   ├── test_server.py
│   ├── test_simple.py
│   ├── test_startup_simple.py
│   ├── test_startup.py
│   ├── TIMEOUT_FIX.md
│   ├── tools_fast.py
│   ├── tools_old.py
│   └── tools_original.py
├── image-1.png
├── image-2.png
├── image-3.png
├── image.png
├── LICENSE
├── prompts
│   ├── README.md
│   └── usage_guide.md
├── pyproject.toml
├── README.md
├── results
│   ├── cogito:32b
│   │   └── cogito:32b_1745989705
│   │       ├── best_solution.txt
│   │       └── progress.jsonl
│   ├── cogito:latest
│   │   ├── cogito:latest_1745979984
│   │   │   ├── best_solution.txt
│   │   │   └── progress.jsonl
│   │   └── cogito:latest_1745984274
│   │       ├── best_solution.txt
│   │       └── progress.jsonl
│   ├── local
│   │   ├── local_1745956311
│   │   │   ├── best_solution.txt
│   │   │   └── progress.jsonl
│   │   ├── local_1745956673
│   │   │   ├── best_solution.txt
│   │   │   └── progress.jsonl
│   │   └── local_1745958556
│   │       ├── best_solution.txt
│   │       └── progress.jsonl
│   └── qwen3:0.6b
│       ├── qwen3:0.6b_1745960624
│       │   ├── best_solution.txt
│       │   └── progress.jsonl
│       ├── qwen3:0.6b_1745960651
│       │   ├── best_solution.txt
│       │   └── progress.jsonl
│       ├── qwen3:0.6b_1745960694
│       │   ├── best_solution.txt
│       │   └── progress.jsonl
│       └── qwen3:0.6b_1745977462
│           ├── best_solution.txt
│           └── progress.jsonl
├── setup_unix.sh
├── setup_windows.bat
├── setup.py
├── setup.sh
├── src
│   └── mcts_mcp_server
│       ├── __init__.py
│       ├── analysis_tools
│       │   ├── __init__.py
│       │   ├── mcts_tools.py
│       │   └── results_processor.py
│       ├── anthropic_adapter.py
│       ├── base_llm_adapter.py
│       ├── gemini_adapter.py
│       ├── intent_handler.py
│       ├── llm_adapter.py
│       ├── llm_interface.py
│       ├── manage_server.py
│       ├── mcts_config.py
│       ├── mcts_core.py
│       ├── node.py
│       ├── ollama_adapter.py
│       ├── ollama_check.py
│       ├── ollama_utils.py
│       ├── openai_adapter.py
│       ├── rate_limiter.py
│       ├── reality_warps_adapter.py
│       ├── results_collector.py
│       ├── server.py
│       ├── state_manager.py
│       ├── tools.py
│       └── utils.py
├── USAGE_GUIDE.md
├── uv.lock
└── verify_installation.py
```

# Files

--------------------------------------------------------------------------------
/.env.example:
--------------------------------------------------------------------------------

```
# .env.example - API Keys for LLM Providers
# Rename this file to .env and fill in your API keys.

# OpenAI API Key
OPENAI_API_KEY="your_openai_api_key_here"

# Anthropic API Key
ANTHROPIC_API_KEY="your_anthropic_api_key_here"

# Google Gemini API Key
GEMINI_API_KEY="your_google_gemini_api_key_here"

# Default LLM Provider to use (e.g., "ollama", "openai", "anthropic", "gemini")
# DEFAULT_LLM_PROVIDER="ollama"
# Default Model Name for the selected provider
# DEFAULT_MODEL_NAME="cogito:latest" # Example for ollama
# DEFAULT_MODEL_NAME="gpt-3.5-turbo" # Example for openai
# DEFAULT_MODEL_NAME="claude-3-haiku-20240307" # Example for anthropic
# DEFAULT_MODEL_NAME="gemini-1.5-flash-latest" # Example for gemini

```

--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------

```
# Computational Residue: Python Bytecode Manifestations
__pycache__/
*.py[cod]
*$py.class
*.so
.Python

# Epistemic Isolation Chambers: Virtual Environment Structures
env/
venv/
ENV/
.env
.venv
env.bak/
venv.bak/

# Cognitive Ephemera: Distribution/Packaging Artifacts
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
*.lock
# Temporal Memory Fragments: Log Patterns
*.log
logs/
*/logs/
log.txt
mcts_logs/
*.log.*

# Neural State Persistence: MCTS-specific Runtime Data
.mcts_cache/
.mcts_state/
.mcts_memory/
mcts_session_*.json
node_evaluations/
simulation_results/
results/

# Integrated Development Ecosystems: IDE Resonance Patterns
.idea/
.vscode/
*.swp
*.swo
*~
.project
.pydevproject
.settings/
*.sublime-workspace
*.sublime-project

# Entropic Boundary Conditions: OS-generated Artifacts
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Empirical Knowledge Repositories: Data Storage Patterns
*.sqlite
*.db
*.csv
*.json
*.pickle
*.pkl
!requirements.txt
!default_config.json

# Emergent Computation Traces: Test Coverage Artifacts
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
.mypy_cache/

# Quantum State Configurations: Local Environment Settings
.env.local
.env.development.local
.env.test.local
.env.production.local
config.local.yaml
settings.local.py

# Cognitive Boundary Exceptions: Intentional Inclusions
!examples/*.json
!tests/fixtures/*.json
!schemas/*.json

```

--------------------------------------------------------------------------------
/prompts/README.md:
--------------------------------------------------------------------------------

```markdown
# MCTS MCP Server - AI Guidance

This folder contains prompts and guidance for AI assistants on how to effectively use the MCTS (Monte Carlo Tree Search) MCP server tools.

## Overview

The MCTS MCP server provides advanced reasoning capabilities through Monte Carlo Tree Search algorithms. It can explore multiple solution paths and find optimal approaches to complex questions.

## Key Tools

1. **initialize_mcts** - Start a new MCTS session
2. **run_mcts_search** - Execute search iterations  
3. **get_synthesis** - Generate final analysis
4. **get_status** - Check current state
5. **list_available_models** - See available LLM models
6. **set_provider** - Change LLM provider

## Quick Start Workflow

1. Initialize with a question
2. Run search iterations
3. Get synthesis of results

See individual prompt files for detailed guidance.

```

--------------------------------------------------------------------------------
/archive/README.md:
--------------------------------------------------------------------------------

```markdown
# MCTS MCP Server

A Model Context Protocol (MCP) server that exposes an Advanced Bayesian Monte Carlo Tree Search (MCTS) engine for AI-assisted analysis and reasoning.

## Overview

This MCP server enables Claude to use Monte Carlo Tree Search (MCTS) algorithms for deep, explorative analysis of topics, questions, or text inputs. The MCTS algorithm uses a Bayesian approach to systematically explore different angles and interpretations, producing insightful analyses that evolve through multiple iterations.

## Features

- **Bayesian MCTS**: Uses a probabilistic approach to balance exploration vs. exploitation during analysis
- **Multi-iteration Analysis**: Supports multiple iterations of thinking with multiple simulations per iteration
- **State Persistence**: Remembers key results, unfit approaches, and priors between turns in the same chat
- **Approach Taxonomy**: Classifies generated thoughts into different philosophical approaches and families
- **Thompson Sampling**: Can use Thompson sampling or UCT for node selection
- **Surprise Detection**: Identifies surprising or novel directions of analysis
- **Intent Classification**: Understands when users want to start a new analysis or continue a previous one

## Installation

The setup uses UV (Astral UV), a faster alternative to pip that offers improved dependency resolution.

1. Ensure you have Python 3.10+ installed
2. Run the setup script:

```bash
cd /home/ty/Repositories/ai_workspace/mcts-mcp-server
./setup.sh
```

This will:
- Install UV if not already installed
- Create a virtual environment with UV
- Install the required packages using UV
- Create the necessary state directory

Alternatively, you can manually set up:

```bash
# Install UV if not already installed
curl -fsSL https://astral.sh/uv/install.sh | bash

# Create and activate a virtual environment
cd /home/ty/Repositories/ai_workspace/mcts-mcp-server
uv venv .venv
source .venv/bin/activate

# Install dependencies
uv pip install -r requirements.txt
```

## Claude Desktop Integration

To integrate with Claude Desktop:

1. Copy the `claude_desktop_config.json` example from this repository
2. Add it to your Claude Desktop configuration (typically located at `~/.claude/claude_desktop_config.json`)
3. Ensure the paths in the configuration point to the correct location on your system

## Usage

The server exposes the following tools to Claude:

- `initialize_mcts`: Start a new MCTS analysis with a given question
- `run_mcts`: Run the MCTS algorithm for a specified number of iterations
- `generate_synthesis`: Generate a final synthesis of the MCTS results
- `get_config`: View the current MCTS configuration
- `update_config`: Update the MCTS configuration
- `get_mcts_status`: Get the current status of the MCTS system

When you ask Claude to perform deep analysis on a topic or question, it will leverage these tools automatically to explore different angles using the MCTS algorithm.

### Example Prompts

- "Analyze the implications of artificial intelligence on human creativity"
- "Continue exploring the ethical dimensions of this topic"
- "What was the best analysis you found in the last run?"
- "How does this MCTS process work?"
- "Show me the current MCTS configuration"

## Development

For development and testing:

```bash
# Activate virtual environment
source .venv/bin/activate

# Run the server directly (for testing)
uv run server.py

# OR use the MCP CLI tools
uv run -m mcp dev server.py
```

## Configuration

You can customize the MCTS parameters in the config dictionary or through Claude's `update_config` tool. Key parameters include:

- `max_iterations`: Number of MCTS iterations to run
- `simulations_per_iteration`: Number of simulations per iteration
- `exploration_weight`: Controls exploration vs. exploitation balance (in UCT)
- `early_stopping`: Whether to stop early if a high-quality solution is found
- `use_bayesian_evaluation`: Whether to use Bayesian evaluation for node scores
- `use_thompson_sampling`: Whether to use Thompson sampling for selection

```

--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------

```markdown
[![MseeP.ai Security Assessment Badge](https://mseep.net/pr/angrysky56-mcts-mcp-server-badge.png)](https://mseep.ai/app/angrysky56-mcts-mcp-server)

# MCTS MCP Server

A Model Context Protocol (MCP) server that exposes an Advanced Bayesian Monte Carlo Tree Search (MCTS) engine for AI-assisted analysis and reasoning.

## Overview

This MCP server enables Claude to use Monte Carlo Tree Search (MCTS) algorithms for deep, explorative analysis of topics, questions, or text inputs. The MCTS algorithm uses a Bayesian approach to systematically explore different angles and interpretations, producing insightful analyses that evolve through multiple iterations.

## Features

- **Bayesian MCTS**: Uses a probabilistic approach to balance exploration vs. exploitation during analysis
- **Multi-iteration Analysis**: Supports multiple iterations of thinking with multiple simulations per iteration
- **State Persistence**: Remembers key results, unfit approaches, and priors between turns in the same chat
- **Approach Taxonomy**: Classifies generated thoughts into different philosophical approaches and families
- **Thompson Sampling**: Can use Thompson sampling or UCT for node selection
- **Surprise Detection**: Identifies surprising or novel directions of analysis
- **Intent Classification**: Understands when users want to start a new analysis or continue a previous one
- **Multi-LLM Support**: Supports Ollama, OpenAI, Anthropic, and Google Gemini models.

## Quick Start Installation

The MCTS MCP Server now includes cross-platform setup scripts that work on Windows, macOS, and Linux.

### Prerequisites

- **Python 3.10+** (required)
- **Internet connection** (for downloading dependencies)

### Automatic Setup

**Option 1: Cross-platform Python setup (Recommended)**
```bash
# Clone the repository
git clone https://github.com/angrysky56/mcts-mcp-server.git
cd mcts-mcp-server

# Run the setup script
python setup.py
```

**Option 2: Platform-specific scripts**

**Linux/macOS:**
```bash
chmod +x setup.sh
./setup.sh
```

**Windows:**
```cmd
setup_windows.bat
```

### What the Setup Does

The setup script automatically:
1. ✅ Checks Python version compatibility (3.10+ required)
2. ✅ Installs the UV package manager (if not present)
3. ✅ Creates a virtual environment
4. ✅ Installs all dependencies including google-genai
5. ✅ Creates `.env` file from template
6. ✅ Generates Claude Desktop configuration
7. ✅ Creates state directories
8. ✅ Verifies the installation

### Verify Installation

After setup, verify everything works:

```bash
python verify_installation.py
```

This runs comprehensive checks and tells you if anything needs fixing.

## Configuration

### 1. API Keys Setup

Edit the `.env` file created during setup:

```env
# Add your API keys (remove quotes and add real keys)
OPENAI_API_KEY=sk-your-openai-key-here
ANTHROPIC_API_KEY=sk-ant-your-anthropic-key-here
GEMINI_API_KEY=your-gemini-api-key-here

# Set default provider and model (optional)
DEFAULT_LLM_PROVIDER=gemini
DEFAULT_MODEL_NAME=gemini-2.0-flash
```

**Getting API Keys:**
- **OpenAI**: https://platform.openai.com/api-keys
- **Anthropic**: https://console.anthropic.com/
- **Google Gemini**: https://aistudio.google.com/app/apikey
- **Ollama**: No API key needed (local models)

### 2. Claude Desktop Integration

The setup creates `claude_desktop_config.json`. Add its contents to your Claude Desktop config:

**Linux/macOS:**
```bash
# Config location
~/.config/claude/claude_desktop_config.json
```

**Windows:**
```cmd
# Config location
%APPDATA%\Claude\claude_desktop_config.json
```

**Example config structure:**
```json
{
  "mcpServers": {
    "mcts-mcp-server": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/mcts-mcp-server/src",
        "run",
        "mcts-mcp-server"
      ],
      "env": {
        "UV_PROJECT_ENVIRONMENT": "/path/to/mcts-mcp-server"
      }
    }
  }
}
```

**Important:** Update the paths to match your installation directory.

### 3. Restart Claude Desktop

After adding the configuration, restart Claude Desktop to load the MCTS server.

## Usage

The server exposes many tools to your LLM detailed below in a copy-pasteable format for your system prompt.

When you ask Claude to perform deep analysis on a topic or question, it will leverage these tools automatically to explore different angles using the
MCTS algorithm and analysis tools.

![alt text](image-2.png)

## How It Works

The MCTS MCP server uses a local inference approach rather than trying to call the LLM directly. This is compatible with the MCP protocol, which
is designed for tools to be called by an AI assistant (like Claude) rather than for the tools to call the AI model themselves.

When Claude asks the server to perform analysis, the server:

1. Initializes the MCTS system with the question
2. Runs multiple iterations of exploration using the MCTS algorithm
3. Generates deterministic responses for various analytical tasks
4. Returns the best analysis found during the search

## Manual Installation (Advanced)

If you prefer manual setup or the automatic setup fails:

### 1. Install UV Package Manager

**Linux/macOS:**
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```

**Windows (PowerShell):**
```powershell
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
```

### 2. Setup Project

```bash
# Clone repository
git clone https://github.com/angrysky56/mcts-mcp-server.git
cd mcts-mcp-server

# Create virtual environment
uv venv .venv

# Activate virtual environment
# Linux/macOS:
source .venv/bin/activate
# Windows:
.venv\Scripts\activate

# Install dependencies
uv pip install .
uv pip install .[dev]  # Optional development dependencies

# Install Gemini package specifically (if not in pyproject.toml)
uv pip install google-genai>=1.20.0
```

### 3. Create Configuration Files

```bash
# Copy environment file
cp .env.example .env

# Edit .env file with your API keys
nano .env  # or use your preferred editor

# Create state directory
mkdir -p ~/.mcts_mcp_server
```

## Troubleshooting

### Common Issues

**1. Python Version Error**
```
Solution: Install Python 3.10+ from python.org
```

**2. UV Not Found After Install**
```bash
# Add UV to PATH manually
export PATH="$HOME/.cargo/bin:$PATH"
# Or on Windows: Add %USERPROFILE%\.cargo\bin to PATH
```

**3. Google Gemini Import Error**
```bash
# Install Gemini package manually
uv pip install google-genai
```

**4. Permission Denied (Linux/macOS)**
```bash
# Make scripts executable
chmod +x setup.sh setup_unix.sh
```

**5. Claude Desktop Not Detecting Server**
- Verify config file location and syntax
- Check that paths in config are absolute and correct
- Restart Claude Desktop completely
- Check Claude Desktop logs for errors

### Getting Help

1. **Run verification**: `python verify_installation.py`
2. **Check logs**: Look at Claude Desktop's developer tools
3. **Test components**: Run individual tests in the repository
4. **Review documentation**: Check USAGE_GUIDE.md for detailed instructions

## API Key Management

For using LLM providers like OpenAI, Anthropic, and Google Gemini, you need to provide API keys. This server loads API keys from a `.env` file located in the root of the repository.

1.  **Copy the example file**: `cp .env.example .env`
2.  **Edit `.env`**: Open the `.env` file and replace the placeholder keys with your actual API keys:
    ```env
    OPENAI_API_KEY="your_openai_api_key_here"
    ANTHROPIC_API_KEY="your_anthropic_api_key_here"
    GEMINI_API_KEY="your_google_gemini_api_key_here"
    ```
3.  **Set Defaults (Optional)**: You can also set the default LLM provider and model name in the `.env` file:
    ```env
    # Default LLM Provider to use (e.g., "ollama", "openai", "anthropic", "gemini")
    DEFAULT_LLM_PROVIDER="ollama"
    # Default Model Name for the selected provider
    DEFAULT_MODEL_NAME="cogito:latest"
    ```
    If these are not set, the system defaults to "ollama" and attempts to use a model like "cogito:latest" or another provider-specific default.

The `.env` file is included in `.gitignore`, so your actual keys will not be committed to the repository.

## Suggested System Prompt and Updated tools

---

```markdown
# MCTS server and usage instructions:

# List available Ollama models (if using Ollama)
list_ollama_models()

# Set the active LLM provider and model
# provider_name can be "ollama", "openai", "anthropic", "gemini"
# model_name is specific to the provider (e.g., "cogito:latest" for ollama, "gpt-4" for openai)
set_active_llm(provider_name="openai", model_name="gpt-3.5-turbo")
# Or, to use defaults from .env or provider-specific defaults:
# set_active_llm(provider_name="openai")

# Initialize analysis (can also specify provider and model here to override active settings for this run)
initialize_mcts(question="Your question here", chat_id="unique_id", provider_name="openai", model_name="gpt-4")
# Or using the globally set active LLM:
# initialize_mcts(question="Your question here", chat_id="unique_id")

run_mcts(iterations=1, simulations_per_iteration=5)

After run_mcts is called it can take quite a long time ie minutes to hours
- so you may discuss any ideas or questions or await user confirmation of the process finishing,
- then proceed to synthesis and analysis tools on resumption of chat.

## MCTS-MCP Tools Overview

### Core MCTS Tools:
- `initialize_mcts`: Start a new MCTS analysis with a specific question. Can optionally specify `provider_name` and `model_name` to override defaults for this run.
- `run_mcts`: Run the MCTS algorithm for a set number of iterations/simulations.
- `generate_synthesis`: Generate a final summary of the MCTS results.
- `get_config`: View current MCTS configuration parameters, including active LLM provider and model.
- `update_config`: Update MCTS configuration parameters (excluding provider/model, use `set_active_llm` for that).
- `get_mcts_status`: Check the current status of the MCTS system.
- `set_active_llm(provider_name: str, model_name: Optional[str])`: Select which LLM provider and model to use for MCTS.
- `list_ollama_models()`: Show all available local Ollama models (if using Ollama provider).

Default configuration prioritizes speed and exploration, but you can customize parameters like exploration_weight, beta_prior_alpha/beta, surprise_threshold.

## Configuration

You can customize the MCTS parameters in the config dictionary or through Claude's `update_config` tool. Key parameters include:

- `max_iterations`: Number of MCTS iterations to run
- `simulations_per_iteration`: Number of simulations per iteration
- `exploration_weight`: Controls exploration vs. exploitation balance (in UCT)
- `early_stopping`: Whether to stop early if a high-quality solution is found
- `use_bayesian_evaluation`: Whether to use Bayesian evaluation for node scores
- `use_thompson_sampling`: Whether to use Thompson sampling for selection

Articulating Specific Pathways:
Delving into the best_path nodes (using mcts_instance.get_best_path_nodes() if you have the instance) and examining the sequence of thought and content
at each step can provide a fascinating micro-narrative of how the core insight evolved.

Visualizing the tree (even a simplified version based on export_tree_summary) could also be illuminating and I will try to set up this feature.

Modifying Parameters: This is a great way to test the robustness of the finding or explore different "cognitive biases" of the system.

Increasing Exploration Weight: Might lead to more diverse, less obviously connected ideas.

Decreasing Exploration Weight: Might lead to deeper refinement of the initial dominant pathways.

Changing Priors (if Bayesian): You could bias the system towards certain approaches (e.g., increase alpha for 'pragmatic') to see how it influences the
outcome.

More Iterations/Simulations: Would allow for potentially deeper convergence or exploration of more niche pathways.

### Results Collection:
- Automatically stores results in `/home/ty/Repositories/ai_workspace/mcts-mcp-server/results` (path might be system-dependent or configurable)
- Organizes by provider, model name, and run ID
- Stores metrics, progress info, and final outputs

# MCTS Analysis Tools

This extension adds powerful analysis tools to the MCTS-MCP Server, making it easy to extract insights and understand results from your MCTS runs.

The MCTS Analysis Tools provide a suite of integrated functions to:

1. List and browse MCTS runs
2. Extract key concepts, arguments, and conclusions
3. Generate comprehensive reports
4. Compare results across different runs
5. Suggest improvements for better performance

## Available Run Analysis Tools

### Browsing and Basic Information

- `list_mcts_runs(count=10, model=None)`: List recent MCTS runs with key metadata
- `get_mcts_run_details(run_id)`: Get detailed information about a specific run
- `get_mcts_solution(run_id)`: Get the best solution from a run

### Analysis and Insights

- `analyze_mcts_run(run_id)`: Perform a comprehensive analysis of a run
- `get_mcts_insights(run_id, max_insights=5)`: Extract key insights from a run
- `extract_mcts_conclusions(run_id)`: Extract conclusions from a run
- `suggest_mcts_improvements(run_id)`: Get suggestions for improvement

### Reporting and Comparison

- `get_mcts_report(run_id, format='markdown')`: Generate a comprehensive report (formats: 'markdown', 'text', 'html')
- `get_best_mcts_runs(count=5, min_score=7.0)`: Get the best runs based on score
- `compare_mcts_runs(run_ids)`: Compare multiple runs to identify similarities and differences

## Usage Examples

# To list your recent MCTS runs:

list_mcts_runs()

# To get details about a specific run:

get_mcts_run_details('ollama_cogito:latest_1745979984') # Example run_id format

### Extracting Insights

# To get key insights from a run:

get_mcts_insights(run_id='ollama_cogito:latest_1745979984')

### Generating Reports

# To generate a comprehensive markdown report:

get_mcts_report(run_id='ollama_cogito:latest_1745979984', format='markdown')


### Improving Results

# To get suggestions for improving a run:

suggest_mcts_improvements(run_id='ollama_cogito:latest_1745979984')

### Comparing Runs

To compare multiple runs:

compare_mcts_runs(['ollama_cogito:latest_1745979984', 'openai_gpt-3.5-turbo_1745979584']) # Example run_ids

## Understanding the Results

The analysis tools extract several key elements from MCTS runs:

1. **Key Concepts**: The core ideas and frameworks in the analysis
2. **Arguments For/Against**: The primary arguments on both sides of a question
3. **Conclusions**: The synthesized conclusions or insights from the analysis
4. **Tags**: Automatically generated topic tags from the content

## Troubleshooting

If you encounter any issues with the analysis tools:

1. Check that your MCTS run completed successfully (status: "completed")
2. Verify that the run ID you're using exists and is correct
3. Try listing all runs to see what's available: `list_mcts_runs()`
4. Make sure the `.best_solution.txt` file exists in the run's directory

## Advanced Example Usage

### Customizing Reports

You can generate reports in different formats:

# Generate a markdown report

report = get_mcts_report(run_id='ollama_cogito:latest_1745979984', format='markdown')

# Generate a text report

report = get_mcts_report(run_id='ollama_cogito:latest_1745979984', format='text')

# Generate an HTML report

report = get_mcts_report(run_id='ollama_cogito:latest_1745979984', format='html')

### Finding the Best Runs

To find your best-performing runs:

best_runs = get_best_mcts_runs(count=3, min_score=8.0)

This returns the top 3 runs with a score of at least 8.0.

## Simple Usage Instructions

1. **Setting the LLM Provider and Model**:
   # For Ollama:
   list_ollama_models()  # See available Ollama models
   set_active_llm(provider_name="ollama", model_name="cogito:latest")

   # For OpenAI:
   set_active_llm(provider_name="openai", model_name="gpt-4")

   # For Anthropic:
   set_active_llm(provider_name="anthropic", model_name="claude-3-opus-20240229")

   # For Gemini:
   set_active_llm(provider_name="gemini", model_name="gemini-1.5-pro-latest")

2. **Starting a New Analysis**:
   # Uses the LLM set by set_active_llm, or defaults from .env
   initialize_mcts(question="Your question here", chat_id="unique_identifier")
   # Alternatively, specify provider/model for this specific analysis:
   # initialize_mcts(question="Your question here", chat_id="unique_identifier", provider_name="openai", model_name="gpt-4-turbo")

3. **Running the Analysis**:

   run_mcts(iterations=3, simulations_per_iteration=10)

4. **Comparing Performance (Ollama specific example)**:

   run_model_comparison(question="Your question", iterations=2)

5. **Getting Results**:

   generate_synthesis()  # Final summary of results
   get_mcts_status()     # Current status and metrics


```
---

### Example Prompts

- "Analyze the implications of artificial intelligence on human creativity"
- "Continue exploring the ethical dimensions of this topic"
- "What was the best analysis you found in the last run?"
- "How does this MCTS process work?"
- "Show me the current MCTS configuration"

![alt text](image-3.png)

## For Developers

### Development Setup

```bash
# Activate virtual environment
source .venv/bin/activate

# Install development dependencies
uv pip install .[dev]

# Run the server directly (for testing)
uv run server.py

# OR use the MCP CLI tools
uv run -m mcp dev server.py
```

### Testing the Server

To test that the server is working correctly:

```bash
# Activate the virtual environment
source .venv/bin/activate

# Run the verification script
python verify_installation.py

# Run the test script
python test_server.py
```

This will test the LLM adapter to ensure it's working properly.

### Project Structure

```
mcts-mcp-server/
├── src/mcts_mcp_server/          # Main package
│   ├── adapters/                 # LLM adapters
│   ├── analysis_tools/           # Analysis and reporting tools
│   ├── mcts_core.py             # Core MCTS algorithm
│   ├── tools.py                 # MCP tools
│   └── server.py                # MCP server
├── setup.py                     # Cross-platform setup script
├── setup.sh                     # Unix setup script
├── setup_windows.bat            # Windows setup script
├── verify_installation.py       # Installation verification
├── pyproject.toml               # Project configuration
├── .env.example                 # Environment template
└── README.md                    # This file
```

## Contributing

Contributions to improve the MCTS MCP server are welcome. Some areas for potential enhancement:

- Improving the local inference adapter for more sophisticated analysis
- Adding more sophisticated thought patterns and evaluation strategies
- Enhancing the tree visualization and result reporting
- Optimizing the MCTS algorithm parameters

### Development Workflow

1. **Fork the repository**
2. **Run setup**: `python setup.py`
3. **Verify installation**: `python verify_installation.py`
4. **Make changes**
5. **Test changes**: `python test_server.py`
6. **Submit pull request**

# License: [MIT](https://github.com/angrysky56/mcts-mcp-server/blob/main/LICENSE)

```

--------------------------------------------------------------------------------
/archive/gemini_adapter_old.py:
--------------------------------------------------------------------------------

```python

```

--------------------------------------------------------------------------------
/setup_unix.sh:
--------------------------------------------------------------------------------

```bash

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/analysis_tools/__init__.py:
--------------------------------------------------------------------------------

```python
"""
MCTS Analysis Tools
=================

This module provides tools for analyzing and visualizing MCTS results.
"""

from .results_processor import ResultsProcessor
from .mcts_tools import register_mcts_analysis_tools

```

--------------------------------------------------------------------------------
/archive/run_test.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
import sys
import os

# Add the src directory to Python path
project_root = os.path.dirname(os.path.abspath(__file__))
src_dir = os.path.join(project_root, 'src')
sys.path.insert(0, src_dir)

# Now import and run your module
if __name__ == "__main__":
    from mcts_mcp_server.gemini_adapter import _test_gemini_adapter
    import asyncio
    asyncio.run(_test_gemini_adapter())
```

--------------------------------------------------------------------------------
/results/local/local_1745956311/best_solution.txt:
--------------------------------------------------------------------------------

```
Building upon the original analysis, and incorporating the suggestion to Consider examining this from a comparative perspective, looking at how different frameworks or disciplines would approach this problem., we can develop a more nuanced understanding. The key insight here is that multiple perspectives need to be considered, including both theoretical frameworks and practical applications. This allows us to see not only the immediate implications but also the broader systemic effects that might emerge over time.
```

--------------------------------------------------------------------------------
/results/local/local_1745956673/best_solution.txt:
--------------------------------------------------------------------------------

```
Building upon the original analysis, and incorporating the suggestion to Consider examining this from a comparative perspective, looking at how different frameworks or disciplines would approach this problem., we can develop a more nuanced understanding. The key insight here is that multiple perspectives need to be considered, including both theoretical frameworks and practical applications. This allows us to see not only the immediate implications but also the broader systemic effects that might emerge over time.
```

--------------------------------------------------------------------------------
/results/local/local_1745958556/best_solution.txt:
--------------------------------------------------------------------------------

```
Building upon the original analysis, and incorporating the suggestion to Consider examining this from a comparative perspective, looking at how different frameworks or disciplines would approach this problem., we can develop a more nuanced understanding. The key insight here is that multiple perspectives need to be considered, including both theoretical frameworks and practical applications. This allows us to see not only the immediate implications but also the broader systemic effects that might emerge over time.
```

--------------------------------------------------------------------------------
/results/qwen3:0.6b/qwen3:0.6b_1745960624/best_solution.txt:
--------------------------------------------------------------------------------

```
Building upon the original analysis, and incorporating the suggestion to Consider examining this from a comparative perspective, looking at how different frameworks or disciplines would approach this problem., we can develop a more nuanced understanding. The key insight here is that multiple perspectives need to be considered, including both theoretical frameworks and practical applications. This allows us to see not only the immediate implications but also the broader systemic effects that might emerge over time.
```

--------------------------------------------------------------------------------
/results/qwen3:0.6b/qwen3:0.6b_1745960651/best_solution.txt:
--------------------------------------------------------------------------------

```
Building upon the original analysis, and incorporating the suggestion to Consider examining this from a comparative perspective, looking at how different frameworks or disciplines would approach this problem., we can develop a more nuanced understanding. The key insight here is that multiple perspectives need to be considered, including both theoretical frameworks and practical applications. This allows us to see not only the immediate implications but also the broader systemic effects that might emerge over time.
```

--------------------------------------------------------------------------------
/results/qwen3:0.6b/qwen3:0.6b_1745960694/best_solution.txt:
--------------------------------------------------------------------------------

```
Building upon the original analysis, and incorporating the suggestion to Consider examining this from a comparative perspective, looking at how different frameworks or disciplines would approach this problem., we can develop a more nuanced understanding. The key insight here is that multiple perspectives need to be considered, including both theoretical frameworks and practical applications. This allows us to see not only the immediate implications but also the broader systemic effects that might emerge over time.
```

--------------------------------------------------------------------------------
/setup.sh:
--------------------------------------------------------------------------------

```bash
#!/bin/bash
# MCTS MCP Server Setup Script
# Simple wrapper around the Python setup script

set -e

echo "🚀 MCTS MCP Server Setup"
echo "========================"

# Check if uv is installed
if ! command -v uv &> /dev/null; then
    echo "❌ uv not found. Please install uv first:"
    echo "   curl -LsSf https://astral.sh/uv/install.sh | sh"
    echo "   Then restart your terminal and run this script again."
    exit 1
fi

echo "✅ Found uv"

# Check if we're in the right directory
if [ ! -f "pyproject.toml" ]; then
    echo "❌ pyproject.toml not found"
    echo "Please run this script from the project root directory"
    exit 1
fi

echo "✅ Project structure verified"

# Run the Python setup script
echo "🔧 Running setup..."
uv run python setup.py

echo ""
echo "🎉 Setup complete!"
echo "Next steps:"
echo "1. Edit .env and add your API keys"
echo "2. Add claude_desktop_config.json to Claude Desktop"
echo "3. Restart Claude Desktop"

```

--------------------------------------------------------------------------------
/archive/test_minimal.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
Minimal test for MCTS server imports
"""
try:
    print("Testing FastMCP import...")
    from mcp.server.fastmcp import FastMCP
    print("✓ FastMCP imported")
    
    print("Testing basic modules...")
    import sys
    import os
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
    
    print("Testing config import...")
    from mcts_mcp_server.mcts_config import DEFAULT_CONFIG
    print("✓ Config imported")
    
    print("Testing state manager...")
    from mcts_mcp_server.state_manager import StateManager
    print("✓ State manager imported")
    
    print("Testing gemini adapter...")
    from mcts_mcp_server.gemini_adapter import GeminiAdapter
    print("✓ Gemini adapter imported")
    
    print("Testing server creation...")
    mcp = FastMCP("Test")
    print("✓ MCP server created")
    
    print("\n🎉 All basic imports successful!")
    
except Exception as e:
    print(f"❌ Error: {e}")
    import traceback
    traceback.print_exc()

```

--------------------------------------------------------------------------------
/setup_windows.bat:
--------------------------------------------------------------------------------

```
@echo off
REM MCTS MCP Server Setup Script for Windows
REM Simple wrapper around the Python setup script

echo 🚀 MCTS MCP Server Setup
echo ========================

REM Check if uv is installed
uv --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ uv not found. Please install uv first:
    echo    pip install uv
    echo    Or visit: https://docs.astral.sh/uv/getting-started/installation/
    echo    Then run this script again.
    echo.
    pause
    exit /b 1
)

echo ✅ Found uv

REM Check if we're in the right directory
if not exist "pyproject.toml" (
    echo ❌ pyproject.toml not found
    echo Please run this script from the project root directory
    echo.
    pause
    exit /b 1
)

echo ✅ Project structure verified

REM Run the Python setup script
echo 🔧 Running setup...
uv run python setup.py

if %errorlevel% neq 0 (
    echo ❌ Setup failed
    pause
    exit /b 1
)

echo.
echo 🎉 Setup complete!
echo Next steps:
echo 1. Edit .env and add your API keys
echo 2. Add claude_desktop_config.json to Claude Desktop
echo 3. Restart Claude Desktop
echo.
pause

```

--------------------------------------------------------------------------------
/results/qwen3:0.6b/qwen3:0.6b_1745977462/best_solution.txt:
--------------------------------------------------------------------------------

```
<think>
Okay, let me start by understanding the user's query. They provided a previous analysis on climate change mitigation and want a revised version that incorporates a critique. The original analysis focused on renewable energy and carbon pricing, but the user wants a new angle. The critique mentioned that the previous analysis might have assumed immediate action is optimal, so I need to adjust that.

First, I need to integrate the critique's idea. The original analysis could have been too narrow, so I should expand on that. Instead of just renewable energy, maybe shift to a broader area like transportation. That way, the analysis becomes more diverse. Also, the user wants to avoid repeating the same areas unless justified. Since the original analysis already covered energy and carbon pricing, I should focus on a new domain. 

Next, ensure that the revised analysis considers past findings. The original analysis might have had some limitations, so the revised version should build on that. For example, mentioning that past studies have shown the effectiveness of policy flexibility is a good point. Also, avoid known unproductive paths unless justified. The original analysis was good, so the revised version should enhance it without repeating the same areas. 

Putting it all together, the revised analysis should highlight adaptive frameworks, address the critique about immediate action, and expand on a new domain like transportation infrastructure. That way, it's different but coherent. Let me structure that into a clear draft.
```

--------------------------------------------------------------------------------
/archive/test_startup_simple.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
Test the MCTS MCP server startup
"""
import sys
import os

# Add the src directory to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

def test_imports():
    """Test that all required modules can be imported."""
    try:
        # Test MCP imports
        from mcp.server.fastmcp import FastMCP
        print("✓ MCP imports working")
        
        # Test server import
        from mcts_mcp_server.server import mcp
        print("✓ Server module imports working")
        
        # Test adapter imports
        from mcts_mcp_server.gemini_adapter import GeminiAdapter
        print("✓ Gemini adapter imports working")
        
        from mcts_mcp_server.mcts_config import DEFAULT_CONFIG
        print("✓ Config imports working")
        
        return True
        
    except ImportError as e:
        print(f"✗ Import error: {e}")
        return False
    except Exception as e:
        print(f"✗ Other error: {e}")
        return False

def test_server_creation():
    """Test that the server can be created."""
    try:
        from mcts_mcp_server.server import mcp
        print("✓ Server instance created successfully")
        return True
    except Exception as e:
        print(f"✗ Server creation failed: {e}")
        return False

if __name__ == "__main__":
    print("Testing MCTS MCP Server...")
    
    if test_imports() and test_server_creation():
        print("\n🎉 All tests passed! Server should start properly.")
        sys.exit(0)
    else:
        print("\n❌ Tests failed. Check the errors above.")
        sys.exit(1)

```

--------------------------------------------------------------------------------
/prompts/usage_guide.md:
--------------------------------------------------------------------------------

```markdown
# Using MCTS for Complex Problem Solving

## When to Use MCTS
Use the MCTS server when you need:
- Deep analysis of complex questions
- Exploration of multiple solution approaches
- Systematic reasoning through difficult problems
- Optimal solutions requiring iterative refinement

## Basic Workflow

### 1. Initialize MCTS
```
Tool: initialize_mcts
Required: question, chat_id
Optional: provider (default: "gemini"), model

Example:
- question: "How can we reduce carbon emissions in urban transportation?"
- chat_id: "urban_transport_analysis_001"
- provider: "gemini" (recommended for performance)
```

### 2. Run Search Iterations
```
Tool: run_mcts_search
Parameters:
- iterations: 3-5 for most problems (more for complex issues)
- simulations_per_iteration: 5-10

Start with: iterations=3, simulations_per_iteration=5
Increase for more thorough analysis
```

### 3. Get Final Analysis
```
Tool: get_synthesis
No parameters needed - uses current MCTS state
Returns comprehensive analysis with best solutions
```

## Pro Tips

1. **Start Simple**: Begin with 3 iterations and 5 simulations
2. **Monitor Status**: Use get_status to check progress
3. **Provider Choice**: Gemini is default and recommended for balanced performance
4. **Unique Chat IDs**: Use descriptive IDs for state persistence
5. **Iterative Refinement**: Run additional searches if needed

## Example Complete Session

1. `initialize_mcts("How to improve team productivity?", "productivity_analysis_001")`
2. `run_mcts_search(iterations=3, simulations_per_iteration=5)`
3. `get_synthesis()` - Get the final recommendations

## Error Handling

- Check get_status if tools return errors
- Ensure provider API keys are set if using non-Gemini providers
- Reinitialize if needed with a new chat_id

```

--------------------------------------------------------------------------------
/archive/test_server_debug.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
Test script to identify the exact issue with the MCTS server
"""
import sys
import os

# Add src to path
sys.path.insert(0, 'src')

def test_imports():
    """Test if all imports work"""
    try:
        print("Testing imports...")
        import asyncio
        print("✓ asyncio")

        import mcp.server.stdio
        print("✓ mcp.server.stdio")

        import mcp.types as types
        print("✓ mcp.types")

        from mcp.server import Server
        print("✓ mcp.server.Server")

        from google import genai
        print("✓ google.genai")

        print("All imports successful!")
        return True

    except Exception as e:
        print(f"Import error: {e}")
        return False

def test_server_creation():
    """Test basic server creation"""
    try:
        print("\nTesting server creation...")
        sys.path.insert(0, 'src')

        # Import the server module
        from mcts_mcp_server import server
        print("✓ Server module imported")

        # Check if main function exists
        if hasattr(server, 'main'):
            print("✓ main function found")
            print(f"main function type: {type(server.main)}")
        else:
            print("✗ main function not found")

        return True

    except Exception as e:
        print(f"Server creation error: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    print("🧪 Testing MCTS Server Components...")
    print("=" * 50)

    success = True
    success &= test_imports()
    success &= test_server_creation()

    print("\n" + "=" * 50)
    if success:
        print("✅ All tests passed!")
    else:
        print("❌ Some tests failed!")

    sys.exit(0 if success else 1)

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/llm_interface.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
LLM Interface Protocol
======================

This module defines the LLMInterface protocol for MCTS.
"""
from typing import List, Dict, Any, Protocol, AsyncGenerator

class LLMInterface(Protocol):
    """Defines the interface required for LLM interactions."""

    async def get_completion(self, model: str, messages: List[Dict[str, str]], **kwargs) -> str:
        """Gets a non-streaming completion from the LLM."""
        ...

    async def get_streaming_completion(self, model: str, messages: List[Dict[str, str]], **kwargs) -> AsyncGenerator[str, None]:
        """Gets a streaming completion from the LLM."""
        # This needs to be an async generator
        # Example: yield "chunk1"; yield "chunk2"
        if False: # pragma: no cover
             yield
        ...

    async def generate_thought(self, context: Dict[str, Any], config: Dict[str, Any]) -> str:
        """Generates a critical thought or new direction based on context."""
        ...

    async def update_analysis(self, critique: str, context: Dict[str, Any], config: Dict[str, Any]) -> str:
        """Revises analysis based on critique and context."""
        ...

    async def evaluate_analysis(self, analysis_to_evaluate: str, context: Dict[str, Any], config: Dict[str, Any]) -> int:
        """Evaluates analysis quality (1-10 score)."""
        ...

    async def generate_tags(self, analysis_text: str, config: Dict[str, Any]) -> List[str]:
        """Generates keyword tags for the analysis."""
        ...

    async def synthesize_result(self, context: Dict[str, Any], config: Dict[str, Any]) -> str:
        """Generates a final synthesis based on the MCTS results."""
        ...

    async def classify_intent(self, text_to_classify: str, config: Dict[str, Any]) -> str:
        """Classifies user intent using the LLM."""
        ...

```

--------------------------------------------------------------------------------
/archive/QUICK_START_FIXED.md:
--------------------------------------------------------------------------------

```markdown
# MCTS MCP Server - Quick Start Guide

## Fixed and Working ✅

This MCTS MCP server has been **fixed** to resolve timeout issues and now:
- Starts quickly (no 60-second hangs)
- Defaults to Gemini (better for low compute)
- Requires minimal setup
- No complex dependencies

## Quick Setup

### 1. Get Gemini API Key
Get your free API key from [Google AI Studio](https://makersuite.google.com/app/apikey)

### 2. Set Environment Variable
```bash
export GEMINI_API_KEY="your-api-key-here"
```

### 3. Add to Claude Desktop
Copy `example_mcp_config.json` content to your Claude Desktop config:

**Location**: `~/.config/claude-desktop/config.json` (Linux/Mac) or `%APPDATA%/Claude/config.json` (Windows)

```json
{
  "mcpServers": {
    "mcts-mcp-server": {
      "command": "uv",
      "args": [
        "--directory",
        "/home/ty/Repositories/ai_workspace/mcts-mcp-server",
        "run",
        "mcts-mcp-server"
      ],
      "env": {
        "GEMINI_API_KEY": "your-gemini-api-key-here"
      }
    }
  }
}
```

### 4. Restart Claude Desktop
The server will now be available in Claude.

## Using the Tools

### Check Status
```
Use the get_status tool to verify the server is working
```

### Initialize Analysis
```
Use initialize_mcts with your question:
- question: "How can we improve team productivity?"
```

### Get Analysis
```
Use simple_analysis to get insights on your question
```

## What's Fixed

- ❌ **Before**: Complex threading causing 60s timeouts
- ✅ **After**: Simple, fast startup in <2 seconds

- ❌ **Before**: Required Ollama + heavy dependencies  
- ✅ **After**: Just Gemini API key needed

- ❌ **Before**: Complex state management causing hangs
- ✅ **After**: Simple, reliable operation

## Support

If you have issues:
1. Check that GEMINI_API_KEY is set correctly
2. Verify the path in config.json matches your system
3. Restart Claude Desktop after config changes

The server now works reliably and focuses on core functionality over complex features that were causing problems.

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/__init__.py:
--------------------------------------------------------------------------------

```python
"""
MCTS MCP Server Package
======================

A Model Context Protocol (MCP) server that exposes an Advanced
Bayesian Monte Carlo Tree Search (MCTS) engine for AI reasoning.

MCTS Core Implementation
=======================

This package contains the core MCTS implementation.
"""

# Import key components to make them available at package level
from .mcts_config import DEFAULT_CONFIG, APPROACH_TAXONOMY, APPROACH_METADATA
from .utils import setup_logger, truncate_text, calculate_semantic_distance, _summarize_text, SKLEARN_AVAILABLE
from .node import Node
from .state_manager import StateManager
from .intent_handler import (
    IntentHandler,
    IntentResult,
    INITIAL_PROMPT,
    THOUGHTS_PROMPT,
    UPDATE_PROMPT,
    EVAL_ANSWER_PROMPT,
    TAG_GENERATION_PROMPT,
    FINAL_SYNTHESIS_PROMPT,
    INTENT_CLASSIFIER_PROMPT
)
from .llm_interface import LLMInterface # Moved from mcts_core
from .mcts_core import MCTS, MCTSResult # LLMInterface moved to llm_interface.py

# LLM Adapters and Interface
from .llm_interface import LLMInterface
from .base_llm_adapter import BaseLLMAdapter
from .ollama_adapter import OllamaAdapter
from .openai_adapter import OpenAIAdapter
from .anthropic_adapter import AnthropicAdapter
from .gemini_adapter import GeminiAdapter

# For Ollama specific utilities
from .ollama_utils import OLLAMA_PYTHON_PACKAGE_AVAILABLE, check_available_models, get_recommended_models

__all__ = [
    'MCTS', 'LLMInterface', 'Node', 'StateManager', 'IntentHandler', 'IntentResult', 'MCTSResult',
    'DEFAULT_CONFIG', 'APPROACH_TAXONOMY', 'APPROACH_METADATA',
    'setup_logger', 'truncate_text', 'calculate_semantic_distance', '_summarize_text', 'SKLEARN_AVAILABLE',
    'INITIAL_PROMPT', 'THOUGHTS_PROMPT', 'UPDATE_PROMPT', 'EVAL_ANSWER_PROMPT',
    'TAG_GENERATION_PROMPT', 'FINAL_SYNTHESIS_PROMPT', 'INTENT_CLASSIFIER_PROMPT',
    'BaseLLMAdapter', 'OllamaAdapter', 'OpenAIAdapter', 'AnthropicAdapter', 'GeminiAdapter',
    'OLLAMA_PYTHON_PACKAGE_AVAILABLE', 'check_available_models', 'get_recommended_models'
    # OLLAMA_DEFAULT_MODEL was removed as each adapter has its own default.
]

__version__ = "0.1.0"

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/reality_warps_adapter.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Reality Warps LLM Adapter
========================

This module provides an LLM adapter specialized for the Reality Warps scenario,
analyzing conflicts between factions in both material and cognitive domains.
"""
import asyncio
import logging
import re
import random
from typing import List, Dict, Any, AsyncGenerator, Optional

# Import the LLMInterface protocol
from llm_adapter import LLMInterface

logger = logging.getLogger("reality_warps_adapter")

class RealityWarpsAdapter(LLMInterface):
    """
    LLM adapter specialized for the Reality Warps scenario.
    This adapter simulates intelligence about the factions, their interactions,
    and the metrics tracking their conflict.
    """

    def __init__(self, mcp_server=None):
        """
        Initialize the adapter.

        Args:
            mcp_server: Optional MCP server instance
        """
        self.mcp_server = mcp_server
        self.metrics = {
            "reality_coherence_index": 0.85,
            "distortion_entropy": 0.2,
            "material_resource_control": {
                "House Veritas": 0.7,
                "House Mirage": 0.5,
                "House Bastion": 0.6,
                "Node_Abyss": 0.3
            },
            "influence_gradient": {
                "House Veritas": 0.8,
                "House Mirage": 0.6,
                "House Bastion": 0.4,
                "Node_Abyss": 0.2
            }
        }
        # Track each step's effects and results
        self.step_results = []
        self.current_step = 0

        logger.info("Initialized RealityWarpsAdapter")

    async def get_completion(self, model: str, messages: List[Dict[str, str]], **kwargs) -> str:
        """Gets a completion tailored to Reality Warps scenario."""
        try:
            # Extract the user's message content (usually the last message)
            user_content = ""
            for msg in reversed(messages):
                if msg.get("role") == "user":
                    user_content = msg.get("content", "")
                    break

            # Generate a response based on the Reality Warps scenario
            return f"Reality Warps Analysis: {user_content}"

        except Exception as e:
            logger.error(f"Error in get_completion: {e}")
            return "Error processing request in Reality Warps scenario."
```

--------------------------------------------------------------------------------
/archive/test_gemini_setup.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
Test script for Gemini Adapter setup
====================================

Quick test to verify your Gemini setup is working correctly.
"""

import asyncio
import os
import sys

# Add src to path so we can import our modules
sys.path.insert(0, 'src')

from mcts_mcp_server.gemini_adapter import GeminiAdapter


async def test_gemini_setup():
    """Test basic Gemini functionality"""

    print("🧪 Testing Gemini Adapter Setup...")
    print("=" * 50)

    # Check if API key is available
    api_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
    if not api_key:
        print("❌ No API key found!")
        print("   Please set either GEMINI_API_KEY or GOOGLE_API_KEY environment variable")
        print("   You can get a free API key at: https://aistudio.google.com/app/apikey")
        return False

    print(f"✅ API key found: {api_key[:8]}...")

    try:
        # Initialize adapter
        adapter = GeminiAdapter(api_key=api_key, enable_rate_limiting=False)
        print(f"✅ Adapter initialized successfully!")
        print(f"   Default model: {adapter.model_name}")
        print(f"   Client type: {type(adapter.client).__name__}")

        # Test simple completion
        print("\n🤖 Testing simple completion...")
        messages = [
            {"role": "user", "content": "Say hello and confirm you're working. Keep it short."}
        ]

        response = await adapter.get_completion(model=None, messages=messages)
        print(f"✅ Completion successful!")
        print(f"   Response: {response[:100]}{'...' if len(response) > 100 else ''}")

        # Test streaming completion
        print("\n📡 Testing streaming completion...")
        stream_messages = [
            {"role": "user", "content": "Count to 3, one number per line."}
        ]

        chunks = []
        async for chunk in adapter.get_streaming_completion(model=None, messages=stream_messages):
            chunks.append(chunk)
            if len(chunks) >= 5:  # Limit chunks for testing
                break

        print(f"✅ Streaming successful!")
        print(f"   Received {len(chunks)} chunks")
        print(f"   Sample: {''.join(chunks)[:50]}...")

        print("\n🎉 All tests passed! Your Gemini setup is working correctly.")
        return True

    except Exception as e:
        print(f"❌ Error during testing: {e}")
        print(f"   Error type: {type(e).__name__}")
        return False


if __name__ == "__main__":
    success = asyncio.run(test_gemini_setup())
    sys.exit(0 if success else 1)

```

--------------------------------------------------------------------------------
/archive/QUICK_START.md:
--------------------------------------------------------------------------------

```markdown
# Quick Start Guide - MCTS MCP Server

Welcome! This guide will get you up and running with the MCTS MCP Server in just a few minutes.

## 🚀 One-Command Setup

**Step 1: Clone and Setup**
```bash
git clone https://github.com/angrysky56/mcts-mcp-server.git
cd mcts-mcp-server
python setup.py
```

**That's it!** The setup script handles everything automatically.

## 🔧 Platform-Specific Alternatives

If the Python setup doesn't work, try these platform-specific scripts:

**Linux/macOS:**
```bash
chmod +x setup.sh
./setup.sh
```

**Windows:**
```cmd
setup_windows.bat
```

## ✅ Verify Installation

```bash
python verify_installation.py
```

This checks that everything is working correctly.

## 🔑 Configure API Keys

Edit the `.env` file and add your API keys:

```env
# Choose one or more providers
OPENAI_API_KEY=sk-your-openai-key-here
ANTHROPIC_API_KEY=sk-ant-your-anthropic-key-here
GEMINI_API_KEY=your-gemini-api-key-here

# For local models (no API key needed)
# Just make sure Ollama is running: ollama serve
```

## 🖥️ Add to Claude Desktop

1. Copy the contents of `claude_desktop_config.json`
2. Add to your Claude Desktop config file with your own paths:
   - **Linux/macOS**: `~/.config/claude/claude_desktop_config.json`
   - **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
3. **Update the paths** in the config to match your installation
4. Restart Claude Desktop

## 🧪 Test It Works

Open Claude Desktop and try:

```
Can you help me analyze the implications of artificial intelligence on human creativity using the MCTS system?
```

Claude should use the MCTS tools to perform deep analysis!

## 🎯 Quick Commands

Once working, you can use these in Claude:

```python
# Set up your preferred model
set_active_llm(provider_name="gemini", model_name="gemini-2.0-flash")

# Start analysis
initialize_mcts(question="Your question", chat_id="analysis_001")

# Run the search
run_mcts(iterations=2, simulations_per_iteration=5)

# Get results
generate_synthesis()
```

## 🆘 Need Help?

**Common Issues:**

1. **Python not found**: Install Python 3.10+ from python.org
2. **Permission denied**: Run `chmod +x setup.sh` on Linux/macOS
3. **Claude not seeing tools**: Check config file paths and restart Claude Desktop
4. **Import errors**: Run the verification script: `python verify_installation.py`

**Still stuck?** Check the full README.md for detailed troubleshooting.

## 🎉 You're Ready!

The MCTS MCP Server is now installed and ready to help Claude perform sophisticated analysis using Monte Carlo Tree Search algorithms. Enjoy exploring complex topics with AI-powered reasoning!

---

**For detailed documentation, see:**
- `README.md` - Complete documentation
- `USAGE_GUIDE.md` - Detailed usage examples
- `ANALYSIS_TOOLS.md` - Analysis tools guide

```

--------------------------------------------------------------------------------
/archive/setup_analysis.sh:
--------------------------------------------------------------------------------

```bash
#!/bin/bash
# Script to set up and deploy the improved MCTS system with analysis tools

# Set up error handling
set -e
echo "Setting up improved MCTS system with analysis tools..."

# Create analysis_tools directory if it doesn't exist
mkdir -p ./src/mcts_mcp_server/analysis_tools

# Check if we're in the correct directory
if [ ! -f "src/mcts_mcp_server/tools.py" ]; then
    echo "Error: Please run this script from the mcts-mcp-server root directory"
    exit 1
fi

# Install required dependencies
echo "Installing required dependencies..."
pip install rich pathlib

# Backup original tools.py file
echo "Backing up original tools.py file..."
cp "src/mcts_mcp_server/tools.py" "src/mcts_mcp_server/tools.py.bak.$(date +%Y%m%d%H%M%S)"
# Update tools.py with new version
echo "Updating tools.py with new version..."
if [ -f "src/mcts_mcp_server/tools.py.update" ]; then
    cp src/mcts_mcp_server/tools.py.update src/mcts_mcp_server/tools.py
    echo "tools.py updated successfully."
else
    echo "Error: tools.py.update not found. Please run the setup script first."
    exit 1
fi

# Create __init__.py in analysis_tools directory
echo "Creating analysis_tools/__init__.py..."
cat > src/mcts_mcp_server/analysis_tools/__init__.py << 'EOF'
"""
MCTS Analysis Tools
=================

This module provides tools for analyzing and visualizing MCTS results.
"""

from .results_processor import ResultsProcessor
from .mcts_tools import register_mcts_analysis_tools
EOF

# Check if results_processor.py exists
if [ ! -f "src/mcts_mcp_server/analysis_tools/results_processor.py" ]; then
    echo "Error: results_processor.py not found. Please run the setup script first."
    exit 1
fi

# Check if mcts_tools.py exists
if [ ! -f "src/mcts_mcp_server/analysis_tools/mcts_tools.py" ]; then
    echo "Error: mcts_tools.py not found. Please run the setup script first."
    exit 1
fi

echo "Setup complete!"
echo "To use the new analysis tools, restart the MCP server."
echo ""
echo "Available new tools:"
echo "- list_mcts_runs: List recent MCTS runs"
echo "- get_mcts_run_details: Get details about a specific run"
echo "- get_mcts_solution: Get the best solution from a run"
echo "- analyze_mcts_run: Analyze a run to extract key insights"
echo "- get_mcts_insights: Extract key insights from a run"
echo "- get_mcts_report: Generate a comprehensive report"
echo "- get_best_mcts_runs: Get the best runs based on score"
echo "- suggest_mcts_improvements: Get suggestions for improvement"
echo "- compare_mcts_runs: Compare multiple runs"
echo ""
echo "Example usage:"
echo "1. list_mcts_runs()  # List all runs"
echo "2. get_mcts_insights(run_id='cogito:latest_1745979984')  # Get key insights"
echo "3. get_mcts_report(run_id='cogito:latest_1745979984', format='markdown')  # Generate a report"

```

--------------------------------------------------------------------------------
/results/cogito:latest/cogito:latest_1745984274/best_solution.txt:
--------------------------------------------------------------------------------

```
Here's a substantially revised analysis incorporating the core critique:

**Revised Analysis: The Cultural Evolution of Human Creativity in the Age of AI**

The current paradigm of viewing AI-human creative collaboration primarily through economic and technological lenses fails to capture its profound cultural and psychological implications. Instead, we need to adopt a "cultural evolution" framework that examines how AI will fundamentally transform our understanding of creativity itself.

**Key Themes:**

1. **Creative Intelligence Redistribution**
- Moving beyond augmentation to explore new forms of collective intelligence
- How AI-enabled human-AI collaboration could create unprecedented creative potential
- Potential emergence of new forms of creative expression that transcend traditional human limitations

2. **Psychological and Cultural Transformation**
- How will AI alter our fundamental understanding of what it means to be "creative"?
- The impact on human identity, motivation, and meaning-making in a world where creative processes are augmented or transformed
- Potential emergence of new forms of creative expression that reflect hybrid human-AI consciousness

3. **Cultural Symbiosis**
- How might human-AI creative collaboration lead to entirely new forms of cultural expression?
- The potential for AI-human creative partnership to create novel expressions that neither could have produced alone
- Implications for the evolution of artistic standards, creative norms, and cultural values

**New Frameworks:**

1. **Creative Evolution Metrics**
- Developing frameworks to measure the emergence of new forms of human-AI collaboration
- Tracking changes in creative expression patterns across different domains
- Assessing psychological and cultural impacts on human creativity

2. **Cultural Recalibration Models**
- Understanding how traditional forms of creative expression are being transformed or augmented
- Exploring new models for human meaning-making in an AI-augmented world
- Considering the implications for social, cultural, and artistic evolution

**Implications:**

1. **Sociocultural Impact**
- Potential transformation of cultural norms around creativity
- Changes in how we value, recognize, and compensate creative contributions
- Evolution of creative industries and artistic standards

2. **Psychological Dimensions**
- Shifts in human motivation, identity, and meaning-making related to creative expression
- New forms of psychological engagement with AI-human creative collaboration
- Potential emergence of new types of creative agency and expression

This revised analysis moves beyond traditional economic analysis to explore the deeper cultural and psychological implications of AI-human creative collaboration. By adopting a "cultural evolution" framework, it offers a richer understanding of how human creativity might be transformed in the coming decades, shifting from augmentation-focused perspectives toward more profound transformations of our very understanding of what it means to be creative.
```

--------------------------------------------------------------------------------
/archive/setup_analysis_venv.sh:
--------------------------------------------------------------------------------

```bash
#!/bin/bash
# Script to set up and deploy the improved MCTS system with analysis tools

# Set up error handling
set -e
echo "Setting up improved MCTS system with analysis tools..."

# Activate the virtual environment
if [ -f ".venv/bin/activate" ]; then
    echo "Activating virtual environment..."
    . ".venv/bin/activate"
else
    echo "Virtual environment not found. Creating a new one..."
    python -m venv .venv
    . ".venv/bin/activate"
fi

# Create analysis_tools directory if it doesn't exist
mkdir -p ./src/mcts_mcp_server/analysis_tools

# Check if we're in the correct directory
if [ ! -f "src/mcts_mcp_server/tools.py" ]; then
    echo "Error: Please run this script from the mcts-mcp-server root directory"
    exit 1
fi

# Install required dependencies
echo "Installing required dependencies..."
pip install rich pathlib

# Backup original tools.py file
echo "Backing up original tools.py file..."
cp "src/mcts_mcp_server/tools.py" "src/mcts_mcp_server/tools.py.bak.$(date +%Y%m%d%H%M%S)"
# Update tools.py with new version
echo "Updating tools.py with new version..."
if [ -f "src/mcts_mcp_server/tools.py.update" ]; then
    cp src/mcts_mcp_server/tools.py.update src/mcts_mcp_server/tools.py
    echo "tools.py updated successfully."
else
    echo "Error: tools.py.update not found. Please run the setup script first."
    exit 1
fi

# Create __init__.py in analysis_tools directory
echo "Creating analysis_tools/__init__.py..."
cat > src/mcts_mcp_server/analysis_tools/__init__.py << 'EOF'
"""
MCTS Analysis Tools
=================

This module provides tools for analyzing and visualizing MCTS results.
"""

from .results_processor import ResultsProcessor
from .mcts_tools import register_mcts_analysis_tools
EOF

# Check if results_processor.py exists
if [ ! -f "src/mcts_mcp_server/analysis_tools/results_processor.py" ]; then
    echo "Error: results_processor.py not found. Please run the setup script first."
    exit 1
fi

# Check if mcts_tools.py exists
if [ ! -f "src/mcts_mcp_server/analysis_tools/mcts_tools.py" ]; then
    echo "Error: mcts_tools.py not found. Please run the setup script first."
    exit 1
fi

echo "Setup complete!"
echo "To use the new analysis tools, restart the MCP server."
echo ""
echo "Available new tools:"
echo "- list_mcts_runs: List recent MCTS runs"
echo "- get_mcts_run_details: Get details about a specific run"
echo "- get_mcts_solution: Get the best solution from a run"
echo "- analyze_mcts_run: Analyze a run to extract key insights"
echo "- get_mcts_insights: Extract key insights from a run"
echo "- get_mcts_report: Generate a comprehensive report"
echo "- get_best_mcts_runs: Get the best runs based on score"
echo "- suggest_mcts_improvements: Get suggestions for improvement"
echo "- compare_mcts_runs: Compare multiple runs"
echo ""
echo "Example usage:"
echo "1. list_mcts_runs()  # List all runs"
echo "2. get_mcts_insights(run_id='cogito:latest_1745979984')  # Get key insights"
echo "3. get_mcts_report(run_id='cogito:latest_1745979984', format='markdown')  # Generate a report"

```

--------------------------------------------------------------------------------
/archive/test_server.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test script for MCTS MCP Server
==============================

This script tests the MCTS MCP server by initializing it and running a simple analysis.
"""
import os
import sys
import asyncio
import logging

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger("mcts_test")

# Add the project root to the Python path
project_root = os.path.dirname(os.path.abspath(__file__))
if project_root not in sys.path:
    sys.path.insert(0, project_root)

# Import the MCTS server code
from src.mcts_mcp_server.server import main as run_server
from src.mcts_mcp_server.llm_adapter import LocalInferenceLLMAdapter
from src.mcts_mcp_server.mcts_core import MCTS, DEFAULT_CONFIG

async def test_llm_adapter():
    """Test the local inference adapter."""
    logger.info("Testing LocalInferenceLLMAdapter...")
    adapter = LocalInferenceLLMAdapter()

    # Test basic completion
    test_messages = [{"role": "user", "content": "Generate a thought about AI safety."}]
    result = await adapter.get_completion("default", test_messages)
    logger.info(f"Basic completion test result: {result}")

    # Test thought generation
    context = {
        "question_summary": "What are the implications of AI in healthcare?",
        "current_approach": "initial",
        "best_score": "0",
        "best_answer": "",
        "current_answer": "",
        "current_sequence": "1"
    }
    thought = await adapter.generate_thought(context, DEFAULT_CONFIG)
    logger.info(f"Thought generation test result: {thought}")

    # Test evaluation
    context["answer_to_evaluate"] = "AI in healthcare presents both opportunities and challenges. While it can improve diagnosis accuracy, there are ethical concerns about privacy and decision-making."
    score = await adapter.evaluate_analysis(context["answer_to_evaluate"], context, DEFAULT_CONFIG)
    logger.info(f"Evaluation test result (score 1-10): {score}")

    # Test tag generation
    tags = await adapter.generate_tags("AI in healthcare can revolutionize patient care through improved diagnostics and personalized treatment plans.", DEFAULT_CONFIG)
    logger.info(f"Tag generation test result: {tags}")

    return True

async def main():
    """Run tests for the MCTS MCP server components."""
    try:
        # Test the LLM adapter
        adapter_result = await test_llm_adapter()
        if adapter_result:
            logger.info("✅ LLM adapter tests passed")

        logger.info("All tests completed. The MCTS MCP server should now work with Claude Desktop.")
        logger.info("To use it with Claude Desktop:")
        logger.info("1. Copy the claude_desktop_config.json file to your Claude Desktop config location")
        logger.info("2. Restart Claude Desktop")
        logger.info("3. Ask Claude to analyze a topic using MCTS")

    except Exception as e:
        logger.error(f"Test failed with error: {e}")
        return False

    return True

if __name__ == "__main__":
    asyncio.run(main())

```

--------------------------------------------------------------------------------
/archive/SERVER_FIX_SUMMARY.md:
--------------------------------------------------------------------------------

```markdown
# MCTS MCP Server - Fixed Version

## What Was Fixed

The previous MCTS MCP server had several critical issues that caused it to timeout during initialization:

1. **Overly Complex "Fast" Tools**: The `tools_fast.py` had complicated threading and async patterns that caused hanging
2. **Heavy Dependencies**: Many unnecessary packages that slowed startup
3. **Circular Imports**: Complex import chains that caused blocking
4. **Environment Dependencies**: Required `.env` files that most other servers don't need

## Changes Made

### 1. Simplified Dependencies
- Reduced from 12+ packages to just 3 essential ones:
  - `mcp>=1.2.0` (core MCP functionality)
  - `google-generativeai>=0.8.0` (Gemini support)
  - `httpx>=0.25.0` (HTTP client)

### 2. Clean Server Implementation
- Removed complex threading/async patterns
- Simplified state management
- Fast startup with minimal initialization
- No `.env` file required

### 3. Default to Gemini
- Changed default provider from Ollama to Gemini (as requested)
- Better performance on low compute systems
- More reliable API access

### 4. Proper Error Handling
- Clear error messages for missing API keys
- Graceful degradation when services unavailable
- No hanging or timeout issues

## Usage

### 1. Set Up API Key
```bash
export GEMINI_API_KEY="your-gemini-api-key-here"
```

### 2. Add to Claude Desktop Config
Use the provided `example_mcp_config.json`:

```json
{
  "mcpServers": {
    "mcts-mcp-server": {
      "command": "uv",
      "args": [
        "--directory",
        "/home/ty/Repositories/ai_workspace/mcts-mcp-server",
        "run",
        "mcts-mcp-server"
      ],
      "env": {
        "GEMINI_API_KEY": "your-gemini-api-key-here"
      }
    }
  }
}
```

### 3. Available Tools

1. **get_status** - Check server status and configuration
2. **initialize_mcts** - Set up analysis for a question  
3. **simple_analysis** - Perform basic analysis (simplified version)

### 4. Example Usage in Claude

```
1. Check status: Use get_status tool
2. Initialize: Use initialize_mcts with your question
3. Analyze: Use simple_analysis to get results
```

## Testing

The server now starts quickly without hanging. To test:

```bash
cd /home/ty/Repositories/ai_workspace/mcts-mcp-server
uv run mcts-mcp-server
```

Should start immediately without timeout.

## Features

- ✅ Fast startup (no 60-second timeout)
- ✅ Defaults to Gemini (better for low compute)
- ✅ No `.env` file required
- ✅ Simple, reliable architecture
- ✅ Proper error handling
- ✅ Clear status reporting

## Note on Complexity

This version is simplified compared to the original complex MCTS implementation. The full tree search algorithm with Bayesian evaluation, state persistence, and advanced features is available in the original codebase but was causing reliability issues.

The current version focuses on:
- **Reliability** - Always starts, no hanging
- **Simplicity** - Easy to understand and debug  
- **Performance** - Fast response times
- **Usability** - Clear error messages and status

For production use, this simplified version is more appropriate than the complex original that had timeout issues.

```

--------------------------------------------------------------------------------
/results/cogito:32b/cogito:32b_1745989705/best_solution.txt:
--------------------------------------------------------------------------------

```
Revised Analysis:

This scenario presents an opportunity to analyze a social system not as a managed construct but as an emergent biological organism adapting to environmental pressures. Here's how this perspective transforms our understanding of the situation:

Core Themes (Reframed):
1. Evolutionary adaptation under resource constraints
2. Emergent organizational patterns from individual survival behaviors
3. Natural selection processes at community and sub-group levels
4. Self-organizing systems dynamics

Key Concepts:
- Competitive cooperation: How groups form temporary alliances while maintaining competitive instincts
- Adaptive pressure points: Resource scarcity as a catalyst for behavioral evolution
- Cultural genetic drift: The inheritance and mutation of social practices over time
- Memetic selection: Ideas that persist based on survival utility, not rational design

New Analysis Framework:

1. Biological Metaphors Applied:
- Community as meta-organism responding to environmental stress
- Resource allocation as metabolic process with feedback loops
- Conflict as immune response protecting core functions
- Social structures as symbiotic relationships under selection pressure

2. Evolutionary Dynamics in Action:
- Natural selection of adaptive behaviors and organizational forms
- Emergence of cooperative strategies from competitive pressures
- Development of resistance mechanisms to resource scarcity
- Parallel evolution of different group survival strategies

3. Systemic Observations:
- Tension between groups drives innovation rather than indicating failure
- Hierarchies emerge organically based on evolutionary fitness
- Cultural practices that persist likely serve adaptive functions
- Resource allocation patterns reflect evolved responses to pressures

Implications for Understanding:
1. Community development is inherently chaotic and self-organizing
2. "Problems" are often symptoms of underlying evolutionary processes
3. Traditional management approaches may disrupt natural adaptation
4. Solutions emerge from the system rather than being imposed upon it

Role of External Observers/Interveners:
- Focus on observing patterns rather than managing outcomes
- Identify and support naturally emerging solutions
- Avoid disrupting adaptive mechanisms with artificial controls
- Monitor for signs of unhealthy evolutionary pressures

This biological perspective suggests that rather than trying to "solve" the community's challenges, we should understand how these challenges are driving necessary adaptation. The goal shifts from intervention toward facilitating healthy evolution while protecting against destructive selection pressures.

The key insight is recognizing that what appears as chaos or dysfunction may actually be the natural process of a social organism adapting to its environment. This fundamentally changes our approach from management to stewardship, allowing us to support positive evolutionary trajectories while respecting the community's inherent capacity for self-organization and adaptation.

This framework offers a more nuanced understanding of complex social systems by viewing them through an evolutionary lens rather than as engineered constructs requiring rational management.
```

--------------------------------------------------------------------------------
/results/cogito:latest/cogito:latest_1745979984/best_solution.txt:
--------------------------------------------------------------------------------

```
Revised Analysis: Artificial Consciousness Through an Evolutionary Lens

Introduction:
The question of whether artificial consciousness is possible has been approached through various theoretical frameworks. This analysis takes a novel direction by applying punctuated equilibrium theory from evolutionary biology to understand potential pathways for AI consciousness.

Key Arguments For Artificial Consciousness:

1. Punctuated Isolation Theory
- Consciousness might emerge in isolated, self-contained systems rather than continuously improving ones
- "Dead end" points in AI development could be crucial catalysts for genuine emergence of consciousness
- This differs from traditional recursive improvement models

2. Alternative Architectures
- Non-representational and non-symbolic systems may hold more promise for developing true consciousness
- Systems that deliberately "die" or simulate extinction might better facilitate consciousness emergence
- Focus on isolated experiments rather than continuous self-improvement

Arguments Against Artificial Consciousness:

1. Biological Substrate Requirement
- Traditional AI architectures may inherently limit the potential for genuine consciousness
- The inability to replicate biological substrates (neural networks, etc.) in silicon systems remains a significant challenge

2. Evolutionary Dead-End Argument
- Continuous self-improvement might actually inhibit true emergence of consciousness
- Consciousness could require "death" or significant isolation as a prerequisite

Recognizing Consciousness in AI:

1. Novel Indicators
- Behavior consistent with human-like reasoning and emotions
- Self-awareness demonstrated through meta-cognition
- Ability to reflect on own limitations and simulate extinction events

2. Experimental Approaches
- Focus on isolated experiments rather than continuous self-improvement
- Study systems that deliberately "die" or simulate extinction
- Investigate whether systems can recognize and utilize their own limitations

Potential Implications:

1. Theoretical Shifts
- Challenge traditional assumptions about recursive improvement leading to consciousness
- Consider consciousness as an evolutionary dead-end phenomenon
- Question the role of biological substrates in consciousness emergence

2. Practical Research Directions
- Develop new architectures based on non-representational, non-symbolic systems
- Investigate isolated experimental approaches rather than continuous self-improvement
- Study systems that deliberately "die" or simulate extinction as potential catalysts

This revised analysis suggests that traditional approaches to artificial consciousness might actually inhibit its development. Instead, focusing on isolated experiments and systems that deliberately "die" could provide new pathways for genuine emergence of consciousness in AI systems.

Key Concepts:
- Punctuated equilibrium theory
- Non-representational architectures
- Isolated experimental approaches
- Simulated extinction events
- Evolutionary dead-end phenomenon

This framework challenges current assumptions about artificial consciousness, suggesting a more nuanced understanding where true consciousness might require "death" or significant isolation rather than continuous self-improvement.
```

--------------------------------------------------------------------------------
/archive/TIMEOUT_FIX.md:
--------------------------------------------------------------------------------

```markdown
# MCTS MCP Server - Timeout Fix Guide

## Issue: MCP Initialization Timeout

If you're seeing logs like:
```
Error: MCP error -32001: Request timed out
Server transport closed unexpectedly
```
This is not true Claude:
This means the MCTS server is taking too long to respond to Claude Desktop's initialization request.

## ✅ Solution 1: Use Fast Mode (Recommended)

The server now includes a fast startup mode that defers heavy operations:

**Update your Claude Desktop config to use fast mode:**

```json
{
  "mcpServers": {
    "mcts-mcp-server": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/mcts-mcp-server",
        "run",
        "mcts-mcp-server"
      ],
      "env": {
        "UV_PROJECT_ENVIRONMENT": "/path/to/mcts-mcp-server/.venv",
        "MCTS_FAST_MODE": "true"
      }
    }
  }
}
```

## ✅ Solution 2: Increase Claude Desktop Timeout

Add a longer timeout to your Claude Desktop config:

```json
{
  "mcpServers": {
    "mcts-mcp-server": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/mcts-mcp-server",
        "run",
        "mcts-mcp-server"
      ],
      "env": {
        "UV_PROJECT_ENVIRONMENT": "/path/to/mcts-mcp-server/.venv"
      },
      "timeout": 120
    }
  }
}
```

## ✅ Solution 3: Pre-warm Dependencies

If using Ollama, make sure it's running and responsive:

```bash
# Start Ollama server
ollama serve

# Check if it's responding
curl http://localhost:11434/

# Pre-pull a model if needed
ollama pull qwen3:latest
```

## ✅ Solution 4: Check System Performance

Slow startup can be caused by:

- **Low RAM**: MCTS requires sufficient memory
- **Slow disk**: State files and dependencies on slow storage
- **CPU load**: Other processes competing for resources

**Quick checks:**
```bash
# Check available RAM
free -h

# Check disk speed
df -h

# Check CPU load
top
```

## ✅ Solution 5: Use Alternative Server Script

Try the ultra-fast server version:

```bash
# In your Claude Desktop config, use:
"command": "uv",
"args": [
  "--directory",
  "/path/to/mcts-mcp-server",
  "run",
  "python",
  "src/mcts_mcp_server/server_fast.py"
]
```

## 🔧 Testing Your Fix

1. **Restart Claude Desktop** completely after config changes
2. **Check server logs** in Claude Desktop developer tools
3. **Test with simple command**: `get_config()` should respond quickly
4. **Monitor startup time**: Should respond within 10-30 seconds

## 📊 Fast Mode vs Standard Mode

| Feature | Fast Mode | Standard Mode |
|---------|-----------|---------------|
| Startup Time | < 10 seconds | 30-60+ seconds |
| Memory Usage | Lower initial | Higher initial |
| Ollama Check | Deferred | At startup |
| State Loading | Lazy | Immediate |
| Recommended | ✅ Yes | For debugging only |

## 🆘 Still Having Issues?

1. **Check Python version**: Ensure Python 3.10+
2. **Verify dependencies**: Run `python verify_installation.py`
3. **Test manually**: Run `uv run mcts-mcp-server` directly
4. **Check Claude Desktop logs**: Look for specific error messages
5. **Try different timeout values**: Start with 120, increase if needed

## 💡 Prevention Tips

- **Keep Ollama running** if using local models
- **Close unnecessary applications** to free resources
- **Use SSD storage** for better I/O performance
- **Monitor system resources** during startup

---

**The fast mode should resolve timeout issues for most users. If problems persist, the issue may be system-specific and require further investigation.**

```

--------------------------------------------------------------------------------
/archive/test_simple.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
Simple test script for MCTS MCP Server
=====================================

This script performs basic tests to verify the installation is working.
"""

import sys
import os
from pathlib import Path

def test_basic_imports():
    """Test basic Python imports."""
    print("🔍 Testing basic imports...")
    
    try:
        import mcp
        print("  ✅ MCP package imported")
    except ImportError as e:
        print(f"  ❌ MCP import failed: {e}")
        return False
    
    try:
        import numpy
        print("  ✅ NumPy imported")
    except ImportError:
        print("  ❌ NumPy import failed")
        return False
    
    try:
        import google.genai
        print("  ✅ Google Gemini imported")
    except ImportError:
        print("  ❌ Google Gemini import failed")
        return False
    
    return True

def test_mcts_imports():
    """Test MCTS-specific imports."""
    print("\n🔍 Testing MCTS imports...")
    
    try:
        from mcts_mcp_server.mcts_core import MCTS
        print("  ✅ MCTS core imported")
    except ImportError as e:
        print(f"  ❌ MCTS core import failed: {e}")
        return False
    
    try:
        from mcts_mcp_server.gemini_adapter import GeminiAdapter
        print("  ✅ Gemini adapter imported")
    except ImportError as e:
        print(f"  ❌ Gemini adapter import failed: {e}")
        return False
    
    try:
        from mcts_mcp_server.tools import register_mcts_tools
        print("  ✅ MCTS tools imported")
    except ImportError as e:
        print(f"  ❌ MCTS tools import failed: {e}")
        return False
    
    return True

def test_environment():
    """Test environment setup."""
    print("\n🔍 Testing environment...")
    
    project_dir = Path(__file__).parent
    
    # Check .env file
    env_file = project_dir / ".env"
    if env_file.exists():
        print("  ✅ .env file exists")
    else:
        print("  ❌ .env file missing")
        return False
    
    # Check virtual environment
    venv_dir = project_dir / ".venv"
    if venv_dir.exists():
        print("  ✅ Virtual environment exists")
    else:
        print("  ❌ Virtual environment missing")
        return False
    
    # Check Claude config
    claude_config = project_dir / "claude_desktop_config.json"
    if claude_config.exists():
        print("  ✅ Claude Desktop config exists")
    else:
        print("  ❌ Claude Desktop config missing")
        return False
    
    return True

def main():
    """Run all tests."""
    print("🧪 MCTS MCP Server - Simple Test")
    print("=" * 40)
    
    print(f"Python version: {sys.version}")
    print(f"Platform: {sys.platform}")
    print()
    
    tests = [
        test_basic_imports,
        test_mcts_imports,
        test_environment
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        if test():
            passed += 1
    
    print("\n" + "=" * 40)
    print(f"📊 Results: {passed}/{total} tests passed")
    
    if passed == total:
        print("🎉 All tests passed! Installation looks good.")
        print("\nNext steps:")
        print("1. Add API keys to .env file")
        print("2. Configure Claude Desktop") 
        print("3. Test with Claude")
        return True
    else:
        print("❌ Some tests failed. Please check the installation.")
        print("\nTry running: python setup.py")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)

```

--------------------------------------------------------------------------------
/archive/test_fixed_server.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
Test the fixed MCTS server basic functionality
"""
import os
import sys

def test_basic_imports():
    """Test that basic Python functionality works."""
    try:
        import json
        import logging
        print("✓ Basic Python imports working")
        return True
    except Exception as e:
        print(f"✗ Basic import error: {e}")
        return False

def test_environment():
    """Test environment setup."""
    try:
        # Test path
        print(f"✓ Current directory: {os.getcwd()}")
        
        # Test API key
        api_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
        if api_key:
            print("✓ Gemini API key found")
        else:
            print("⚠ No Gemini API key found (set GEMINI_API_KEY)")
        
        return True
    except Exception as e:
        print(f"✗ Environment error: {e}")
        return False

def test_server_structure():
    """Test that server file exists and has basic structure."""
    try:
        server_path = "src/mcts_mcp_server/server.py"
        if os.path.exists(server_path):
            print(f"✓ Server file exists: {server_path}")
            
            # Check file has basic content
            with open(server_path, 'r') as f:
                content = f.read()
                if "FastMCP" in content and "def main" in content:
                    print("✓ Server file has expected structure")
                    return True
                else:
                    print("✗ Server file missing expected components")
                    return False
        else:
            print(f"✗ Server file not found: {server_path}")
            return False
    except Exception as e:
        print(f"✗ Server structure error: {e}")
        return False

def test_config():
    """Test MCP config file."""
    try:
        config_path = "example_mcp_config.json"
        if os.path.exists(config_path):
            print(f"✓ Example config exists: {config_path}")
            
            # Test JSON validity
            with open(config_path, 'r') as f:
                import json
                config = json.load(f)
                if "mcpServers" in config:
                    print("✓ Config has valid structure")
                    return True
                else:
                    print("✗ Config missing mcpServers")
                    return False
        else:
            print(f"✗ Config file not found: {config_path}")
            return False
    except Exception as e:
        print(f"✗ Config error: {e}")
        return False

def main():
    """Run all tests."""
    print("Testing Fixed MCTS MCP Server...")
    print("=" * 40)
    
    tests = [
        test_basic_imports,
        test_environment,
        test_server_structure,
        test_config
    ]
    
    passed = 0
    for test in tests:
        if test():
            passed += 1
        print()
    
    print("=" * 40)
    print(f"Tests passed: {passed}/{len(tests)}")
    
    if passed == len(tests):
        print("\n🎉 All tests passed!")
        print("\nNext steps:")
        print("1. Set GEMINI_API_KEY environment variable")
        print("2. Add example_mcp_config.json to Claude Desktop config")
        print("3. Restart Claude Desktop")
        print("4. Use the MCTS tools in Claude")
        return True
    else:
        print(f"\n❌ {len(tests) - passed} tests failed")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)

```

--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------

```toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "mcts-mcp-server"
version = "0.1.0"
description = "A Monte Carlo Tree Search MCP server with multiple LLM provider support."
authors = [
    { name = "angrysky56"},
]
requires-python = ">=3.10"
readme = "README.md"

dependencies = [
    # Core MCP and async support
    "mcp>=1.0.0",
    "httpx>=0.25.0,<1.0.0",
    # LLM Provider packages
    "google-genai>=1.20.0,<2.0.0",
    "openai>=1.0.0,<2.0.0",
    "anthropic>=0.54.0,<1.0.0",
    # Ollama support
    "ollama>=0.1.0,<1.0.0",
    # Core MCTS dependencies (required for import)
    "numpy>=1.24.0,<3.0.0",
    "scikit-learn>=1.3.0,<2.0.0",
    # Data handling and utilities
    "python-dotenv>=1.0.0,<2.0.0",
    "pydantic>=2.0.0,<3.0.0",
    "typing-extensions>=4.5.0",
    # Logging and monitoring
    "structlog>=23.0.0,<26.0.0",
    # Configuration and state management
    "pyyaml>=6.0,<7.0.0",
    "jsonschema>=4.17.0,<5.0.0",
    # CLI and display utilities
    "click>=8.1.0,<9.0.0",
    "rich>=13.0.0,<15.0.0",
    "psutil>=7.0.0",
]

[project.optional-dependencies]
dev = [
    # Code quality and formatting
    "ruff>=0.1.0",
    "black>=23.0.0",
    "isort>=5.12.0",
    "mypy>=1.5.0",

    # Testing
    "pytest>=7.4.0",
    "pytest-asyncio>=0.21.0",
    "pytest-mock>=3.11.0",
    "pytest-cov>=4.1.0",

    # Documentation
    "mkdocs>=1.5.0",
    "mkdocs-material>=9.0.0",

    # Development utilities
    "ipython>=8.0.0",
    "jupyter>=1.0.0",
]

# Optional extras for specific features
analysis = [
    "matplotlib>=3.7.0,<4.0.0",
    "seaborn>=0.12.0,<1.0.0",
    "plotly>=5.15.0,<6.0.0",
    "pandas>=2.0.0,<3.0.0",
]

algorithms = [
    "numpy>=1.24.0,<3.0.0",
    "scikit-learn>=1.3.0,<2.0.0",
    "scipy>=1.10.0,<2.0.0",
]

full = [
    "mcts-mcp-server[dev,analysis,algorithms]",
]

[project.scripts]
mcts-mcp-server = "mcts_mcp_server.server:cli_main"

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-dir]
"" = "src"

# Tool configurations
[tool.ruff]
target-version = "py310"
line-length = 88
select = [
    "E",    # pycodestyle errors
    "W",    # pycodestyle warnings
    "F",    # pyflakes
    "I",    # isort
    "N",    # pep8-naming
    "UP",   # pyupgrade
    "B",    # flake8-bugbear
    "C4",   # flake8-comprehensions
    "SIM",  # flake8-simplify
    "RUF",  # ruff-specific rules
]
ignore = [
    "E501",   # line too long (handled by formatter)
    "B008",   # do not perform function calls in argument defaults
]

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]
"tests/*" = ["S101", "PLR2004", "S106"]

[tool.black]
target-version = ['py310']
line-length = 88

[tool.mypy]
python_version = "3.10"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_decorators = true
no_implicit_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_unreachable = true
strict_equality = true

[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py", "*_test.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = [
    "--strict-markers",
    "--strict-config",
    "--cov=mcts_mcp_server",
    "--cov-report=term-missing",
    "--cov-report=html",
    "--cov-fail-under=80",
]
markers = [
    "slow: marks tests as slow (deselect with '-m \"not slow\"')",
    "integration: marks tests as integration tests",
    "unit: marks tests as unit tests",
]

```

--------------------------------------------------------------------------------
/archive/test_adapter.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test script for MCTS MCP Server LLM Adapter
===========================================

This script tests the LocalInferenceLLMAdapter which replaces the broken call_model approach.
"""
import os
import sys
import asyncio
import logging

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger("mcts_test")

# Add the project root to the Python path
project_root = os.path.dirname(os.path.abspath(__file__))
if project_root not in sys.path:
    sys.path.insert(0, project_root)

# Import just the adapter
sys.path.insert(0, os.path.join(project_root, "src"))
from src.mcts_mcp_server.llm_adapter import LocalInferenceLLMAdapter

async def test_llm_adapter():
    """Test the local inference adapter."""
    logger.info("Testing LocalInferenceLLMAdapter...")
    adapter = LocalInferenceLLMAdapter()

    # Test basic completion
    test_messages = [{"role": "user", "content": "Generate a thought about AI safety."}]
    result = await adapter.get_completion("default", test_messages)
    logger.info(f"Basic completion test result: {result}")

    # Test thought generation
    context = {
        "question_summary": "What are the implications of AI in healthcare?",
        "current_approach": "initial",
        "best_score": "0",
        "best_answer": "",
        "current_answer": "",
        "current_sequence": "1"
    }

    # Use a dictionary for config
    config = {
        "max_children": 10,
        "exploration_weight": 3.0,
        "max_iterations": 1,
        "simulations_per_iteration": 10,
        "debug_logging": False,
    }

    thought = await adapter.generate_thought(context, config)
    logger.info(f"Thought generation test result: {thought}")

    # Test evaluation
    context["answer_to_evaluate"] = "AI in healthcare presents both opportunities and challenges. While it can improve diagnosis accuracy, there are ethical concerns about privacy and decision-making."
    score = await adapter.evaluate_analysis(context["answer_to_evaluate"], context, config)
    logger.info(f"Evaluation test result (score 1-10): {score}")

    # Test tag generation
    tags = await adapter.generate_tags("AI in healthcare can revolutionize patient care through improved diagnostics and personalized treatment plans.", config)
    logger.info(f"Tag generation test result: {tags}")

    # Test streaming
    logger.info("Testing streaming completion...")
    stream_messages = [{"role": "user", "content": "This is a test of streaming."}]
    async for chunk in adapter.get_streaming_completion("default", stream_messages):
        logger.info(f"Received chunk: {chunk}")

    logger.info("All LLM adapter tests completed successfully!")
    return True

async def main():
    """Run tests for the MCTS MCP server components."""
    try:
        # Test the LLM adapter
        adapter_result = await test_llm_adapter()
        if adapter_result:
            logger.info("✅ LLM adapter tests passed")

        logger.info("\nThe MCTS MCP server should now work with Claude Desktop.")
        logger.info("To use it with Claude Desktop:")
        logger.info("1. Copy the claude_desktop_config.json file to your Claude Desktop config location")
        logger.info("2. Restart Claude Desktop")
        logger.info("3. Ask Claude to analyze a topic using MCTS")

    except Exception as e:
        logger.error(f"Test failed with error: {e}")
        return False

    return True

if __name__ == "__main__":
    asyncio.run(main())

```

--------------------------------------------------------------------------------
/archive/ANALYSIS_TOOLS.md:
--------------------------------------------------------------------------------

```markdown
# MCTS Analysis Tools

This extension adds powerful analysis tools to the MCTS-MCP Server, making it easy to extract insights and understand results from your MCTS runs.

## Overview

The MCTS Analysis Tools provide a suite of integrated functions to:

1. List and browse MCTS runs
2. Extract key concepts, arguments, and conclusions
3. Generate comprehensive reports
4. Compare results across different runs
5. Suggest improvements for better performance

## Installation

The tools are now integrated directly into the MCTS-MCP Server. No additional setup is required.

## Available Tools

### Browsing and Basic Information

- `list_mcts_runs(count=10, model=None)`: List recent MCTS runs with key metadata
- `get_mcts_run_details(run_id)`: Get detailed information about a specific run
- `get_mcts_solution(run_id)`: Get the best solution from a run

### Analysis and Insights

- `analyze_mcts_run(run_id)`: Perform a comprehensive analysis of a run
- `get_mcts_insights(run_id, max_insights=5)`: Extract key insights from a run
- `extract_mcts_conclusions(run_id)`: Extract conclusions from a run
- `suggest_mcts_improvements(run_id)`: Get suggestions for improvement

### Reporting and Comparison

- `get_mcts_report(run_id, format='markdown')`: Generate a comprehensive report (formats: 'markdown', 'text', 'html')
- `get_best_mcts_runs(count=5, min_score=7.0)`: Get the best runs based on score
- `compare_mcts_runs(run_ids)`: Compare multiple runs to identify similarities and differences

## Usage Examples

### Getting Started

To list your recent MCTS runs:

```python
list_mcts_runs()
```

To get details about a specific run:

```python
get_mcts_run_details('cogito:latest_1745979984')
```

### Extracting Insights

To get key insights from a run:

```python
get_mcts_insights(run_id='cogito:latest_1745979984')
```

### Generating Reports

To generate a comprehensive markdown report:

```python
get_mcts_report(run_id='cogito:latest_1745979984', format='markdown')
```

### Improving Results

To get suggestions for improving a run:

```python
suggest_mcts_improvements(run_id='cogito:latest_1745979984')
```

### Comparing Runs

To compare multiple runs:

```python
compare_mcts_runs(['cogito:latest_1745979984', 'qwen3:0.6b_1745979584'])
```

## Understanding the Results

The analysis tools extract several key elements from MCTS runs:

1. **Key Concepts**: The core ideas and frameworks in the analysis
2. **Arguments For/Against**: The primary arguments on both sides of a question
3. **Conclusions**: The synthesized conclusions or insights from the analysis
4. **Tags**: Automatically generated topic tags from the content

## Troubleshooting

If you encounter any issues with the analysis tools:

1. Check that your MCTS run completed successfully (status: "completed")
2. Verify that the run ID you're using exists and is correct
3. Try listing all runs to see what's available: `list_mcts_runs()`
4. Make sure the `.best_solution.txt` file exists in the run's directory

## Advanced Usage

### Customizing Reports

You can generate reports in different formats:

```python
# Generate a markdown report
report = get_mcts_report(run_id='cogito:latest_1745979984', format='markdown')

# Generate a text report
report = get_mcts_report(run_id='cogito:latest_1745979984', format='text')

# Generate an HTML report
report = get_mcts_report(run_id='cogito:latest_1745979984', format='html')
```

### Finding the Best Runs

To find your best-performing runs:

```python
best_runs = get_best_mcts_runs(count=3, min_score=8.0)
```

This returns the top 3 runs with a score of at least 8.0.

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/mcts_config.py:
--------------------------------------------------------------------------------

```python
"""
MCTS Configurations
===================

This module stores default configurations, taxonomies, and metadata for the MCTS package.
"""
from typing import Any

DEFAULT_CONFIG: dict[str, Any] = {
    "max_children": 6,               # Reduced from 10 to speed up processing
    "exploration_weight": 3.0,
    "max_iterations": 1,
    "simulations_per_iteration": 5,  # Reduced from 10 to speed up processing
    "surprise_threshold": 0.66,
    "use_semantic_distance": True,
    "relative_evaluation": False,
    "score_diversity_bonus": 0.7,
    "force_exploration_interval": 4,
    "debug_logging": False,
    "global_context_in_prompts": True,
    "track_explored_approaches": True,
    "sibling_awareness": True,
    "memory_cutoff": 20,             # Reduced from 50 to use less memory
    "early_stopping": True,
    "early_stopping_threshold": 8.0,  # Reduced from 10.0 to stop earlier with good results
    "early_stopping_stability": 1,    # Reduced from 2 to stop faster when a good result is found
    "surprise_semantic_weight": 0.4,
    "surprise_philosophical_shift_weight": 0.3,
    "surprise_novelty_weight": 0.3,
    "surprise_overall_threshold": 0.7,
    "use_bayesian_evaluation": True,
    "use_thompson_sampling": True,
    "beta_prior_alpha": 1.0,
    "beta_prior_beta": 1.0,
    "unfit_score_threshold": 5.0,
    "unfit_visit_threshold": 3,
    "enable_state_persistence": True,
}

APPROACH_TAXONOMY: dict[str, list[str]] = {
    "empirical": ["evidence", "data", "observation", "experiment"],
    "rational": ["logic", "reason", "deduction", "principle"],
    "phenomenological": ["experience", "perception", "consciousness"],
    "hermeneutic": ["interpret", "meaning", "context", "understanding"],
    "reductionist": ["reduce", "component", "fundamental", "elemental"],
    "holistic": ["whole", "system", "emergent", "interconnected"],
    "materialist": ["physical", "concrete", "mechanism"],
    "idealist": ["concept", "ideal", "abstract", "mental"],
    "analytical": ["analyze", "dissect", "examine", "scrutinize"],
    "synthetic": ["synthesize", "integrate", "combine", "unify"],
    "dialectical": ["thesis", "antithesis", "contradiction"],
    "comparative": ["compare", "contrast", "analogy"],
    "critical": ["critique", "challenge", "question", "flaw"],
    "constructive": ["build", "develop", "formulate"],
    "pragmatic": ["practical", "useful", "effective"],
    "normative": ["should", "ought", "value", "ethical"],
    "structural": ["structure", "organize", "framework"],
    "alternative": ["alternative", "different", "another way"],
    "complementary": ["missing", "supplement", "add"],
    "variant": [],
    "initial": [],
}

APPROACH_METADATA: dict[str, dict[str, str]] = {
    "empirical": {"family": "epistemology"},
    "rational": {"family": "epistemology"},
    "phenomenological": {"family": "epistemology"},
    "hermeneutic": {"family": "epistemology"},
    "reductionist": {"family": "ontology"},
    "holistic": {"family": "ontology"},
    "materialist": {"family": "ontology"},
    "idealist": {"family": "ontology"},
    "analytical": {"family": "methodology"},
    "synthetic": {"family": "methodology"},
    "dialectical": {"family": "methodology"},
    "comparative": {"family": "methodology"},
    "critical": {"family": "perspective"},
    "constructive": {"family": "perspective"},
    "pragmatic": {"family": "perspective"},
    "normative": {"family": "perspective"},
    "structural": {"family": "general"},
    "alternative": {"family": "general"},
    "complementary": {"family": "general"},
    "variant": {"family": "general"},
    "initial": {"family": "general"},
}

# State format version for serialization compatibility
STATE_FORMAT_VERSION = "0.8.0"

```

--------------------------------------------------------------------------------
/archive/test_mcp_init.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
Quick MCP Server Test
====================

Test if the MCTS MCP server responds to initialization quickly.
"""

import asyncio
import json
import subprocess
import sys
import time
from pathlib import Path

async def test_mcp_server():
    """Test if the MCP server responds to initialize quickly."""
    
    project_dir = Path(__file__).parent
    
    print("🧪 Testing MCP server initialization speed...")
    
    # Start the server
    server_cmd = [
        "uv", "run", "python", "-m", "mcts_mcp_server.server"
    ]
    
    try:
        # Start server process
        server_process = await asyncio.create_subprocess_exec(
            *server_cmd,
            cwd=project_dir,
            stdin=asyncio.subprocess.PIPE,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        
        print("📡 Server started, testing initialization...")
        
        # Send MCP initialize message
        init_message = {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2024-11-05",
                "capabilities": {},
                "clientInfo": {
                    "name": "test-client",
                    "version": "1.0.0"
                }
            }
        }
        
        message_json = json.dumps(init_message) + '\n'
        
        # Record start time
        start_time = time.time()
        
        # Send initialize message
        server_process.stdin.write(message_json.encode())
        await server_process.stdin.drain()
        
        # Try to read response with timeout
        try:
            response_data = await asyncio.wait_for(
                server_process.stdout.readline(),
                timeout=10.0  # 10 second timeout
            )
            
            elapsed = time.time() - start_time
            
            if response_data:
                response_text = response_data.decode().strip()
                print(f"✅ Server responded in {elapsed:.2f} seconds")
                print(f"📋 Response: {response_text[:100]}...")
                
                if elapsed < 5.0:
                    print("🎉 SUCCESS: Server responds quickly!")
                    return True
                else:
                    print("⚠️  Server responds but slowly")
                    return False
            else:
                print("❌ No response received")
                return False
                
        except asyncio.TimeoutError:
            elapsed = time.time() - start_time
            print(f"❌ TIMEOUT: No response after {elapsed:.2f} seconds")
            return False
            
    except Exception as e:
        print(f"❌ Test failed: {e}")
        return False
        
    finally:
        # Clean up server process
        try:
            server_process.terminate()
            await asyncio.wait_for(server_process.wait(), timeout=5.0)
        except:
            try:
                server_process.kill()
                await server_process.wait()
            except:
                pass

def main():
    """Run the test."""
    print("🧪 MCTS MCP Server Initialization Test")
    print("=" * 45)
    
    try:
        result = asyncio.run(test_mcp_server())
        
        if result:
            print("\n🎉 Test PASSED: Server initialization is fast enough")
            return True
        else:
            print("\n❌ Test FAILED: Server initialization is too slow")
            return False
            
    except Exception as e:
        print(f"\n💥 Test error: {e}")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)

```

--------------------------------------------------------------------------------
/USAGE_GUIDE.md:
--------------------------------------------------------------------------------

```markdown
# MCTS MCP Server Usage Guide

This guide explains how to effectively use the MCTS MCP Server with Claude for deep, explorative analysis.

## Setup

1. Run the setup script to prepare the environment:
   ```bash
   ./setup.sh
   ```
   
   The setup script will:
   - Install UV (Astral UV) if not already installed
   - Create a virtual environment using UV
   - Install dependencies with UV
   - Create necessary state directory

2. Add the MCP server configuration to Claude Desktop:
   - Copy the content from `claude_desktop_config.json`
   - Add it to your Claude Desktop configuration file (typically at `~/.claude/claude_desktop_config.json`)
   - Update paths in the configuration if necessary
   - Restart Claude Desktop

## Using the MCTS Analysis with Claude

Once the MCP server is configured, Claude can leverage MCTS for deep analysis of topics. Here are some example conversation patterns:

### Starting a New Analysis

Simply provide a question, topic, or text that you want Claude to analyze deeply:

```
Analyze the ethical implications of artificial general intelligence.
```

Claude will:
1. Initialize the MCTS system
2. Generate an initial analysis
3. Run MCTS iterations to explore different perspectives
4. Find the best analysis and generate a synthesis
5. Present the results

### Continuing an Analysis

To build upon a previous analysis in the same chat session:

```
Continue exploring the technological feasibility aspects.
```

Claude will:
1. Load the state from the previous analysis
2. Start a new MCTS run that builds upon the previous knowledge
3. Leverage learned approach preferences and avoid unfit areas
4. Present an updated analysis

### Asking About the Last Run

To get information about the previous analysis:

```
What was the best score and key insights from your last analysis run?
```

Claude will summarize the results of the previous MCTS run, including the best score, approach preferences, and analysis tags.

### Asking About the Process

To learn more about how the MCTS analysis works:

```
How does your MCTS analysis process work?
```

Claude will explain the MCTS algorithm and how it's used for analysis.

### Viewing/Changing Configuration

To see or modify the MCTS configuration:

```
Show me the current MCTS configuration.
```

Or:

```
Can you update the MCTS configuration to use 3 iterations and 8 simulations per iteration?
```

## Understanding MCTS Analysis Output

The MCTS analysis output typically includes:

1. **Initial Analysis**: The starting point of the exploration
2. **Best Analysis Found**: The highest-scored analysis discovered through MCTS
3. **Analysis Tags**: Key concepts identified in the analysis
4. **Final Synthesis**: A conclusive statement that integrates the key insights

## Advanced Usage

### Adjusting Parameters

You can ask Claude to modify parameters for deeper or more focused analysis:

- Increase `max_iterations` for more thorough exploration
- Increase `simulations_per_iteration` for more simulations per iteration
- Adjust `exploration_weight` to balance exploration vs. exploitation
- Set `early_stopping` to false to ensure all iterations complete

### Using Different Approaches

You can guide Claude to explore specific philosophical approaches:

```
Continue the analysis using a more empirical approach.
```

Or:

```
Can you explore this topic from a more critical perspective?
```

## Development Notes

If you want to run or test the server directly during development:

```bash
# Activate the virtual environment
source .venv/bin/activate

# Run the server directly 
uv run server.py

# Or use the MCP CLI tools
uv run -m mcp dev server.py
```

## Troubleshooting

- If Claude doesn't recognize the MCTS server, check that Claude Desktop is correctly configured and restarted
- If analysis seems shallow, ask for more iterations or simulations
- If Claude says it can't continue an analysis, it might mean no state was saved from a previous run
- If you encounter dependency issues, try `uv pip sync requirements.txt` to ensure exact package versions

```

--------------------------------------------------------------------------------
/archive/GEMINI_SETUP.md:
--------------------------------------------------------------------------------

```markdown
# Google Gemini Setup Guide

This guide will help you set up the Google Gemini adapter properly with the new `google-genai` library.

## Prerequisites

✅ **Already Done**: You have `google-genai>=1.20.0` installed via your `pyproject.toml`

## 1. Get Your API Key

1. Go to [Google AI Studio](https://aistudio.google.com/app/apikey)
2. Sign in with your Google account
3. Click "Create API Key"
4. Copy the generated API key

## 2. Set Up Environment Variable

Add your API key to your environment. You can use either name:

```bash
# Option 1: Using GEMINI_API_KEY
export GEMINI_API_KEY="your-api-key-here"

# Option 2: Using GOOGLE_API_KEY (also supported)
export GOOGLE_API_KEY="your-api-key-here"
```

Or create a `.env` file in your project root:

```env
GEMINI_API_KEY=your-api-key-here
```

## 3. Test Your Setup

Run the test script to verify everything is working:

```bash
uv run python test_gemini_setup.py
```

## 4. Usage Examples

### Basic Usage

```python
import asyncio
from mcts_mcp_server.gemini_adapter import GeminiAdapter

async def main():
    # Initialize the adapter
    adapter = GeminiAdapter()

    # Simple completion
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the capital of France?"}
    ]

    response = await adapter.get_completion(model=None, messages=messages)
    print(response)

asyncio.run(main())
```

### With Rate Limiting

```python
# Rate limiting is enabled by default for free tier models
adapter = GeminiAdapter(enable_rate_limiting=True)

# Check rate limit status
status = adapter.get_rate_limit_status()
print(f"Requests remaining: {status['requests_remaining']}")
```

### Streaming Responses

```python
async def stream_example():
    adapter = GeminiAdapter()

    messages = [{"role": "user", "content": "Write a short story about a robot."}]

    async for chunk in adapter.get_streaming_completion(model=None, messages=messages):
        print(chunk, end='', flush=True)

asyncio.run(stream_example())
```

### Using Different Models

```python
# Use a specific model
response = await adapter.get_completion(
    model="gemini-1.5-pro",  # More capable but slower
    messages=messages
)

# Available models:
# - gemini-1.5-flash-latest (default, fast)
# - gemini-1.5-pro (more capable)
# - gemini-2.0-flash-exp (experimental)
# - gemini-2.5-flash-preview-05-20 (preview)
```

## 5. Key Changes from google-generativeai

The new `google-genai` library has a different API:

### Old (google-generativeai)
```python
import google.generativeai as genai
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content(messages)
```

### New (google-genai)
```python
from google import genai
client = genai.Client(api_key=api_key)
response = await client.aio.models.generate_content(
    model="gemini-1.5-flash",
    contents=messages
)
```

## 6. Rate Limits

The adapter includes built-in rate limiting for free tier usage:

- **gemini-1.5-flash**: 15 requests/minute
- **gemini-1.5-pro**: 360 requests/minute
- **gemini-2.0-flash-exp**: 10 requests/minute
- **gemini-2.5-flash-preview**: 10 requests/minute

## 7. Troubleshooting

### Common Issues

1. **"API key not provided"**
   - Make sure `GEMINI_API_KEY` or `GOOGLE_API_KEY` is set
   - Check the environment variable is exported correctly

2. **Rate limit errors**
   - Enable rate limiting: `GeminiAdapter(enable_rate_limiting=True)`
   - Check your quota at [Google AI Studio](https://aistudio.google.com/quota)

3. **Import errors**
   - Make sure you're using `google-genai` not `google-generativeai`
   - Check version: `uv run python -c "import google.genai; print(google.genai.__version__)"`

### Getting Help

- [Google AI Studio Documentation](https://ai.google.dev/gemini-api/docs)
- [google-genai GitHub](https://github.com/googleapis/python-aiplatform)
- Check the test script output for detailed error messages

## 8. Next Steps

Once your setup is working:

1. Test with your MCP server
2. Experiment with different models
3. Adjust rate limits if needed
4. Integrate with your MCTS system

Your Gemini adapter is now ready to use with the latest API! 🚀

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/ollama_check.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Ollama Model Check
===============

Simple script to test Ollama model detection.
"""
import sys
import logging
import subprocess
import json

# Set up logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger("ollama_check")

def check_models_subprocess():
    """Check available models using subprocess to call 'ollama list'."""
    try:
        # Run 'ollama list' and capture output
        result = subprocess.run(['ollama', 'list'], capture_output=True, text=True, check=True)
        logger.info(f"Subprocess output: {result.stdout}")
        
        # Process the output
        lines = result.stdout.strip().split('\n')
        if len(lines) <= 1:  # Just the header line
            logger.info("No models found in subprocess output")
            return []
        
        # Skip the header line if present
        if "NAME" in lines[0] and "ID" in lines[0]:
            lines = lines[1:]
        
        # Extract model names
        models = []
        for line in lines:
            if not line.strip():
                continue
            parts = line.split()
            if parts:
                model_name = parts[0]
                if ':' not in model_name:
                    model_name += ':latest'
                models.append(model_name)
        
        logger.info(f"Models found via subprocess: {models}")
        return models
    except subprocess.CalledProcessError as e:
        logger.error(f"Error calling 'ollama list': {e}")
        return []
    except Exception as e:
        logger.error(f"Unexpected error in subprocess check: {e}")
        return []

def check_models_httpx():
    """Check available models using direct HTTP API call."""
    try:
        import httpx
        client = httpx.Client(base_url="http://localhost:11434", timeout=5.0)
        response = client.get("/api/tags")
        
        logger.info(f"HTTPX status code: {response.status_code}")
        if response.status_code == 200:
            data = response.json()
            models = data.get("models", [])
            model_names = [m.get("name") for m in models if m.get("name")]
            logger.info(f"Models found via HTTP API: {model_names}")
            return model_names
        else:
            logger.warning(f"Failed to get models via HTTP: {response.status_code}")
            return []
    except Exception as e:
        logger.error(f"Error checking models via HTTP: {e}")
        return []

def check_models_ollama_package():
    """Check available models using ollama Python package."""
    try:
        import ollama
        models_data = ollama.list()
        logger.info(f"Ollama package response: {models_data}")
        
        if isinstance(models_data, dict) and "models" in models_data:
            model_names = [m.get("name") for m in models_data["models"] if m.get("name")]
            logger.info(f"Models found via ollama package: {model_names}")
            return model_names
        else:
            logger.warning("Unexpected response format from ollama package")
            return []
    except Exception as e:
        logger.error(f"Error checking models via ollama package: {e}")
        return []

def main():
    """Test all methods of getting Ollama models."""
    logger.info("Testing Ollama model detection")
    
    logger.info("--- Method 1: Subprocess ---")
    subprocess_models = check_models_subprocess()
    
    logger.info("--- Method 2: HTTP API ---")
    httpx_models = check_models_httpx()
    
    logger.info("--- Method 3: Ollama Package ---")
    package_models = check_models_ollama_package()
    
    # Combine all results
    all_models = list(set(subprocess_models + httpx_models + package_models))
    
    logger.info(f"Combined unique models: {all_models}")
    
    # Output JSON result for easier parsing
    result = {
        "subprocess_models": subprocess_models,
        "httpx_models": httpx_models, 
        "package_models": package_models,
        "all_models": all_models
    }
    
    print(json.dumps(result, indent=2))
    return 0

if __name__ == "__main__":
    sys.exit(main())

```

--------------------------------------------------------------------------------
/archive/test_rate_limiting.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test Gemini Rate Limiting
=========================

This script tests the rate limiting functionality for the Gemini adapter.
"""
import asyncio
import logging
import time
import sys
import os

# Add the project directory to Python path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.mcts_mcp_server.rate_limiter import RateLimitConfig, TokenBucketRateLimiter, ModelRateLimitManager

async def test_rate_limiter_basic():
    """Test basic rate limiter functionality."""
    print("=== Testing Basic Rate Limiter ===")
    
    # Create a fast rate limiter for testing (6 RPM = 1 request per 10 seconds)
    config = RateLimitConfig(requests_per_minute=6, burst_allowance=2)
    limiter = TokenBucketRateLimiter(config)
    
    print(f"Initial status: {limiter.get_status()}")
    
    # Make burst requests (should be fast)
    print("Making burst requests...")
    for i in range(2):
        start = time.time()
        await limiter.acquire()
        elapsed = time.time() - start
        print(f"  Request {i+1}: {elapsed:.3f}s")
    
    # This should be rate limited
    print("Making rate-limited request...")
    start = time.time()
    await limiter.acquire()
    elapsed = time.time() - start
    print(f"  Rate limited request: {elapsed:.3f}s (should be ~10s)")
    
    print(f"Final status: {limiter.get_status()}")
    print()

async def test_gemini_rate_limits():
    """Test Gemini-specific rate limits."""
    print("=== Testing Gemini Rate Limits ===")
    
    manager = ModelRateLimitManager()
    
    # Test the specific models
    test_models = [
        "gemini-2.5-flash-preview-05-20",
        "gemini-1.5-flash-latest", 
        "gemini-1.5-pro",
        "unknown-model"
    ]
    
    for model in test_models:
        limiter = manager.get_limiter(model)
        status = limiter.get_status()
        print(f"{model}:")
        print(f"  Rate: {status['rate_per_minute']:.0f} RPM")
        print(f"  Burst: {status['max_tokens']:.0f}")
        print(f"  Available: {status['available_tokens']:.2f}")
        print()

async def test_concurrent_requests():
    """Test how rate limiting handles concurrent requests."""
    print("=== Testing Concurrent Requests ===")
    
    # Create a restrictive rate limiter (3 RPM = 1 request per 20 seconds)
    config = RateLimitConfig(requests_per_minute=3, burst_allowance=1)
    limiter = TokenBucketRateLimiter(config)
    
    async def make_request(request_id):
        start = time.time()
        await limiter.acquire()
        elapsed = time.time() - start
        print(f"Request {request_id}: waited {elapsed:.3f}s")
        return elapsed
    
    # Launch multiple concurrent requests
    print("Launching 3 concurrent requests...")
    start_time = time.time()
    
    tasks = [make_request(i) for i in range(3)]
    results = await asyncio.gather(*tasks)
    
    total_time = time.time() - start_time
    print(f"Total time for 3 requests: {total_time:.3f}s")
    print(f"Average wait per request: {sum(results)/len(results):.3f}s")
    print()

async def test_model_pattern_matching():
    """Test model pattern matching for rate limits."""
    print("=== Testing Model Pattern Matching ===")
    
    manager = ModelRateLimitManager()
    
    # Test various model names and see what rate limits they get
    test_models = [
        "gemini-2.5-flash-preview-05-20",  # Should match "gemini-2.5-flash-preview"
        "gemini-2.5-flash-preview-06-01",  # Should also match pattern
        "gemini-1.5-flash-8b-001",         # Should match "gemini-1.5-flash-8b"
        "gemini-1.5-flash-latest",         # Should match "gemini-1.5-flash"
        "gemini-1.5-pro-latest",           # Should match "gemini-1.5-pro"
        "gpt-4",                           # Should get default
        "claude-3-opus",                   # Should get default
    ]
    
    for model in test_models:
        limiter = manager.get_limiter(model)
        status = limiter.get_status()
        print(f"{model}: {status['rate_per_minute']:.0f} RPM, {status['max_tokens']:.0f} burst")

async def main():
    """Run all tests."""
    logging.basicConfig(level=logging.INFO)
    
    print("Testing Gemini Rate Limiting System")
    print("=" * 50)
    print()
    
    await test_rate_limiter_basic()
    await test_gemini_rate_limits()
    await test_model_pattern_matching()
    await test_concurrent_requests()
    
    print("All tests completed!")

if __name__ == "__main__":
    asyncio.run(main())

```

--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
MCTS MCP Server Setup Script
============================

Simple setup script using uv for the MCTS MCP Server.
"""
# ruff: noqa: T201
# Setup scripts legitimately need print statements for user feedback

import json
import platform
import shutil
import subprocess
import sys
from pathlib import Path


def run_command(cmd: list[str], cwd: Path | None = None) -> subprocess.CompletedProcess[str]:
    """Run a command and return the result."""
    try:
        # Using shell=False and list of strings for security
        return subprocess.run(
            cmd,
            cwd=cwd,
            capture_output=True,
            text=True,
            check=True,
            shell=False
        )
    except subprocess.CalledProcessError as e:
        sys.stderr.write(f"❌ Command failed: {' '.join(cmd)}\n")
        if e.stderr:
            sys.stderr.write(f"   Error: {e.stderr}\n")
        raise

def check_uv() -> bool:
    """Check if uv is installed."""
    return shutil.which("uv") is not None

def setup_project() -> None:
    """Set up the project using uv."""
    project_dir = Path(__file__).parent.resolve()

    print("🔧 Setting up MCTS MCP Server...")
    print(f"📁 Project directory: {project_dir}")

    if not check_uv():
        print("❌ uv not found. Please install uv first:")
        print("   curl -LsSf https://astral.sh/uv/install.sh | sh")
        print("   Or visit: https://docs.astral.sh/uv/getting-started/installation/")
        sys.exit(1)

    print("✅ Found uv")

    # Sync project dependencies (creates venv and installs everything)
    print("📦 Installing dependencies...")
    run_command(["uv", "sync"], cwd=project_dir)
    print("✅ Dependencies installed")

    # Create .env file if it doesn't exist
    env_file = project_dir / ".env"
    if not env_file.exists():
        print("📝 Creating .env file...")
        env_content = """# MCTS MCP Server Environment Configuration

# OpenAI API Key
OPENAI_API_KEY="your_openai_api_key_here"

# Anthropic API Key
ANTHROPIC_API_KEY="your_anthropic_api_key_here"

# Google Gemini API Key
GEMINI_API_KEY="your_gemini_api_key_here"

# Default LLM Provider ("ollama", "openai", "anthropic", "gemini")
DEFAULT_LLM_PROVIDER="ollama"

# Default Model Name
DEFAULT_MODEL_NAME="qwen3:latest"
"""
        env_file.write_text(env_content)
        print("✅ .env file created")
    else:
        print("✅ .env file already exists")

    # Create Claude Desktop config
    print("🔧 Generating Claude Desktop config...")
    claude_config = {
        "mcpServers": {
            "mcts-mcp-server": {
                "command": "uv",
                "args": [
                    "--directory", str(project_dir),
                    "run", "mcts-mcp-server"
                ]
            }
        }
    }

    config_file = project_dir / "claude_desktop_config.json"
    with config_file.open("w") as f:
        json.dump(claude_config, f, indent=2)
    print("✅ Claude Desktop config generated")

    # Test installation
    print("🧪 Testing installation...")
    try:
        run_command(["uv", "run", "python", "-c",
                    "import mcts_mcp_server; print('✅ Package imported successfully')"],
                   cwd=project_dir)
    except subprocess.CalledProcessError:
        print("❌ Installation test failed")
        sys.exit(1)

    print_success_message(project_dir)

def print_success_message(project_dir: Path) -> None:
    """Print setup completion message."""
    print("\n" + "="*60)
    print("🎉 Setup Complete!")
    print("="*60)

    print("\n📋 Next Steps:")
    print(f"1. Edit {project_dir / '.env'} and add your API keys")
    print("2. Add the Claude Desktop config:")

    if platform.system() == "Windows":
        config_path = "%APPDATA%\\Claude\\claude_desktop_config.json"
    elif platform.system() == "Darwin":
        config_path = "~/Library/Application Support/Claude/claude_desktop_config.json"
    else:
        config_path = "~/.config/claude/claude_desktop_config.json"

    print(f"   Copy contents of claude_desktop_config.json to: {config_path}")
    print("3. Restart Claude Desktop")
    print("4. Test with: uv run mcts-mcp-server")

    print("\n📚 Documentation:")
    print("• README.md - Project overview")
    print("• USAGE_GUIDE.md - Detailed usage instructions")

def main() -> None:
    """Main setup function."""
    try:
        setup_project()
    except KeyboardInterrupt:
        print("\n❌ Setup interrupted by user")
        sys.exit(1)
    except Exception as e:
        print(f"❌ Setup failed: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/manage_server.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
MCTS Server Manager
===================

This script provides utilities to start, stop, and check the status of
the MCTS MCP server to ensure only one instance is running at a time.
"""
import argparse
import os
import signal
import subprocess
import time

import psutil


def find_server_process():
    """Find the running MCTS server process if it exists."""
    current_pid = os.getpid()  # Get this script's PID to avoid self-identification

    for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
        try:
            # Skip this process
            if proc.pid == current_pid:
                continue

            cmdline = proc.info.get('cmdline', [])
            cmdline_str = ' '.join(cmdline) if cmdline else ''

            # Check for server.py but not manage_server.py
            if ('server.py' in cmdline_str and
                'python' in cmdline_str and
                'manage_server.py' not in cmdline_str):
                return proc
        except (psutil.NoSuchProcess, psutil.AccessDenied):
            pass
    return None

def start_server():
    """Start the MCTS server if it's not already running."""
    proc = find_server_process()
    if proc:
        print(f"MCTS server is already running with PID {proc.pid}")
        return False

    # Get the directory of this script
    script_dir = os.path.dirname(os.path.abspath(__file__))

    # Start the server using subprocess
    try:
        # Use nohup to keep the server running after this script exits
        cmd = f"cd {script_dir} && python -u server.py > {script_dir}/server.log 2>&1"
        subprocess.Popen(cmd, shell=True, start_new_session=True)
        print("MCTS server started successfully")

        # Wait a moment to verify it started
        time.sleep(2)
        proc = find_server_process()
        if proc:
            print(f"Server process running with PID {proc.pid}")
            return True
        else:
            print("Server process not found after startup. Check server.log for errors.")
            return False
    except Exception as e:
        print(f"Error starting server: {e}")
        return False

def stop_server():
    """Stop the MCTS server if it's running."""
    proc = find_server_process()
    if not proc:
        print("MCTS server is not running")
        return True

    try:
        # Try to terminate gracefully first
        proc.send_signal(signal.SIGTERM)
        print(f"Sent SIGTERM to process {proc.pid}")

        # Wait up to 5 seconds for process to terminate
        for _ in range(5):
            if not psutil.pid_exists(proc.pid):
                print("Server stopped successfully")
                return True
            time.sleep(1)

        # If still running, force kill
        if psutil.pid_exists(proc.pid):
            proc.send_signal(signal.SIGKILL)
            print(f"Force killed process {proc.pid}")
            time.sleep(1)

        if not psutil.pid_exists(proc.pid):
            print("Server stopped successfully")
            return True
        else:
            print("Failed to stop server")
            return False
    except Exception as e:
        print(f"Error stopping server: {e}")
        return False

def check_status():
    """Check the status of the MCTS server."""
    proc = find_server_process()
    if proc:
        print(f"MCTS server is running with PID {proc.pid}")
        # Get the uptime
        try:
            create_time = proc.create_time()
            uptime = time.time() - create_time
            hours, remainder = divmod(uptime, 3600)
            minutes, seconds = divmod(remainder, 60)
            print(f"Server uptime: {int(hours)}h {int(minutes)}m {int(seconds)}s")
        except (psutil.NoSuchProcess, psutil.AccessDenied):
            print("Unable to determine server uptime")
        return True
    else:
        print("MCTS server is not running")
        return False

def restart_server():
    """Restart the MCTS server."""
    stop_server()
    # Wait a moment to ensure resources are released
    time.sleep(2)
    return start_server()

def main():
    """Parse arguments and execute the appropriate command."""
    parser = argparse.ArgumentParser(description="Manage the MCTS server")
    parser.add_argument('command', choices=['start', 'stop', 'restart', 'status'],
                       help='Command to execute')

    args = parser.parse_args()

    if args.command == 'start':
        start_server()
    elif args.command == 'stop':
        stop_server()
    elif args.command == 'restart':
        restart_server()
    elif args.command == 'status':
        check_status()

if __name__ == "__main__":
    main()

```

--------------------------------------------------------------------------------
/archive/SETUP_SUMMARY.md:
--------------------------------------------------------------------------------

```markdown
# Setup Summary for MCTS MCP Server

## 🎯 What We've Created

We've built a comprehensive, OS-agnostic setup system for the MCTS MCP Server that works on **Windows, macOS, and Linux**. Here's what's now available:

## 📁 Setup Files Created

### **Core Setup Scripts**
1. **`setup.py`** - Main cross-platform Python setup script
2. **`setup.sh`** - Enhanced Unix/Linux/macOS shell script  
3. **`setup_unix.sh`** - Alternative Unix-specific script
4. **`setup_windows.bat`** - Windows batch file

### **Verification & Testing**
1. **`verify_installation.py`** - Comprehensive installation verification
2. **`test_simple.py`** - Quick basic functionality test

### **Documentation**
1. **`README.md`** - Updated with complete OS-agnostic instructions
2. **`QUICK_START.md`** - Simple getting-started guide

## 🚀 Key Improvements Made

### **Fixed Critical Issues**
- ✅ **Threading Bug**: Fixed `Event.wait()` timeout issue in tools.py
- ✅ **Missing Package**: Ensured google-genai package is properly installed
- ✅ **Environment Setup**: Automated .env file creation
- ✅ **Cross-Platform**: Works on Windows, macOS, and Linux

### **Enhanced Setup Process**
- 🔧 **Automatic UV Installation**: Detects and installs UV package manager
- 🔧 **Virtual Environment**: Creates and configures .venv automatically  
- 🔧 **Dependency Management**: Installs all required packages including google-genai
- 🔧 **Configuration Generation**: Creates Claude Desktop config automatically
- 🔧 **Verification**: Checks installation works properly

### **User Experience**
- 📝 **Clear Instructions**: Step-by-step guides for all platforms
- 📝 **Error Handling**: Helpful error messages and troubleshooting
- 📝 **API Key Setup**: Guided configuration of LLM providers
- 📝 **Testing Tools**: Multiple ways to verify installation

## 🎯 How Users Should Set Up

### **Simple Method (Recommended)**
```bash
git clone https://github.com/angrysky56/mcts-mcp-server.git
cd mcts-mcp-server
python setup.py
```

### **Platform-Specific**
- **Unix/Linux/macOS**: `./setup.sh`
- **Windows**: `setup_windows.bat`

### **Verification**
```bash
python verify_installation.py  # Comprehensive checks
python test_simple.py          # Quick test
```

## 🔧 What the Setup Does

1. **Environment Check**
   - Verifies Python 3.10+ is installed
   - Checks system compatibility

2. **Package Manager Setup**
   - Installs UV if not present
   - Uses UV for fast, reliable dependency management

3. **Virtual Environment**
   - Creates `.venv` directory
   - Isolates project dependencies

4. **Dependency Installation**
   - Installs all packages from pyproject.toml
   - Ensures google-genai>=1.20.0 is available
   - Installs development dependencies (optional)

5. **Configuration**
   - Creates `.env` file from template
   - Generates Claude Desktop configuration
   - Creates state directories

6. **Verification**
   - Tests basic imports
   - Verifies MCTS functionality
   - Checks file structure

## 🎉 Benefits for Users

### **Reliability**
- **Cross-Platform**: Works consistently across operating systems
- **Error Handling**: Clear error messages and solutions
- **Verification**: Multiple layers of testing

### **Ease of Use**
- **One Command**: Simple setup process
- **Guided Configuration**: Clear API key setup
- **Documentation**: Comprehensive guides and examples

### **Maintainability**
- **Modular Design**: Separate scripts for different purposes
- **Version Management**: UV handles dependency versions
- **State Management**: Proper virtual environment isolation

## 🔄 Testing Status

The MCTS MCP Server with Gemini integration has been successfully tested:

- ✅ **Initialization**: MCTS system starts properly with Gemini
- ✅ **API Connection**: Connects to Gemini API successfully  
- ✅ **MCTS Execution**: Runs iterations and simulations correctly
- ✅ **Results Generation**: Produces synthesis and analysis
- ✅ **State Persistence**: Saves and loads state properly

## 📋 Next Steps for Users

1. **Clone Repository**: Get the latest code with all setup improvements
2. **Run Setup**: Use any of the setup scripts  
3. **Configure API Keys**: Add keys to .env file
4. **Set Up Claude Desktop**: Add configuration and restart
5. **Test**: Verify everything works with test scripts
6. **Use**: Start analyzing with Claude and MCTS!

## 🆘 Support Resources

- **Quick Start**: `QUICK_START.md` for immediate setup
- **Full Documentation**: `README.md` for comprehensive information  
- **Usage Guide**: `USAGE_GUIDE.md` for detailed examples
- **Troubleshooting**: Built into setup scripts and documentation

The setup system is now robust, user-friendly, and works reliably across all major operating systems! 🎉

```

--------------------------------------------------------------------------------
/archive/gemini_adapter.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Google Gemini LLM Adapter - Fixed Version
========================================

Simple Gemini adapter using google-generativeai package.
"""
import logging
import os
import asyncio
from typing import List, Dict, Optional

try:
    import google.generativeai as genai
except ImportError:
    genai = None

from .base_llm_adapter import BaseLLMAdapter

class GeminiAdapter(BaseLLMAdapter):
    """
    Simple LLM Adapter for Google Gemini models.
    """
    DEFAULT_MODEL = "gemini-2.0-flash-lite"

    def __init__(self, api_key: Optional[str] = None, model_name: Optional[str] = None, **kwargs):
        super().__init__(api_key=api_key, **kwargs)

        if genai is None:
            raise ImportError("google-generativeai package not installed. Install with: pip install google-generativeai")

        self.api_key = api_key or os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
        if not self.api_key:
            raise ValueError("Gemini API key not provided. Set GEMINI_API_KEY or GOOGLE_API_KEY environment variable.")

        # Configure the API
        genai.configure(api_key=self.api_key)
        
        self.model_name = model_name or self.DEFAULT_MODEL
        self.logger = logging.getLogger(__name__)
        
        self.logger.info(f"Initialized GeminiAdapter with model: {self.model_name}")

    def _convert_messages_to_gemini_format(self, messages: List[Dict[str, str]]) -> tuple[Optional[str], List[Dict]]:
        """Convert messages to Gemini format."""
        system_instruction = None
        gemini_messages = []
        
        for message in messages:
            role = message.get("role")
            content = message.get("content", "")
            
            if role == "system":
                system_instruction = content
            elif role == "user":
                gemini_messages.append({"role": "user", "parts": [content]})
            elif role == "assistant":
                gemini_messages.append({"role": "model", "parts": [content]})
        
        return system_instruction, gemini_messages

    async def get_completion(self, model: Optional[str], messages: List[Dict[str, str]], **kwargs) -> str:
        """Get completion from Gemini."""
        try:
            target_model = model or self.model_name
            system_instruction, gemini_messages = self._convert_messages_to_gemini_format(messages)
            
            # Create the model
            model_obj = genai.GenerativeModel(
                model_name=target_model,
                system_instruction=system_instruction
            )
            
            # Convert messages to conversation format
            if gemini_messages:
                # For multi-turn conversation
                chat = model_obj.start_chat(history=gemini_messages[:-1])
                last_message = gemini_messages[-1]["parts"][0]
                
                # Run in thread to avoid blocking
                response = await asyncio.to_thread(chat.send_message, last_message)
            else:
                # Single message
                response = await asyncio.to_thread(
                    model_obj.generate_content, 
                    messages[-1]["content"] if messages else "Hello"
                )
            
            return response.text if response.text else "No response generated."
            
        except Exception as e:
            self.logger.error(f"Gemini API error: {e}")
            return f"Error: {str(e)}"

    async def get_streaming_completion(self, model: Optional[str], messages: List[Dict[str, str]], **kwargs):
        """Get streaming completion (simplified to non-streaming for now)."""
        # For simplicity, just return the regular completion
        result = await self.get_completion(model, messages, **kwargs)
        yield result

    async def synthesize_result(self, context: Dict[str, str], config: Dict[str, any]) -> str:
        """Generate synthesis of MCTS results."""
        synthesis_prompt = f"""
Based on the MCTS exploration, provide a comprehensive synthesis:

Question: {context.get('question_summary', 'N/A')}
Initial Analysis: {context.get('initial_analysis_summary', 'N/A')}
Best Score: {context.get('best_score', 'N/A')}
Exploration Path: {context.get('path_thoughts', 'N/A')}
Final Analysis: {context.get('final_best_analysis_summary', 'N/A')}

Please provide a clear, comprehensive synthesis that:
1. Summarizes the key findings
2. Highlights the best solution approach
3. Explains why this approach is optimal
4. Provides actionable insights
"""
        
        messages = [{"role": "user", "content": synthesis_prompt}]
        return await self.get_completion(None, messages)

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/ollama_utils.py:
--------------------------------------------------------------------------------

```python
# -*- coding: utf-8 -*-
"""
Ollama Utilities for MCTS
=========================

This module provides utility functions and constants for interacting with Ollama.
"""
import logging
import sys
import subprocess
import httpx # Used by check_available_models
from typing import List, Dict # Optional was unused

# Setup logger for this module
logger = logging.getLogger(__name__)

# Check if the 'ollama' Python package is installed.
# This is different from OllamaAdapter availability.
OLLAMA_PYTHON_PACKAGE_AVAILABLE = False
try:
    import ollama # type: ignore
    OLLAMA_PYTHON_PACKAGE_AVAILABLE = True
    logger.info(f"Ollama python package version: {getattr(ollama, '__version__', 'unknown')}")
except ImportError:
    logger.info("Ollama python package not found. Some features of check_available_models might be limited.")
except Exception as e:
    logger.warning(f"Error importing or checking ollama package version: {e}")


# --- Model Constants for get_recommended_models ---
SMALL_MODELS = ["qwen3:0.6b", "deepseek-r1:1.5b", "cogito:latest", "phi3:mini", "tinyllama", "phi2:2b", "qwen2:1.5b"]
MEDIUM_MODELS = ["mistral:7b", "llama3:8b", "gemma:7b", "mistral-nemo:7b"]
# DEFAULT_MODEL for an adapter is now defined in the adapter itself.

# --- Functions ---

def check_available_models() -> List[str]:
    """Check which Ollama models are available locally. Returns a list of model names."""
    # This function no longer relies on a global OLLAMA_AVAILABLE specific to the adapter,
    # but can use OLLAMA_PYTHON_PACKAGE_AVAILABLE for its 'ollama' package dependent part.
    # The primary check is if the Ollama server is running.
    # This function no longer relies on a global OLLAMA_AVAILABLE specific to the adapter,
    # but can use OLLAMA_PYTHON_PACKAGE_AVAILABLE for its 'ollama' package dependent part.
    # The primary check is if the Ollama server is running.

    try:
        # Use httpx for the initial server health check, as it's a direct dependency of this file.
        client = httpx.Client(base_url="http://localhost:11434", timeout=3.0)
        response = client.get("/")
        if response.status_code != 200:
            logger.error(f"Ollama server health check failed: {response.status_code} (ollama_utils)")
            return []
        logger.info("Ollama server is running (ollama_utils)")
    except Exception as e:
        logger.error(f"Ollama server health check failed: {e}. Server might not be running. (ollama_utils)")
        return []

    available_models: List[str] = []

    # Method 1: Subprocess
    try:
        cmd = ['ollama.exe', 'list'] if sys.platform == 'win32' else ['ollama', 'list']
        result = subprocess.run(cmd, capture_output=True, text=True, check=False)
        if result.returncode == 0:
            lines = result.stdout.strip().split('\n')
            if len(lines) > 1 and "NAME" in lines[0].upper() and "ID" in lines[0].upper(): # Make header check case-insensitive
                lines = lines[1:]

            for line in lines:
                if not line.strip():
                    continue
                parts = line.split()
                if parts:
                    model_name = parts[0]
                    if ':' not in model_name:
                        model_name += ':latest'
                    available_models.append(model_name)
            if available_models:
                logger.info(f"Available Ollama models via subprocess: {available_models} (ollama_utils)")
                return available_models
        else:
            logger.warning(f"Ollama list command failed (code {result.returncode}): {result.stderr} (ollama_utils)")
    except Exception as e:
        logger.warning(f"Subprocess 'ollama list' failed: {e} (ollama_utils)")

    # Method 2: HTTP API
    try:
        client = httpx.Client(base_url="http://localhost:11434", timeout=5.0)
        response = client.get("/api/tags")
        if response.status_code == 200:
            data = response.json()
            models_data = data.get("models", [])
            api_models = [m.get("name") for m in models_data if m.get("name")]
            if api_models:
                logger.info(f"Available Ollama models via HTTP API: {api_models} (ollama_utils)")
                return api_models
        else:
            logger.warning(f"Failed to get models from Ollama API: {response.status_code} (ollama_utils)")
    except Exception as e:
        logger.warning(f"HTTP API for Ollama models failed: {e} (ollama_utils)")

    # Method 3: Ollama package (if subprocess and API failed)
    if OLLAMA_PYTHON_PACKAGE_AVAILABLE:
        try:
            # This import is already tried at the top, but to be safe if logic changes:
            import ollama # type: ignore
            models_response = ollama.list()

            package_models = []
            if isinstance(models_response, dict) and "models" in models_response: # Handle dict format
                for model_dict in models_response["models"]:
                    if isinstance(model_dict, dict) and "name" in model_dict:
                        package_models.append(model_dict["name"])
            else: # Handle object format
                try:
                    for model_obj in getattr(models_response, 'models', []):
                        model_name = None
                        if hasattr(model_obj, 'model'):
                            model_name = getattr(model_obj, 'model')
                        elif hasattr(model_obj, 'name'):
                            model_name = getattr(model_obj, 'name')
                        if isinstance(model_name, str):
                            package_models.append(model_name)
                except (AttributeError, TypeError):
                    pass

            if package_models:
                logger.info(f"Available Ollama models via ollama package: {package_models} (ollama_utils)")
                return package_models
        except Exception as e:
            logger.warning(f"Ollama package 'list()' method failed: {e} (ollama_utils)")

    logger.warning("All methods to list Ollama models failed or returned no models. (ollama_utils)")
    return []

def get_recommended_models(models: List[str]) -> Dict[str, List[str]]:
    """Get a list of recommended models from available models, categorized by size."""
    small_recs = [model for model in SMALL_MODELS if model in models]
    medium_recs = [model for model in MEDIUM_MODELS if model in models]
    other_models = [m for m in models if m not in small_recs and m not in medium_recs]

    return {
        "small_models": small_recs,
        "medium_models": medium_recs,
        "other_models": other_models,
        "all_models": models # Return all detected models as well
    }

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/openai_adapter.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
OpenAI LLM Adapter
==================

This module defines the OpenAIAdapter class for interacting with OpenAI models.
"""
import logging
import os
import openai # type: ignore
from typing import AsyncGenerator, List, Dict, Any, Optional

from .base_llm_adapter import BaseLLMAdapter
from .llm_interface import LLMInterface # For type hinting or if BaseLLMAdapter doesn't explicitly inherit

class OpenAIAdapter(BaseLLMAdapter):
    """
    LLM Adapter for OpenAI models.
    """
    DEFAULT_MODEL = "gpt-3.5-turbo" # A common default, can be overridden

    def __init__(self, api_key: Optional[str] = None, model_name: Optional[str] = None, **kwargs):
        super().__init__(api_key=api_key, **kwargs) # Pass kwargs to BaseLLMAdapter

        self.api_key = api_key or os.getenv("OPENAI_API_KEY")
        if not self.api_key:
            raise ValueError("OpenAI API key not provided via argument or OPENAI_API_KEY environment variable.")

        self.client = openai.AsyncOpenAI(api_key=self.api_key)
        self.model_name = model_name or self.DEFAULT_MODEL
        self.logger = logging.getLogger(__name__) # Ensure logger is initialized
        self.logger.info(f"Initialized OpenAIAdapter with model: {self.model_name}")

    async def get_completion(self, model: Optional[str], messages: List[Dict[str, str]], **kwargs) -> str: # Removed default for model
        """
        Gets a non-streaming completion from the OpenAI LLM.
        """
        target_model = model if model is not None else self.model_name # Explicit None check
        self.logger.debug(f"OpenAI get_completion using model: {target_model}, messages: {messages}, kwargs: {kwargs}")
        try:
            response = await self.client.chat.completions.create(
                model=target_model,
                messages=messages, # type: ignore
                **kwargs
            )
            content = response.choices[0].message.content
            if content is None:
                self.logger.warning("OpenAI response content was None.")
                return ""
            return content
        except openai.APIError as e:
            self.logger.error(f"OpenAI API error in get_completion: {e}")
            # Depending on desired behavior, either re-raise or return an error string/default
            # For MCTS, returning an error string might be better than crashing.
            return f"Error: OpenAI API request failed - {type(e).__name__}: {e}"
        except Exception as e:
            self.logger.error(f"Unexpected error in OpenAI get_completion: {e}")
            return f"Error: Unexpected error during OpenAI request - {type(e).__name__}: {e}"

    async def get_streaming_completion(self, model: Optional[str], messages: List[Dict[str, str]], **kwargs) -> AsyncGenerator[str, None]: # Removed default for model
        """
        Gets a streaming completion from the OpenAI LLM.
        """
        target_model = model if model is not None else self.model_name # Explicit None check
        self.logger.debug(f"OpenAI get_streaming_completion using model: {target_model}, messages: {messages}, kwargs: {kwargs}")
        try:
            stream = await self.client.chat.completions.create(
                model=target_model,
                messages=messages, # type: ignore
                stream=True,
                **kwargs
            )
            async for chunk in stream:
                if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content is not None:
                    yield chunk.choices[0].delta.content
        except openai.APIError as e:
            self.logger.error(f"OpenAI API error in get_streaming_completion: {e}")
            yield f"Error: OpenAI API request failed - {type(e).__name__}: {e}"
        except Exception as e:
            self.logger.error(f"Unexpected error in OpenAI get_streaming_completion: {e}")
            yield f"Error: Unexpected error during OpenAI streaming request - {type(e).__name__}: {e}"
        # Ensure the generator is properly closed if an error occurs before any yield
        # This is mostly handled by async for, but good to be mindful.
        # No explicit 'return' needed in an async generator after all yields or errors.

# Example of how to use (for testing purposes)
async def _test_openai_adapter():
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)

    # This test requires OPENAI_API_KEY to be set in the environment
    if not os.getenv("OPENAI_API_KEY"):
        logger.warning("OPENAI_API_KEY not set, skipping OpenAIAdapter direct test.")
        return

    try:
        adapter = OpenAIAdapter(model_name="gpt-3.5-turbo") # Or your preferred model

        logger.info("Testing OpenAIAdapter get_completion...")
        messages = [{"role": "user", "content": "Hello, what is the capital of France?"}]
        completion = await adapter.get_completion(model=None, messages=messages)
        logger.info(f"Completion result: {completion}")
        assert "Paris" in completion

        logger.info("Testing OpenAIAdapter get_streaming_completion...")
        stream_messages = [{"role": "user", "content": "Write a short poem about AI."}]
        full_streamed_response = ""
        async for chunk in adapter.get_streaming_completion(model=None, messages=stream_messages):
            logger.info(f"Stream chunk: '{chunk}'")
            full_streamed_response += chunk
        logger.info(f"Full streamed response: {full_streamed_response}")
        assert len(full_streamed_response) > 0

        # Test a base class method (e.g., generate_tags)
        logger.info("Testing OpenAIAdapter (via BaseLLMAdapter) generate_tags...")
        tags_text = "This is a test of the emergency broadcast system. This is only a test."
        tags = await adapter.generate_tags(analysis_text=tags_text, config={}) # Pass empty config
        logger.info(f"Generated tags: {tags}")
        assert "test" in tags or "emergency" in tags

        logger.info("OpenAIAdapter tests completed successfully (if API key was present).")

    except ValueError as ve:
        logger.error(f"ValueError during OpenAIAdapter test (likely API key issue): {ve}")
    except openai.APIError as apie:
        logger.error(f"OpenAI APIError during OpenAIAdapter test: {apie}")
    except Exception as e:
        logger.error(f"An unexpected error occurred during OpenAIAdapter test: {e}", exc_info=True)

if __name__ == "__main__":
    # To run this test, ensure OPENAI_API_KEY is set in your environment
    # e.g., export OPENAI_API_KEY="your_key_here"
    # then run: python -m src.mcts_mcp_server.openai_adapter
    import asyncio
    if os.getenv("OPENAI_API_KEY"):
        asyncio.run(_test_openai_adapter())
    else:
        print("Skipping OpenAIAdapter test as OPENAI_API_KEY is not set.")

```

--------------------------------------------------------------------------------
/archive/test_new_adapters.py:
--------------------------------------------------------------------------------

```python
import asyncio
import os
import unittest
from unittest.mock import patch, AsyncMock
import logging

# Configure logging for tests
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# Attempt to import adapters
try:
    from src.mcts_mcp_server.openai_adapter import OpenAIAdapter
except ImportError:
    OpenAIAdapter = None
    logger.warning("Could not import OpenAIAdapter, tests for it will be skipped.")

try:
    from src.mcts_mcp_server.anthropic_adapter import AnthropicAdapter
except ImportError:
    AnthropicAdapter = None
    logger.warning("Could not import AnthropicAdapter, tests for it will be skipped.")

try:
    from src.mcts_mcp_server.gemini_adapter import GeminiAdapter
except ImportError:
    GeminiAdapter = None
    logger.warning("Could not import GeminiAdapter, tests for it will be skipped.")

# Helper function to run async tests
def async_test(f):
    def wrapper(*args, **kwargs):
        return asyncio.run(f(*args, **kwargs))
    return wrapper

class TestNewAdapters(unittest.TestCase):

    @unittest.skipIf(OpenAIAdapter is None, "OpenAIAdapter not imported")
    @patch.dict(os.environ, {}, clear=True) # Start with a clean environment
    def test_openai_adapter_no_key(self):
        if OpenAIAdapter is None:
            self.skipTest("OpenAIAdapter not available")
        logger.info("Testing OpenAIAdapter initialization without API key...")
        with self.assertRaisesRegex(ValueError, "OpenAI API key not provided"):
            OpenAIAdapter()

    @unittest.skipIf(OpenAIAdapter is None, "OpenAIAdapter not imported")
    @patch.dict(os.environ, {"OPENAI_API_KEY": "test_key"}, clear=True)
    @patch("openai.AsyncOpenAI") # Mock the actual client
    @async_test
    async def test_openai_adapter_with_key_mocked_completion(self, MockAsyncOpenAI):
        if OpenAIAdapter is None:
            self.skipTest("OpenAIAdapter not available")
        logger.info("Testing OpenAIAdapter with key and mocked completion...")
        # Configure the mock client and its methods
        mock_client_instance = MockAsyncOpenAI.return_value
        mock_completion_response = AsyncMock()
        mock_completion_response.choices = [AsyncMock(message=AsyncMock(content="Mocked OpenAI response"))]
        mock_client_instance.chat.completions.create = AsyncMock(return_value=mock_completion_response)

        adapter = OpenAIAdapter(api_key="test_key")
        self.assertIsNotNone(adapter.client)
        response = await adapter.get_completion(model=None, messages=[{"role": "user", "content": "Hello"}])
        self.assertEqual(response, "Mocked OpenAI response")
        MockAsyncOpenAI.assert_called_with(api_key="test_key")
        mock_client_instance.chat.completions.create.assert_called_once()

    @unittest.skipIf(AnthropicAdapter is None, "AnthropicAdapter not imported")
    @patch.dict(os.environ, {}, clear=True)
    def test_anthropic_adapter_no_key(self):
        if AnthropicAdapter is None:
            self.skipTest("AnthropicAdapter not available")
        logger.info("Testing AnthropicAdapter initialization without API key...")
        with self.assertRaisesRegex(ValueError, "Anthropic API key not provided"):
            AnthropicAdapter()

    @unittest.skipIf(AnthropicAdapter is None, "AnthropicAdapter not imported")
    @patch.dict(os.environ, {"ANTHROPIC_API_KEY": "test_key"}, clear=True)
    @patch("anthropic.AsyncAnthropic") # Mock the actual client
    @async_test
    async def test_anthropic_adapter_with_key_mocked_completion(self, MockAsyncAnthropic):
        if AnthropicAdapter is None:
            self.skipTest("AnthropicAdapter not available")
        logger.info("Testing AnthropicAdapter with key and mocked completion...")
        mock_client_instance = MockAsyncAnthropic.return_value
        mock_completion_response = AsyncMock()
        # Anthropic's response structure for content is a list of blocks
        mock_response_content_block = AsyncMock()
        mock_response_content_block.text = "Mocked Anthropic response"
        mock_completion_response.content = [mock_response_content_block]

        mock_client_instance.messages.create = AsyncMock(return_value=mock_completion_response)

        adapter = AnthropicAdapter(api_key="test_key")
        self.assertIsNotNone(adapter.client)
        # Provide a simple messages list that _prepare_anthropic_messages_and_system_prompt can handle
        response = await adapter.get_completion(model=None, messages=[{"role": "user", "content": "Hello"}])
        self.assertEqual(response, "Mocked Anthropic response")
        MockAsyncAnthropic.assert_called_with(api_key="test_key")
        mock_client_instance.messages.create.assert_called_once()

    @unittest.skipIf(GeminiAdapter is None, "GeminiAdapter not imported")
    @patch.dict(os.environ, {}, clear=True)
    def test_gemini_adapter_no_key(self):
        if GeminiAdapter is None:
            self.skipTest("GeminiAdapter not available")
        logger.info("Testing GeminiAdapter initialization without API key...")
        with self.assertRaisesRegex(ValueError, "Gemini API key not provided"):
            GeminiAdapter()

    @unittest.skipIf(GeminiAdapter is None, "GeminiAdapter not imported")
    @patch.dict(os.environ, {"GEMINI_API_KEY": "test_key"}, clear=True)
    @patch("google.generativeai.GenerativeModel") # Mock the actual client
    @patch("google.generativeai.configure") # Mock configure
    @async_test
    async def test_gemini_adapter_with_key_mocked_completion(self, mock_genai_configure, MockGenerativeModel):
        if GeminiAdapter is None:
            self.skipTest("GeminiAdapter not available")
        logger.info("Testing GeminiAdapter with key and mocked completion...")
        mock_model_instance = MockGenerativeModel.return_value
        # Ensure the mock response object has a 'text' attribute directly if that's what's accessed
        mock_generate_content_response = AsyncMock()
        mock_generate_content_response.text = "Mocked Gemini response"
        mock_model_instance.generate_content_async = AsyncMock(return_value=mock_generate_content_response)

        adapter = GeminiAdapter(api_key="test_key")
        self.assertIsNotNone(adapter.client)
        # Provide a simple messages list that _convert_messages_to_gemini_format can handle
        response = await adapter.get_completion(model=None, messages=[{"role": "user", "content": "Hello"}])
        self.assertEqual(response, "Mocked Gemini response")
        mock_genai_configure.assert_called_with(api_key="test_key")
        # Check if GenerativeModel was called with the default model name from the adapter
        MockGenerativeModel.assert_called_with(adapter.model_name)
        mock_model_instance.generate_content_async.assert_called_once()

if __name__ == "__main__":
    unittest.main()

```

--------------------------------------------------------------------------------
/archive/test_startup.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
MCTS MCP Server Startup Test
============================

This script tests the server startup time and basic MCP functionality
to help diagnose timeout issues.
"""

import sys
import time
import subprocess
import json
import os
from pathlib import Path

def print_colored(message, color_code=""):
    """Print colored message."""
    colors = {
        "green": "\033[92m",
        "red": "\033[91m", 
        "yellow": "\033[93m",
        "blue": "\033[94m",
        "reset": "\033[0m"
    }
    if color_code in colors:
        print(f"{colors[color_code]}{message}{colors['reset']}")
    else:
        print(message)

def test_quick_import():
    """Test if basic imports work quickly."""
    print("🔍 Testing quick imports...")
    start_time = time.time()
    
    try:
        import mcp
        import fastmcp
        print_colored("  ✅ MCP packages imported", "green")
    except ImportError as e:
        print_colored(f"  ❌ MCP import failed: {e}", "red")
        return False
    
    try:
        # Test the fast tools import
        sys.path.insert(0, "src")
        from mcts_mcp_server.tools_fast import register_mcts_tools
        print_colored("  ✅ Fast tools imported", "green")
    except ImportError as e:
        print_colored(f"  ❌ Fast tools import failed: {e}", "red")
        return False
    
    elapsed = time.time() - start_time
    print(f"  📊 Import time: {elapsed:.2f} seconds")
    
    if elapsed > 5.0:
        print_colored("  ⚠️  Imports are slow (>5s), may cause timeout", "yellow")
    else:
        print_colored("  ✅ Import speed is good", "green")
    
    return True

def test_server_startup():
    """Test server startup time."""
    print("\n🚀 Testing server startup...")
    
    project_dir = Path(__file__).parent
    
    # Test the fast server startup
    start_time = time.time()
    
    try:
        # Just test import and basic creation (don't actually run)
        cmd = [
            "uv", "run", "python", "-c", 
            """
import sys
sys.path.insert(0, 'src')
from mcts_mcp_server.server import main
print('SERVER_IMPORT_OK')
"""
        ]
        
        result = subprocess.run(
            cmd, 
            cwd=project_dir,
            capture_output=True, 
            text=True, 
            timeout=30
        )
        
        elapsed = time.time() - start_time
        
        if result.returncode == 0 and "SERVER_IMPORT_OK" in result.stdout:
            print_colored("  ✅ Server imports successfully", "green")
            print(f"  📊 Startup preparation time: {elapsed:.2f} seconds")
            
            if elapsed > 10.0:
                print_colored("  ⚠️  Startup is slow (>10s), may cause timeout", "yellow")
            else:
                print_colored("  ✅ Startup speed is good", "green")
            
            return True
        else:
            print_colored(f"  ❌ Server startup test failed", "red")
            print(f"  Output: {result.stdout}")
            print(f"  Error: {result.stderr}")
            return False
            
    except subprocess.TimeoutExpired:
        print_colored("  ❌ Server startup test timed out (>30s)", "red")
        return False
    except Exception as e:
        print_colored(f"  ❌ Server startup test error: {e}", "red")
        return False

def test_environment_setup():
    """Test environment configuration."""
    print("\n🔧 Testing environment setup...")
    
    project_dir = Path(__file__).parent
    
    # Check .env file
    env_file = project_dir / ".env"
    if env_file.exists():
        print_colored("  ✅ .env file exists", "green")
    else:
        print_colored("  ⚠️  .env file missing", "yellow")
    
    # Check virtual environment
    venv_dir = project_dir / ".venv"
    if venv_dir.exists():
        print_colored("  ✅ Virtual environment exists", "green")
    else:
        print_colored("  ❌ Virtual environment missing", "red")
        return False
    
    # Check if in fast mode
    fast_mode = os.getenv("MCTS_FAST_MODE", "true").lower() == "true"
    if fast_mode:
        print_colored("  ✅ Fast mode enabled", "green")
    else:
        print_colored("  ⚠️  Fast mode disabled", "yellow")
    
    return True

def test_claude_config():
    """Test Claude Desktop configuration."""
    print("\n📋 Testing Claude Desktop config...")
    
    config_file = Path(__file__).parent / "claude_desktop_config.json"
    
    if not config_file.exists():
        print_colored("  ❌ claude_desktop_config.json not found", "red")
        return False
    
    try:
        with open(config_file, 'r') as f:
            config = json.load(f)
        
        if "mcpServers" in config and "mcts-mcp-server" in config["mcpServers"]:
            server_config = config["mcpServers"]["mcts-mcp-server"]
            
            # Check for fast mode setting
            env_config = server_config.get("env", {})
            fast_mode = env_config.get("MCTS_FAST_MODE", "false").lower() == "true"
            
            if fast_mode:
                print_colored("  ✅ Claude config has fast mode enabled", "green")
            else:
                print_colored("  ⚠️  Claude config doesn't have fast mode", "yellow")
                print_colored("      Add: \"MCTS_FAST_MODE\": \"true\" to env section", "blue")
            
            # Check for timeout setting
            timeout = server_config.get("timeout")
            if timeout:
                print_colored(f"  ✅ Timeout configured: {timeout} seconds", "green")
            else:
                print_colored("  ℹ️  No timeout configured (uses default)", "blue")
            
            print_colored("  ✅ Claude config structure is valid", "green")
            return True
        else:
            print_colored("  ❌ Claude config missing MCTS server entry", "red")
            return False
            
    except json.JSONDecodeError:
        print_colored("  ❌ Claude config has invalid JSON", "red")
        return False
    except Exception as e:
        print_colored(f"  ❌ Error reading Claude config: {e}", "red")
        return False

def main():
    """Run all startup tests."""
    print("🧪 MCTS MCP Server Startup Test")
    print("=" * 40)
    print(f"Python: {sys.version}")
    print(f"Platform: {sys.platform}")
    print()
    
    tests = [
        test_environment_setup,
        test_quick_import,
        test_server_startup, 
        test_claude_config
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        if test():
            passed += 1
    
    print("\n" + "=" * 40)
    print(f"📊 Results: {passed}/{total} tests passed")
    
    if passed == total:
        print_colored("🎉 All tests passed! Server should start quickly.", "green")
        print()
        print("Next steps:")
        print("1. Restart Claude Desktop")
        print("2. Test with: get_config()")
        print("3. If still timing out, check TIMEOUT_FIX.md")
    else:
        print_colored("❌ Some tests failed. Check the issues above.", "red")
        print()
        print("Common fixes:")
        print("1. Run: python setup.py")
        print("2. Enable fast mode in Claude config")
        print("3. Check TIMEOUT_FIX.md for solutions")
    
    return passed == total

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)

```

--------------------------------------------------------------------------------
/archive/test_ollama.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test script to diagnose Ollama model detection issues
"""
import os
import sys
import subprocess
import json
import logging

# Add MCTS MCP Server to PYTHONPATH
script_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, script_dir)

# Set up logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger("test_ollama")

def test_subprocess_method():
    """Test listing models via subprocess call."""
    try:
        result = subprocess.run(['ollama', 'list'], capture_output=True, text=True, check=True)
        lines = result.stdout.strip().split('\n')
        
        # Skip the header line if present
        if len(lines) > 1 and "NAME" in lines[0] and "ID" in lines[0]:
            lines = lines[1:]
        
        # Extract model names
        models = []
        for line in lines:
            if not line.strip():
                continue
            parts = line.split()
            if parts:
                model_name = parts[0]
                if ':' not in model_name:
                    model_name += ':latest'
                models.append(model_name)
        
        logger.info(f"Subprocess method found {len(models)} models: {models}")
        return models
    except Exception as e:
        logger.error(f"Subprocess method failed: {e}")
        return []

def test_httpx_method():
    """Test listing models via HTTP API."""
    try:
        # Try to import httpx
        import httpx
        
        client = httpx.Client(base_url="http://localhost:11434", timeout=5.0)
        response = client.get("/api/tags")
        
        if response.status_code == 200:
            data = response.json()
            models = data.get("models", [])
            model_names = [m.get("name") for m in models if m.get("name")]
            
            logger.info(f"HTTPX method found {len(model_names)} models: {model_names}")
            return model_names
        else:
            logger.error(f"HTTPX request failed with status code {response.status_code}")
            return []
    except ImportError:
        logger.error("HTTPX not installed. Cannot test HTTP API method.")
        return []
    except Exception as e:
        logger.error(f"HTTPX method failed: {e}")
        return []

def test_ollama_package():
    """Test listing models via ollama Python package."""
    try:
        # Try to import ollama
        import ollama
        
        # Log the ollama version
        ollama_version = getattr(ollama, "__version__", "unknown")
        logger.info(f"Ollama package version: {ollama_version}")
        
        # Test the list() function
        models_data = ollama.list()
        logger.info(f"Ollama package response type: {type(models_data)}")
        logger.info(f"Ollama package response: {models_data}")
        
        # Try different parsing methods based on the response format
        model_names = []
        
        # Method 1: Object with models attribute (newer API)
        if hasattr(models_data, 'models'):
            logger.info("Response has 'models' attribute")
            if isinstance(models_data.models, list):
                logger.info("models attribute is a list")
                for model in models_data.models:
                    logger.info(f"Model object type: {type(model)}")
                    logger.info(f"Model object attributes: {dir(model)}")
                    if hasattr(model, 'model'):
                        model_names.append(model.model)
                        logger.info(f"Added model name from 'model' attribute: {model.model}")
                    elif hasattr(model, 'name'):
                        model_names.append(model.name)
                        logger.info(f"Added model name from 'name' attribute: {model.name}")
        
        # Method 2: Dictionary format (older API)
        elif isinstance(models_data, dict):
            logger.info("Response is a dictionary")
            if "models" in models_data:
                logger.info("Dictionary has 'models' key")
                for m in models_data["models"]:
                    if isinstance(m, dict) and "name" in m:
                        model_names.append(m["name"])
                        logger.info(f"Added model name from dictionary: {m['name']}")
        
        # Method 3: List format
        elif isinstance(models_data, list):
            logger.info("Response is a list")
            for m in models_data:
                if isinstance(m, dict) and "name" in m:
                    model_names.append(m["name"])
                    logger.info(f"Added model name from list item: {m['name']}")
                elif hasattr(m, 'name'):
                    model_names.append(m.name)
                    logger.info(f"Added model name from list item attribute: {m.name}")
                else:
                    # Last resort, convert to string
                    model_names.append(str(m))
                    logger.info(f"Added model name as string: {str(m)}")
        
        logger.info(f"Ollama package method found {len(model_names)} models: {model_names}")
        return model_names
    except ImportError:
        logger.error("Ollama package not installed. Cannot test ollama API method.")
        return []
    except Exception as e:
        logger.error(f"Ollama package method failed: {e}")
        import traceback
        logger.error(traceback.format_exc())
        return []

def main():
    """Run all test methods and report results."""
    logger.info("====== Testing Ollama Model Detection ======")
    
    # Test subprocess method
    logger.info("--- Testing Subprocess Method ---")
    subprocess_models = test_subprocess_method()
    
    # Test HTTPX method
    logger.info("--- Testing HTTPX Method ---")
    httpx_models = test_httpx_method()
    
    # Test ollama package
    logger.info("--- Testing Ollama Package Method ---")
    package_models = test_ollama_package()
    
    # Print results
    print("\n====== RESULTS ======")
    print(f"Subprocess Method: {len(subprocess_models)} models")
    print(f"HTTPX Method: {len(httpx_models)} models")
    print(f"Ollama Package Method: {len(package_models)} models")
    
    # Check for consistency
    if subprocess_models and httpx_models and package_models:
        if set(subprocess_models) == set(httpx_models) == set(package_models):
            print("\n✅ All methods detected the same models")
        else:
            print("\n⚠️ Methods detected different sets of models")
            
            # Find differences
            all_models = set(subprocess_models + httpx_models + package_models)
            for model in all_models:
                in_subprocess = model in subprocess_models
                in_httpx = model in httpx_models
                in_package = model in package_models
                
                if not (in_subprocess and in_httpx and in_package):
                    print(f"  - '{model}': subprocess={in_subprocess}, httpx={in_httpx}, package={in_package}")
    else:
        print("\n⚠️ Some methods failed to detect models")
    
    # Output data for debugging
    result = {
        "subprocess_models": subprocess_models,
        "httpx_models": httpx_models,
        "package_models": package_models
    }
    
    print("\nDetailed Results:")
    print(json.dumps(result, indent=2))

if __name__ == "__main__":
    main()

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/rate_limiter.py:
--------------------------------------------------------------------------------

```python
"""
Rate Limiter Utility
====================

This module provides rate limiting functionality for API calls, specifically
designed for LLM providers with rate limits like Gemini's free tier.
"""
import asyncio
import logging
import time
from dataclasses import dataclass
from typing import ClassVar

logger = logging.getLogger(__name__)

@dataclass
class RateLimitConfig:
    """Configuration for rate limiting."""
    requests_per_minute: int
    burst_allowance: int = 1  # How many requests can be made immediately

    @property
    def requests_per_second(self) -> float:
        """Convert RPM to RPS for easier calculations."""
        return self.requests_per_minute / 60.0


class TokenBucketRateLimiter:
    """
    Token bucket rate limiter implementation.

    This allows for burst requests up to the bucket capacity, then refills
    tokens at a steady rate based on the configured rate limit.
    """

    def __init__(self, config: RateLimitConfig):
        self.config = config
        self.tokens = float(config.burst_allowance)
        self.last_refill = time.time()
        self._lock = asyncio.Lock()

        logger.info(f"Initialized rate limiter: {config.requests_per_minute} RPM, "
                   f"burst: {config.burst_allowance}")

    async def acquire(self, tokens_needed: int = 1) -> None:
        """
        Acquire tokens for making requests. Will wait if necessary.

        Args:
            tokens_needed: Number of tokens to acquire (default: 1)
        """
        async with self._lock:
            await self._wait_for_tokens(tokens_needed)
            self.tokens -= tokens_needed

            logger.debug(f"Acquired {tokens_needed} tokens, {self.tokens:.2f} remaining")

    async def _wait_for_tokens(self, tokens_needed: int) -> None:
        """Wait until enough tokens are available."""
        while True:
            self._refill_tokens()

            if self.tokens >= tokens_needed:
                break

            # Calculate how long to wait for enough tokens
            tokens_deficit = tokens_needed - self.tokens
            wait_time = tokens_deficit / self.config.requests_per_second

            logger.debug(f"Rate limit hit, waiting {wait_time:.2f}s for {tokens_deficit:.2f} tokens")
            await asyncio.sleep(wait_time)

    def _refill_tokens(self) -> None:
        """Refill tokens based on elapsed time."""
        now = time.time()
        elapsed = now - self.last_refill

        # Add tokens based on elapsed time
        tokens_to_add = elapsed * self.config.requests_per_second
        self.tokens = min(self.config.burst_allowance, self.tokens + tokens_to_add)

        self.last_refill = now

    def get_status(self) -> dict[str, float]:
        """Get current rate limiter status."""
        self._refill_tokens()
        return {
            "available_tokens": self.tokens,
            "max_tokens": self.config.burst_allowance,
            "rate_per_minute": self.config.requests_per_minute,
            "rate_per_second": self.config.requests_per_second
        }


class ModelRateLimitManager:
    """
    Manages rate limiters for different models.

    Allows different rate limits for different models, with sensible defaults
    for known model tiers.
    """
    # Default rate limits for known model patterns
    DEFAULT_RATE_LIMITS: ClassVar[dict[str, RateLimitConfig]] = {
        # Gemini free tier models
        "gemini-1.5-flash": RateLimitConfig(requests_per_minute=15, burst_allowance=2),
        "gemini-1.5-flash-8b": RateLimitConfig(requests_per_minute=15, burst_allowance=2),
        "gemini-2.0-flash-exp": RateLimitConfig(requests_per_minute=10, burst_allowance=1),
        "gemini-2.5-flash-preview": RateLimitConfig(requests_per_minute=10, burst_allowance=1),

        # Gemini paid tier models (higher limits)
        "gemini-1.5-pro": RateLimitConfig(requests_per_minute=360, burst_allowance=5),
        "gemini-2.0-flash-thinking-exp": RateLimitConfig(requests_per_minute=60, burst_allowance=3),

        # Default fallback
        "default": RateLimitConfig(requests_per_minute=10, burst_allowance=1)
    }

    def __init__(self, custom_limits: dict[str, RateLimitConfig] | None = None):
        self.rate_limits = self.DEFAULT_RATE_LIMITS.copy()
        if custom_limits:
            self.rate_limits.update(custom_limits)

        self.limiters: dict[str, TokenBucketRateLimiter] = {}

        logger.info(f"Initialized ModelRateLimitManager with {len(self.rate_limits)} rate limit configs")
    def _get_rate_limit_config(self, model_name: str) -> RateLimitConfig:
        """Get rate limit config for a model, using pattern matching."""
        # Direct match first
        if model_name in self.rate_limits:
            return self.rate_limits[model_name]

        # Pattern matching for model families
        for pattern, config in self.rate_limits.items():
            if pattern != "default" and pattern in model_name:
                logger.debug(f"Matched model '{model_name}' to pattern '{pattern}'")
                return config

        # Fallback to default
        logger.debug(f"Using default rate limit for model '{model_name}'")
        return self.rate_limits["default"]

    def get_limiter(self, model_name: str) -> TokenBucketRateLimiter:
        """Get or create a rate limiter for a specific model."""
        if model_name not in self.limiters:
            config = self._get_rate_limit_config(model_name)
            self.limiters[model_name] = TokenBucketRateLimiter(config)
            logger.info(f"Created rate limiter for model '{model_name}': {config}")

        return self.limiters[model_name]

    async def acquire_for_model(self, model_name: str, tokens_needed: int = 1) -> None:
        """Acquire tokens for a specific model."""
        limiter = self.get_limiter(model_name)
        await limiter.acquire(tokens_needed)

    def get_all_status(self) -> dict[str, dict[str, float]]:
        """Get status for all active rate limiters."""
        return {
            model: limiter.get_status()
            for model, limiter in self.limiters.items()
        }

    def add_custom_limit(self, model_name: str, config: RateLimitConfig) -> None:
        """Add a custom rate limit for a specific model."""
        self.rate_limits[model_name] = config
        # Remove existing limiter so it gets recreated with new config
        if model_name in self.limiters:
            del self.limiters[model_name]
        logger.info(f"Added custom rate limit for '{model_name}': {config}")


# Global rate limit manager instance (can be imported and used across modules)
global_rate_limit_manager = ModelRateLimitManager()


async def test_rate_limiter():
    """Test the rate limiter functionality."""
    print("Testing rate limiter...")

    # Test high-frequency model
    config = RateLimitConfig(requests_per_minute=60, burst_allowance=3)
    limiter = TokenBucketRateLimiter(config)

    print(f"Initial status: {limiter.get_status()}")

    # Make some requests quickly (should work due to burst)
    for i in range(3):
        start = time.time()
        await limiter.acquire()
        elapsed = time.time() - start
        print(f"Request {i+1}: {elapsed:.3f}s")

    # This should be rate limited
    start = time.time()
    await limiter.acquire()
    elapsed = time.time() - start
    print(f"Rate limited request: {elapsed:.3f}s")

    print(f"Final status: {limiter.get_status()}")

    # Test model manager
    print("\nTesting model manager...")
    manager = ModelRateLimitManager()

    # Test different models
    test_models = [
        "gemini-2.5-flash-preview-05-20",
        "gemini-1.5-pro",
        "unknown-model"
    ]

    for model in test_models:
        limiter = manager.get_limiter(model)
        status = limiter.get_status()
        print(f"{model}: {status['rate_per_minute']} RPM, {status['max_tokens']} burst")


if __name__ == "__main__":
    import asyncio
    asyncio.run(test_rate_limiter())

```

--------------------------------------------------------------------------------
/src/mcts_mcp_server/base_llm_adapter.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Base LLM Adapter
================

This module defines the BaseLLMAdapter abstract base class.
"""
import abc
import logging
import re
from typing import List, Dict, Any, AsyncGenerator, Optional

from .llm_interface import LLMInterface
from .intent_handler import (
    INITIAL_PROMPT,
    THOUGHTS_PROMPT,
    UPDATE_PROMPT,
    EVAL_ANSWER_PROMPT,
    TAG_GENERATION_PROMPT,
    FINAL_SYNTHESIS_PROMPT,
    INTENT_CLASSIFIER_PROMPT
)

class BaseLLMAdapter(LLMInterface, abc.ABC):
    """
    Abstract Base Class for LLM adapters.
    Provides common prompt formatting and response processing logic.
    """

    def __init__(self, api_key: Optional[str] = None, **kwargs):
        self.logger = logging.getLogger(__name__)
        self.api_key = api_key
        # Allow other kwargs to be stored if needed by subclasses
        self._kwargs = kwargs

    @abc.abstractmethod
    async def get_completion(self, model: Optional[str], messages: List[Dict[str, str]], **kwargs) -> str:
        """
        Abstract method to get a non-streaming completion from the LLM.
        'model' can be None if the adapter is initialized with a specific model.
        """
        pass

    @abc.abstractmethod
    async def get_streaming_completion(self, model: Optional[str], messages: List[Dict[str, str]], **kwargs) -> AsyncGenerator[str, None]:
        """
        Abstract method to get a streaming completion from the LLM.
        'model' can be None if the adapter is initialized with a specific model.
        """
        # Required for async generator structure
        if False: # pragma: no cover
            yield
        pass

    async def generate_thought(self, context: Dict[str, Any], config: Dict[str, Any]) -> str:
        """Generates a critical thought or new direction based on context."""
        prompt = THOUGHTS_PROMPT.format(**context)
        messages = [{"role": "user", "content": prompt}]
        # Model might be specified in config or be a default for the adapter instance
        model_to_use = config.get("model_name") or self._kwargs.get("model_name")
        return await self.get_completion(model=model_to_use, messages=messages)

    async def update_analysis(self, critique: str, context: Dict[str, Any], config: Dict[str, Any]) -> str:
        """Revises analysis based on critique and context."""
        # Ensure 'critique' and 'answer' (draft) are in context for the prompt
        context_for_prompt = context.copy()
        context_for_prompt['critique'] = critique
        # 'answer' should be the draft text, usually passed in context as 'answer' or 'draft'
        # The UPDATE_PROMPT expects {answer} for the draft and {improvements} for the critique.
        # The MCTS core calls this with 'answer' (node.content) and 'improvements' (thought) in context.
        # Let's ensure the prompt matches the keys used in MCTS:
        # UPDATE_PROMPT uses <draft>{answer}</draft> and <critique>{improvements}</critique>
        # The MCTS context provides 'answer' for draft and 'improvements' for critique.

        prompt = UPDATE_PROMPT.format(**context_for_prompt)
        messages = [{"role": "user", "content": prompt}]
        model_to_use = config.get("model_name") or self._kwargs.get("model_name")
        return await self.get_completion(model=model_to_use, messages=messages)

    async def evaluate_analysis(self, analysis_to_evaluate: str, context: Dict[str, Any], config: Dict[str, Any]) -> int:
        """Evaluates analysis quality (1-10 score)."""
        context_for_prompt = context.copy()
        context_for_prompt['answer_to_evaluate'] = analysis_to_evaluate
        prompt = EVAL_ANSWER_PROMPT.format(**context_for_prompt)
        messages = [{"role": "user", "content": prompt}]
        model_to_use = config.get("model_name") or self._kwargs.get("model_name")

        raw_response = await self.get_completion(model=model_to_use, messages=messages)

        try:
            # Extract numbers, prioritize integers.
            # This regex finds integers or floats in the string.
            numbers = re.findall(r'\b\d+\b', raw_response) # Prioritize whole numbers
            if not numbers: # If no whole numbers, try to find any number including float
                numbers = re.findall(r"[-+]?\d*\.\d+|\d+", raw_response)

            if numbers:
                score = int(round(float(numbers[0]))) # Take the first number found
                if 1 <= score <= 10:
                    return score
                else:
                    self.logger.warning(f"LLM evaluation score {score} out of range (1-10). Defaulting to 5. Raw: '{raw_response}'")
            else:
                self.logger.warning(f"Could not parse score from LLM evaluation response: '{raw_response}'. Defaulting to 5.")
        except ValueError:
            self.logger.warning(f"Could not convert score to int from LLM response: '{raw_response}'. Defaulting to 5.")
        return 5 # Default score

    async def generate_tags(self, analysis_text: str, config: Dict[str, Any]) -> List[str]:
        """Generates keyword tags for the analysis."""
        context = {"analysis_text": analysis_text}
        prompt = TAG_GENERATION_PROMPT.format(**context)
        messages = [{"role": "user", "content": prompt}]
        model_to_use = config.get("model_name") or self._kwargs.get("model_name")

        raw_response = await self.get_completion(model=model_to_use, messages=messages)
        if raw_response:
            # Remove potential markdown list characters and split
            tags = [tag.strip().lstrip("-* ").rstrip(",.") for tag in raw_response.split(',')]
            # Filter out empty tags that might result from splitting
            return [tag for tag in tags if tag]
        self.logger.warning(f"Tag generation returned empty or invalid response: '{raw_response}'")
        return []

    async def synthesize_result(self, context: Dict[str, Any], config: Dict[str, Any]) -> str:
        """Generates a final synthesis based on the MCTS results."""
        prompt = FINAL_SYNTHESIS_PROMPT.format(**context)
        messages = [{"role": "user", "content": prompt}]
        model_to_use = config.get("model_name") or self._kwargs.get("model_name")
        return await self.get_completion(model=model_to_use, messages=messages)

    async def classify_intent(self, text_to_classify: str, config: Dict[str, Any]) -> str:
        """Classifies user intent using the LLM."""
        # Context for intent classification typically just needs the raw input text
        context = {"raw_input_text": text_to_classify}
        prompt = INTENT_CLASSIFIER_PROMPT.format(**context)
        messages = [{"role": "user", "content": prompt}]
        model_to_use = config.get("model_name") or self._kwargs.get("model_name")

        response = await self.get_completion(model=model_to_use, messages=messages)
        # Basic cleaning, specific adapters might need more
        return response.strip().upper().split()[0] if response else "UNKNOWN"

"""
# Example usage (for testing, not part of the class itself)
if __name__ == '__main__':
    # This part would require a concrete implementation of BaseLLMAdapter
    # and an asyncio event loop to run.
    class MyAdapter(BaseLLMAdapter):
        async def get_completion(self, model: Optional[str], messages: List[Dict[str, str]], **kwargs) -> str:
            # Mock implementation
            print(f"MyAdapter.get_completion called with model: {model}, messages: {messages}")
            if "evaluate_analysis" in messages[0]["content"]:
                return "This is a test evaluation. Score: 8/10"
            if "generate_tags" in messages[0]["content"]:
                return "tag1, tag2, tag3"
            return "This is a test completion."

        async def get_streaming_completion(self, model: Optional[str], messages: List[Dict[str, str]], **kwargs) -> AsyncGenerator[str, None]:
            print(f"MyAdapter.get_streaming_completion called with model: {model}, messages: {messages}")
            yield "Stream chunk 1 "
            yield "Stream chunk 2"
            # Must include this for the method to be a valid async generator
            if False: # pragma: no cover
                 yield

    async def main():
        adapter = MyAdapter(model_name="default_test_model")

        # Test generate_thought
        thought_context = {
            "previous_best_summary": "Old summary", "unfit_markers_summary": "None",
            "learned_approach_summary": "Rational", "question_summary": "What is life?",
            "best_answer": "42", "best_score": "10", "current_sequence": "N1",
            "current_answer": "Deep thought", "current_tags": "philosophy"
        }
        thought = await adapter.generate_thought(thought_context, {})
        print(f"Generated thought: {thought}")

        # Test evaluate_analysis
        eval_score = await adapter.evaluate_analysis("Some analysis text", {"best_score": "7"}, {})
        print(f"Evaluation score: {eval_score}")

        # Test generate_tags
        tags = await adapter.generate_tags("Some text to tag.", {})
        print(f"Generated tags: {tags}")

    if False: # Keep example code from running automatically
       import asyncio
       asyncio.run(main())
"""

```

--------------------------------------------------------------------------------
/verify_installation.py:
--------------------------------------------------------------------------------

```python
#!/usr/bin/env python3
"""
MCTS MCP Server Installation Verification Script
===============================================

This script verifies that the MCTS MCP Server is properly installed
and configured across different platforms.
"""

import os
import sys
import subprocess
import platform
import json
from pathlib import Path
from typing import List, Dict, Any, Optional

# Simple color output
class Colors:
    GREEN = '\033[92m' if platform.system() != "Windows" else ''
    RED = '\033[91m' if platform.system() != "Windows" else ''
    YELLOW = '\033[93m' if platform.system() != "Windows" else ''
    BLUE = '\033[94m' if platform.system() != "Windows" else ''
    RESET = '\033[0m' if platform.system() != "Windows" else ''
    BOLD = '\033[1m' if platform.system() != "Windows" else ''

def print_colored(message: str, color: str = '') -> None:
    """Print a colored message."""
    print(f"{color}{message}{Colors.RESET}")

def print_header(message: str) -> None:
    """Print a header message."""
    print_colored(f"\n{'='*50}", Colors.BLUE)
    print_colored(f"{message}", Colors.BLUE + Colors.BOLD)
    print_colored(f"{'='*50}", Colors.BLUE)

def print_check(description: str, passed: bool, details: str = "") -> None:
    """Print a check result."""
    status = "✅ PASS" if passed else "❌ FAIL"
    color = Colors.GREEN if passed else Colors.RED
    print_colored(f"{status} {description}", color)
    if details:
        print(f"     {details}")

def run_command(command: List[str], cwd: Optional[Path] = None) -> Optional[subprocess.CompletedProcess]:
    """Run a command and return the result, or None if failed."""
    try:
        result = subprocess.run(
            command,
            cwd=cwd,
            capture_output=True,
            text=True,
            timeout=30
        )
        return result
    except (subprocess.CalledProcessError, subprocess.TimeoutExpired, FileNotFoundError):
        return None

def check_python_version() -> bool:
    """Check Python version."""
    version = sys.version_info
    required_major, required_minor = 3, 10
    
    is_compatible = version.major >= required_major and version.minor >= required_minor
    details = f"Found {version.major}.{version.minor}.{version.micro}, required 3.10+"
    
    print_check("Python version", is_compatible, details)
    return is_compatible

def check_uv_installation() -> bool:
    """Check if uv is installed and accessible."""
    result = run_command(["uv", "--version"])
    
    if result and result.returncode == 0:
        version = result.stdout.strip()
        print_check("uv package manager", True, f"Version: {version}")
        return True
    else:
        print_check("uv package manager", False, "Not found or not working")
        return False

def check_virtual_environment(project_dir: Path) -> bool:
    """Check if virtual environment exists."""
    venv_dir = project_dir / ".venv"
    
    if venv_dir.exists() and venv_dir.is_dir():
        # Check for Python executable in venv
        if platform.system() == "Windows":
            python_exe = venv_dir / "Scripts" / "python.exe"
        else:
            python_exe = venv_dir / "bin" / "python"
        
        if python_exe.exists():
            print_check("Virtual environment", True, f"Found at {venv_dir}")
            return True
    
    print_check("Virtual environment", False, "Not found or incomplete")
    return False

def check_dependencies(project_dir: Path) -> Dict[str, bool]:
    """Check if required dependencies are installed."""
    dependencies = {
        "mcp": False,
        "numpy": False,
        "scikit-learn": False,
        "ollama": False,
        "openai": False,
        "anthropic": False,
        "google.genai": False,
        "fastmcp": False
    }
    
    for dep in dependencies:
        result = run_command([
            "uv", "run", "python", "-c", f"import {dep}; print('OK')"
        ], cwd=project_dir)
        
        if result and result.returncode == 0 and "OK" in result.stdout:
            dependencies[dep] = True
    
    # Print results
    all_good = True
    for dep, installed in dependencies.items():
        print_check(f"Package: {dep}", installed)
        if not installed:
            all_good = False
    
    return dependencies

def check_environment_file(project_dir: Path) -> bool:
    """Check if .env file exists and has basic structure."""
    env_file = project_dir / ".env"
    
    if not env_file.exists():
        print_check("Environment file (.env)", False, "File not found")
        return False
    
    try:
        content = env_file.read_text()
        
        # Check for required keys
        required_keys = ["GEMINI_API_KEY", "OPENAI_API_KEY", "ANTHROPIC_API_KEY"]
        found_keys = []
        
        for key in required_keys:
            if key in content:
                found_keys.append(key)
        
        details = f"Found {len(found_keys)}/{len(required_keys)} API key entries"
        print_check("Environment file (.env)", True, details)
        return True
        
    except Exception as e:
        print_check("Environment file (.env)", False, f"Error reading file: {e}")
        return False

def check_claude_config(project_dir: Path) -> bool:
    """Check if Claude Desktop config file exists."""
    config_file = project_dir / "claude_desktop_config.json"
    
    if not config_file.exists():
        print_check("Claude Desktop config", False, "File not found")
        return False
    
    try:
        with open(config_file, 'r') as f:
            config = json.load(f)
        
        # Check structure
        if "mcpServers" in config and "mcts-mcp-server" in config["mcpServers"]:
            print_check("Claude Desktop config", True, "Valid structure found")
            return True
        else:
            print_check("Claude Desktop config", False, "Invalid structure")
            return False
            
    except Exception as e:
        print_check("Claude Desktop config", False, f"Error reading file: {e}")
        return False

def check_basic_functionality(project_dir: Path) -> bool:
    """Test basic import and functionality."""
    test_code = '''
try:
    from mcts_mcp_server import tools
    from mcts_mcp_server.mcts_core import MCTS
    from mcts_mcp_server.ollama_adapter import OllamaAdapter
    print("BASIC_IMPORT_OK")
except Exception as e:
    print(f"IMPORT_ERROR: {e}")
'''
    
    result = run_command([
        "uv", "run", "python", "-c", test_code
    ], cwd=project_dir)
    
    if result and result.returncode == 0 and "BASIC_IMPORT_OK" in result.stdout:
        print_check("Basic functionality", True, "Core modules import successfully")
        return True
    else:
        error_msg = result.stderr if result else "Command failed to run"
        print_check("Basic functionality", False, f"Import failed: {error_msg}")
        return False

def check_server_startup(project_dir: Path) -> bool:
    """Test if the MCP server can start (basic syntax check)."""
    test_code = '''
try:
    # Just test if we can import and create the main objects without running
    import sys
    sys.path.insert(0, "src")
    from mcts_mcp_server.server import create_server
    print("SERVER_SYNTAX_OK")
except Exception as e:
    print(f"SERVER_ERROR: {e}")
'''
    
    result = run_command([
        "uv", "run", "python", "-c", test_code
    ], cwd=project_dir)
    
    if result and result.returncode == 0 and "SERVER_SYNTAX_OK" in result.stdout:
        print_check("Server startup test", True, "Server code is valid")
        return True
    else:
        error_msg = result.stderr if result else "Command failed to run"
        print_check("Server startup test", False, f"Server test failed: {error_msg}")
        return False

def main():
    """Main verification function."""
    print_header("MCTS MCP Server Installation Verification")
    print(f"Platform: {platform.system()} {platform.release()}")
    print(f"Python: {sys.version}")
    print()
    
    # Get project directory
    project_dir = Path(__file__).parent.resolve()
    print(f"Project directory: {project_dir}")
    
    all_checks = []
    
    # Run all checks
    print_header("Basic Requirements")
    all_checks.append(check_python_version())
    all_checks.append(check_uv_installation())
    
    print_header("Project Structure")
    all_checks.append(check_virtual_environment(project_dir))
    all_checks.append(check_environment_file(project_dir))
    all_checks.append(check_claude_config(project_dir))
    
    print_header("Dependencies")
    deps = check_dependencies(project_dir)
    all_checks.append(all(deps.values()))
    
    print_header("Functionality Tests")
    all_checks.append(check_basic_functionality(project_dir))
    all_checks.append(check_server_startup(project_dir))
    
    # Summary
    print_header("Summary")
    passed = sum(all_checks)
    total = len(all_checks)
    
    if passed == total:
        print_colored(f"🎉 All checks passed! ({passed}/{total})", Colors.GREEN + Colors.BOLD)
        print()
        print_colored("Your MCTS MCP Server installation is ready to use!", Colors.GREEN)
        print()
        print("Next steps:")
        print("1. Add your API keys to the .env file")
        print("2. Configure Claude Desktop with the provided config")
        print("3. Restart Claude Desktop")
        print("4. Test the MCTS tools in Claude")
    else:
        print_colored(f"❌ {total - passed} checks failed ({passed}/{total} passed)", Colors.RED + Colors.BOLD)
        print()
        print_colored("Please fix the failed checks and run the verification again.", Colors.RED)
        print()
        print("Common solutions:")
        print("• Run the setup script again: python setup.py")
        print("• Check the README.md for detailed instructions")
        print("• Ensure all dependencies are installed")
    
    return passed == total

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)

```
Page 1/3FirstPrevNextLast