Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Reflection;
using System.Text.Json;
using Azure.Mcp.Tests;
using Azure.Mcp.Tests.Client;
using Azure.Mcp.Tests.Client.Helpers;
using ModelContextProtocol;
using ModelContextProtocol.Client;
Expand All @@ -12,15 +12,12 @@

namespace Azure.Mcp.Core.LiveTests;

public class ClientToolTests : IAsyncLifetime
public class ClientToolTests : CommandTestsBase
{
private readonly ITestOutputHelper _output;
private IMcpClient _client = default!;
private LiveTestSettings _settings = default!;

public ClientToolTests(ITestOutputHelper output)
public ClientToolTests(ITestOutputHelper output) : base(output)
{
_output = output;
// Set arguments for all mode with debug logging
SetArguments("server", "start", "--mode", "all", "--debug");
}

public async ValueTask InitializeAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,23 @@ public virtual async ValueTask InitializeAsync()
Name = "Test Server",
Command = executablePath,
Arguments = arguments,
// Redirect stderr to shared log for later output during test failure
// Redirect stderr to both shared log and current test output
StandardErrorLines = line =>
{
lock (_lock)
{
_serverErrorLog.Add(line);
}

// Also output to current test's output helper for immediate visibility
try
{
Output.WriteLine($"[MCP Server] {line}");
}
catch
{
// Ignore if output helper is not available (e.g., during disposal)
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Reflection;
using ModelContextProtocol.Client;
using Xunit;

namespace Azure.Mcp.Tests.Client.Helpers;

Expand All @@ -11,6 +12,7 @@ public class LiveTestFixture : LiveTestSettingsFixture
public IMcpClient Client { get; private set; } = default!;

private string[]? _customArguments;
private readonly List<string> _serverErrors = new();

/// <summary>
/// Sets custom arguments for the MCP server. Call this before InitializeAsync().
Expand All @@ -21,6 +23,11 @@ public void SetArguments(params string[] arguments)
_customArguments = arguments;
}

/// <summary>
/// Gets any stderr output from the MCP server for debugging purposes.
/// </summary>
public IReadOnlyList<string> ServerErrors => _serverErrors.AsReadOnly();

public override async ValueTask InitializeAsync()
{
await base.InitializeAsync();
Expand All @@ -35,7 +42,24 @@ public override async ValueTask InitializeAsync()
{
Name = "Test Server",
Command = executablePath,
Arguments = arguments
Arguments = arguments,
// Capture stderr output for debugging
StandardErrorLines = line =>
{
_serverErrors.Add(line);
// Also output to test context if available for immediate visibility
try
{
if (TestContext.Current != null)
{
System.Diagnostics.Debug.WriteLine($"[MCP Server] {line}");
}
}
catch
{
// Ignore if TestContext is not available
}
}
};

if (!string.IsNullOrEmpty(Settings.TestPackage))
Expand All @@ -47,6 +71,26 @@ public override async ValueTask InitializeAsync()

var clientTransport = new StdioClientTransport(transportOptions);

Client = await McpClientFactory.CreateAsync(clientTransport);
try
{
// Add timeout protection like in CommandTestsBase
using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(2));
var clientTask = McpClientFactory.CreateAsync(clientTransport);
Client = await clientTask.WaitAsync(cts.Token);
}
catch (TimeoutException)
{
var errorSummary = _serverErrors.Count > 0 ?
$"Server errors: {string.Join("; ", _serverErrors.TakeLast(5))}" :
"No server error output captured";
throw new TimeoutException($"MCP client initialization timed out after 2 minutes. {errorSummary}");
}
catch (OperationCanceledException)
{
var errorSummary = _serverErrors.Count > 0 ?
$"Server errors: {string.Join("; ", _serverErrors.TakeLast(5))}" :
"No server error output captured";
throw new OperationCanceledException($"MCP client initialization was cancelled or timed out. {errorSummary}");
}
}
}
Empty file.
Loading