Skip to main content

Mathematical optimization MCP server with PuLP and OR-Tools support

Project description

MCP Optimizer

๐Ÿš€ Mathematical Optimization MCP Server with PuLP and OR-Tools support

Tests Coverage Python License

๐Ÿš€ Quick Start

Integration with LLM Clients

Claude Desktop Integration

Option 1: Using uvx (Recommended)

  1. Install Claude Desktop from claude.ai
  2. Open Claude Desktop โ†’ Settings โ†’ Developer โ†’ Edit Config
  3. Add to your claude_desktop_config.json:
{
  "mcpServers": {
    "mcp-optimizer": {
      "command": "uvx",
      "args": ["mcp-optimizer"]
    }
  }
}
  1. Restart Claude Desktop and look for the ๐Ÿ”จ tools icon

Option 2: Using pip

pip install mcp-optimizer

Then add to your Claude Desktop config:

{
  "mcpServers": {
    "mcp-optimizer": {
      "command": "mcp-optimizer"
    }
  }
}

Option 3: Using Docker

Method A: Docker with STDIO transport (Recommended for MCP clients)

docker pull ghcr.io/dmitryanchikov/mcp-optimizer:latest

Then add to your Claude Desktop config:

{
  "mcpServers": {
    "mcp-optimizer": {
      "command": "docker",
      "args": [
        "run", "--rm", "-i",
        "ghcr.io/dmitryanchikov/mcp-optimizer:latest",
        "python", "main.py"
      ]
    }
  }
}

Method B: Docker with SSE transport (for HTTP/web clients)

# Run SSE server on port 8000
docker run -d -p 8000:8000 ghcr.io/dmitryanchikov/mcp-optimizer:latest \
  python -m mcp_optimizer.main --transport sse --host 0.0.0.0

# Or with custom port
docker run -d -p 9000:9000 ghcr.io/dmitryanchikov/mcp-optimizer:latest \
  python -m mcp_optimizer.main --transport sse --host 0.0.0.0 --port 9000

Then use HTTP client to connect to http://localhost:8000 (requires additional MCP HTTP client setup)

Cursor Integration

  1. Install the MCP extension in Cursor
  2. Add mcp-optimizer to your workspace settings:
{
  "mcp.servers": {
    "mcp-optimizer": {
      "command": "uvx",
      "args": ["mcp-optimizer"]
    }
  }
}

Other LLM Clients

For other MCP-compatible clients (Continue, Cody, etc.), use similar configuration patterns with the appropriate command for your installation method.

Advanced Installation Options

Local Development

# Clone the repository
git clone https://github.com/dmitryanchikov/mcp-optimizer.git
cd mcp-optimizer

# Install dependencies with uv
uv sync --extra dev

# Run the server
uv run python main.py

Local Package Build and Run

For testing and development, you can build the package locally and run it with uvx:

# Build the package locally
uv build

# Run with uvx from local wheel file
uvx --from ./dist/mcp_optimizer-0.3.9-py3-none-any.whl mcp-optimizer

# Or run with help to see available options
uvx --from ./dist/mcp_optimizer-0.3.9-py3-none-any.whl mcp-optimizer --help

# Test the local package with a simple MCP message
echo '{"jsonrpc": "2.0", "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test", "version": "1.0"}}, "id": 1}' | uvx --from ./dist/mcp_optimizer-0.3.9-py3-none-any.whl mcp-optimizer

Note: The local build creates both wheel (.whl) and source distribution (.tar.gz) files in the dist/ directory. The wheel file is recommended for uvx installation as it's faster and doesn't require compilation.

Troubleshooting: If you encounter event loop issues when using uvx, the package includes automatic detection and handling of existing event loops using nest-asyncio.

Docker with Custom Configuration

# Build locally with optimization
git clone https://github.com/dmitryanchikov/mcp-optimizer.git
cd mcp-optimizer
docker build -t mcp-optimizer:optimized .
docker run -p 8000:8000 mcp-optimizer:optimized

# Check optimized image size (398MB vs 1.03GB original - 61% reduction!)
docker images mcp-optimizer:optimized

# Test the optimized image
./scripts/test_docker_optimization.sh

Standalone Server Commands

# Run directly with uvx (no installation needed)
uvx mcp-optimizer

# Or run specific commands
uvx mcp-optimizer --help

# With pip installation
mcp-optimizer

# Or run with Python module (use main.py for stdio mode)
python main.py

Transport Modes

MCP Optimizer supports two transport protocols:

  • STDIO: Standard input/output for direct MCP client integration (Claude Desktop, Cursor, etc.)
  • SSE: Server-Sent Events over HTTP for web-based clients and custom integrations

STDIO Transport (Default - for MCP clients like Claude Desktop)

# Default STDIO mode for MCP protocol
uvx mcp-optimizer
# or
uvx mcp-optimizer --transport stdio
# or
uv run python -m mcp_optimizer.main --transport stdio
# or
python main.py

SSE Transport (for HTTP/web clients)

# SSE mode for HTTP clients (default port 8000)
uvx mcp-optimizer --transport sse
# or
uv run python -m mcp_optimizer.main --transport sse

# Custom host and port
uvx mcp-optimizer --transport sse --host 0.0.0.0 --port 9000
# or
uv run python -m mcp_optimizer.main --transport sse --host 0.0.0.0 --port 9000

# With debug mode
uvx mcp-optimizer --transport sse --debug --log-level DEBUG

Available CLI Options

# Show all available options
uvx mcp-optimizer --help

# Options:
#   --transport {stdio,sse}    MCP transport protocol (default: stdio)
#   --port PORT               Port for SSE transport (default: 8000)
#   --host HOST               Host for SSE transport (default: 127.0.0.1)
#   --debug                   Enable debug mode
#   --reload                  Enable auto-reload for development
#   --log-level {DEBUG,INFO,WARNING,ERROR}  Logging level (default: INFO)

๐ŸŽฏ Features

Supported Optimization Problem Types:

  • Linear Programming - Maximize/minimize linear objective functions
  • Assignment Problems - Optimal resource allocation using Hungarian algorithm
  • Transportation Problems - Logistics and supply chain optimization
  • Knapsack Problems - Optimal item selection (0-1, bounded, unbounded)
  • Routing Problems - TSP and VRP with time windows
  • Scheduling Problems - Job and shift scheduling
  • Integer Programming - Discrete optimization problems
  • Financial Optimization - Portfolio optimization and risk management
  • Production Planning - Multi-period production planning

Testing

# Run simple functionality tests
uv run python tests/test_integration/comprehensive_test.py

# Run comprehensive integration tests
uv run python tests/test_integration/comprehensive_test.py

# Run all unit tests
uv run pytest tests/ -v

# Run with coverage
uv run pytest tests/ --cov=src/mcp_optimizer --cov-report=html

๐Ÿ“Š Usage Examples

Linear Programming

from mcp_optimizer.tools.linear_programming import solve_linear_program

# Maximize 3x + 2y subject to:
# x + y <= 4
# 2x + y <= 6
# x, y >= 0

objective = {"sense": "maximize", "coefficients": {"x": 3, "y": 2}}
variables = {
    "x": {"type": "continuous", "lower": 0},
    "y": {"type": "continuous", "lower": 0}
}
constraints = [
    {"expression": {"x": 1, "y": 1}, "operator": "<=", "rhs": 4},
    {"expression": {"x": 2, "y": 1}, "operator": "<=", "rhs": 6}
]

result = solve_linear_program(objective, variables, constraints)
# Result: x=2.0, y=2.0, objective=10.0

Assignment Problem

from mcp_optimizer.tools.assignment import solve_assignment_problem

workers = ["Alice", "Bob", "Charlie"]
tasks = ["Task1", "Task2", "Task3"]
costs = [
    [4, 1, 3],  # Alice's costs for each task
    [2, 0, 5],  # Bob's costs for each task
    [3, 2, 2]   # Charlie's costs for each task
]

result = solve_assignment_problem(workers, tasks, costs)
# Result: Total cost = 5.0 with optimal assignments

Knapsack Problem

from mcp_optimizer.tools.knapsack import solve_knapsack_problem

items = [
    {"name": "Item1", "weight": 10, "value": 60},
    {"name": "Item2", "weight": 20, "value": 100},
    {"name": "Item3", "weight": 30, "value": 120}
]

result = solve_knapsack_problem(items, capacity=50)
# Result: Total value = 220.0 with optimal item selection

Portfolio Optimization

from mcp_optimizer.tools.financial import optimize_portfolio

assets = [
    {"name": "Stock A", "expected_return": 0.12, "risk": 0.18},
    {"name": "Stock B", "expected_return": 0.10, "risk": 0.15},
    {"name": "Bond C", "expected_return": 0.06, "risk": 0.08}
]

result = optimize_portfolio(
    assets=assets,
    objective="minimize_risk",
    budget=10000,
    risk_tolerance=0.15
)
# Result: Optimal portfolio allocation with minimized risk

๐Ÿ—๏ธ Architecture

mcp-optimizer/
โ”œโ”€โ”€ src/mcp_optimizer/
โ”‚   โ”œโ”€โ”€ tools/           # 9 categories of optimization tools
โ”‚   โ”‚   โ”œโ”€โ”€ linear_programming.py
โ”‚   โ”‚   โ”œโ”€โ”€ assignment.py
โ”‚   โ”‚   โ”œโ”€โ”€ knapsack.py
โ”‚   โ”‚   โ”œโ”€โ”€ routing.py
โ”‚   โ”‚   โ”œโ”€โ”€ scheduling.py
โ”‚   โ”‚   โ”œโ”€โ”€ financial.py
โ”‚   โ”‚   โ””โ”€โ”€ production.py
โ”‚   โ”œโ”€โ”€ solvers/         # PuLP and OR-Tools integration
โ”‚   โ”‚   โ”œโ”€โ”€ pulp_solver.py
โ”‚   โ”‚   โ””โ”€โ”€ ortools_solver.py
โ”‚   โ”œโ”€โ”€ schemas/         # Pydantic validation schemas
โ”‚   โ”œโ”€โ”€ utils/           # Utility functions
โ”‚   โ”œโ”€โ”€ config.py        # Configuration
โ”‚   โ””โ”€โ”€ mcp_server.py    # Main MCP server
โ”œโ”€โ”€ tests/               # Comprehensive test suite
โ”œโ”€โ”€ docs/                # Documentation
โ”œโ”€โ”€ k8s/                 # Kubernetes deployment
โ”œโ”€โ”€ monitoring/          # Grafana/Prometheus setup
โ””โ”€โ”€ main.py             # Entry point

๐Ÿงช Test Results

โœ… Comprehensive Test Suite

๐Ÿงช Starting Comprehensive MCP Optimizer Tests
==================================================
โœ… Server Health PASSED
โœ… Linear Programming PASSED
โœ… Assignment Problems PASSED  
โœ… Knapsack Problems PASSED
โœ… Routing Problems PASSED
โœ… Scheduling Problems PASSED
โœ… Financial Optimization PASSED
โœ… Production Planning PASSED
โœ… Performance Test PASSED

๐Ÿ“Š Test Results: 9 passed, 0 failed
๐ŸŽ‰ All tests passed! MCP Optimizer is ready for production!

โœ… Unit Tests

  • 66 tests passed, 9 skipped
  • Execution time: 0.45 seconds
  • All core components functional

๐Ÿ“ˆ Performance Metrics

  • Linear Programming: ~0.01s
  • Assignment Problems: ~0.01s
  • Knapsack Problems: ~0.01s
  • Complex test suite: 0.02s for 3 optimization problems
  • Overall performance: ๐Ÿš€ Excellent!

๐Ÿ”ง Technical Details

Core Solvers

  • OR-Tools: For assignment, transportation, knapsack problems
  • PuLP: For linear/integer programming
  • FastMCP: For MCP server integration

Supported Solvers

  • CBC, GLPK, GUROBI, CPLEX (via PuLP)
  • SCIP, CP-SAT (via OR-Tools)

Key Features

  • โœ… Full MCP protocol integration
  • โœ… Comprehensive input validation
  • โœ… Robust error handling
  • โœ… High-performance optimization
  • โœ… Production-ready architecture
  • โœ… Extensive test coverage
  • โœ… Docker and Kubernetes support

๐Ÿ“‹ Requirements

  • Python 3.11+
  • uv (for dependency management)
  • OR-Tools (automatically installed)
  • PuLP (automatically installed)

๐Ÿš€ Production Deployment

Docker

# Build image
docker build -t mcp-optimizer .

# Run container
docker run -p 8000:8000 mcp-optimizer

Kubernetes

# Deploy to Kubernetes
kubectl apply -f k8s/

Monitoring

# Start monitoring stack
docker-compose up -d

๐ŸŽฏ Project Status

โœ… PRODUCTION READY ๐Ÿš€

  • All core optimization tools implemented and tested
  • MCP server fully functional
  • Comprehensive test coverage (66 unit tests + 9 integration tests)
  • OR-Tools integration confirmed working
  • Performance optimized (< 30s for complex test suites)
  • Ready for production deployment

๐Ÿ“– Usage Examples

The examples/ directory contains practical examples and prompts for using MCP Optimizer with Large Language Models (LLMs):

Available Examples

  • ๐Ÿ“Š Linear Programming (RU | EN)
    • Production optimization, diet planning, transportation, blending problems
  • ๐Ÿ‘ฅ Assignment Problems (RU | EN)
    • Employee-project assignment, machine-order allocation, task distribution
  • ๐Ÿ’ฐ Portfolio Optimization (RU | EN)
    • Investment portfolios, retirement planning, risk management

How to Use Examples

  1. For LLM Integration: Copy the prompt text and provide it to your LLM with MCP Optimizer access
  2. For Direct API Usage: Use the provided API structures directly with MCP Optimizer functions
  3. For Learning: Understand different optimization problem types and formulations

Each example includes:

  • Problem descriptions and real-world scenarios
  • Ready-to-use prompts for LLMs
  • Technical API structures
  • Common activation phrases
  • Practical applications

๐Ÿ”„ Recent Updates

Latest Release Features:

  1. Function Exports - Added exportable functions to all tool modules:

    • solve_linear_program() in linear_programming.py
    • solve_assignment_problem() in assignment.py
    • solve_knapsack_problem() in knapsack.py
    • optimize_portfolio() in financial.py
    • optimize_production() in production.py
  2. Enhanced Testing - Updated comprehensive test suite with correct function signatures

  3. OR-Tools Integration - Confirmed full functionality of all OR-Tools components

๐Ÿš€ Fully Automated Release Process

New Simplified Git Flow (3 steps!)

The project uses a fully automated release process:

1. Create Release Branch

# For minor release (auto-increment)
uv run python scripts/release.py --type minor

# For specific version
uv run python scripts/release.py 0.2.0

# For hotfix
uv run python scripts/release.py --hotfix --type patch

# Preview changes
uv run python scripts/release.py --type minor --dry-run

2. Create PR to main

# Create PR: release/v0.3.0 โ†’ main
gh pr create --base main --head release/v0.3.0 --title "Release v0.3.0"

3. Merge PR - DONE! ๐ŸŽ‰

After PR merge, automatically happens:

  • โœ… Create tag v0.3.0
  • โœ… Publish to PyPI
  • โœ… Publish Docker images
  • โœ… Create GitHub Release
  • โœ… Merge main back to develop
  • โœ… Cleanup release branch

NO NEED to run finalize_release.py manually anymore!

๐Ÿ”’ Secure Detection: Uses hybrid approach combining GitHub branch protection with automated release detection. See Release Process for details.

Automated Release Pipeline

The CI/CD pipeline automatically handles:

  • โœ… Release Candidates: Built from release/* branches
  • โœ… Production Releases: Triggered by version tags on main
  • โœ… PyPI Publishing: Automatic on tag creation
  • โœ… Docker Images: Multi-architecture builds
  • โœ… GitHub Releases: With artifacts and release notes

CI/CD Pipeline

The GitHub Actions workflow automatically:

  • โœ… Runs tests on Python 3.11 and 3.12
  • โœ… Performs security scanning
  • โœ… Builds and pushes Docker images
  • โœ… Publishes to PyPI on tag creation
  • โœ… Creates GitHub releases

Requirements for PyPI Publication

  • Set PYPI_API_TOKEN secret in GitHub repository
  • Ensure all tests pass
  • Follow semantic versioning

๐Ÿ› ๏ธ Development Tools

Debug Tools

Use the debug script to inspect MCP server structure:

# Run debug tools to check server structure
uv run python scripts/debug_tools.py

# This will show:
# - Available MCP tools
# - Tool types and attributes
# - Server configuration

Comprehensive Testing

Run the full integration test suite:

# Run comprehensive tests
uv run python tests/test_integration/comprehensive_test.py

# This tests:
# - All optimization tools (9 categories)
# - Server health and functionality
# - Performance benchmarks
# - End-to-end workflows

Docker Build Instructions

Image Details

  • Base: Python 3.12 Slim (Debian-based)
  • Size: ~649MB (optimized with multi-stage builds)
  • Architecture: Multi-platform support (x86_64, ARM64)
  • Security: Non-root user, minimal dependencies
  • Performance: Optimized Python bytecode, cleaned build artifacts

Local Build Commands

# Standard build
docker build -t mcp-optimizer:latest .

# Build with development dependencies
docker build --build-arg ENV=development -t mcp-optimizer:dev .

# Build with cache mount for faster rebuilds
docker build --mount=type=cache,target=/build/.uv -t mcp-optimizer .

# Check image size
docker images mcp-optimizer

# Run container
docker run -p 8000:8000 mcp-optimizer:latest

# For development with volume mounting
docker run -p 8000:8000 -v $(pwd):/app mcp-optimizer:latest

# Test container functionality
docker run --rm mcp-optimizer:latest python -c "from mcp_optimizer.mcp_server import create_mcp_server; print('โœ… MCP Optimizer works!')"

๐Ÿค Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Git Flow Policy

This project follows a standard Git Flow workflow:

  • Feature branches โ†’ develop branch
  • Release branches โ†’ main branch
  • Hotfix branches โ†’ main and develop branches

๐Ÿ“š Documentation:

Development Setup

# Clone and setup
git clone https://github.com/dmitryanchikov/mcp-optimizer.git
cd mcp-optimizer

# Create feature branch from develop
git checkout develop
git checkout -b feature/your-feature-name

# Install dependencies
uv sync --extra dev

# Run tests
uv run pytest tests/ -v

# Run linting
uv run ruff check src/
uv run mypy src/

# Create PR to develop branch (not main!)

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • OR-Tools - Google's optimization tools
  • PuLP - Linear programming in Python
  • FastMCP - Fast MCP server implementation

๐Ÿ“ž Support


Made with โค๏ธ for the optimization community

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

mcp_optimizer-0.3.9.tar.gz (51.5 kB view details)

Uploaded Source

Built Distribution

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

mcp_optimizer-0.3.9-py3-none-any.whl (52.6 kB view details)

Uploaded Python 3

File details

Details for the file mcp_optimizer-0.3.9.tar.gz.

File metadata

  • Download URL: mcp_optimizer-0.3.9.tar.gz
  • Upload date:
  • Size: 51.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcp_optimizer-0.3.9.tar.gz
Algorithm Hash digest
SHA256 50ce98d30fa9ab2fbe5205156c9efecc61c6dd6288287d5858a72a3fa7f2cc31
MD5 7a8c42f42c176bd2e733375102fb42ae
BLAKE2b-256 3cbcf83a5c7437fce890ab57fcccdcc977c573c220c520eff3f9fe2056483994

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_optimizer-0.3.9.tar.gz:

Publisher: ci.yml on dmitryanchikov/mcp-optimizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mcp_optimizer-0.3.9-py3-none-any.whl.

File metadata

  • Download URL: mcp_optimizer-0.3.9-py3-none-any.whl
  • Upload date:
  • Size: 52.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcp_optimizer-0.3.9-py3-none-any.whl
Algorithm Hash digest
SHA256 2a514f78512de8a8e17d7c551577e5e9247f84a2a908f59672da952ffd655dbf
MD5 a20ef46d9fd57d2955c716e5028c343d
BLAKE2b-256 a13cf58b23abb2ad51db2cebc8633cf263795b3383a9834e3c6907d439e64e48

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_optimizer-0.3.9-py3-none-any.whl:

Publisher: ci.yml on dmitryanchikov/mcp-optimizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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