connectServer method
Returns a connection for an MCP server using a channel
, which is already
established.
Each String sent over channel
represents an entire JSON request or
response.
If protocolLogSink
is provided, all messages sent on channel
will be
forwarded to that Sink as well, with <<<
preceding incoming messages
and >>>
preceding outgoing messages. It is the responsibility of the
caller to close this sink.
To perform cleanup when this connection is closed, use the
ServerConnection.done
future.
Implementation
ServerConnection connectServer(
StreamChannel<String> channel, {
Sink<String>? protocolLogSink,
}) {
// For type promotion in this function.
final self = this;
final connection = ServerConnection.fromStreamChannel(
channel,
protocolLogSink: protocolLogSink,
rootsSupport: self is RootsSupport ? self : null,
samplingSupport: self is SamplingSupport ? self : null,
elicitationSupport: self is ElicitationSupport ? self : null,
);
connections.add(connection);
channel.sink.done.then((_) => connections.remove(connection));
return connection;
}