14 KiB
Spice86 MCP HTTP Server
Overview
Spice86 exposes a Model Context Protocol (MCP) server over HTTP, enabling structured programmatic control of the emulator. AI clients, automation scripts, and external tooling can use MCP to inspect, manipulate, and drive execution of DOS programs running in Spice86.
This server provides 65+ built-in tools covering:
- CPU state inspection (registers, flags, instruction pointer)
- Memory read/write/search/disassembly
- I/O port read/write
- Execution control (pause, resume, step, step-over)
- Breakpoint management (execution, memory read/write)
- Function discovery and CFG (Control Flow Graph) traversal
- Video state inspection and screenshot capture
- Sound device state (SoundBlaster, OPL, MIDI, PC Speaker)
- DOS structures (PSP, MCB, File handle table)
- EMS and XMS memory management
The HTTP transport is stateless by default to maximize compatibility with real-world AI clients that may skip session negotiation or reuse connection state unpredictably.
Endpoints
Video inspection and PNG captures use the last completed logical framebuffer. Mode 13h produces 320x200 pixels; a program can change VGA registers without changing the BIOS mode record (for example, 256x224 unchained graphics). Pixel and scanline doubling are presentation concerns, not duplicate pixels in the returned framebuffer. GUI graphics retain 4:3 aspect correction.
| Endpoint | Description |
|---|---|
http://localhost:<port>/mcp |
MCP protocol endpoint (JSON-RPC over HTTP) |
http://localhost:<port>/health |
Health check endpoint returning {"status":"ok"} |
Default port: 8081
CLI option: --mcp-http-port <port>
Quick Start
1. Start Spice86 with MCP enabled (enabled by default)
Spice86 -e program.exe --mcp-http-port 8081
2. Connect your MCP client
Point your MCP client to:
http://localhost:8081/mcp
3. Discover available tools
POST /mcp
Content-Type: application/json
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": { "name": "MyClient", "version": "1.0" }
}
}
Then call tools/list to enumerate all available tools, or call mcp_about for high-level capability metadata.
Protocol Behavior
- Transport: HTTP (stateless by default)
- Protocol: JSON-RPC 2.0
- Session management: The server does not issue
Mcp-Session-Idheaders by default (options.Stateless = true). This prevents 404 errors when AI clients reuse session IDs from fresh TCP connections or skip thenotifications/initializedhandshake. - Standard MCP flow:
initialize→tools/list→tools/call
Stateless mode is recommended for AI agents and automated workflows. If you need stateful sessions (e.g., for long-lived interactive clients), you can modify McpHttpHost.cs to set options.Stateless = false.
Built-in Tool Categories
Spice86 ships with 65+ MCP tools organized into these capability scopes:
CPU State & Execution Control
read_cpu_state: Read all general-purpose registers, segment registers, IP, flags, and cycle countpause: Pause emulationresume: Resume emulationstep: Execute one instruction and pausestep_over: Step over CALL instructions (run until RET)get_pause_status: Check if emulator is paused
Memory Operations
read_memory: Read a memory range (segment, offset, length) as hex byteswrite_memory: Write hex bytes to memorysearch_memory: Search for a hex pattern in conventional RAMread_disassembly: Disassemble instructions at a given address
I/O Ports
read_io_port: Read from an I/O portwrite_io_port: Write to an I/O port
Breakpoints
add_execution_breakpoint: Break when CS:IP reaches an addressadd_memory_read_breakpoint: Break on memory readadd_memory_write_breakpoint: Break on memory writeremove_execution_breakpointremove_memory_read_breakpointremove_memory_write_breakpointlist_execution_breakpointslist_memory_breakpoints
Functions & CFG
list_functions: List discovered functions sorted by call countread_cfg_cpu_graph: Dump the Control Flow Graph (CFG) built byCfgCpu
Video
read_video_state: Current video mode, resolution, text/graphics flag, cursor positionread_video_state_detailed: Full VGA register dumpvideo_set_mode: Change video moderead_vga_memory: Read VGA VRAMwrite_vga_memory: Write VGA VRAMvga_set_palette_entry: Set VGA palette colorcapture_screenshot: Save a screenshot to disk and return the path
Sound Devices
- SoundBlaster:
read_sound_blaster_state,sound_blaster_set_speaker,read_sound_blaster_dsp_version,sound_blaster_write_mixer_register, etc. - OPL (Adlib/SB OPL2/OPL3):
read_opl_state,opl_write_register - PC Speaker:
read_pc_speaker_state,pc_speaker_set_control - MIDI:
read_midi_state,midi_reset,midi_enter_uart_mode,midi_send_bytes
Input Automation
send_keyboard_key: Send a keystroke (press + release or hold)send_mouse_packet: Send raw mouse data packetsend_mouse_move: Move mouse cursorsend_mouse_button: Press/release mouse button
DOS & BIOS Structures
read_dos_psp: Read the DOS Program Segment Prefixread_dos_mcb_chain: Dump the DOS Memory Control Block chainread_dos_file_handle_table: List open file handlesread_dos_current_directory: Get current drive and directory pathread_bios_equipment_word: Read BIOS equipment flags
EMS & XMS
read_ems_state: EMS handle allocation, page frame mappingread_xms_state: XMS handle allocation, HMA usageread_ems_page_frame: Dump EMS page frame contentread_xms_block: Read an XMS memory block
Metadata & Diagnostics
mcp_about: High-level server metadata, capability scopes, extension points, tool count
For a complete tool list with parameter details, call tools/list via the MCP endpoint.
Tool Invocation & Auto-Pause
Most tools automatically pause the emulator before execution and resume after. This ensures consistent state during inspection and prevents race conditions. A few tools (marked with [McpManualControl]) skip auto-pause and require the client to explicitly call pause if needed.
Example: Reading CPU state
POST /mcp
Content-Type: application/json
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "read_cpu_state",
"arguments": {}
}
}
Response:
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"content": [
{
"type": "resource",
"resource": {
"uri": "data:application/json;base64,eyJFQVgiOjEyMywgIkVCWCI6NDU2LCAuLi59"
}
}
]
}
}
The structured content (JSON) is base64-encoded in the resource field.
Example: Writing memory
POST /mcp
{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "write_memory",
"arguments": {
"segment": 4096,
"offset": 256,
"data": "B80200CD21"
}
}
}
Extending MCP from User Projects
Spice86 supports external MCP tool registration. You can add project-specific tools for game-specific operations (e.g., reading player stats, manipulating inventory, triggering events).
Extension Model
- Implement
IMcpToolSupplierin your project to provide custom tool assemblies and injectable services. - Mark your tool classes with
[McpServerToolType]and individual methods with[McpServerTool(Name = "...")]. - Register your tools at startup by passing additional assemblies and services to
McpHttpHost.Start.
Extension Entry Points
| Method | Purpose |
|---|---|
IMcpToolSupplier.GetMcpToolAssemblies() |
Return additional assemblies containing [McpServerToolType] classes |
IMcpToolSupplier.GetMcpServices() |
Return injectable services used by custom tools |
McpHttpHost.Start(additionalToolAssemblies, additionalServices) |
Register external tools at server start |
Minimal Extension Example
using Spice86.Core.Emulator.Mcp;
using ModelContextProtocol.Server;
// 1. Implement IMcpToolSupplier in your override project
public class MyGameMcpToolSupplier : IMcpToolSupplier {
public IEnumerable<Assembly> GetMcpToolAssemblies() {
return [typeof(MyGameMcpTools).Assembly];
}
public IEnumerable<object> GetMcpServices() {
return [new MyGameContext(...)];
}
}
// 2. Define your custom tools
[McpServerToolType]
public sealed class MyGameMcpTools {
private readonly MyGameContext _context;
public MyGameMcpTools(MyGameContext context, EmulatorMcpServices emulatorServices) {
_context = context;
}
[McpServerTool(Name = "read_player_health", UseStructuredContent = true)]
public object ReadPlayerHealth() {
// Read from memory via _context or emulatorServices
int health = _context.GetPlayerHealth();
return new { Health = health, MaxHealth = 100 };
}
[McpServerTool(Name = "set_player_gold", UseStructuredContent = true)]
public object SetPlayerGold(int amount) {
_context.SetPlayerGold(amount);
return new { Success = true, Gold = amount };
}
}
Registering Custom Tools at Startup
Modify your startup wiring (typically in Spice86DependencyInjection.cs or a custom entry point):
IMcpToolSupplier supplier = new MyGameMcpToolSupplier();
McpHttpHost host = new(loggerService);
host.Start(
services: emulatorMcpServices,
port: configuration.McpHttpPort,
additionalToolAssemblies: supplier.GetMcpToolAssemblies(),
additionalServices: supplier.GetMcpServices()
);
Note: The default Spice86 startup does not load external tool assemblies. You must wire them explicitly if you want project-specific tools.
Practical Guidance for Extension Authors
Tool Design Best Practices
- Keep tools deterministic: Avoid relying on global mutable state outside the emulator.
- Prefer semantic tools: Expose high-level operations (e.g.,
read_player_inventory) instead of raw memory offsets. - Keep low-level tools available: Also provide
read_memory_at_player_inventory_addressfor diagnostics when the semantic tool breaks. - Return compact structured payloads: Avoid dumping large arrays unless necessary. Use pagination or limits.
- Add integration tests: Write real MCP
tools/calltests that verify your tools work end-to-end.
Debugging Extension Tools
- Check the MCP log:
logs/mcp.logcontains startup and invocation errors. - Verify tool registration: Call
tools/listand ensure your custom tools appear. - Test auto-pause behavior: If your tool accesses emulator state, ensure it pauses correctly or mark it with
[McpManualControl].
Common Use Cases
AI-Driven Reverse Engineering
An AI agent can:
- Call
read_cpu_stateto see where the program is stuck. - Call
read_disassemblyto inspect the next 10 instructions. - Call
search_memoryto find a string or data pattern. - Call
add_execution_breakpointto pause at a suspect function. - Call
list_functionsto see which functions are called most often. - Call
capture_screenshotto see the current video output.
Automated Testing
A test script can:
- Call
pauseto halt execution. - Call
write_memoryto inject test data. - Call
resumeand wait for a breakpoint. - Call
read_dos_file_handle_tableto verify the program opened the expected file. - Call
capture_screenshotand compare against a baseline image.
Game Trainer / Cheat Tool
A trainer tool can:
- Call
read_dos_pspto locate the game's data segment. - Call
search_memoryto find the player's health value. - Call
write_memoryto set health to max. - Call
add_memory_write_breakpointto detect when the game decrements health.
Live Debugging Dashboard
A web dashboard can:
- Poll
read_cpu_stateevery 500ms to display registers. - Call
read_video_stateto show current video mode. - Call
read_sound_blaster_stateto visualize audio channels. - Call
list_functionsto show a live call-count heatmap.
Logs & Troubleshooting
- MCP server log:
logs/mcp.log(warning level by default) - Emulator logs: Console or file (controlled by
--VerboseLogs,--WarningLogs,--SilencedLogs) - Health check:
GET http://localhost:8081/healthshould return{"status":"ok","service":"Spice86 MCP Server"}
Common issues:
| Problem | Solution |
|---|---|
Client gets 404 on /mcp |
Check that --mcp-http-port is set and the server started successfully |
| Tools return "Tool disabled" error | Some tools may be disabled if the emulator is not in the expected state (e.g., video tools when no VGA card is initialized) |
| Tools time out | If emulator is not responding to pause requests, ensure the emulation loop is running and not deadlocked |
| Client skips session ID and gets 404 | Ensure stateless mode is enabled (default); if using stateful mode, ensure client sends Mcp-Session-Id header |
Reference
Related Files
Spice86.Core/Emulator/Mcp/McpHttpHost.cs- HTTP server setup and lifecycleSpice86.Core/Emulator/Mcp/EmulatorMcpTools.cs- Built-in tool implementationsSpice86.Core/Emulator/Mcp/EmulatorMcpServices.cs- Injected services for built-in toolsSpice86.Core/Emulator/Mcp/IMcpToolSupplier.cs- Extension interface for custom toolsSpice86.Core/Emulator/Mcp/Response/McpAboutResponse.cs- Metadata response structure
Further Reading
- Model Context Protocol Specification
- Spice86 CFG CPU Documentation
- Spice86 Internal Debugger Wiki
- Cryogenic Project (MCP Extension Example)
Quick links:
- Health check:
http://localhost:8081/health - MCP endpoint:
http://localhost:8081/mcp - Tool discovery: Call
tools/listormcp_about