Skip to main content
Glama
dmang-dev

mcp-retroarch

retroarch_write_memory

Write a byte sequence to emulated memory at a specified address. Uses the system memory map and returns the number of bytes actually written.

Instructions

Write a byte sequence to emulated memory via the system memory map. Returns the number of bytes actually written (may be less than requested if a read-only descriptor is hit). Disables hardcore mode for the session.

RetroArch exposes two distinct memory APIs:

  • READ_CORE_MEMORY / WRITE_CORE_MEMORY (used by retroarch_read/write_memory): Goes through the libretro core's system memory map. Most reliable when the core advertises a memory map (most cores do). Errors with "no memory map defined" if the loaded core doesn't.

  • READ_CORE_RAM / WRITE_CORE_RAM (used by retroarch_read/write_ram): Uses the achievement (CHEEVOS) address space. Works even when no core memory map is defined, but addresses follow CHEEVOS conventions, not the system bus. Use when read_memory returns "no memory map defined".

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYesMemory address
bytesYes

Implementation Reference

  • Handler for the retroarch_write_memory tool. Calls ra.writeMemory() with the address and bytes from the request args, then returns a success message with the number of bytes written.
    case "retroarch_write_memory": {
      const n = await ra.writeMemory(p.address as number, p.bytes as number[]);
      return ok(`Wrote ${n} bytes → ${addrHex(p.address as number)}`);
    }
  • Schema/registration definition for retroarch_write_memory. Defines name, description (with MEMORY_NOTE about core memory map), and inputSchema requiring 'address' (integer) and 'bytes' (array of integers 0-255, 1-4096 items).
    {
      name: "retroarch_write_memory",
      description: `Write a byte sequence to emulated memory via the system memory map. Returns the number of bytes actually written (may be less than requested if a read-only descriptor is hit). Disables hardcore mode for the session.\n\n${MEMORY_NOTE}`,
      inputSchema: {
        type: "object",
        required: ["address", "bytes"],
        properties: {
          address: { type: "integer", description: "Memory address" },
          bytes: {
            type: "array",
            items: { type: "integer", minimum: 0, maximum: 255 },
            minItems: 1,
            maxItems: 4096,
          },
        },
      },
    },
  • The RetroArchClient.writeMemory() method that performs the actual WRITE_CORE_MEMORY UDP command. Converts bytes to hex string, sends to RetroArch via UDP, and parses the response to return the count of bytes written.
    /** Memory write via libretro's system memory map (preferred). */
    async writeMemory(addr: number, bytes: Uint8Array | number[]): Promise<number> {
      if (bytes.length === 0)    throw new Error("at least one byte required");
      if (bytes.length > 4096)   throw new Error("byte count exceeds 4096 limit");
      const hex = Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join(" ");
      const cmd = `WRITE_CORE_MEMORY 0x${addr.toString(16)} ${hex}`;
      const r = (await this.query(cmd)).toString().trim();
      // "WRITE_CORE_MEMORY <addr> <bytes_written>" or "<addr> -1 <error>"
      const m = r.match(/^WRITE_CORE_MEMORY\s+\S+\s+(-?\d+)(?:\s+(.+))?$/);
      if (!m) throw new Error(`unexpected WRITE_CORE_MEMORY reply: ${r}`);
      const n = parseInt(m[1], 10);
      if (n < 0) throw new Error(`WRITE_CORE_MEMORY failed: ${m[2] ?? "(no error message)"}`);
      return n;
    }
  • src/tools.ts:176-246 (registration)
    The registerTools function that registers all tools including retroarch_write_memory via the MCP CallToolRequestSchema handler.
    export function registerTools(server: Server, ra: RetroArchClient): void {
      server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: TOOLS }));
    
      server.setRequestHandler(CallToolRequestSchema, async (req) => {
        const { name, arguments: args = {} } = req.params;
        const p = args as Record<string, unknown>;
    
        switch (name) {
          case "retroarch_ping": {
            const v = await ra.getVersion();
            return ok(`OK — RetroArch ${v}`);
          }
    
          case "retroarch_get_status": {
            const s = await ra.getStatus();
            if (s.state === "contentless") return ok("No content loaded");
            return ok(
              `State:  ${s.state}\n` +
              `System: ${s.system}\n` +
              `Game:   ${s.game}\n` +
              `CRC32:  ${s.crc32 ?? "(none reported)"}`,
            );
          }
    
          case "retroarch_get_config": {
            const v = await ra.getConfigParam(p.name as string);
            return ok(`${p.name} = ${v}`);
          }
    
          case "retroarch_read_memory": {
            const bytes = await ra.readMemory(p.address as number, p.length as number);
            const hex = Array.from(bytes).map((b) => b.toString(16).padStart(2, "0").toUpperCase()).join(" ");
            return ok(`${addrHex(p.address as number)} [${bytes.length} bytes]:\n${hex}`);
          }
    
          case "retroarch_write_memory": {
            const n = await ra.writeMemory(p.address as number, p.bytes as number[]);
            return ok(`Wrote ${n} bytes → ${addrHex(p.address as number)}`);
          }
    
          case "retroarch_read_ram": {
            const bytes = await ra.readRam(p.address as number, p.length as number);
            const hex = Array.from(bytes).map((b) => b.toString(16).padStart(2, "0").toUpperCase()).join(" ");
            return ok(`${addrHex(p.address as number)} [${bytes.length} bytes, CHEEVOS]:\n${hex}`);
          }
    
          case "retroarch_write_ram": {
            await ra.writeRam(p.address as number, p.bytes as number[]);
            return ok(`Wrote ${(p.bytes as number[]).length} bytes → ${addrHex(p.address as number)} (CHEEVOS, no ack)`);
          }
    
          case "retroarch_pause_toggle":  await ra.pauseToggle();   return ok("Pause toggled");
          case "retroarch_frame_advance": await ra.frameAdvance();  return ok("Advanced one frame");
          case "retroarch_reset":         await ra.reset();         return ok("Game reset");
          case "retroarch_screenshot":    await ra.screenshot();    return ok("Screenshot saved to RetroArch's configured screenshot directory");
          case "retroarch_show_message": {
            await ra.showMessage(p.message as string);
            return ok(`Showed: ${p.message}`);
          }
    
          case "retroarch_save_state_current":  await ra.saveStateCurrent();          return ok("Saved to current slot");
          case "retroarch_load_state_current":  await ra.loadStateCurrent();          return ok("Loaded from current slot");
          case "retroarch_load_state_slot":     await ra.loadStateSlot(p.slot as number); return ok(`Loaded from slot ${p.slot}`);
          case "retroarch_state_slot_plus":     await ra.stateSlotPlus();             return ok("Incremented current slot");
          case "retroarch_state_slot_minus":    await ra.stateSlotMinus();            return ok("Decremented current slot");
    
          default:
            throw new Error(`Unknown tool: ${name}`);
        }
      });
    }
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description fully covers behavior: returns bytes actually written (may be less), disables hardcore mode, and explains error conditions and API differences. This is comprehensive and goes well beyond basic description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is moderately long but well-structured: first sentence states main action and return value, then a note about disabling hardcore mode, followed by a clear bullet list of two APIs. Every sentence is informative, though it could be slightly more compact.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool complexity (interacts with emulated memory, has a sibling for fallback), the description is complete. It explains return value, side effects, error handling, and relationship to sibling. Without an output schema, it adequately describes what the agent can expect.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 50% (only address has a description). The description adds 'byte sequence' context but doesn't clarify the address format (e.g., hex vs decimal) or expand on the bytes array beyond schema constraints. The schema already specifies min/max for bytes values and array length, so the description adds marginal value.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool writes a byte sequence to emulated memory via the system memory map, and distinguishes it from siblings by explaining the two distinct RetroArch memory APIs (read/write_core_memory vs. read/write_core_ram). This leaves no ambiguity about what the tool does.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Provides explicit guidance on when to use this tool vs. retroarch_write_ram: use this when the core advertises a memory map, and use write_ram when this one fails with 'no memory map defined'. Also warns about read-only descriptors and disables hardcore mode, offering clear usage boundaries.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/dmang-dev/mcp-retroarch'

If you have feedback or need assistance with the MCP directory API, please join our Discord server