---
id: SI0043
title: "MCP live RPC example tool"
url: https://carbium.sh/library/si/mcp-live-rpc-example-tool
collection: AI and blockchain
category: Your own MCP server on Carbium RPC
revision: 1
updated: 2026-11-22
author: Carbium
code: MIT (LICENSE in the zip); text: all rights reserved, Carbium Corporation
---

# MCP live RPC example tool

You get the handler for an MCP tool that returns a Solana RPC example and a verdict on the answer it got just now. Each example lists the keys its answer should hold, and the result says whether the live answer still has them.

## Why a stored example goes wrong

A model asked to write a Solana call usually writes it from memory. Memory holds the method name and a rough parameter list, and it is often a version or two behind. A sample pasted into the tool's description helps only while it stays true. The method may still exist while the response shape in the comment has drifted, or the account it points at has changed hands.

The fix is to replay each example on request and compare what comes back with what the example says it holds. The tool then returns the request, the keys it expects and a verdict, and the model copies a call that worked a moment ago.

What gets stored is the request and a list of key paths the answer must contain. Values stay out. A slot is wrong almost at once, and a lamport balance and a feature set drift later, so a stored value goes stale. The key paths of a `getSlot` or `getAccountInfo` answer change only when the shape of the response does, and that is the event the check exists to catch.

## The catalogue

| Example | Params | Keys the answer must hold | Cells | Req/s on Free, Dev, Business, Pro |
|---|---|---|---|---|
| `getSlot` | `commitment: "confirmed"` | a number | 1 | 5, 30, 100, 500 |
| `getVersion` | none | `solana-core`, `feature-set` | 1 | 5, 30, 100, 500 |
| `getAccountInfo` | USDC mint, `confirmed`, `base64` | `context.slot`, `value.lamports`, `value.owner`, `value.executable`, `value.data` | 10 | 0.5, 15, 50, 400 |

Every example that takes a commitment sets `commitment: "confirmed"` itself, so a copy of the stored request does not depend on a client's default. `getVersion` takes no params. The account read asks for `base64`, a fixed shape that any account can return, unlike the default `base58`, which fails on large accounts.

A key path is dotted. A bare numeric answer such as the one from `getSlot` has no keys, so the catalogue writes `number` for the result itself.

`getSlot` and `getVersion` together take 2 cells from the cheap-read class, and `getAccountInfo` takes 10 from the account-read class. [Each class has its own bucket](https://carbium.sh/docs#limits) that absorbs bursts up to 1,024 cells, so one replay of the catalogue sits well inside both.

## What the handler returns

`liveExample` takes a method name, sends the stored request and returns five fields.

- `request`: the method and params as sent, which the model can copy into its own code.
- `keys`: the paths the example promises.
- `missing`: the paths the live answer lacks, an empty array on a match.
- `match`: true when `missing` is empty.
- `slot`: the slot the answer carries. That is `context.slot` for `getAccountInfo`, the result itself for `getSlot` and null for `getVersion`, which carries none.

A mismatch comes back as a normal result. The model sees which keys are missing and can tell the user that the stored example is out of date. A JSON-RPC error object leaves nothing to compare, so the handler throws with the code and message. A method Carbium does not serve answers -32601, which means check the method and do not retry; `getTokenLargestAccounts` is one of them.

In an MCP server, `liveExample` is the `run` function of one tool, called for instance `rpc_example`, with the method name as its only argument and the catalogue names as an enum in its schema. The lookup uses `Object.hasOwn`, so a name outside the catalogue never becomes a request and the model never supplies a method of its own. The [read-only MCP server entry](https://carbium.sh/library/si/read-only-mcp-server) shows the tool table this one joins.

## The file and its run

`main.ts` (run: `node main.ts`)

```ts
// SI0043 · MCP live RPC example tool
// https://carbium.sh/library/si/mcp-live-rpc-example-tool
// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.
// SPDX-License-Identifier: MIT

// Live RPC example tool. Run: CARBIUM_KEY=... node main.ts [method ...]
import { createDefaultRpcTransport } from "@solana/kit";

const key = process.env.CARBIUM_KEY;
if (!key) {
  console.error("Set CARBIUM_KEY to your Carbium API key.");
  process.exit(1);
}
const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";
const transport = createDefaultRpcTransport({ url, headers: { "x-api-key": key } });

type Example = { params: unknown[]; keys: string[] };
// A key path is dotted. "number" stands for a bare numeric result.
const CATALOGUE: Record<string, Example> = {
  getSlot: { params: [{ commitment: "confirmed" }], keys: ["number"] },
  getVersion: { params: [], keys: ["solana-core", "feature-set"] },
  getAccountInfo: {
    params: ["EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", { commitment: "confirmed", encoding: "base64" }],
    keys: ["context.slot", "value.lamports", "value.owner", "value.executable", "value.data"],
  },
};

const has = (result: any, path: string): boolean =>
  path === "number" ? ["number", "bigint"].includes(typeof result) : path.split(".").reduce((o, p) => o?.[p], result) !== undefined;

// The tool body: replay one stored example and report whether the live answer still holds its keys.
async function liveExample(method: string) {
  if (!Object.hasOwn(CATALOGUE, method)) throw new Error(`no example for ${method}`);
  const { params, keys } = CATALOGUE[method] as Example;
  const res = await transport<any>({ payload: { jsonrpc: "2.0", id: 1, method, params } });
  if (res.error) throw new Error(`${method} answered ${res.error.code}: ${res.error.message}`);
  const missing = keys.filter((k) => !has(res.result, k));
  const slot = res.result?.context?.slot ?? (has(res.result, "number") ? res.result : null);
  return { request: JSON.stringify({ method, params }), keys, missing, match: missing.length === 0, slot };
}

const names = process.argv.length > 2 ? process.argv.slice(2) : Object.keys(CATALOGUE);
const settled = await Promise.allSettled(names.map((n) => liveExample(n)));
settled.forEach((r, i) => {
  if (r.status === "rejected") {
    console.log(`${names[i]}: failed, ${(r.reason as Error).message}`);
    return;
  }
  const { request, keys, missing, match, slot } = r.value;
  console.log(`${request}\n  keys ${keys.join(" ")}\n  ${match ? "match" : `mismatch, missing ${missing.join(" ")}`}, slot ${slot ?? "none"}`);
});
```

`CARBIUM_KEY` is required, and the file exits with a message when it is unset. `CARBIUM_RPC_URL` overrides the endpoint, which defaults to `https://services.carbium.sh`.

The run replays all three examples through `Promise.allSettled`, so one failing call prints its error and the other two still print. Each example shows its request, the keys it expects, match or mismatch and the slot. Names after the file name pick a subset, as in `node main.ts getSlot`.

A passing run prints three matches, with a slot beside `getSlot` and `getAccountInfo` and none beside `getVersion`. A slot lasts about 200 ms and the three calls go out together, so the slot beside `getAccountInfo` can differ from the one beside `getSlot` by a few. For a first look at the call itself, the [first RPC call entry](https://carbium.sh/library/howsolana/first-solana-rpc-api-call) walks through one.

## Where it ends

- The check looks for keys. An `owner` that changed to another program still matches, and so does `value.data` of a different length. Values that matter to the reader need their own test.
- Each new example needs key paths written by someone who has read a real answer. The handler cannot work them out.
- An example for an account that can close or empty out reports `value` as null and `value.lamports` as missing. Point account examples at addresses that stay open, such as a major mint.
- The catalogue holds reads only. A write example would need a signer, and nothing here signs or sends.
- Every replay spends cells. On Free, an account read has a ceiling of 0.5 req/s, so one replay of the catalogue every 2 s is the most it supports, and a model that calls the tool every turn reaches that fast. Cap the calls per task in the client's loop.
- The answer is one node's view at confirmed. It says nothing about the other commitment levels.

## When it goes wrong

| What goes wrong | What to do |
|---|---|
| `getAccountInfo` reports a mismatch with `value.lamports` missing. | The answer's `value` is null, so the account does not exist at that address. Check the address in the example and pick one that stays open. |
| Every call prints failed, HTTP error (401). | The door rejected the key as UNKNOWN_KEY: it was cut short or has a stray character. Attach the whole key in `CARBIUM_KEY` and replay. |
| Replays fail with 429 once a model calls the tool more often than every 2 s for long enough. | The calls have drained the 1,024-cell burst, which holds about 100 account reads at 10 cells each, and Free refills at 0.5 req/s. Wait for the retry-after hint and replay once, space the calls, and check whether other keys on the account spend the shared budget. |
| A new example answers -32601. | Carbium does not serve that method. Do not retry: check the method name and the list of served methods before adding the example. |
| The model quotes the printed slot as the current slot. | Have the tool call run again when the model needs a current number, and tell the model the slot in a result is a reading from call time. |

## Questions

### Why store key paths and not values?

A value such as a slot or a balance changes between calls, so a stored one would fail at once. Key paths change only when the response shape does, which is the case the check is for.

### Can the model make the tool call any method?

No. The tool's schema lists the catalogue names and `Object.hasOwn` refuses every other name before a request goes out. The model supplies a name from the list and never a method.

### Why does every example name its commitment?

@solana/kit fills in confirmed when a call leaves it out, while the JSON-RPC server's default is finalized. Writing `confirmed` into the stored request makes a copy ask for the same level through kit or through raw JSON-RPC.

### How do I add an example?

Call the method once by hand, read the answer and write the key paths it must hold. Cheap reads cost 1 cell and account reads 10, so check the class before an example joins a frequent replay.

## Related

- [Read-only MCP server](https://carbium.sh/library/si/read-only-mcp-server) (AI and blockchain, SI0002)
- [First Solana RPC API call](https://carbium.sh/library/howsolana/first-solana-rpc-api-call) (How to use Solana, HS0010)
- [carbium.sh/docs#rpc](https://carbium.sh/docs#rpc)
- [carbium.sh/docs#limits](https://carbium.sh/docs#limits)
- [carbium.sh/docs#errors](https://carbium.sh/docs#errors)
- [carbium.sh/docs#endpoints](https://carbium.sh/docs#endpoints)
- [carbium.sh/docs#auth](https://carbium.sh/docs#auth)
