---
title: "Set a tool client request timeout in C#"
url: https://recursiveai.org/onemodelcontext/cookbook/set-tool-client-request-timeout/
description: "Set a request timeout for a Model Context Protocol® tool client in C#. Run a local slow tool and see the TimeoutException OneModelContext reports."
version: "1.0.2"
---

# 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.

[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/set-tool-client-request-timeout/set-tool-client-request-timeout.zip)

```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;

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](https://recursiveai.org/onemodelcontext/cookbook/set-tool-client-request-timeout/set-tool-client-request-timeout.zip), 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.

```csharp
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`.

```csharp
RequestTimeout = TimeSpan.FromSeconds(10)
```

```text
Finished
```

## Related recipes

- [Cancel a long-running server tool call in C#](https://recursiveai.org/onemodelcontext/cookbook/cancel-long-running-tools/)
- [Report tool progress](https://recursiveai.org/onemodelcontext/cookbook/report-tool-progress/)
- [Close a standard-input/output client cleanly](https://recursiveai.org/onemodelcontext/cookbook/close-stdio-client-cleanly/)

## 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](https://recursiveai.org/onemodelcontext/docs/stdio/) for how the client picks a protocol version.
