Skip to main content

Model Context Protocol server for Statsig client SDK

Project description

Statsig MCP Server

Model Context Protocol server for Statsig feature flags and experimentation platform.

This MCP server enables AI assistants to interact with Statsig's feature management and experimentation platform, allowing them to check feature flags, retrieve dynamic configurations, get experiment assignments, and log custom events.

Features

  • 🚩 Feature Gate Checking: Determine if features are enabled for users
  • ⚙️ Dynamic Configuration: Retrieve configuration values based on user context
  • 🧪 Experiment Assignment: Get experiment variations for A/B testing
  • 📊 Layer Parameter Access: Retrieve values from Statsig layers
  • 📈 Event Logging: Track custom events for analytics

Installation

Using uv (Recommended)

# Clone the repository
git clone https://github.com/GeLi2001/statsig-mcp.git
cd statsig-mcp

# Install dependencies with uv
uv sync

# Install with dev dependencies
uv sync --extra dev

Using pip

# Install from source
pip install -e .

# Or install with dev dependencies
pip install -e ".[dev]"

Quick Start

1. Run the Server

With uv (Recommended):

# Using command-line arguments (MCP best practice)
uv run -m statsig_mcp --api-key "your-secret-key"

# With additional options
uv run -m statsig_mcp \
  --api-key "your-secret-key" \
  --environment "production" \
  --api-timeout 5000 \
  --debug

# Or with environment variables
STATSIG_SERVER_SECRET_KEY="your-key" uv run -m statsig_mcp

With Python:

# Using flags (recommended)
python -m statsig_mcp --api-key "your-secret-key"

# Or environment variables
export STATSIG_SERVER_SECRET_KEY="your-secret-key"
python -m statsig_mcp

2. Configuration Options

The server supports both command-line arguments (recommended for MCP) and environment variables:

Command-Line Arguments (MCP Best Practice)

uv run -m statsig_mcp --help
Argument Type Default Description
--api-key string None Statsig server secret key (required)
--environment string development Environment tier
--api-timeout int 3000 API timeout in milliseconds
--disable-logging flag false Disable event logging to Statsig
--debug flag false Enable debug logging

Environment Variables (Fallback)

Variable Description
STATSIG_SERVER_SECRET_KEY Statsig server secret key
STATSIG_ENVIRONMENT Environment tier
STATSIG_API_TIMEOUT API timeout in milliseconds
STATSIG_DISABLE_LOGGING Disable logging (true/false)
STATSIG_DEBUG Enable debug mode (true/false)

3. MCP Client Configuration

Claude Desktop

Add to your claude_desktop_config.json:

{
  "mcpServers": {
    "statsig": {
      "command": "uv",
      "args": [
        "run",
        "-m",
        "statsig_mcp",
        "--api-key",
        "your-secret-key",
        "--environment",
        "production"
      ]
    }
  }
}

Alternative with Environment Variables

{
  "mcpServers": {
    "statsig": {
      "command": "uv",
      "args": ["run", "-m", "statsig_mcp"],
      "env": {
        "STATSIG_SERVER_SECRET_KEY": "your-secret-key",
        "STATSIG_ENVIRONMENT": "production"
      }
    }
  }
}

Available Tools

1. check_feature_gate

Check if a feature gate is enabled for a user.

Parameters:

  • user_id (string): User identifier
  • gate_name (string): Name of the feature gate
  • user_email (string, optional): User email
  • user_country (string, optional): User country code
  • custom_attributes (object, optional): Custom user attributes

Example:

{
  "user_id": "user123",
  "gate_name": "new_checkout_flow",
  "user_email": "user@example.com",
  "custom_attributes": { "plan": "premium" }
}

2. get_dynamic_config

Get dynamic configuration values for a user.

Parameters:

  • user_id (string): User identifier
  • config_name (string): Name of the dynamic config
  • user_email (string, optional): User email
  • user_country (string, optional): User country code
  • custom_attributes (object, optional): Custom user attributes

3. get_experiment

Get experiment assignment for a user.

Parameters:

  • user_id (string): User identifier
  • experiment_name (string): Name of the experiment
  • user_email (string, optional): User email
  • user_country (string, optional): User country code
  • custom_attributes (object, optional): Custom user attributes

4. get_layer

Get layer parameter values for a user.

Parameters:

  • user_id (string): User identifier
  • layer_name (string): Name of the layer
  • user_email (string, optional): User email
  • user_country (string, optional): User country code
  • custom_attributes (object, optional): Custom user attributes

5. log_event

Log a custom event.

Parameters:

  • user_id (string): User identifier
  • event_name (string): Name of the event
  • value (string|number, optional): Event value
  • metadata (object, optional): Event metadata
  • user_email (string, optional): User email
  • user_country (string, optional): User country code
  • custom_attributes (object, optional): Custom user attributes

Example MCP Client Usage

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

# Create server parameters with flags (recommended)
server_params = StdioServerParameters(
    command="uv",
    args=[
        "run", "-m", "statsig_mcp",
        "--api-key", "your-secret-key",
        "--environment", "production",
        "--debug"
    ]
)

async def main():
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()

            # Check a feature gate
            result = await session.call_tool(
                "check_feature_gate",
                arguments={
                    "user_id": "user123",
                    "gate_name": "new_feature",
                    "custom_attributes": {"plan": "premium"}
                }
            )
            print(f"Feature gate result: {result}")

            # Get dynamic config
            config = await session.call_tool(
                "get_dynamic_config",
                arguments={
                    "user_id": "user123",
                    "config_name": "ui_config"
                }
            )
            print(f"Config: {config}")

            # Log an event
            await session.call_tool(
                "log_event",
                arguments={
                    "user_id": "user123",
                    "event_name": "button_click",
                    "metadata": {"button": "checkout"}
                }
            )

Development

Setup Development Environment

Using uv (Recommended):

# Clone and setup
git clone https://github.com/GeLi2001/statsig-mcp.git
cd statsig-mcp

# Install all dependencies including dev tools
uv sync --extra dev

# Run tests
uv run pytest

# Format code
uv run black .

# Type checking
uv run mypy .

# Linting
uv run ruff check .

Using pip:

# Install in development mode with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Format code
black .

# Type checking
mypy .

Running Commands

With uv, you can run any command in the virtual environment:

# Run the server with flags
uv run -m statsig_mcp --api-key "test-key" --debug

# Run tests with verbose output
uv run pytest -v

# Run validation script
uv run python validate.py

# Format and lint
uv run black .
uv run ruff check --fix .

Project Structure

statsig_mcp/
├── __init__.py
├── __main__.py        # Module entry point
├── server.py          # Main MCP server implementation
├── statsig_client.py  # Statsig client wrapper
└── types.py           # Type definitions

tests/                 # Test suite
├── test_server.py

pyproject.toml         # Project configuration
uv.lock               # Locked dependencies (uv)
.venv/                # Virtual environment (uv managed)

Security Notes

  • Never expose your Statsig server secret key in client-side code
  • Use command-line arguments or environment variables for API keys
  • The server secret key provides full access to your Statsig project
  • Consider using separate API keys for different environments

Requirements

  • Python 3.10+
  • uv (recommended) or pip for package management

Troubleshooting

Common Issues

  1. "Statsig not initialized" error: Ensure --api-key is provided or STATSIG_SERVER_SECRET_KEY is set
  2. Network timeouts: Increase timeout with --api-timeout 5000
  3. Feature gate not found: Verify the gate name exists in your Statsig console
  4. Python version error: This package requires Python 3.10+ (MCP requirement)

Debug Mode

Enable debug logging for troubleshooting:

# With command-line flag (recommended)
uv run -m statsig_mcp --api-key "your-key" --debug

# With environment variable
STATSIG_DEBUG=true uv run -m statsig_mcp --api-key "your-key"

Help

Get help with command-line options:

uv run -m statsig_mcp --help

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Run tests and linting: uv run pytest && uv run ruff check
  6. Submit a pull request

Resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

statsig_mcp-1.0.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

statsig_mcp-1.0.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file statsig_mcp-1.0.0.tar.gz.

File metadata

  • Download URL: statsig_mcp-1.0.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.4

File hashes

Hashes for statsig_mcp-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2f7d82185b75da1ee4bed7ca3cdbf0a3d525846542b8328da2a109ac43970f5e
MD5 e455c69ca89572076232aef62f85e215
BLAKE2b-256 eb52dbd64c149a629d36654734137004c955b7386e2403b6e9a339eb26e3a934

See more details on using hashes here.

File details

Details for the file statsig_mcp-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: statsig_mcp-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.4

File hashes

Hashes for statsig_mcp-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 94aa2e68ed51352f72c6b735f23054b07615ff62368ea307b43ca7460d0cadb2
MD5 931ec547e8d07e46e11ae39d8de2f2e3
BLAKE2b-256 3c263e18ad5263c6eb54736f7209301172a0f2af433b6abc20aceeab880a962a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page