Recursive AI

Set a tool client request timeout 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.

Terminal
dotnet add package RecursiveAI.OneModelContext

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneModelContext;
 
var options = new ContextClientOptions {
    RequestTimeout = TimeSpan.FromSeconds(1)
};
var transport =
    ContextTransport.Process("dotnet", args);
await using var client = await
    ContextClient.ConnectAsync(
        transport, options);
await client.CallToolAsync("slow");

Unzip the example, then dotnet run.

args contains the arguments that start your server; the download sets them up for you.

The local slow tool waits five seconds, so the call throws TimeoutException with this message:

Text
No response to 'tools/call' within 00:00:01.

The full program

The download launches itself as the server with serve, so you don’t need to pass any arguments. The partial class lets the package’s source generator register the tool. The client catches the timeout and prints its message.

C#
using RecursiveAI.OneModelContext;
 
if (args is ["serve"])
{
    await ModelContext.ServeAsync(new Demo());
    return;
}
 
var host = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet";
var dll = typeof(Program).Assembly.Location;
var transport = ContextTransport.Process(host, [dll, "serve"]);
var seconds = args is ["wait"] ? 10 : 1;
var options = new ContextClientOptions
{
    RequestTimeout = TimeSpan.FromSeconds(seconds)
};
await using var client = await ContextClient.ConnectAsync(transport, options);
try
{
    Console.WriteLine((await client.CallToolAsync("slow")).Text);
}
catch (TimeoutException ex)
{
    Console.WriteLine(ex.Message);
}
 
public partial class Demo
{
    [Tool("slow")]
    public async Task<string> Slow(CancellationToken token)
    {
        await Task.Delay(TimeSpan.FromSeconds(5), token);
        return "Finished";
    }
}
Text
No response to 'tools/call' within 00:00:01.

Variations

Give the tool ten seconds instead of one by setting the option below. The download does this with dotnet run -- wait.

C#
RequestTimeout = TimeSpan.FromSeconds(10)
Text
Finished

The short answer

Set ContextClientOptions.RequestTimeout and pass the options to ContextClient.ConnectAsync. It defaults to five minutes. This setting is for waiting for the response to a protocol request, not for negotiating the connection. If the tool needs more time and you intend to wait for it, set a longer timeout when you create the client.

What happens when a tool call times out?

The client stops waiting for the reply. In this example, it throws TimeoutException and sends a cancellation notification to the server. The server tool takes a CancellationToken and passes it to Task.Delay, so the delay can stop when the tool is notified. A timeout doesn’t prove the server stopped or that its changes were undone. Before retrying a tool that writes data, check whether the first call already took effect.

The request timeout is different from caller cancellation. Use the request timeout to set how long the client normally waits; pass a cancellation token to CallToolAsync when the caller can stop one particular call sooner. If the caller cancels, handle OperationCanceledException rather than TimeoutException.

Is a tool call timeout the same as a connection timeout?

No. ConnectTimeout limits negotiation with the server and defaults to 30 seconds. ProbeTimeout is the wait for a protocol-version probe before trying another version; it defaults to five seconds, with an extra allowance for the first probe of a new server process. Changing the request timeout won’t help a server that never finishes negotiation. If the client connects and the tool starts but takes too long to reply, change RequestTimeout instead. See the standard input/output transport docs for how the client picks a protocol version.

All onemodelcontext recipes