Add McpSessions.NegotiatedProtocolVersion property. (#794)
This commit is contained in:
parent
38b4a26998
commit
7cfe0ef621
4 changed files with 57 additions and 20 deletions
|
|
@ -26,6 +26,7 @@ internal sealed partial class McpClientImpl : McpClient
|
|||
private ServerCapabilities? _serverCapabilities;
|
||||
private Implementation? _serverInfo;
|
||||
private string? _serverInstructions;
|
||||
private string? _negotiatedProtocolVersion;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
|
|
@ -112,6 +113,9 @@ internal sealed partial class McpClientImpl : McpClient
|
|||
/// <inheritdoc/>
|
||||
public override string? SessionId => _transport.SessionId;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string? NegotiatedProtocolVersion => _negotiatedProtocolVersion;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ServerCapabilities ServerCapabilities => _serverCapabilities ?? throw new InvalidOperationException("The client is not connected.");
|
||||
|
||||
|
|
@ -177,6 +181,8 @@ internal sealed partial class McpClientImpl : McpClient
|
|||
throw new McpException($"Server protocol version mismatch. Expected {requestProtocol}, got {initializeResponse.ProtocolVersion}");
|
||||
}
|
||||
|
||||
_negotiatedProtocolVersion = initializeResponse.ProtocolVersion;
|
||||
|
||||
// Send initialized notification
|
||||
await this.SendNotificationAsync(
|
||||
NotificationMethods.InitializedNotification,
|
||||
|
|
|
|||
|
|
@ -38,6 +38,15 @@ public abstract partial class McpSession : IMcpEndpoint, IAsyncDisposable
|
|||
/// </remarks>
|
||||
public abstract string? SessionId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the negotiated protocol version for the current MCP session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Returns the protocol version negotiated during session initialization,
|
||||
/// or <see langword="null"/> if initialization hasn't yet occurred.
|
||||
/// </remarks>
|
||||
public abstract string? NegotiatedProtocolVersion { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Sends a JSON-RPC request to the connected session and waits for a response.
|
||||
/// </summary>
|
||||
|
|
@ -45,7 +54,7 @@ public abstract partial class McpSession : IMcpEndpoint, IAsyncDisposable
|
|||
/// <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 session's response.</returns>
|
||||
/// <exception cref="InvalidOperationException">The transport is not connected, or another error occurs during request processing.</exception>
|
||||
/// <exception cref="McpException">An error occured during request processing.</exception>
|
||||
/// <exception cref="McpException">An error occurred during request processing.</exception>
|
||||
/// <remarks>
|
||||
/// This method provides low-level access to send raw JSON-RPC requests. For most use cases,
|
||||
/// consider using the strongly-typed methods that provide a more convenient API.
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ namespace ModelContextProtocol.Server;
|
|||
internal sealed class DestinationBoundMcpServer(McpServerImpl server, ITransport? transport) : McpServer
|
||||
{
|
||||
public override string? SessionId => transport?.SessionId ?? server.SessionId;
|
||||
public override string? NegotiatedProtocolVersion => server.NegotiatedProtocolVersion;
|
||||
public override ClientCapabilities? ClientCapabilities => server.ClientCapabilities;
|
||||
public override Implementation? ClientInfo => server.ClientInfo;
|
||||
public override McpServerOptions ServerOptions => server.ServerOptions;
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ using System.Text.Json.Serialization.Metadata;
|
|||
|
||||
namespace ModelContextProtocol.Server;
|
||||
|
||||
// TODO: Fix merge conflicts in this file.
|
||||
|
||||
/// <inheritdoc />
|
||||
internal sealed partial class McpServerImpl : McpServer
|
||||
{
|
||||
|
|
@ -31,6 +29,7 @@ internal sealed partial class McpServerImpl : McpServer
|
|||
private Implementation? _clientInfo;
|
||||
|
||||
private readonly string _serverOnlyEndpointName;
|
||||
private string? _negotiatedProtocolVersion;
|
||||
private string _endpointName;
|
||||
private int _started;
|
||||
|
||||
|
|
@ -116,6 +115,9 @@ internal sealed partial class McpServerImpl : McpServer
|
|||
/// <inheritdoc/>
|
||||
public override string? SessionId => _sessionTransport.SessionId;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string? NegotiatedProtocolVersion => _negotiatedProtocolVersion;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ServerCapabilities ServerCapabilities { get; } = new();
|
||||
|
||||
|
|
@ -212,6 +214,8 @@ internal sealed partial class McpServerImpl : McpServer
|
|||
McpSessionHandler.LatestProtocolVersion;
|
||||
}
|
||||
|
||||
_negotiatedProtocolVersion = protocolVersion;
|
||||
|
||||
return new InitializeResult
|
||||
{
|
||||
ProtocolVersion = protocolVersion,
|
||||
|
|
|
|||
Loading…
Reference in a new issue