Class StreamServerTransport
- Namespace
- ModelContextProtocol.Protocol.Transport
- Assembly
- ModelContextProtocol.dll
Provides an ITransport implemented using a pair of input and output streams.
public class StreamServerTransport : TransportBase, ITransport, IAsyncDisposable
- Inheritance
-
StreamServerTransport
- Implements
- Derived
- Inherited Members
Remarks
The StreamServerTransport class implements bidirectional JSON-RPC messaging over arbitrary streams, allowing MCP communication with clients through various I/O channels such as network sockets, memory streams, or pipes.
Constructors
StreamServerTransport(Stream, Stream, string?, ILoggerFactory?)
Initializes a new instance of the StreamServerTransport class with explicit input/output streams.
public StreamServerTransport(Stream inputStream, Stream outputStream, string? serverName = null, ILoggerFactory? loggerFactory = null)
Parameters
inputStream
StreamThe input Stream to use as standard input.
outputStream
StreamThe output Stream to use as standard output.
serverName
stringOptional name of the server, used for diagnostic purposes, like logging.
loggerFactory
ILoggerFactoryOptional logger factory used for logging employed by the transport.
Exceptions
- ArgumentNullException
inputStream
is null.- ArgumentNullException
outputStream
is null.
Methods
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public override ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
SendMessageAsync(IJsonRpcMessage, CancellationToken)
Sends a JSON-RPC message through the transport.
public override Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)
Parameters
message
IJsonRpcMessageThe JSON-RPC message to send.
cancellationToken
CancellationTokenThe CancellationToken to monitor for cancellation requests. The default is None.
Returns
- Task
A task that represents the asynchronous send operation.
Remarks
This method serializes and sends the provided JSON-RPC message through the transport connection.
This is a core method used by higher-level abstractions in the MCP protocol implementation. Most client code should use the higher-level methods provided by IMcpEndpoint, McpEndpointExtensions, McpClientExtensions, or McpServerExtensions, rather than accessing this method directly.
Exceptions
- InvalidOperationException
The transport is not connected.