forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamClientTransport.cs
More file actions
55 lines (49 loc) · 1.97 KB
/
StreamClientTransport.cs
File metadata and controls
55 lines (49 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Client;
namespace ModelContextProtocol.Protocol;
/// <summary>
/// Provides an <see cref="IClientTransport"/> implemented around a pair of input/output streams.
/// </summary>
/// <remarks>
/// This transport is useful for scenarios where you already have established streams for communication,
/// such as custom network protocols, pipe connections, or for testing purposes. It works with any
/// readable and writable streams.
/// </remarks>
public sealed class StreamClientTransport : IClientTransport
{
private readonly Stream _serverInput;
private readonly Stream _serverOutput;
private readonly ILoggerFactory? _loggerFactory;
/// <summary>
/// Initializes a new instance of the <see cref="StreamClientTransport"/> class.
/// </summary>
/// <param name="serverInput">
/// The stream representing the connected server's input.
/// Writes to this stream will be sent to the server.
/// </param>
/// <param name="serverOutput">
/// The stream representing the connected server's output.
/// Reads from this stream will receive messages from the server.
/// </param>
/// <param name="loggerFactory">A logger factory for creating loggers.</param>
public StreamClientTransport(
Stream serverInput, Stream serverOutput, ILoggerFactory? loggerFactory = null)
{
Throw.IfNull(serverInput);
Throw.IfNull(serverOutput);
_serverInput = serverInput;
_serverOutput = serverOutput;
_loggerFactory = loggerFactory;
}
/// <inheritdoc />
public string Name => "in-memory-stream";
/// <inheritdoc />
public Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult<ITransport>(new StreamClientSessionTransport(
new StreamWriter(_serverInput),
new StreamReader(_serverOutput),
"Client (stream)",
_loggerFactory));
}
}