ServerConnection.fromStreamChannel constructor

ServerConnection.fromStreamChannel(
  1. StreamChannel<Map<String, Object?>> channel, {
  2. Sink<String>? protocolLogSink,
  3. RootsSupport? rootsSupport,
  4. SamplingSupport? samplingSupport,
  5. @Deprecated('Use elicitationFormSupport instead') ElicitationSupport? elicitationSupport,
  6. ElicitationFormSupport? elicitationFormSupport,
  7. ElicitationUrlSupport? elicitationUrlSupport,
})

A 1:1 connection from a client to a server using channel.

If the client supports "roots", then it should provide an implementation through rootsSupport.

If the client supports "sampling", then it should provide an implementation through samplingSupport.

Implementation

ServerConnection.fromStreamChannel(
  super.channel, {
  super.protocolLogSink,
  RootsSupport? rootsSupport,
  SamplingSupport? samplingSupport,
  @Deprecated('Use elicitationFormSupport instead')
  ElicitationSupport? elicitationSupport,
  ElicitationFormSupport? elicitationFormSupport,
  ElicitationUrlSupport? elicitationUrlSupport,
}) : _elicitationFormSupport = elicitationFormSupport ?? elicitationSupport,
     _elicitationUrlSupport = elicitationUrlSupport,
     _samplingSupport = samplingSupport,
     _rootsSupport = rootsSupport,
     _requestCancellation =
         channel is RequestCancellation
             ? channel as RequestCancellation
             : null {
  if (rootsSupport != null) {
    registerRequestHandler(
      ListRootsRequest.methodName,
      rootsSupport.handleListRoots,
    );
  }

  if (samplingSupport != null) {
    registerRequestHandler(
      CreateMessageRequest.methodName,
      (CreateMessageRequest request) =>
          samplingSupport.handleCreateMessage(request, _samplingServerInfo),
    );
  }

  if (_elicitationFormSupport != null || _elicitationUrlSupport != null) {
    registerRequestHandler(ElicitRequest.methodName, (ElicitRequest request) {
      final badMode = _invalidElicitationMode(request);
      if (badMode != null) throw RpcException.invalidParams(badMode);
      switch (request.mode) {
        case ElicitationMode.form:
          final formSupport = _elicitationFormSupport;
          if (formSupport == null) {
            throw RpcException.invalidParams(
              'This client did not declare the elicitation.form capability',
            );
          }
          return formSupport.handleElicitation(request, this);
        case ElicitationMode.url:
          final urlSupport = _elicitationUrlSupport;
          if (urlSupport == null) {
            throw RpcException.invalidParams(
              'This client did not declare the elicitation.url capability',
            );
          }
          return urlSupport.handleElicitation(request, this);
      }
    });
  }

  // Subscription notifications reach these handlers on both transports,
  // since the Streamable HTTP client merges each listen response into this
  // connection's incoming stream. They also go to the matching subscription.
  registerNotificationHandler<PromptListChangedNotification?>(
    PromptListChangedNotification.methodName,
    (notification) {
      _responseCache.invalidateMethod(ListPromptsRequest.methodName);
      _forwardSubscriptionNotification(
        PromptListChangedNotification.methodName,
        notification,
      );
      _promptListChangedController.sink.add(notification);
    },
  );

  registerNotificationHandler<ToolListChangedNotification?>(
    ToolListChangedNotification.methodName,
    (notification) {
      _responseCache.invalidateMethod(ListToolsRequest.methodName);
      _forwardSubscriptionNotification(
        ToolListChangedNotification.methodName,
        notification,
      );
      _toolListChangedController.sink.add(notification);
    },
  );

  registerNotificationHandler<ResourceListChangedNotification?>(
    ResourceListChangedNotification.methodName,
    (notification) {
      _responseCache
        ..invalidateMethod(ListResourcesRequest.methodName)
        ..invalidateMethod(ListResourceTemplatesRequest.methodName);
      _forwardSubscriptionNotification(
        ResourceListChangedNotification.methodName,
        notification,
      );
      _resourceListChangedController.sink.add(notification);
    },
  );

  registerNotificationHandler<ResourceUpdatedNotification>(
    ResourceUpdatedNotification.methodName,
    (notification) {
      _responseCache.invalidateResource(notification.uri);
      _forwardSubscriptionNotification(
        ResourceUpdatedNotification.methodName,
        notification,
      );
      _resourceUpdatedController.sink.add(notification);
    },
  );

  registerNotificationHandler(
    LoggingMessageNotification.methodName,
    _logController.sink.add,
  );

  registerNotificationHandler(
    ElicitationCompleteNotification.methodName,
    _elicitationCompleteController.sink.add,
  );

  registerNotificationHandler<SubscriptionsAcknowledgedNotification>(
    SubscriptionsAcknowledgedNotification.methodName,
    _handleSubscriptionsAcknowledged,
  );
  cancellations.listen(_handleSubscriptionCancelled);
}