---
title: "Paginate a tool server’s tool list in C#"
url: https://recursiveai.org/onemodelcontext/cookbook/paginate-a-tool-server-catalog/
description: "Use OneModelContext to list server tools one page at a time in C#. Pass the next cursor back unchanged and stop when the server returns no cursor."
version: "1.0.2"
---

# Paginate a tool server’s tool list in C#

**OneModelContext** is a free, open-source .NET SDK for the Model Context Protocol® from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneModelContext) · [GitHub](https://github.com/recursive-ai-oss/onemodelcontext-csharp) · [Docs](https://recursiveai.org/onemodelcontext/docs/) · [Download example (.zip)](https://recursiveai.org/onemodelcontext/cookbook/paginate-a-tool-server-catalog/paginate-a-tool-server-catalog.zip)

Fetch more tools only when you need them. The server sends a cursor telling it where to resume.

```shell
dotnet add package RecursiveAI.OneModelContext
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneModelContext;

// args[0]: the server launch command.
await using var client = await ModelContext
    .ConnectAsync(args[0]);
string? cursor = null;
do
{
    var page = await client
        .ListToolsPageAsync(cursor);
    foreach (var tool in page.Tools)
        Console.WriteLine(tool.Name);
    cursor = page.NextCursor;
} while (cursor is not null);
```

For the snippet, pass the server launch command as a single argument; the code reads it from `args[0]`.

[The example](https://recursiveai.org/onemodelcontext/cookbook/paginate-a-tool-server-catalog/paginate-a-tool-server-catalog.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

The zip runs both sides on its own, with no argument. Against its sample catalog, the snippet prints:

```text
add
double
square
```

## The full program

```csharp
using System.Reflection;
using RecursiveAI.OneModelContext;

if (args is ["--serve"])
{
    var server = new ContextServer("catalog", "1.0",
        new ContextServerOptions { MaxPageSize = 2 });
    ((IContextRegistrar)new Catalog()).AddTo(server);
    await ModelContext.ServeAsync(server);
    return;
}

var dll = Assembly.GetExecutingAssembly().Location;
var command = $"dotnet \"{dll}\" --serve";
await using var client = await ModelContext.ConnectAsync(command);
var first = await client.ListToolsPageAsync();
Console.WriteLine("Page 1: " + string.Join(", ", first.Tools.Select(t => t.Name)));

// In an app, do this when the user asks for more tools.
if (first.NextCursor is string cursor)
{
    var next = await client.ListToolsPageAsync(cursor);
    Console.WriteLine("Page 2: " + string.Join(", ", next.Tools.Select(t => t.Name)));
    Console.WriteLine($"More pages: {next.NextCursor is not null}");
}

public partial class Catalog
{
    [Tool("add")]
    public int Add(int a, int b) => a + b;

    [Tool("double")]
    public int Double(int value) => value * 2;

    [Tool("square")]
    public int Square(int value) => value * value;
}
```

`dotnet run` prints:

```text
Page 1: add, double
Page 2: square
More pages: False
```

The program launches itself with `--serve` as a child process and talks to it over standard input and output. The server registers the three tools defined in `Catalog` and caps pages at two entries; no external server or input file is needed. The NuGet package’s source generator implements `IContextRegistrar` for the `partial` class. An invalid cursor can cause an invalid-params error (`-32602`); start a fresh listing without a cursor if the saved one is no longer usable.

## Variations

**Get the whole catalog in one client call.** Replace the client code after `ConnectAsync` with:

```csharp
var tools = await client.ListToolsAsync();
foreach (var tool in tools)
    Console.WriteLine(tool.Name);
```

This prints `add`, `double`, and `square` on separate lines. The client can still make multiple protocol requests behind that one API call.

## Related recipes

- [List tool server capabilities](https://recursiveai.org/onemodelcontext/cookbook/list-tool-server-capabilities/)
- [Connect to a tool server](https://recursiveai.org/onemodelcontext/cookbook/connect-to-a-server/)
- [Add tools at runtime](https://recursiveai.org/onemodelcontext/cookbook/add-tools-at-runtime/)

---

## The short answer

Call `ListToolsPageAsync()` with no cursor to get the first page. Display the entries in `Tools` and keep `NextCursor` in memory. When the user requests the next page, call `ListToolsPageAsync(cursor)` with that exact string. Use the newly returned `NextCursor` for the next request, and stop when it is `null`.

## How does cursor pagination work for a tool list?

The [pagination specification](https://modelcontextprotocol.io/specification/2025-11-25/server/utilities/pagination) describes `tools/list` as a paginated operation. The client doesn’t request page 2 by number. Instead, the response’s `nextCursor` becomes the next request’s `cursor`. From the client’s perspective it’s an opaque string: don’t decode it, change it, or save it across sessions.

The server decides how many entries to send. The example sets `ContextServerOptions.MaxPageSize` to 2 and registers three tools, so the first response contains two names and the second contains one. With another server, use `NextCursor`, not the page length, to decide whether to offer a “Load more” button. A server that returns everything on the first request can leave the cursor out entirely.

Fetching entries discovers tool names and definitions. It doesn’t invoke them. The sample’s three arithmetic methods are registered so we have a real list to page through, but their bodies don’t run during listing.

## When should I use pagination instead of loading every tool?

Fetch one page at a time if the caller will inspect a small part of the catalog, as in a tool picker. Keep the latest cursor alongside the currently displayed results. Clear both when switching servers or refreshing from the beginning. To load another page later, use the same connected client and the cursor from its most recent page, not the cursor originally returned with page one.

Use `ListToolsAsync()` when you need all the definitions before doing anything else. It follows the cursors for you, accumulates the results, and throws `ContextException` if the number of pages exceeds `ContextClientOptions.MaxListPages`. The one-page API leaves that decision with your application: you can stop after the first page, stop when you find a tool, or let the user ask for the next group.
