Class StreamServerTransport
- Namespace
- ModelContextProtocol.Server
- Assembly
- ModelContextProtocol.Core.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
inputStreamStreamThe input Stream to use as standard input.
outputStreamStreamThe output Stream to use as standard output.
serverNamestringOptional name of the server, used for diagnostic purposes, like logging.
loggerFactoryILoggerFactoryOptional logger factory used for logging employed by the transport.
Exceptions
- ArgumentNullException
inputStreamoroutputStreamis 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(JsonRpcMessage, CancellationToken)
Sends a JSON-RPC message through the transport.
public override Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default)
Parameters
messageJsonRpcMessageThe JSON-RPC message to send.
cancellationTokenCancellationTokenThe 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 McpSession, McpClient, or McpServer, rather than accessing this method directly.
Exceptions
- InvalidOperationException
The transport is not connected.