-
Notifications
You must be signed in to change notification settings - Fork 8
Make MCP client initialization timeout configurable via ToolOptions #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using FluentAssertions; | ||
| using Microsoft.Agents.A365.Tooling.Models; | ||
| using Microsoft.Agents.A365.Tooling.Services; | ||
| using Microsoft.Agents.Builder; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Http; | ||
| using Microsoft.Extensions.Logging; | ||
| using Moq; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Agents.A365.Tooling.Tests.Services | ||
| { | ||
| /// <summary> | ||
| /// Unit tests for McpClientInitializationTimeoutSeconds in ToolOptions | ||
| /// and its application in McpToolServerConfigurationService. | ||
| /// </summary> | ||
| public class McpClientInitializationTimeoutTests | ||
| { | ||
| private readonly Mock<ILogger<IMcpToolServerConfigurationService>> _loggerMock; | ||
| private readonly Mock<IConfiguration> _configurationMock; | ||
| private readonly Mock<IServiceProvider> _serviceProviderMock; | ||
| private readonly Mock<IHttpClientFactory> _httpClientFactoryMock; | ||
|
|
||
| public McpClientInitializationTimeoutTests() | ||
| { | ||
| _loggerMock = new Mock<ILogger<IMcpToolServerConfigurationService>>(); | ||
| _configurationMock = new Mock<IConfiguration>(); | ||
| _serviceProviderMock = new Mock<IServiceProvider>(); | ||
| _httpClientFactoryMock = new Mock<IHttpClientFactory>(); | ||
|
|
||
| _httpClientFactoryMock.Setup(f => f.CreateClient(It.IsAny<string>())) | ||
| .Returns(new HttpClient()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToolOptions_McpClientInitializationTimeoutSeconds_DefaultsToNull() | ||
| { | ||
| // Arrange & Act | ||
| var options = new ToolOptions(); | ||
|
|
||
| // Assert | ||
| options.McpClientInitializationTimeoutSeconds.Should().BeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToolOptions_McpClientInitializationTimeoutSeconds_CanBeSetToValidValue() | ||
| { | ||
| // Arrange & Act | ||
| var options = new ToolOptions | ||
| { | ||
| McpClientInitializationTimeoutSeconds = 180 | ||
| }; | ||
|
|
||
| // Assert | ||
| options.McpClientInitializationTimeoutSeconds.Should().Be(180); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToolOptions_McpClientInitializationTimeoutSeconds_CanBeSetToNull() | ||
| { | ||
| // Arrange | ||
| var options = new ToolOptions | ||
| { | ||
| McpClientInitializationTimeoutSeconds = 120 | ||
| }; | ||
|
|
||
| // Act | ||
| options.McpClientInitializationTimeoutSeconds = null; | ||
|
|
||
| // Assert | ||
| options.McpClientInitializationTimeoutSeconds.Should().BeNull(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(0)] | ||
| [InlineData(-1)] | ||
| [InlineData(-100)] | ||
| [InlineData(601)] | ||
| [InlineData(int.MaxValue)] | ||
| public async Task GetMcpClientToolsAsync_InvalidTimeoutValue_ThrowsArgumentOutOfRangeException(int invalidTimeout) | ||
| { | ||
| // Arrange | ||
| var configMock = new Mock<IConfiguration>(); | ||
| configMock.Setup(c => c["ASPNETCORE_ENVIRONMENT"]).Returns("Development"); | ||
|
|
||
| var service = new McpToolServerConfigurationService( | ||
| _loggerMock.Object, | ||
| configMock.Object, | ||
| _serviceProviderMock.Object, | ||
| _httpClientFactoryMock.Object); | ||
|
|
||
| var serverConfig = new MCPServerConfig | ||
| { | ||
| mcpServerName = "test_server", | ||
| url = "https://localhost:52856/agents/servers/test_server", | ||
| id = "test-id", | ||
| scope = "test-scope", | ||
| audience = "test-audience", | ||
| publisher = "test-publisher", | ||
| }; | ||
|
|
||
| var toolOptions = new ToolOptions | ||
| { | ||
| McpClientInitializationTimeoutSeconds = invalidTimeout | ||
| }; | ||
|
|
||
| var turnContextMock = new Mock<ITurnContext>(); | ||
|
|
||
| // Act | ||
| Func<Task> act = async () => await service.GetMcpClientToolsAsync( | ||
| turnContextMock.Object, serverConfig, "test-token", toolOptions); | ||
|
|
||
| // Assert - ArgumentOutOfRangeException is wrapped in InvalidOperationException by the catch block | ||
| var ex = await act.Should().ThrowAsync<InvalidOperationException>(); | ||
| ex.WithInnerException<ArgumentOutOfRangeException>(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(1)] | ||
| [InlineData(60)] | ||
| [InlineData(120)] | ||
| [InlineData(300)] | ||
| [InlineData(600)] | ||
| public void ToolOptions_McpClientInitializationTimeoutSeconds_AcceptsValidValues(int validTimeout) | ||
| { | ||
| // Arrange & Act | ||
| var options = new ToolOptions | ||
| { | ||
| McpClientInitializationTimeoutSeconds = validTimeout | ||
| }; | ||
|
|
||
| // Assert | ||
| options.McpClientInitializationTimeoutSeconds.Should().Be(validTimeout); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -503,10 +503,36 @@ private async Task<IMcpClient> CreateMcpClientWithAuthHandlers(ITurnContext turn | |
| // Create HTTP client with the authentication handler chain | ||
| var httpClient = new HttpClient(loggingHandler); | ||
|
|
||
| // Apply custom timeout only when explicitly configured | ||
| if (toolOptions.McpClientInitializationTimeoutSeconds.HasValue) | ||
| { | ||
| var timeoutSeconds = toolOptions.McpClientInitializationTimeoutSeconds.Value; | ||
| if (timeoutSeconds < 1 || timeoutSeconds > 600) | ||
| { | ||
| throw new ArgumentOutOfRangeException( | ||
| nameof(toolOptions), | ||
| timeoutSeconds, | ||
| "McpClientInitializationTimeoutSeconds must be between 1 and 600 seconds."); | ||
| } | ||
|
|
||
| httpClient.Timeout = TimeSpan.FromSeconds(timeoutSeconds); | ||
| } | ||
|
Comment on lines
+506
to
+519
|
||
|
|
||
| var clientTransport = new SseClientTransport(options, httpClient); | ||
|
|
||
| try | ||
| { | ||
| // Only pass McpClientOptions when a custom timeout is set to preserve default SDK behavior | ||
| if (toolOptions.McpClientInitializationTimeoutSeconds.HasValue) | ||
| { | ||
| var clientOptions = new McpClientOptions | ||
| { | ||
| InitializationTimeout = TimeSpan.FromSeconds(toolOptions.McpClientInitializationTimeoutSeconds.Value), | ||
| }; | ||
|
|
||
| return await McpClientFactory.CreateAsync(clientTransport, clientOptions, loggerFactory: this._loggerFactory); | ||
| } | ||
|
|
||
| return await McpClientFactory.CreateAsync(clientTransport, loggerFactory: this._loggerFactory); | ||
| } | ||
| catch (Exception ex) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
McpClientInitializationTimeoutSecondsproperty doesn’t document or enforce an allowed range. Since invalid values (<= 0, extremely large) can cause runtime exceptions when converted to aTimeSpanor applied toHttpClient.Timeout, it would help to document the expected bounds here (and ideally validate it at the call site).