Add a callback for receiving stderr output (#525)
* Add a callback for receiving stderr output * Add asserts to test
This commit is contained in:
parent
4e2c1b949f
commit
b9b4a23005
3 changed files with 35 additions and 0 deletions
|
|
@ -146,6 +146,8 @@ public sealed partial class StdioClientTransport : IClientTransport
|
||||||
stderrRollingLog.Enqueue(data);
|
stderrRollingLog.Enqueue(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_options.StandardErrorLines?.Invoke(data);
|
||||||
|
|
||||||
LogReadStderr(logger, endpointName, data);
|
LogReadStderr(logger, endpointName, data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -69,4 +69,9 @@ public sealed class StdioClientTransportOptions
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public TimeSpan ShutdownTimeout { get; set; } = TimeSpan.FromSeconds(5);
|
public TimeSpan ShutdownTimeout { get; set; } = TimeSpan.FromSeconds(5);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a callback that is invoked for each line of stderr received from the server process.
|
||||||
|
/// </summary>
|
||||||
|
public Action<string>? StandardErrorLines { get; set; }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
using ModelContextProtocol.Client;
|
using ModelContextProtocol.Client;
|
||||||
using ModelContextProtocol.Tests.Utils;
|
using ModelContextProtocol.Tests.Utils;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace ModelContextProtocol.Tests.Transport;
|
namespace ModelContextProtocol.Tests.Transport;
|
||||||
|
|
||||||
|
|
@ -18,4 +19,31 @@ public class StdioClientTransportTests(ITestOutputHelper testOutputHelper) : Log
|
||||||
IOException e = await Assert.ThrowsAsync<IOException>(() => McpClientFactory.CreateAsync(transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken));
|
IOException e = await Assert.ThrowsAsync<IOException>(() => McpClientFactory.CreateAsync(transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken));
|
||||||
Assert.Contains(id, e.ToString());
|
Assert.Contains(id, e.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CreateAsync_ValidProcessInvalidServer_StdErrCallbackInvoked()
|
||||||
|
{
|
||||||
|
string id = Guid.NewGuid().ToString("N");
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
StringBuilder sb = new();
|
||||||
|
Action<string> stdErrCallback = line =>
|
||||||
|
{
|
||||||
|
Assert.NotNull(line);
|
||||||
|
lock (sb)
|
||||||
|
{
|
||||||
|
sb.AppendLine(line);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
StdioClientTransport transport = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
|
||||||
|
new(new() { Command = "cmd", Arguments = ["/C", $"echo \"{id}\" >&2"], StandardErrorLines = stdErrCallback }, LoggerFactory) :
|
||||||
|
new(new() { Command = "ls", Arguments = [id], StandardErrorLines = stdErrCallback }, LoggerFactory);
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<IOException>(() => McpClientFactory.CreateAsync(transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken));
|
||||||
|
|
||||||
|
Assert.InRange(count, 1, int.MaxValue);
|
||||||
|
Assert.Contains(id, sb.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue