using Microsoft.Extensions.AI; using Microsoft.Extensions.Logging; using ModelContextProtocol.Protocol; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Text.Json; namespace ModelContextProtocol.Server; /// /// Provides extension methods for interacting with an instance. /// public static class McpServerExtensions { /// /// Requests to sample an LLM via the client using the specified request parameters. /// /// The server instance initiating the request. /// The parameters for the sampling request. /// The to monitor for cancellation requests. /// A task containing the sampling result from the client. /// is . /// The client does not support sampling. /// /// This method requires the client to support sampling capabilities. /// It allows detailed control over sampling parameters including messages, system prompt, temperature, /// and token limits. /// [Obsolete($"Use {nameof(McpServer)}.{nameof(McpServer.SampleAsync)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774 [EditorBrowsable(EditorBrowsableState.Never)] public static ValueTask SampleAsync( this IMcpServer server, CreateMessageRequestParams request, CancellationToken cancellationToken = default) => AsServerOrThrow(server).SampleAsync(request, cancellationToken); /// /// Requests to sample an LLM via the client using the provided chat messages and options. /// /// The server initiating the request. /// The messages to send as part of the request. /// The options to use for the request, including model parameters and constraints. /// The to monitor for cancellation requests. The default is . /// A task containing the chat response from the model. /// is . /// is . /// The client does not support sampling. /// /// This method converts the provided chat messages into a format suitable for the sampling API, /// handling different content types such as text, images, and audio. /// [Obsolete($"Use {nameof(McpServer)}.{nameof(McpServer.SampleAsync)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774 [EditorBrowsable(EditorBrowsableState.Never)] public static Task SampleAsync( this IMcpServer server, IEnumerable messages, ChatOptions? options = default, CancellationToken cancellationToken = default) => AsServerOrThrow(server).SampleAsync(messages, options, cancellationToken); /// /// Creates an wrapper that can be used to send sampling requests to the client. /// /// The server to be wrapped as an . /// The that can be used to issue sampling requests to the client. /// is . /// The client does not support sampling. [Obsolete($"Use {nameof(McpServer)}.{nameof(McpServer.AsSamplingChatClient)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774 [EditorBrowsable(EditorBrowsableState.Never)] public static IChatClient AsSamplingChatClient(this IMcpServer server) => AsServerOrThrow(server).AsSamplingChatClient(); /// Gets an on which logged messages will be sent as notifications to the client. /// The server to wrap as an . /// An that can be used to log to the client.. [Obsolete($"Use {nameof(McpServer)}.{nameof(McpServer.AsSamplingChatClient)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774 [EditorBrowsable(EditorBrowsableState.Never)] public static ILoggerProvider AsClientLoggerProvider(this IMcpServer server) => AsServerOrThrow(server).AsClientLoggerProvider(); /// /// Requests the client to list the roots it exposes. /// /// The server initiating the request. /// The parameters for the list roots request. /// The to monitor for cancellation requests. /// A task containing the list of roots exposed by the client. /// is . /// The client does not support roots. /// /// This method requires the client to support the roots capability. /// Root resources allow clients to expose a hierarchical structure of resources that can be /// navigated and accessed by the server. These resources might include file systems, databases, /// or other structured data sources that the client makes available through the protocol. /// [Obsolete($"Use {nameof(McpServer)}.{nameof(McpServer.RequestRootsAsync)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774 [EditorBrowsable(EditorBrowsableState.Never)] public static ValueTask RequestRootsAsync( this IMcpServer server, ListRootsRequestParams request, CancellationToken cancellationToken = default) => AsServerOrThrow(server).RequestRootsAsync(request, cancellationToken); /// /// Requests additional information from the user via the client, allowing the server to elicit structured data. /// /// The server initiating the request. /// The parameters for the elicitation request. /// The to monitor for cancellation requests. /// A task containing the elicitation result. /// is . /// The client does not support elicitation. /// /// This method requires the client to support the elicitation capability. /// [Obsolete($"Use {nameof(McpServer)}.{nameof(McpServer.ElicitAsync)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774 [EditorBrowsable(EditorBrowsableState.Never)] public static ValueTask ElicitAsync( this IMcpServer server, ElicitRequestParams request, CancellationToken cancellationToken = default) => AsServerOrThrow(server).ElicitAsync(request, cancellationToken); [MethodImpl(MethodImplOptions.AggressiveInlining)] #pragma warning disable CS0618 // Type or member is obsolete private static McpServer AsServerOrThrow(IMcpServer server, [CallerMemberName] string memberName = "") #pragma warning restore CS0618 // Type or member is obsolete { if (server is not McpServer mcpServer) { ThrowInvalidSessionType(memberName); } return mcpServer; [DoesNotReturn] [MethodImpl(MethodImplOptions.NoInlining)] static void ThrowInvalidSessionType(string memberName) => throw new InvalidOperationException( $"Only arguments assignable to '{nameof(McpServer)}' are supported. " + $"Prefer using '{nameof(McpServer)}.{memberName}' instead, as " + $"'{nameof(McpServerExtensions)}.{memberName}' is obsolete and will be " + $"removed in the future."); } }