Skip to main content
Glama
README.md
# kayamcp ๐ŸŽจ๐Ÿค–

**LibreSprite Visual MCP Server** โ€” Real-time screen capture, visual UI interaction, and self-healing pixel art automation for [LibreSprite](https://libresprite.github.io/).

> An AI agent that can **see** your screen, **detect** LibreSprite's UI, **draw** alongside you in real-time, and **self-correct** any pixel errors automatically.

[![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://python.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
[![Tests: 71 passed](https://img.shields.io/badge/tests-71%20passed-brightgreen.svg)](#testing)

---

## โœจ Features

| Feature | Module | Description |
|---------|--------|-------------|
| ๐Ÿ–ฅ๏ธ **Ultra-Fast Screen Streaming** | `capture.py` | 10-60 FPS screen capture via MSS with pre-allocated buffers and zero-copy reads |
| ๐ŸŽฏ **Dynamic Calibration** | `calibration.py` | Auto-detects LibreSprite window and canvas boundaries using Win32 + OpenCV contour analysis |
| ๐Ÿ”ง **Visual UI Interaction** | `ui.py` | Selects tools (pencil, eraser, fillโ€ฆ), colours from the palette, and manages layers |
| โœ๏ธ **Cooperative Drawing** | `draw.py` | Moves cursor, executes stabilised strokes, and draws alongside the user in real-time |
| ๐Ÿ”„ **Self-Healing Loop** | `feedback.py` | Verifies drawn pixels post-stroke and auto-corrects mismatches with retry logic |
| ๐ŸŒ **MCP Server** | `mcp_server.py` | JSON-RPC 2.0 server (stdio/HTTP) implementing the Model Context Protocol |

---

## ๐Ÿš€ Quick Start

### Prerequisites

- **Windows 10/11** (uses Win32 API for desktop switching and window detection)
- **Python 3.10+**
- **LibreSprite** installed and running

### Installation

```bash
# Clone the repository
git clone https://github.com/YOUR_USERNAME/kayamcp.git
cd kayamcp

# Install with pip
pip install -e .

# Or with uv (recommended)
uv pip install -e .
```

### Run the MCP Server

```bash
# stdio mode (for MCP clients like Claude Desktop, Cursor, etc.)
python -m kayamcp

# HTTP mode (for development/testing)
python -m kayamcp --http --port 7799
```

---

## ๐Ÿ”Œ MCP Client Configuration

### Claude Desktop / Cursor

Add this to your MCP client configuration:

```json
{
  "mcpServers": {
    "kayamcp": {
      "command": "python",
      "args": ["-m", "kayamcp"],
      "cwd": "C:/path/to/kayamcp"
    }
  }
}
```

### Available MCP Tools

| Tool | Description |
|------|-------------|
| `kayamcp_capture_frame` | Capture current screen frame and return dimensions |
| `kayamcp_calibrate` | Re-calibrate LibreSprite window and canvas boundaries |
| `kayamcp_get_canvas_rect` | Get current canvas rectangle `{left, top, width, height}` |
| `kayamcp_select_tool` | Select a drawing tool (`pencil`, `eraser`, `fill`, `eyedropper`, etc.) |
| `kayamcp_select_color` | Select an RGB colour for drawing |
| `kayamcp_select_layer` | Select an existing layer by name |
| `kayamcp_add_layer` | Add a new layer |
| `kayamcp_draw_stroke` | Draw a stroke through canvas coordinate points |
| `kayamcp_stabilize_stroke` | Smooth points for jitter-free drawing |
| `kayamcp_verify_pixels` | Verify canvas pixels match expected colours |
| `kayamcp_heal_canvas` | Self-heal: verify + redraw mismatched pixels with retries |

---

## ๐Ÿ—๏ธ Architecture

```
kayamcp/
โ”œโ”€โ”€ __init__.py          # Package exports
โ”œโ”€โ”€ __main__.py          # python -m kayamcp entry point
โ”œโ”€โ”€ desktop.py           # Win32 desktop switching (Default desktop context)
โ”œโ”€โ”€ capture.py           # High-FPS screen streaming (MSS, threaded)
โ”œโ”€โ”€ calibration.py       # LibreSprite window & canvas detection (Win32 + OpenCV)
โ”œโ”€โ”€ ui.py                # Visual UI interaction (tools, palette, layers)
โ”œโ”€โ”€ draw.py              # Cooperative drawing & cursor control
โ”œโ”€โ”€ feedback.py          # Closed-loop pixel verification & self-healing
โ””โ”€โ”€ mcp_server.py        # JSON-RPC 2.0 MCP server (stdio + HTTP)
```

### Data Flow

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ MCP Client  โ”‚โ—„โ”€โ”€โ–บโ”‚  mcp_server  โ”‚โ—„โ”€โ”€โ–บโ”‚   capture    โ”‚
โ”‚ (AI Agent)  โ”‚    โ”‚  (JSON-RPC)  โ”‚    โ”‚ (Screen FPS) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                          โ”‚                    โ”‚
                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                   โ”‚  calibration โ”‚โ—„โ”€โ”€โ–บโ”‚   desktop    โ”‚
                   โ”‚ (Win32+CV2)  โ”‚    โ”‚ (Win32 API)  โ”‚
                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                          โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚           โ”‚           โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ”‚   ui     โ”‚ โ”‚  draw    โ”‚ โ”‚ feedback  โ”‚
       โ”‚ (tools,  โ”‚ โ”‚ (cursor, โ”‚ โ”‚ (verify,  โ”‚
       โ”‚  colors) โ”‚ โ”‚ strokes) โ”‚ โ”‚  heal)    โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

---

## ๐Ÿงช Testing

The project includes a comprehensive test suite with **71 tests** across 4 tiers:

```bash
# Run all tests
uv run pytest -v

# Run only the adversarial stress tests
uv run pytest tests/test_adversarial_streamer.py -v

# Run only E2E integration tests
uv run pytest tests/test_e2e.py -v
```

### Test Tiers

| Tier | Focus | Tests |
|------|-------|-------|
| **Tier 1** | Feature coverage (streaming, calibration, UI, drawing, healing) | 25 |
| **Tier 2** | Boundary & corner cases (FPS limits, invalid inputs, occlusion) | 25 |
| **Tier 3** | Cross-feature combinations (calibrate+draw, select+draw+heal) | 5 |
| **Tier 4** | Real-world scenarios (interactive painting, colour-by-number, zoom/pan) | 5 |
| **Tier 5** | Adversarial stress (handle leaks, memory leaks, CPU efficiency, thread cleanup) | 4 |
| | | **71 total** |

---

## ๐Ÿ“ฆ Packaging & Distribution

### Build a distributable package

```bash
# Build wheel and sdist
pip install build
python -m build

# The built files will be in dist/
#   dist/kayamcp-0.1.0-py3-none-any.whl
#   dist/kayamcp-0.1.0.tar.gz
```

### Publish to PyPI

```bash
pip install twine
twine upload dist/*
```

---

## ๐Ÿ”ง Dependencies

| Package | Version | Purpose |
|---------|---------|---------|
| `mss` | โ‰ฅ9.0.0 | High-performance screen capture |
| `opencv-python` | โ‰ฅ4.8.0 | Canvas detection via contour analysis |
| `pywin32` | โ‰ฅ306 | Win32 API (desktop switching, window enumeration) |
| `pydirectinput` | โ‰ฅ0.1.7 | Direct mouse/keyboard input |
| `numpy` | โ‰ฅ1.24.0 | Frame buffer operations |
| `pillow` | โ‰ฅ9.5.0 | Image format support |

---

## ๐Ÿค Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Run the test suite (`uv run pytest -v`)
4. Commit your changes (`git commit -m 'Add amazing feature'`)
5. Push to the branch (`git push origin feature/amazing-feature`)
6. Open a Pull Request

---

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---

## ๐Ÿ™ Acknowledgements

- [LibreSprite](https://libresprite.github.io/) โ€” Free and open-source pixel art editor
- [MSS](https://python-mss.readthedocs.io/) โ€” Ultra-fast cross-platform screen capture
- [Model Context Protocol](https://modelcontextprotocol.io/) โ€” Open protocol for AI tool integration