Skip to main content
Glama

mgba_save_state

Save the current emulator state to a numbered slot or absolute file path. Use as a checkpoint for later loading or to seed cartridge SRAM on Game Boy.

Instructions

Save the current emulator state. Pass either slot (0-9, mGBA-managed slot file) or path (absolute file path; only works on builds that expose the file API). Useful for capturing checkpoints to load later — and as a clean way to seed cartridge SRAM on Game Boy without fighting the MBC.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
slotNoSave state slot (0-9)
pathNoAbsolute file path (alternative to slot)

Implementation Reference

  • Tool definition (name, description, inputSchema) for the mgba_save_state tool. Accepts optional 'slot' (0-9) or 'path' (absolute file path).
      name: "mgba_save_state",
      description: "Save the current emulator state. Pass either `slot` (0-9, mGBA-managed slot file) or `path` (absolute file path; only works on builds that expose the file API). Useful for capturing checkpoints to load later — and as a clean way to seed cartridge SRAM on Game Boy without fighting the MBC.",
      inputSchema: {
        type: "object",
        properties: {
          slot: { type: "integer", minimum: 0, maximum: 9, description: "Save state slot (0-9)" },
          path: { type: "string", description: "Absolute file path (alternative to slot)" },
        },
      },
    },
  • Handler/case for mgba_save_state in the CallToolRequestSchema switch. Validates that either slot or path is provided, then calls mgba.call('save_state', ...) to delegate to the mGBA Lua bridge. Returns the saved path or slot number.
    case "mgba_save_state": {
      if (p.slot === undefined && p.path === undefined) {
        throw new Error("provide either `slot` (0-9) or `path`");
      }
      const r = await mgba.call<{ slot?: number; path?: string }>("save_state", {
        ...(p.slot !== undefined ? { slot: p.slot } : {}),
        ...(p.path !== undefined ? { path: p.path } : {}),
      });
      return ok(r.path ? `Saved state to ${r.path}` : `Saved state to slot ${r.slot}`);
    }
  • src/tools.ts:258-411 (registration)
    The registerTools function registers all tools (including mgba_save_state) via server.setRequestHandler for ListToolsRequestSchema and CallToolRequestSchema. The tool name is matched by switch-case at line 385.
    export function registerTools(server: Server, mgba: MgbaClient): 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 "mgba_ping": {
            const r = await mgba.call<string>("ping");
            return ok(r);
          }
    
          case "mgba_get_info": {
            const r = await mgba.call<{
              title?: string;
              code?: string;
              frame?: number;
              platform?: number | string;
              capabilities?: Record<string, boolean>;
            }>("get_info");
            const lines = [
              `Title:    ${r.title ?? "(unavailable)"}`,
              `Code:     ${r.code ?? "(unavailable)"}`,
              `Platform: ${r.platform ?? "(unavailable)"}`,
              `Frame:    ${r.frame ?? "(unavailable)"}`,
            ];
            if (r.capabilities) {
              const present = Object.entries(r.capabilities).filter(([, v]) => v).map(([k]) => k);
              const missing = Object.entries(r.capabilities).filter(([, v]) => !v).map(([k]) => k);
              lines.push("");
              lines.push(`Capabilities present: ${present.length ? present.join(", ") : "(none)"}`);
              if (missing.length) lines.push(`Missing on this build: ${missing.join(", ")}`);
            }
            return ok(lines.join("\n"));
          }
    
          case "mgba_read8": {
            const v = await mgba.call<number>("read8", { address: p.address });
            return ok(`0x${(p.address as number).toString(16).toUpperCase()}: ${formatHex(v)}`);
          }
    
          case "mgba_read16": {
            const v = await mgba.call<number>("read16", { address: p.address });
            return ok(`0x${(p.address as number).toString(16).toUpperCase()}: ${formatHex(v)}`);
          }
    
          case "mgba_read32": {
            const v = await mgba.call<number>("read32", { address: p.address });
            return ok(`0x${(p.address as number).toString(16).toUpperCase()}: ${formatHex(v)}`);
          }
    
          case "mgba_write8": {
            await mgba.call("write8", { address: p.address, value: p.value });
            return ok(`Wrote ${formatHex(p.value)} → 0x${(p.address as number).toString(16).toUpperCase()}`);
          }
    
          case "mgba_write16": {
            await mgba.call("write16", { address: p.address, value: p.value });
            return ok(`Wrote ${formatHex(p.value)} → 0x${(p.address as number).toString(16).toUpperCase()}`);
          }
    
          case "mgba_write32": {
            await mgba.call("write32", { address: p.address, value: p.value });
            return ok(`Wrote ${formatHex(p.value)} → 0x${(p.address as number).toString(16).toUpperCase()}`);
          }
    
          case "mgba_read_range": {
            const bytes = await mgba.call<number[]>("read_range", {
              address: p.address,
              length:  p.length,
            });
            const hex = bytes
              .map((b) => b.toString(16).padStart(2, "0").toUpperCase())
              .join(" ");
            const addr = (p.address as number).toString(16).toUpperCase();
            return ok(`0x${addr} [${bytes.length} bytes]:\n${hex}`);
          }
    
          case "mgba_write_range": {
            const r = await mgba.call<{ written: number }>("write_range", {
              address: p.address,
              bytes:   p.bytes,
            });
            const addr = (p.address as number).toString(16).toUpperCase();
            return ok(`Wrote ${r.written} bytes → 0x${addr}`);
          }
    
          case "mgba_press_buttons": {
            const r = await mgba.call<{ queued: boolean; queue_size: number }>("press_buttons", {
              buttons:        p.buttons,
              frames:         p.frames         ?? 1,
              release_frames: p.release_frames ?? 1,
            });
            const keys = (p.buttons as string[]).join("+");
            return ok(
              `Queued press: ${keys} ` +
              `(hold ${p.frames ?? 1}f, release ${p.release_frames ?? 1}f). ` +
              `Queue size: ${r.queue_size}`,
            );
          }
    
          case "mgba_advance_frames": {
            const frame = await mgba.call<number>("advance_frames", { count: p.count ?? 1 });
            return ok(`Advanced ${p.count ?? 1} frame(s). Current frame: ${frame}`);
          }
    
          case "mgba_pause": {
            await mgba.call("pause");
            return ok("Emulation paused");
          }
    
          case "mgba_unpause": {
            await mgba.call("unpause");
            return ok("Emulation resumed");
          }
    
          case "mgba_reset": {
            await mgba.call("reset");
            return ok("ROM reset");
          }
    
          case "mgba_screenshot": {
            const path = await mgba.call<string>("screenshot", p.path ? { path: p.path } : {});
            return ok(`Screenshot saved: ${path}`);
          }
    
          case "mgba_save_state": {
            if (p.slot === undefined && p.path === undefined) {
              throw new Error("provide either `slot` (0-9) or `path`");
            }
            const r = await mgba.call<{ slot?: number; path?: string }>("save_state", {
              ...(p.slot !== undefined ? { slot: p.slot } : {}),
              ...(p.path !== undefined ? { path: p.path } : {}),
            });
            return ok(r.path ? `Saved state to ${r.path}` : `Saved state to slot ${r.slot}`);
          }
    
          case "mgba_load_state": {
            if (p.slot === undefined && p.path === undefined) {
              throw new Error("provide either `slot` (0-9) or `path`");
            }
            const r = await mgba.call<{ slot?: number; path?: string }>("load_state", {
              ...(p.slot !== undefined ? { slot: p.slot } : {}),
              ...(p.path !== undefined ? { path: p.path } : {}),
            });
            return ok(r.path ? `Loaded state from ${r.path}` : `Loaded state from slot ${r.slot}`);
          }
    
          default:
            throw new Error(`Unknown tool: ${name}`);
        }
      });
    }
Behavior2/5

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

No annotations are provided, so the description must fully disclose behavior. It states it saves state (a write operation) but does not mention if it overwrites existing states, if it requires a game loaded, if it pauses emulation, or any side effects beyond the SRAM seeding hint. Missing critical behavioral details for a mutation tool.

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

Conciseness5/5

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

Two focused sentences with a third adding a relevant use case. No redundant words. Purpose is front-loaded, and every sentence contributes meaning.

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

Completeness4/5

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

For a 2-param optional tool with no output schema and no annotations, the description covers purpose, parameter differentiation, and a practical use case. It lacks mention of error conditions, whether the tool is safe to call mid-emulation, or if it works without an active game, but overall it's sufficiently complete for common usage.

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

Parameters4/5

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

Schema coverage is 100%, baseline 3. The description adds value by explaining slot as 'mGBA-managed slot file (0-9)' and path with the caveat about builds exposing the file API, which the schema's brief descriptions do not convey. This enables correct selection between the two options.

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 'Save the current emulator state' with a specific verb and resource. It distinguishes from siblings like mgba_load_state and mgba_screenshot by naming the save action and the two methods (slot/path).

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

Usage Guidelines4/5

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

The description tells the agent to pass either slot or path, explains path's limitation ('only works on builds that expose the file API'), and provides a use case (checkpoints, seeding SRAM). It does not explicitly exclude scenarios but gives enough context for when to use this tool vs alternatives.

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-mgba'

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