Skip to content
mentoor

Topic 1 of 5 · 5 min read

Connecting to servers and listing capabilities

Goal: Write client code that connects to a server, discovers its capabilities, lists its tools and refreshes them when they change.

A client does not know what a server offers until it asks: connect, agree on a protocol version, read the capabilities, then list the tools page by page and keep that list fresh.

Check yourself · 1 of 3

A server answers tools/list with 40 tools and a nextCursor value. What should your client do?

Choose one answer

Why it matters

Your model can only call tools it has been told about. If the client lists only the first page, caches the list forever, or ignores a version mismatch, the model works with a wrong picture of the server. The failures are quiet: a tool is missing, or a renamed tool is still offered.

How to do it

  1. Settle the version. Since revision 2026-07-28 there is no handshake. Every request carries the protocol version and the client's capabilities in _meta. A client may call server/discover first to get the server's versions and capabilities in one call. If the server does not support the version a request names, it answers with UnsupportedProtocolVersionError and a list of the versions it does support, and the client retries with one they share. Servers on 2025-11-25 or earlier still expect the legacy handshake: initialize, the server's chosen version and capabilities, then notifications/initialized. A client that must reach both kinds probes first and falls back.
  2. Read the capabilities. A server that has tools declares a tools capability. Inside it, listChanged: true means the server will announce changes to its tool list. Do not call tools/list on a server that never declared tools.
  3. List with a cursor loop. tools/list returns a page of tools and, when there is more, a nextCursor. Send the cursor back in the next request until it is absent. Treat the cursor as an opaque string.
  4. Cache and refresh. Keep the list in memory so you do not list on every model turn. Modern list results carry ttlMs, a freshness hint in milliseconds: list again when it runs out. If the server declared listChanged, also handle notifications/tools/list_changed by listing again. On a 2026-07-28 server you receive it by opening a subscriptions/listen stream with toolsListChanged: true. A legacy server gives no expiry value, so choose your own refresh time, for example on reconnect or every few minutes.

Before and after

// Before: only the first page, no capability check
const { tools } = await client.listTools();
// After: follow every cursor, refresh on change
async function loadTools(client: Client) {
  const all = [];
  let cursor: string | undefined;
  do {
    const page = await client.listTools({ cursor });
    all.push(...page.tools);
    cursor = page.nextCursor;
  } while (cursor);
  return all;
}

client.setNotificationHandler(ToolListChangedNotificationSchema, async () => {
  tools = await loadTools(client);
});

What goes wrong

  • Silent truncation. A server with 120 tools pages its answer, and the client keeps the first page.
  • Stale cache. The server deploys a new tool schema, the client still sends the old argument names, and every call fails validation.
  • Registering the handler too late. A change notice that arrives before the handler exists is lost.

Examples

A missing tool traced to paging

A client shows the model 50 tools from a server that exposes 83. The log shows one tools/list request and a response with nextCursor: "eyJwIjoyfQ". The loop in the client never reads the cursor. Adding the do-while loop makes all 83 tools appear.

A refresh that follows the capability

A server's server/discover result declares capabilities: { tools: { listChanged: true } }. The client opens a subscriptions/listen stream for tool-list changes and re-lists on each notice. A second, legacy server declares tools: {} in its initialize result. For that one the client re-lists on reconnect, because it will never be told about changes.

Key points

  • Settle the protocol version first: per-request _meta (optionally after server/discover) on 2026-07-28 servers, the initialize handshake on legacy ones.
  • A client should call tools/list only when the server declared a tools capability.
  • tools/list is paginated, so follow nextCursor until it is absent and treat the cursor as opaque.
  • With listChanged declared, refresh on notifications/tools/list_changed; without it, pick your own refresh policy.
  • Cache the tool list and refresh it when its ttlMs hint runs out or a list-changed notice arrives, instead of listing on every model turn.

Common mistakes

  • Calling listTools once and assuming the result is complete.
  • Caching the tool list for the life of the process, ignoring the ttlMs hint and with no other refresh path.
  • Continuing with a server that picked a protocol version the client cannot handle.
  • Calling tools/list on a server that only declared resources or prompts.

Check yourself · 2 of 3

Which signal tells a client that a server will announce changes to its tool list?

Choose one answer

Check yourself · 3 of 3

A server did not declare listChanged and its tools change on each deploy. Which refresh policy is sound?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic