P2 cookbook MCP

Top-level Files of tip
Login

Top-level Files of tip

Files in the top-level directory from the latest check-in


P2-cookbook • P2 Spin2 MCP Server

Production-ready Model Context Protocol (MCP) server for the Parallax Propeller 2 (P2) microcontroller and Spin2/PASM2 languages.

Enables AI agents (Grok, Claude, Cursor, etc.) to maximally exploit the P2's unique architecture:

This MCP turns agents into expert P2 hardware architects, low-level optimizers, and multi-cog system designers.

All version control is Fossil SCM only (see fossil-scm-mcp). Never git.

Features (as powerful as possible)

Spin2 & PASM2 Mastery

XBYTE Advanced Exploitation

Multi-Cog Reasoning Engine

Memory Architecture Expertise

CORDIC Solver (Pipelined, Shared)

Smart Pins (64 pins, 34+ modes)

Cooperative Multitasking, Interrupts, Streamer, Video

Build, Compile, Hardware, Debug Orchestration

Novel Paradigms Promoted

Rich Context Always Available

Supports sync + streaming responses for long ops (compiles, sims, live debug).

Project Structure

P2-cookbook/
├── .fossil-settings/
│   └── ignore-glob          # versioned, P2/Spin2/Python/Node aware
├── .grok/
│   ├── config.toml          # project MCPs (fossil-scm-mcp + future)
│   └── skills/
│       ├── fossil-agent/    # strict Fossil VC discipline (local highest prio)
│       └── README.md
├── p2_mcp/                  # The MCP server package
│   ├── __init__.py
│   ├── __main__.py
│   └── server.py            # FastMCP + all @tool / @resource (P2 power tools)
├── templates/               # Spin2/PASM2 project & file templates
│   ├── p2-edge-multi-cog/
│   ├── xbyte-vm/
│   ├── cordic-dsp/
│   ├── smartpin-periph/
│   └── ...
├── examples/                # Ready-to-run demo objects + host scripts
│   ├── hello_8cogs.spin2
│   ├── xbyte_demo.spin2
│   ├── cordic_vector.spin2
│   ├── multicore_blink_pwm.spin2
│   ├── client_demo.py       # example MCP client usage
│   └── ...
├── docs/
│   ├── ARCHITECTURE.md
│   ├── BEST_PRACTICES.md
│   ├── XBYTE_GUIDE.md
│   ├── MULTI_COG_PATTERNS.md
│   ├── HARDWARE_SETUP.md     # P2 Edge, USB, power, multiple boards
│   └── ...
├── tests/
│   └── integration_test.py  # like fossil-scm-mcp (spawns server, JSON-RPC)
├── bootstrap.sh             # uv/venv + editable install (agent friendly)
├── pyproject.toml
├── README.md
└── LICENSE

The layout supports both:

Installation & Running the P2 Spin2 MCP (Fossil way)

Prerequisites

Exact agent-friendly bootstrap (from public Fossil remote)

# 1. Clone with Fossil (never git)
fossil clone https://refaqtor@refaqtory.net/P2-cookbook /tmp/p2-cookbook.fossil

# 2. Open checkout
mkdir -p ~/agent-work/p2-cookbook
cd ~/agent-work/p2-cookbook
fossil open /tmp/p2-cookbook.fossil

# 3. Bootstrap (creates .venv, installs editable; .venv is ignored)
./bootstrap.sh

# 4. Run the MCP server (stdio)
.venv/bin/python -m p2_mcp
# or
.venv/bin/p2-mcp

The server will say it's starting. Configure your MCP host (Grok, Claude Desktop, Cursor, etc.) to launch it.

Example config (Grok ~/.grok/config.toml or project .grok/config.toml)

[mcp_servers.p2-mcp]
command = "/home/ben/agent-work/p2-cookbook/.venv/bin/p2-mcp"
enabled = true
startup_timeout_sec = 20
tool_timeout_sec = 180   # long compiles/sims/debug

For uv (hermetic, recommended):

[mcp_servers.p2-mcp]
command = "uv"
args = ["run", "--with-editable", ".", "--python", "3.12", "python", "-m", "p2_mcp"]
cwd = "/path/to/your/p2-cookbook/checkout"
enabled = true

Claude / Cursor / other hosts: similar stdio command pointing at the console script or python -m inside the checkout venv.

Once connected, the agent immediately has 20+ powerful P2-specific tools + resources.

Usage from an Agent

Typical flow an agent will follow (the server guides this):

  1. Use p2_info or resources for architecture refreshers.
  2. p2_scaffold_project for new P2 Edge multi-cog app.
  3. spin2_generate / pasm2_optimize / xbyte_create for code.
  4. cordic_generate, smartpin_configure for accel/periph.
  5. multicog_simulate or multicog_allocate for 8-cog design.
  6. pnut_compile (with listing), pnut_list_devices, pnut_download (single or parallel).
  7. p2_debug_session or live terminal orchestration.
  8. Profile suggestions via p2_optimize_suggest.

All tools return LLM-optimized structured text/JSON. Many accept stream for long-running.

Resources (always available context):

P2 Edge Hardware Setup (Quick)

See docs/HARDWARE_SETUP.md for full.

Example one-liner after scaffold:

pnut-term-ts -l                 # list ports
pnut_ts -l myproject.spin2
pnut-term-ts -p /dev/ttyUSB0 -d myproject.bin -t

The MCP automates all of this + multi-board + error recovery.

Example Client (Python)

See examples/client_demo.py . Rough:

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

async with stdio_client(StdioServerParameters(command=".venv/bin/p2-mcp")) as (r, w):
    async with ClientSession(r, w) as session:
        await session.initialize()
        tools = await session.list_tools()
        result = await session.call_tool("spin2_generate", {"spec": "8-cog blink with lock"})
        print(result)

Development of the MCP Server Itself

./bootstrap.sh
.venv/bin/python -m p2_mcp
# or for editable dev loop
uv run python -m p2_mcp

Run integration test:

.venv/bin/python tests/integration_test.py

Fossil Workflow (Mandatory)

This entire history (including how every tool and template was created) lives in Fossil.

The final output of this project includes the exact commands showing fossil status, fossil timeline -n 30, fossil tag list.

Status & Roadmap

Current: Full skeleton + core server + rich P2 exploitation tools + build/hardware + scaffolding + demos + docs. Production quality, heavily tested patterns from p2kb.

See Fossil timeline for the complete build log (technotes + commits tell the story).

Contributions via Fossil: clone, branch (e.g. fossil branch new feature/my-xbyte), commit, push (or bundle).

License

MIT (matching fossil-scm-mcp spirit). Parallax/Spin2/PASM2 IP respected per their licenses.

Acknowledgments


Start immediately with agents: clone via Fossil, bootstrap, connect the MCP, and ask it to "design an 8-cog XBYTE-based Forth VM with CORDIC accel and Smart Pin serial console for P2 Edge".

It will know exactly what to do.