Skip to content

Commit 36dd101

Browse files
committed
Add back old API, but obsoleted
1 parent c6a019d commit 36dd101

File tree

12 files changed

+1754
-3
lines changed

12 files changed

+1754
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using ModelContextProtocol.Protocol;
2+
3+
namespace ModelContextProtocol.Client;
4+
5+
/// <summary>
6+
/// Represents an instance of a Model Context Protocol (MCP) client that connects to and communicates with an MCP server.
7+
/// </summary>
8+
[Obsolete($"Use {nameof(McpClient)} instead.")]
9+
public interface IMcpClient : IMcpEndpoint
10+
{
11+
/// <summary>
12+
/// Gets the capabilities supported by the connected server.
13+
/// </summary>
14+
/// <exception cref="InvalidOperationException">The client is not connected.</exception>
15+
ServerCapabilities ServerCapabilities { get; }
16+
17+
/// <summary>
18+
/// Gets the implementation information of the connected server.
19+
/// </summary>
20+
/// <remarks>
21+
/// <para>
22+
/// This property provides identification details about the connected server, including its name and version.
23+
/// It is populated during the initialization handshake and is available after a successful connection.
24+
/// </para>
25+
/// <para>
26+
/// This information can be useful for logging, debugging, compatibility checks, and displaying server
27+
/// information to users.
28+
/// </para>
29+
/// </remarks>
30+
/// <exception cref="InvalidOperationException">The client is not connected.</exception>
31+
Implementation ServerInfo { get; }
32+
33+
/// <summary>
34+
/// Gets any instructions describing how to use the connected server and its features.
35+
/// </summary>
36+
/// <remarks>
37+
/// <para>
38+
/// This property contains instructions provided by the server during initialization that explain
39+
/// how to effectively use its capabilities. These instructions can include details about available
40+
/// tools, expected input formats, limitations, or any other helpful information.
41+
/// </para>
42+
/// <para>
43+
/// This can be used by clients to improve an LLM's understanding of available tools, prompts, and resources.
44+
/// It can be thought of like a "hint" to the model and may be added to a system prompt.
45+
/// </para>
46+
/// </remarks>
47+
string? ServerInstructions { get; }
48+
}

src/ModelContextProtocol.Core/Client/McpClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ namespace ModelContextProtocol.Client;
1010
/// <summary>
1111
/// Represents an instance of a Model Context Protocol (MCP) client session that connects to and communicates with an MCP server.
1212
/// </summary>
13-
public abstract class McpClient : McpSession
13+
#pragma warning disable CS0618 // Type or member is obsolete
14+
public abstract class McpClient : McpSession, IMcpClient
15+
#pragma warning restore CS0618 // Type or member is obsolete
1416
{
1517
/// <summary>
1618
/// Gets the capabilities supported by the connected server.

src/ModelContextProtocol.Core/Client/McpClientExtensions.cs

Lines changed: 573 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using ModelContextProtocol.Client;
2+
using ModelContextProtocol.Protocol;
3+
using ModelContextProtocol.Server;
4+
5+
namespace ModelContextProtocol;
6+
7+
/// <summary>
8+
/// Represents a client or server Model Context Protocol (MCP) endpoint.
9+
/// </summary>
10+
/// <remarks>
11+
/// <para>
12+
/// The MCP endpoint provides the core communication functionality used by both clients and servers:
13+
/// <list type="bullet">
14+
/// <item>Sending JSON-RPC requests and receiving responses.</item>
15+
/// <item>Sending notifications to the connected endpoint.</item>
16+
/// <item>Registering handlers for receiving notifications.</item>
17+
/// </list>
18+
/// </para>
19+
/// <para>
20+
/// <see cref="IMcpEndpoint"/> serves as the base interface for both <see cref="IMcpClient"/> and
21+
/// <see cref="IMcpServer"/> interfaces, providing the common functionality needed for MCP protocol
22+
/// communication. Most applications will use these more specific interfaces rather than working with
23+
/// <see cref="IMcpEndpoint"/> directly.
24+
/// </para>
25+
/// <para>
26+
/// All MCP endpoints should be properly disposed after use as they implement <see cref="IAsyncDisposable"/>.
27+
/// </para>
28+
/// </remarks>
29+
[Obsolete($"Use {nameof(McpSession)} instead.")]
30+
public interface IMcpEndpoint : IAsyncDisposable
31+
{
32+
/// <summary>Gets an identifier associated with the current MCP session.</summary>
33+
/// <remarks>
34+
/// Typically populated in transports supporting multiple sessions such as Streamable HTTP or SSE.
35+
/// Can return <see langword="null"/> if the session hasn't initialized or if the transport doesn't
36+
/// support multiple sessions (as is the case with STDIO).
37+
/// </remarks>
38+
string? SessionId { get; }
39+
40+
/// <summary>
41+
/// Sends a JSON-RPC request to the connected endpoint and waits for a response.
42+
/// </summary>
43+
/// <param name="request">The JSON-RPC request to send.</param>
44+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
45+
/// <returns>A task containing the endpoint's response.</returns>
46+
/// <exception cref="InvalidOperationException">The transport is not connected, or another error occurs during request processing.</exception>
47+
/// <exception cref="McpException">An error occured during request processing.</exception>
48+
/// <remarks>
49+
/// This method provides low-level access to send raw JSON-RPC requests. For most use cases,
50+
/// consider using the strongly-typed extension methods that provide a more convenient API.
51+
/// </remarks>
52+
Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, CancellationToken cancellationToken = default);
53+
54+
/// <summary>
55+
/// Sends a JSON-RPC message to the connected endpoint.
56+
/// </summary>
57+
/// <param name="message">
58+
/// The JSON-RPC message to send. This can be any type that implements JsonRpcMessage, such as
59+
/// JsonRpcRequest, JsonRpcResponse, JsonRpcNotification, or JsonRpcError.
60+
/// </param>
61+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
62+
/// <returns>A task that represents the asynchronous send operation.</returns>
63+
/// <exception cref="InvalidOperationException">The transport is not connected.</exception>
64+
/// <exception cref="ArgumentNullException"><paramref name="message"/> is <see langword="null"/>.</exception>
65+
/// <remarks>
66+
/// <para>
67+
/// This method provides low-level access to send any JSON-RPC message. For specific message types,
68+
/// consider using the higher-level methods such as <see cref="SendRequestAsync"/> or extension methods
69+
/// like <see cref="McpEndpointExtensions.SendNotificationAsync(IMcpEndpoint, string, CancellationToken)"/>,
70+
/// which provide a simpler API.
71+
/// </para>
72+
/// <para>
73+
/// The method will serialize the message and transmit it using the underlying transport mechanism.
74+
/// </para>
75+
/// </remarks>
76+
Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default);
77+
78+
/// <summary>Registers a handler to be invoked when a notification for the specified method is received.</summary>
79+
/// <param name="method">The notification method.</param>
80+
/// <param name="handler">The handler to be invoked.</param>
81+
/// <returns>An <see cref="IDisposable"/> that will remove the registered handler when disposed.</returns>
82+
IAsyncDisposable RegisterNotificationHandler(string method, Func<JsonRpcNotification, CancellationToken, ValueTask> handler);
83+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using ModelContextProtocol.Client;
2+
using ModelContextProtocol.Protocol;
3+
using ModelContextProtocol.Server;
4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization.Metadata;
8+
9+
namespace ModelContextProtocol;
10+
11+
/// <summary>
12+
/// Provides extension methods for interacting with an <see cref="IMcpEndpoint"/>.
13+
/// </summary>
14+
/// <remarks>
15+
/// <para>
16+
/// This class provides strongly-typed methods for working with the Model Context Protocol (MCP) endpoints,
17+
/// simplifying JSON-RPC communication by handling serialization and deserialization of parameters and results.
18+
/// </para>
19+
/// <para>
20+
/// These extension methods are designed to be used with both client (<see cref="IMcpClient"/>) and
21+
/// server (<see cref="IMcpServer"/>) implementations of the <see cref="IMcpEndpoint"/> interface.
22+
/// </para>
23+
/// </remarks>
24+
public static class McpEndpointExtensions
25+
{
26+
/// <summary>
27+
/// Sends a JSON-RPC request and attempts to deserialize the result to <typeparamref name="TResult"/>.
28+
/// </summary>
29+
/// <typeparam name="TParameters">The type of the request parameters to serialize from.</typeparam>
30+
/// <typeparam name="TResult">The type of the result to deserialize to.</typeparam>
31+
/// <param name="endpoint">The MCP client or server instance.</param>
32+
/// <param name="method">The JSON-RPC method name to invoke.</param>
33+
/// <param name="parameters">Object representing the request parameters.</param>
34+
/// <param name="requestId">The request id for the request.</param>
35+
/// <param name="serializerOptions">The options governing request serialization.</param>
36+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
37+
/// <returns>A task that represents the asynchronous operation. The task result contains the deserialized result.</returns>
38+
[Obsolete($"Use {nameof(McpSession)}.{nameof(McpSession.SendRequestAsync)} instead.")]
39+
public static ValueTask<TResult> SendRequestAsync<TParameters, TResult>(
40+
this IMcpEndpoint endpoint,
41+
string method,
42+
TParameters parameters,
43+
JsonSerializerOptions? serializerOptions = null,
44+
RequestId requestId = default,
45+
CancellationToken cancellationToken = default)
46+
where TResult : notnull
47+
=> AsSessionOrThrow(endpoint).SendRequestAsync<TParameters, TResult>(method, parameters, serializerOptions, requestId, cancellationToken);
48+
49+
/// <summary>
50+
/// Sends a parameterless notification to the connected endpoint.
51+
/// </summary>
52+
/// <param name="client">The MCP client or server instance.</param>
53+
/// <param name="method">The notification method name.</param>
54+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
55+
/// <returns>A task that represents the asynchronous send operation.</returns>
56+
/// <remarks>
57+
/// <para>
58+
/// This method sends a notification without any parameters. Notifications are one-way messages
59+
/// that don't expect a response. They are commonly used for events, status updates, or to signal
60+
/// changes in state.
61+
/// </para>
62+
/// </remarks>
63+
[Obsolete($"Use {nameof(McpSession)}.{nameof(McpSession.SendNotificationAsync)} instead.")]
64+
public static Task SendNotificationAsync(this IMcpEndpoint client, string method, CancellationToken cancellationToken = default)
65+
=> AsSessionOrThrow(client).SendNotificationAsync(method, cancellationToken);
66+
67+
/// <summary>
68+
/// Sends a notification with parameters to the connected endpoint.
69+
/// </summary>
70+
/// <typeparam name="TParameters">The type of the notification parameters to serialize.</typeparam>
71+
/// <param name="endpoint">The MCP client or server instance.</param>
72+
/// <param name="method">The JSON-RPC method name for the notification.</param>
73+
/// <param name="parameters">Object representing the notification parameters.</param>
74+
/// <param name="serializerOptions">The options governing parameter serialization. If null, default options are used.</param>
75+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
76+
/// <returns>A task that represents the asynchronous send operation.</returns>
77+
/// <remarks>
78+
/// <para>
79+
/// This method sends a notification with parameters to the connected endpoint. Notifications are one-way
80+
/// messages that don't expect a response, commonly used for events, status updates, or signaling changes.
81+
/// </para>
82+
/// <para>
83+
/// The parameters object is serialized to JSON according to the provided serializer options or the default
84+
/// options if none are specified.
85+
/// </para>
86+
/// <para>
87+
/// The Model Context Protocol defines several standard notification methods in <see cref="NotificationMethods"/>,
88+
/// but custom methods can also be used for application-specific notifications.
89+
/// </para>
90+
/// </remarks>
91+
[Obsolete($"Use {nameof(McpSession)}.{nameof(McpSession.SendNotificationAsync)} instead.")]
92+
public static Task SendNotificationAsync<TParameters>(
93+
this IMcpEndpoint endpoint,
94+
string method,
95+
TParameters parameters,
96+
JsonSerializerOptions? serializerOptions = null,
97+
CancellationToken cancellationToken = default)
98+
=> AsSessionOrThrow(endpoint).SendNotificationAsync(method, parameters, serializerOptions, cancellationToken);
99+
100+
/// <summary>
101+
/// Notifies the connected endpoint of progress for a long-running operation.
102+
/// </summary>
103+
/// <param name="endpoint">The endpoint issuing the notification.</param>
104+
/// <param name="progressToken">The <see cref="ProgressToken"/> identifying the operation for which progress is being reported.</param>
105+
/// <param name="progress">The progress update to send, containing information such as percentage complete or status message.</param>
106+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
107+
/// <returns>A task representing the completion of the notification operation (not the operation being tracked).</returns>
108+
/// <exception cref="ArgumentNullException"><paramref name="endpoint"/> is <see langword="null"/>.</exception>
109+
/// <remarks>
110+
/// <para>
111+
/// This method sends a progress notification to the connected endpoint using the Model Context Protocol's
112+
/// standardized progress notification format. Progress updates are identified by a <see cref="ProgressToken"/>
113+
/// that allows the recipient to correlate multiple updates with a specific long-running operation.
114+
/// </para>
115+
/// <para>
116+
/// Progress notifications are sent asynchronously and don't block the operation from continuing.
117+
/// </para>
118+
/// </remarks>
119+
[Obsolete($"Use {nameof(McpSession)}.{nameof(McpSession.NotifyProgressAsync)} instead.")]
120+
public static Task NotifyProgressAsync(
121+
this IMcpEndpoint endpoint,
122+
ProgressToken progressToken,
123+
ProgressNotificationValue progress,
124+
CancellationToken cancellationToken = default)
125+
=> AsSessionOrThrow(endpoint).NotifyProgressAsync(progressToken, progress, cancellationToken);
126+
127+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
128+
#pragma warning disable CS0618 // Type or member is obsolete
129+
private static McpSession AsSessionOrThrow(IMcpEndpoint endpoint, [CallerMemberName] string memberName = "")
130+
#pragma warning restore CS0618 // Type or member is obsolete
131+
{
132+
if (endpoint is not McpSession session)
133+
{
134+
ThrowInvalidEndpointType(memberName);
135+
}
136+
137+
return session;
138+
139+
[DoesNotReturn]
140+
[MethodImpl(MethodImplOptions.NoInlining)]
141+
static void ThrowInvalidEndpointType(string memberName)
142+
=> throw new InvalidOperationException(
143+
$"Only arguments assignable to '{nameof(McpSession)}' are supported. " +
144+
$"Prefer using '{nameof(McpServer)}.{memberName}' instead, as " +
145+
$"'{nameof(McpEndpointExtensions)}.{memberName}' is obsolete and will be " +
146+
$"removed in the future.");
147+
}
148+
}

src/ModelContextProtocol.Core/McpSession.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ namespace ModelContextProtocol;
2929
/// All MCP sessions should be properly disposed after use as they implement <see cref="IAsyncDisposable"/>.
3030
/// </para>
3131
/// </remarks>
32-
public abstract class McpSession : IAsyncDisposable
32+
#pragma warning disable CS0618 // Type or member is obsolete
33+
public abstract class McpSession : IMcpEndpoint, IAsyncDisposable
34+
#pragma warning restore CS0618 // Type or member is obsolete
3335
{
3436
/// <summary>Gets an identifier associated with the current MCP session.</summary>
3537
/// <remarks>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using ModelContextProtocol.Protocol;
2+
3+
namespace ModelContextProtocol.Server;
4+
5+
/// <summary>
6+
/// Represents an instance of a Model Context Protocol (MCP) server that connects to and communicates with an MCP client.
7+
/// </summary>
8+
[Obsolete($"Use {nameof(McpServer)} instead.")]
9+
public interface IMcpServer : IMcpEndpoint
10+
{
11+
/// <summary>
12+
/// Gets the capabilities supported by the client.
13+
/// </summary>
14+
/// <remarks>
15+
/// <para>
16+
/// These capabilities are established during the initialization handshake and indicate
17+
/// which features the client supports, such as sampling, roots, and other
18+
/// protocol-specific functionality.
19+
/// </para>
20+
/// <para>
21+
/// Server implementations can check these capabilities to determine which features
22+
/// are available when interacting with the client.
23+
/// </para>
24+
/// </remarks>
25+
ClientCapabilities? ClientCapabilities { get; }
26+
27+
/// <summary>
28+
/// Gets the version and implementation information of the connected client.
29+
/// </summary>
30+
/// <remarks>
31+
/// <para>
32+
/// This property contains identification information about the client that has connected to this server,
33+
/// including its name and version. This information is provided by the client during initialization.
34+
/// </para>
35+
/// <para>
36+
/// Server implementations can use this information for logging, tracking client versions,
37+
/// or implementing client-specific behaviors.
38+
/// </para>
39+
/// </remarks>
40+
Implementation? ClientInfo { get; }
41+
42+
/// <summary>
43+
/// Gets the options used to construct this server.
44+
/// </summary>
45+
/// <remarks>
46+
/// These options define the server's capabilities, protocol version, and other configuration
47+
/// settings that were used to initialize the server.
48+
/// </remarks>
49+
McpServerOptions ServerOptions { get; }
50+
51+
/// <summary>
52+
/// Gets the service provider for the server.
53+
/// </summary>
54+
IServiceProvider? Services { get; }
55+
56+
/// <summary>Gets the last logging level set by the client, or <see langword="null"/> if it's never been set.</summary>
57+
LoggingLevel? LoggingLevel { get; }
58+
59+
/// <summary>
60+
/// Runs the server, listening for and handling client requests.
61+
/// </summary>
62+
Task RunAsync(CancellationToken cancellationToken = default);
63+
}

src/ModelContextProtocol.Core/Server/McpServer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ namespace ModelContextProtocol.Server;
1010
/// <summary>
1111
/// Represents an instance of a Model Context Protocol (MCP) server that connects to and communicates with an MCP client.
1212
/// </summary>
13-
public abstract class McpServer : McpSession
13+
#pragma warning disable CS0618 // Type or member is obsolete
14+
public abstract class McpServer : McpSession, IMcpServer
15+
#pragma warning restore CS0618 // Type or member is obsolete
1416
{
1517
/// <summary>
1618
/// Gets the capabilities supported by the client.

0 commit comments

Comments
 (0)