connectServer method

ServerConnection connectServer(
  1. StreamChannel<Map<String, Object?>> channel, {
  2. Sink<String>? protocolLogSink,
})
inherited

Returns a connection for an MCP server using a channel, which is already established.

Each map sent over channel is a single decoded JSON-RPC message.

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<Map<String, Object?>> 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,
    elicitationFormSupport: self is ElicitationFormSupport ? self : null,
    elicitationUrlSupport: self is ElicitationUrlSupport ? self : null,
  );
  connections.add(connection);
  channel.sink.done.then((_) => connections.remove(connection));
  return connection;
}