Remove obsolete APIs from codebase and standardize MCP diagnostic IDs (#985)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: MackinnonBuck <10456961+MackinnonBuck@users.noreply.github.com>
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Co-authored-by: Mackinnon Buck <mackinnon.buck@gmail.com>
Co-authored-by: jeffhandley <1031940+jeffhandley@users.noreply.github.com>
Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com>
This commit is contained in:
Copilot 2025-12-02 10:28:46 -08:00 • committed by GitHub
commit 136755f2be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 663 additions and 2674 deletions

View file

@ -1,65 +0,0 @@
using ModelContextProtocol.Protocol;
using System.ComponentModel;
namespace ModelContextProtocol.Server;
/// <summary>
/// Represents an instance of a Model Context Protocol (MCP) server that connects to and communicates with an MCP client.
/// </summary>
[Obsolete($"Use {nameof(McpServer)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IMcpServer : IMcpEndpoint
{
/// <summary>
/// Gets the capabilities supported by the client.
/// </summary>
/// <remarks>
/// <para>
/// These capabilities are established during the initialization handshake and indicate
/// which features the client supports, such as sampling, roots, and other
/// protocol-specific functionality.
/// </para>
/// <para>
/// Server implementations can check these capabilities to determine which features
/// are available when interacting with the client.
/// </para>
/// </remarks>
ClientCapabilities? ClientCapabilities { get; }
/// <summary>
/// Gets the version and implementation information of the connected client.
/// </summary>
/// <remarks>
/// <para>
/// This property contains identification information about the client that has connected to this server,
/// including its name and version. This information is provided by the client during initialization.
/// </para>
/// <para>
/// Server implementations can use this information for logging, tracking client versions,
/// or implementing client-specific behaviors.
/// </para>
/// </remarks>
Implementation? ClientInfo { get; }
/// <summary>
/// Gets the options used to construct this server.
/// </summary>
/// <remarks>
/// These options define the server's capabilities, protocol version, and other configuration
/// settings that were used to initialize the server.
/// </remarks>
McpServerOptions ServerOptions { get; }
/// <summary>
/// Gets the service provider for the server.
/// </summary>
IServiceProvider? Services { get; }
/// <summary>Gets the last logging level set by the client, or <see langword="null"/> if it's never been set.</summary>
LoggingLevel? LoggingLevel { get; }
/// <summary>
/// Runs the server, listening for and handling client requests.
/// </summary>
Task RunAsync(CancellationToken cancellationToken = default);
}

View file

@ -14,9 +14,7 @@ namespace ModelContextProtocol.Server;
/// <summary>
/// Represents an instance of a Model Context Protocol (MCP) server that connects to and communicates with an MCP client.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
public abstract partial class McpServer : McpSession, IMcpServer
#pragma warning restore CS0618 // Type or member is obsolete
public abstract partial class McpServer : McpSession
{
/// <summary>
/// Caches request schemas for elicitation requests based on the type and serializer options.

View file

@ -5,9 +5,7 @@ namespace ModelContextProtocol.Server;
/// <summary>
/// Represents an instance of a Model Context Protocol (MCP) server that connects to and communicates with an MCP client.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
public abstract partial class McpServer : McpSession, IMcpServer
#pragma warning restore CS0618 // Type or member is obsolete
public abstract partial class McpServer : McpSession
{
/// <summary>
/// Gets the capabilities supported by the client.

View file

@ -1,138 +0,0 @@
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;
/// <summary>
/// Provides extension methods for interacting with an <see cref="IMcpServer"/> instance.
/// </summary>
public static class McpServerExtensions
{
/// <summary>
/// Requests to sample an LLM via the client using the specified request parameters.
/// </summary>
/// <param name="server">The server instance initiating the request.</param>
/// <param name="request">The parameters for the sampling request.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param>
/// <returns>A task containing the sampling result from the client.</returns>
/// <exception cref="ArgumentNullException"><paramref name="server"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The client does not support sampling.</exception>
/// <remarks>
/// This method requires the client to support sampling capabilities.
/// It allows detailed control over sampling parameters including messages, system prompt, temperature,
/// and token limits.
/// </remarks>
[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<CreateMessageResult> SampleAsync(
this IMcpServer server, CreateMessageRequestParams request, CancellationToken cancellationToken = default)
=> AsServerOrThrow(server).SampleAsync(request, cancellationToken);
/// <summary>
/// Requests to sample an LLM via the client using the provided chat messages and options.
/// </summary>
/// <param name="server">The server initiating the request.</param>
/// <param name="messages">The messages to send as part of the request.</param>
/// <param name="options">The options to use for the request, including model parameters and constraints.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>A task containing the chat response from the model.</returns>
/// <exception cref="ArgumentNullException"><paramref name="server"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="messages"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The client does not support sampling.</exception>
/// <remarks>
/// 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.
/// </remarks>
[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<ChatResponse> SampleAsync(
this IMcpServer server,
IEnumerable<ChatMessage> messages, ChatOptions? options = default, CancellationToken cancellationToken = default)
=> AsServerOrThrow(server).SampleAsync(messages, options, cancellationToken);
/// <summary>
/// Creates an <see cref="IChatClient"/> wrapper that can be used to send sampling requests to the client.
/// </summary>
/// <param name="server">The server to be wrapped as an <see cref="IChatClient"/>.</param>
/// <returns>The <see cref="IChatClient"/> that can be used to issue sampling requests to the client.</returns>
/// <exception cref="ArgumentNullException"><paramref name="server"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The client does not support sampling.</exception>
[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();
/// <summary>Gets an <see cref="ILogger"/> on which logged messages will be sent as notifications to the client.</summary>
/// <param name="server">The server to wrap as an <see cref="ILogger"/>.</param>
/// <returns>An <see cref="ILogger"/> that can be used to log to the client..</returns>
[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();
/// <summary>
/// Requests the client to list the roots it exposes.
/// </summary>
/// <param name="server">The server initiating the request.</param>
/// <param name="request">The parameters for the list roots request.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param>
/// <returns>A task containing the list of roots exposed by the client.</returns>
/// <exception cref="ArgumentNullException"><paramref name="server"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The client does not support roots.</exception>
/// <remarks>
/// 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.
/// </remarks>
[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<ListRootsResult> RequestRootsAsync(
this IMcpServer server, ListRootsRequestParams request, CancellationToken cancellationToken = default)
=> AsServerOrThrow(server).RequestRootsAsync(request, cancellationToken);
/// <summary>
/// Requests additional information from the user via the client, allowing the server to elicit structured data.
/// </summary>
/// <param name="server">The server initiating the request.</param>
/// <param name="request">The parameters for the elicitation request.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param>
/// <returns>A task containing the elicitation result.</returns>
/// <exception cref="ArgumentNullException"><paramref name="server"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The client does not support elicitation.</exception>
/// <remarks>
/// This method requires the client to support the elicitation capability.
/// </remarks>
[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<ElicitResult> 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.");
}
}

View file

@ -1,34 +0,0 @@
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Protocol;
using System.ComponentModel;
namespace ModelContextProtocol.Server;
/// <summary>
/// Provides a factory for creating <see cref="IMcpServer"/> instances.
/// </summary>
/// <remarks>
/// This is the recommended way to create <see cref="IMcpServer"/> instances.
/// The factory handles proper initialization of server instances with the required dependencies.
/// </remarks>
[Obsolete($"Use {nameof(McpServer)}.{nameof(McpServer.Create)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774
[EditorBrowsable(EditorBrowsableState.Never)]
public static class McpServerFactory
{
/// <summary>
/// Creates a new instance of an <see cref="IMcpServer"/>.
/// </summary>
/// <param name="transport">Transport to use for the server representing an already-established MCP session.</param>
/// <param name="serverOptions">Configuration options for this server, including capabilities. </param>
/// <param name="loggerFactory">Logger factory to use for logging. If null, logging will be disabled.</param>
/// <param name="serviceProvider">Optional service provider to create new instances of tools and other dependencies.</param>
/// <returns>An <see cref="IMcpServer"/> instance that should be disposed when no longer needed.</returns>
/// <exception cref="ArgumentNullException"><paramref name="transport"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="serverOptions"/> is <see langword="null"/>.</exception>
public static IMcpServer Create(
ITransport transport,
McpServerOptions serverOptions,
ILoggerFactory? loggerFactory = null,
IServiceProvider? serviceProvider = null)
=> McpServer.Create(transport, serverOptions, loggerFactory, serviceProvider);
}

View file

@ -169,7 +169,7 @@ public sealed class McpServerHandlers
/// </para>
/// <para>
/// Handlers provided via <see cref="NotificationHandlers"/> will be registered with the server for the lifetime of the server.
/// For transient handlers, <see cref="IMcpEndpoint.RegisterNotificationHandler"/> may be used to register a handler that can
/// For transient handlers, <see cref="McpSession.RegisterNotificationHandler"/> may be used to register a handler that can
/// then be unregistered by disposing of the <see cref="IAsyncDisposable"/> returned from the method.
/// </para>
/// </remarks>

View file

@ -230,10 +230,6 @@ internal sealed partial class McpServerImpl : McpServer
var completeHandler = options.Handlers.CompleteHandler;
var completionsCapability = options.Capabilities?.Completions;
#pragma warning disable CS0618 // Type or member is obsolete
completeHandler ??= completionsCapability?.CompleteHandler;
#pragma warning restore CS0618 // Type or member is obsolete
if (completeHandler is null && completionsCapability is null)
{
return;
@ -266,14 +262,6 @@ internal sealed partial class McpServerImpl : McpServer
var resources = options.ResourceCollection;
var resourcesCapability = options.Capabilities?.Resources;
#pragma warning disable CS0618 // Type or member is obsolete
listResourcesHandler ??= resourcesCapability?.ListResourcesHandler;
listResourceTemplatesHandler ??= resourcesCapability?.ListResourceTemplatesHandler;
readResourceHandler ??= resourcesCapability?.ReadResourceHandler;
subscribeHandler ??= resourcesCapability?.SubscribeToResourcesHandler;
unsubscribeHandler ??= resourcesCapability?.UnsubscribeFromResourcesHandler;
#pragma warning restore CS0618 // Type or member is obsolete
if (listResourcesHandler is null && listResourceTemplatesHandler is null && readResourceHandler is null &&
subscribeHandler is null && unsubscribeHandler is null && resources is null &&
resourcesCapability is null)
@ -427,11 +415,6 @@ internal sealed partial class McpServerImpl : McpServer
var prompts = options.PromptCollection;
var promptsCapability = options.Capabilities?.Prompts;
#pragma warning disable CS0618 // Type or member is obsolete
listPromptsHandler ??= promptsCapability?.ListPromptsHandler;
getPromptHandler ??= promptsCapability?.GetPromptHandler;
#pragma warning restore CS0618 // Type or member is obsolete
if (listPromptsHandler is null && getPromptHandler is null && prompts is null &&
promptsCapability is null)
{
@ -515,11 +498,6 @@ internal sealed partial class McpServerImpl : McpServer
var tools = options.ToolCollection;
var toolsCapability = options.Capabilities?.Tools;
#pragma warning disable CS0618 // Type or member is obsolete
listToolsHandler ??= toolsCapability?.ListToolsHandler;
callToolHandler ??= toolsCapability?.CallToolHandler;
#pragma warning restore CS0618 // Type or member is obsolete
if (listToolsHandler is null && callToolHandler is null && tools is null &&
toolsCapability is null)
{
@ -618,10 +596,6 @@ internal sealed partial class McpServerImpl : McpServer
// We don't require that the handler be provided, as we always store the provided log level to the server.
var setLoggingLevelHandler = options.Handlers.SetLoggingLevelHandler;
#pragma warning disable CS0618 // Type or member is obsolete
setLoggingLevelHandler ??= options.Capabilities?.Logging?.SetLoggingLevelHandler;
#pragma warning restore CS0618 // Type or member is obsolete
// Apply filters to the handler
if (setLoggingLevelHandler is not null)
{

View file

@ -19,18 +19,13 @@ internal sealed class RequestServiceProvider<TRequestParams>(RequestContext<TReq
public static bool IsAugmentedWith(Type serviceType) =>
serviceType == typeof(RequestContext<TRequestParams>) ||
serviceType == typeof(McpServer) ||
#pragma warning disable CS0618 // Type or member is obsolete
serviceType == typeof(IMcpServer) ||
#pragma warning restore CS0618 // Type or member is obsolete
serviceType == typeof(IProgress<ProgressNotificationValue>) ||
serviceType == typeof(ClaimsPrincipal);
/// <inheritdoc />
public object? GetService(Type serviceType) =>
serviceType == typeof(RequestContext<TRequestParams>) ? request :
#pragma warning disable CS0618 // Type or member is obsolete
serviceType == typeof(McpServer) || serviceType == typeof(IMcpServer) ? request.Server :
#pragma warning restore CS0618 // Type or member is obsolete
serviceType == typeof(McpServer) ? request.Server :
serviceType == typeof(IProgress<ProgressNotificationValue>) ?
(request.Params?.ProgressToken is { } progressToken ? new TokenProgress(request.Server, progressToken) : NullProgress.Instance) :
serviceType == typeof(ClaimsPrincipal) ? request.User :