Skip to content

Make MCP client initialization timeout configurable via ToolOptions - #212

Merged
Grant Harris (gwharris7) merged 7 commits into
microsoft:mainfrom
matiazo:feature/configurable-mcp-timeout
Jun 8, 2026
Merged

Grant Harris (gwharris7) merged 7 commits into
microsoft:mainfrom
matiazo:feature/configurable-mcp-timeout

Conversation

@matiazo

Copy link
Copy Markdown
Contributor

Summary

  • Add nullable McpClientInitializationTimeoutSeconds property to ToolOptions so consumers can override the MCP client initialization timeout
  • When not set (null), the MCP SDK default (60s) is used — no behavioral change for existing consumers
  • When set, both McpClientOptions.InitializationTimeout and HttpClient.Timeout are configured to match

Motivation

Consumers connecting to the MCP platform may experience OperationCanceledException when downstream dependencies are slow. The MCP SDK's default 60-second initialization timeout is not configurable through ToolOptions, leaving no way for consumers to adjust it for their agents.

Usage

// Default behavior — no change needed, SDK default timeout applies
var options = new ToolOptions();
await configService.GetMcpClientToolsAsync(turnContext, serverConfig, token, options);

// Custom timeout for slow environments
var options = new ToolOptions
{
    McpClientInitializationTimeoutSeconds = 180 // 3 minutes
};
await configService.GetMcpClientToolsAsync(turnContext, serverConfig, token, options);

Changes

  • src/Tooling/Core/Models/ToolOptions.cs — Add int? McpClientInitializationTimeoutSeconds property
  • src/Tooling/Core/Services/McpToolServerConfigurationService.cs — Apply timeout only when explicitly set

Test plan

  • Verified default behavior (null) uses MCP SDK default timeout
  • Verified explicit value is applied to both McpClientOptions.InitializationTimeout and HttpClient.Timeout
  • Built and tested end-to-end with sample-agent against MCP platform

Generated with Claude Code

Add nullable McpClientInitializationTimeoutSeconds property to ToolOptions
so consumers can override the MCP client initialization timeout. When null
(default), the MCP SDK default is used. When set, both McpClientOptions
and HttpClient.Timeout are configured to match.

Co-Authored-By: Claude Code <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an opt-in configuration surface to the Tooling layer so SDK consumers can override the MCP client initialization timeout (handshake/connection setup) via ToolOptions, helping environments where MCP initialization regularly exceeds the SDK default.

Changes:

  • Added ToolOptions.McpClientInitializationTimeoutSeconds (nullable) to allow overriding MCP initialization timeout.
  • When set, applies the timeout to both HttpClient.Timeout and McpClientOptions.InitializationTimeout during MCP client creation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/Tooling/Core/Models/ToolOptions.cs Introduces a new nullable option to configure MCP client initialization timeout (in seconds).
src/Tooling/Core/Services/McpToolServerConfigurationService.cs Applies the configured timeout when creating the HTTP client and MCP client options for MCP client initialization.

Comment thread src/Tooling/Core/Services/McpToolServerConfigurationService.cs
Comment thread src/Tooling/Core/Services/McpToolServerConfigurationService.cs Outdated
Comment thread src/Tooling/Core/Models/ToolOptions.cs
Comment thread src/Tooling/Core/Services/McpToolServerConfigurationService.cs Outdated
- Add input validation (1-600s range) with ArgumentOutOfRangeException
- Document valid range in ToolOptions XML docs
- Preserve original SDK behavior when timeout is null: call
  McpClientFactory.CreateAsync without McpClientOptions instead of
  passing a default instance
- Add 13 unit tests covering: null default, valid/invalid values,
  ArgumentOutOfRangeException for out-of-bounds inputs

Co-Authored-By: Claude Code <noreply@anthropic.com>
ajmfehr
ajmfehr previously approved these changes Apr 3, 2026
Copilot AI review requested due to automatic review settings April 15, 2026 17:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

Comment thread src/Tooling/Core/Services/McpToolServerConfigurationService.cs Outdated
Comment thread src/Tooling/Core/Services/McpToolServerConfigurationService.cs
Comment thread src/Tooling/Core/Services/McpToolServerConfigurationService.cs Outdated
Grant Harris (gwharris7) and others added 2 commits June 3, 2026 14:43
- Extract GetValidatedInitializationTimeout internal static helper so the
  timeout TimeSpan is computed once and reused for both HttpClient.Timeout
  and McpClientOptions.InitializationTimeout.
- Use nameof(ToolOptions.McpClientInitializationTimeoutSeconds) as the
  ArgumentOutOfRangeException ParamName so the offending property is
  obvious to consumers.
- Loosen the GetMcpClientToolsAsync exception assertion to accept either
  a direct ArgumentOutOfRangeException or one wrapped by
  InvalidOperationException so the test is decoupled from the outer
  catch behavior.
- Remove the unused IConfiguration mock field from the timeout test class.
- Add direct unit coverage for GetValidatedInitializationTimeout (null,
  valid, and invalid inputs) so the behavioral change is verifiable
  without a McpClientFactory seam.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/Tooling/Core/Services/McpToolServerConfigurationService.cs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 4, 2026 20:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@gwharris7
Grant Harris (gwharris7) enabled auto-merge (squash) June 8, 2026 17:04
auto-merge was automatically disabled June 8, 2026 17:42

Pull request was closed

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@gwharris7
Grant Harris (gwharris7) merged commit 8fc4373 into microsoft:main Jun 8, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants