Fetch more tools only when you need them. The server sends a cursor telling it where to resume.
dotnet add package RecursiveAI.OneModelContextRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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:
add double square
The full program
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:
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:
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
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 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.