Make options types fully mutable (#107)

This commit is contained in:
Stephen Halter 2025-03-26 10:39:49 -07:00 • committed by GitHub
commit 95a017a03a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 97 additions and 172 deletions

View file

@ -7,25 +7,25 @@ namespace ModelContextProtocol.Client;
/// protocol version.
/// <see href="https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle/">See the protocol specification for details on capability negotiation</see>
/// </summary>
public record McpClientOptions
public class McpClientOptions
{
/// <summary>
/// Information about this client implementation.
/// </summary>
public required Implementation ClientInfo { get; init; }
public required Implementation ClientInfo { get; set; }
/// <summary>
/// Client capabilities to advertise to the server.
/// </summary>
public ClientCapabilities? Capabilities { get; init; }
public ClientCapabilities? Capabilities { get; set; }
/// <summary>
/// Protocol version to request from the server.
/// </summary>
public string ProtocolVersion { get; init; } = "2024-11-05";
public string ProtocolVersion { get; set; } = "2024-11-05";
/// <summary>
/// Timeout for initialization sequence.
/// </summary>
public TimeSpan InitializationTimeout { get; init; } = TimeSpan.FromSeconds(60);
public TimeSpan InitializationTimeout { get; set; } = TimeSpan.FromSeconds(60);
}

View file

@ -25,22 +25,14 @@ internal sealed class McpServerOptionsSetup(
// Configure the option's server information based on the current process,
// if it otherwise lacks server information.
var assemblyName = (Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).GetName();
if (options.ServerInfo is not { } serverInfo ||
serverInfo.Name is null ||
serverInfo.Version is null)
if (options.ServerInfo is not { } serverInfo)
{
options.ServerInfo = options.ServerInfo is null ?
new()
{
Name = assemblyName.Name ?? "McpServer",
Version = assemblyName.Version?.ToString() ?? "1.0.0",
} :
options.ServerInfo with
{
Name = options.ServerInfo.Name ?? assemblyName.Name ?? "McpServer",
Version = options.ServerInfo.Version ?? assemblyName.Version?.ToString() ?? "1.0.0",
};
var assemblyName = (Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).GetName();
options.ServerInfo = new()
{
Name = assemblyName.Name ?? "McpServer",
Version = assemblyName.Version?.ToString() ?? "1.0.0",
};
}
// Collect all of the provided tools into a tools collection. If the options already has
@ -55,14 +47,9 @@ internal sealed class McpServerOptionsSetup(
if (!toolsCollection.IsEmpty)
{
options.Capabilities = options.Capabilities is null ?
new() { Tools = new() { ToolCollection = toolsCollection } } :
options.Capabilities with
{
Tools = options.Capabilities.Tools is null ?
new() { ToolCollection = toolsCollection } :
options.Capabilities.Tools with { ToolCollection = toolsCollection },
};
options.Capabilities ??= new();
options.Capabilities.Tools ??= new();
options.Capabilities.Tools.ToolCollection = toolsCollection;
}
// Apply custom server handlers.

View file

@ -7,62 +7,62 @@ namespace ModelContextProtocol.Protocol.Types;
/// Represents the capabilities that a client may support.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public record ClientCapabilities
public class ClientCapabilities
{
/// <summary>
/// Experimental, non-standard capabilities that the client supports.
/// </summary>
[JsonPropertyName("experimental")]
public Dictionary<string, object>? Experimental { get; init; }
public Dictionary<string, object>? Experimental { get; set; }
/// <summary>
/// Present if the client supports listing roots.
/// </summary>
[JsonPropertyName("roots")]
public RootsCapability? Roots { get; init; }
public RootsCapability? Roots { get; set; }
/// <summary>
/// Present if the client supports sampling from an LLM.
/// </summary>
[JsonPropertyName("sampling")]
public SamplingCapability? Sampling { get; init; }
public SamplingCapability? Sampling { get; set; }
}
/// <summary>
/// Represents the roots capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public record RootsCapability
public class RootsCapability
{
/// <summary>
/// Whether the client supports notifications for changes to the roots list.
/// </summary>
[JsonPropertyName("listChanged")]
public bool? ListChanged { get; init; }
public bool? ListChanged { get; set; }
/// <summary>Gets or sets the handler for sampling requests.</summary>
[JsonIgnore]
public Func<ListRootsRequestParams?, CancellationToken, Task<ListRootsResult>>? RootsHandler { get; init; }
public Func<ListRootsRequestParams?, CancellationToken, Task<ListRootsResult>>? RootsHandler { get; set; }
}
/// <summary>
/// Represents the sampling capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public record SamplingCapability
public class SamplingCapability
{
// Currently empty in the spec, but may be extended in the future
/// <summary>Gets or sets the handler for sampling requests.</summary>
[JsonIgnore]
public Func<CreateMessageRequestParams?, CancellationToken, Task<CreateMessageResult>>? SamplingHandler { get; init; }
public Func<CreateMessageRequestParams?, CancellationToken, Task<CreateMessageResult>>? SamplingHandler { get; set; }
}
/// <summary>
/// Represents the logging capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public record LoggingCapability
public class LoggingCapability
{
// Currently empty in the spec, but may be extended in the future
@ -71,106 +71,106 @@ public record LoggingCapability
/// Gets or sets the handler for set logging level requests.
/// </summary>
[JsonIgnore]
public Func<RequestContext<SetLevelRequestParams>, CancellationToken, Task<EmptyResult>>? SetLoggingLevelHandler { get; init; }
public Func<RequestContext<SetLevelRequestParams>, CancellationToken, Task<EmptyResult>>? SetLoggingLevelHandler { get; set; }
}
/// <summary>
/// Represents the prompts capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public record PromptsCapability
public class PromptsCapability
{
/// <summary>
/// Whether this server supports notifications for changes to the prompt list.
/// </summary>
[JsonPropertyName("listChanged")]
public bool? ListChanged { get; init; }
public bool? ListChanged { get; set; }
/// <summary>
/// Gets or sets the handler for list prompts requests.
/// </summary>
[JsonIgnore]
public Func<RequestContext<ListPromptsRequestParams>, CancellationToken, Task<ListPromptsResult>>? ListPromptsHandler { get; init; }
public Func<RequestContext<ListPromptsRequestParams>, CancellationToken, Task<ListPromptsResult>>? ListPromptsHandler { get; set; }
/// <summary>
/// Gets or sets the handler for get prompt requests.
/// </summary>
[JsonIgnore]
public Func<RequestContext<GetPromptRequestParams>, CancellationToken, Task<GetPromptResult>>? GetPromptHandler { get; init; }
public Func<RequestContext<GetPromptRequestParams>, CancellationToken, Task<GetPromptResult>>? GetPromptHandler { get; set; }
}
/// <summary>
/// Represents the resources capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public record ResourcesCapability
public class ResourcesCapability
{
/// <summary>
/// Whether this server supports subscribing to resource updates.
/// </summary>
[JsonPropertyName("subscribe")]
public bool? Subscribe { get; init; }
public bool? Subscribe { get; set; }
/// <summary>
/// Whether this server supports notifications for changes to the resource list.
/// </summary>
[JsonPropertyName("listChanged")]
public bool? ListChanged { get; init; }
public bool? ListChanged { get; set; }
/// <summary>
/// Gets or sets the handler for list resource templates requests.
/// </summary>
[JsonIgnore]
public Func<RequestContext<ListResourceTemplatesRequestParams>, CancellationToken, Task<ListResourceTemplatesResult>>? ListResourceTemplatesHandler { get; init; }
public Func<RequestContext<ListResourceTemplatesRequestParams>, CancellationToken, Task<ListResourceTemplatesResult>>? ListResourceTemplatesHandler { get; set; }
/// <summary>
/// Gets or sets the handler for list resources requests.
/// </summary>
[JsonIgnore]
public Func<RequestContext<ListResourcesRequestParams>, CancellationToken, Task<ListResourcesResult>>? ListResourcesHandler { get; init; }
public Func<RequestContext<ListResourcesRequestParams>, CancellationToken, Task<ListResourcesResult>>? ListResourcesHandler { get; set; }
/// <summary>
/// Gets or sets the handler for read resources requests.
/// </summary>
[JsonIgnore]
public Func<RequestContext<ReadResourceRequestParams>, CancellationToken, Task<ReadResourceResult>>? ReadResourceHandler { get; init; }
public Func<RequestContext<ReadResourceRequestParams>, CancellationToken, Task<ReadResourceResult>>? ReadResourceHandler { get; set; }
/// <summary>
/// Gets or sets the handler for subscribe to resources messages.
/// </summary>
[JsonIgnore]
public Func<RequestContext<SubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? SubscribeToResourcesHandler { get; init; }
public Func<RequestContext<SubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? SubscribeToResourcesHandler { get; set; }
/// <summary>
/// Gets or sets the handler for unsubscribe from resources messages.
/// </summary>
[JsonIgnore]
public Func<RequestContext<UnsubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? UnsubscribeFromResourcesHandler { get; init; }
public Func<RequestContext<UnsubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? UnsubscribeFromResourcesHandler { get; set; }
}
/// <summary>
/// Represents the tools capability configuration.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public record ToolsCapability
public class ToolsCapability
{
/// <summary>
/// Gets or sets whether this server supports notifications for changes to the tool list.
/// </summary>
[JsonPropertyName("listChanged")]
public bool? ListChanged { get; init; }
public bool? ListChanged { get; set; }
/// <summary>
/// Gets or sets the handler for list tools requests.
/// </summary>
[JsonIgnore]
public Func<RequestContext<ListToolsRequestParams>, CancellationToken, Task<ListToolsResult>>? ListToolsHandler { get; init; }
public Func<RequestContext<ListToolsRequestParams>, CancellationToken, Task<ListToolsResult>>? ListToolsHandler { get; set; }
/// <summary>
/// Gets or sets the handler for call tool requests.
/// </summary>
[JsonIgnore]
public Func<RequestContext<CallToolRequestParams>, CancellationToken, Task<CallToolResponse>>? CallToolHandler { get; init; }
public Func<RequestContext<CallToolRequestParams>, CancellationToken, Task<CallToolResponse>>? CallToolHandler { get; set; }
/// <summary>Gets or sets a collection of tools served by the server.</summary>
/// <remarks>
@ -182,5 +182,5 @@ public record ToolsCapability
/// will be invoked as a fallback.
/// </remarks>
[JsonIgnore]
public McpServerToolCollection? ToolCollection { get; init; }
public McpServerToolCollection? ToolCollection { get; set; }
}

View file

@ -6,17 +6,17 @@ namespace ModelContextProtocol.Protocol.Types;
/// Describes the name and version of an MCP implementation.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public record Implementation
public class Implementation
{
/// <summary>
/// Name of the implementation.
/// </summary>
[JsonPropertyName("name")]
public required string Name { get; init; }
public required string Name { get; set; }
/// <summary>
/// Version of the implementation.
/// </summary>
[JsonPropertyName("version")]
public required string Version { get; init; }
public required string Version { get; set; }
}

View file

@ -6,35 +6,35 @@ namespace ModelContextProtocol.Protocol.Types;
/// Represents the capabilities that a server may support.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public record ServerCapabilities
public class ServerCapabilities
{
/// <summary>
/// Experimental, non-standard capabilities that the server supports.
/// </summary>
[JsonPropertyName("experimental")]
public Dictionary<string, object>? Experimental { get; init; }
public Dictionary<string, object>? Experimental { get; set; }
/// <summary>
/// Present if the server supports sending log messages to the client.
/// </summary>
[JsonPropertyName("logging")]
public LoggingCapability? Logging { get; init; }
public LoggingCapability? Logging { get; set; }
/// <summary>
/// Present if the server offers any prompt templates.
/// </summary>
[JsonPropertyName("prompts")]
public PromptsCapability? Prompts { get; init; }
public PromptsCapability? Prompts { get; set; }
/// <summary>
/// Present if the server offers any resources to read.
/// </summary>
[JsonPropertyName("resources")]
public ResourcesCapability? Resources { get; init; }
public ResourcesCapability? Resources { get; set; }
/// <summary>
/// Present if the server offers any tools to call.
/// </summary>
[JsonPropertyName("tools")]
public ToolsCapability? Tools { get; init; }
public ToolsCapability? Tools { get; set; }
}

View file

@ -263,25 +263,14 @@ internal sealed class McpServer : McpJsonRpcEndpoint, IMcpServer
return tool.InvokeAsync(request, cancellationToken);
};
toolsCapability = toolsCapability is null ?
new()
{
CallToolHandler = callToolHandler,
ListToolsHandler = listToolsHandler,
ToolCollection = tools,
ListChanged = true,
} :
toolsCapability with
{
CallToolHandler = callToolHandler,
ListToolsHandler = listToolsHandler,
ToolCollection = tools,
ListChanged = true,
};
toolsCapability ??= new();
toolsCapability.CallToolHandler = callToolHandler;
toolsCapability.ListToolsHandler = listToolsHandler;
toolsCapability.ToolCollection = tools;
toolsCapability.ListChanged = true;
options.Capabilities = options.Capabilities is null ?
new() { Tools = toolsCapability } :
options.Capabilities with { Tools = toolsCapability };
options.Capabilities ??= new();
options.Capabilities.Tools = toolsCapability;
tools.Changed += delegate
{

View file

@ -72,91 +72,47 @@ public sealed class McpServerHandlers
PromptsCapability? promptsCapability = options.Capabilities?.Prompts;
if (ListPromptsHandler is not null || GetPromptHandler is not null)
{
promptsCapability = promptsCapability is null ?
new()
{
ListPromptsHandler = ListPromptsHandler,
GetPromptHandler = GetPromptHandler,
} :
promptsCapability with
{
ListPromptsHandler = ListPromptsHandler ?? promptsCapability.ListPromptsHandler,
GetPromptHandler = GetPromptHandler ?? promptsCapability.GetPromptHandler,
};
promptsCapability ??= new();
promptsCapability.ListPromptsHandler = ListPromptsHandler ?? promptsCapability.ListPromptsHandler;
promptsCapability.GetPromptHandler = GetPromptHandler ?? promptsCapability.GetPromptHandler;
}
ResourcesCapability? resourcesCapability = options.Capabilities?.Resources;
if (ListResourcesHandler is not null ||
ReadResourceHandler is not null)
{
resourcesCapability = resourcesCapability is null ?
new()
{
ListResourceTemplatesHandler = ListResourceTemplatesHandler,
ListResourcesHandler = ListResourcesHandler,
ReadResourceHandler = ReadResourceHandler
} :
resourcesCapability with
{
ListResourceTemplatesHandler = ListResourceTemplatesHandler ?? resourcesCapability.ListResourceTemplatesHandler,
ListResourcesHandler = ListResourcesHandler ?? resourcesCapability.ListResourcesHandler,
ReadResourceHandler = ReadResourceHandler ?? resourcesCapability.ReadResourceHandler
};
resourcesCapability ??= new();
resourcesCapability.ListResourceTemplatesHandler = ListResourceTemplatesHandler ?? resourcesCapability.ListResourceTemplatesHandler;
resourcesCapability.ListResourcesHandler = ListResourcesHandler ?? resourcesCapability.ListResourcesHandler;
resourcesCapability.ReadResourceHandler = ReadResourceHandler ?? resourcesCapability.ReadResourceHandler;
if (SubscribeToResourcesHandler is not null || UnsubscribeFromResourcesHandler is not null)
{
resourcesCapability = resourcesCapability with
{
SubscribeToResourcesHandler = SubscribeToResourcesHandler ?? resourcesCapability.SubscribeToResourcesHandler,
UnsubscribeFromResourcesHandler = UnsubscribeFromResourcesHandler ?? resourcesCapability.UnsubscribeFromResourcesHandler,
Subscribe = true
};
resourcesCapability.SubscribeToResourcesHandler = SubscribeToResourcesHandler ?? resourcesCapability.SubscribeToResourcesHandler;
resourcesCapability.UnsubscribeFromResourcesHandler = UnsubscribeFromResourcesHandler ?? resourcesCapability.UnsubscribeFromResourcesHandler;
resourcesCapability.Subscribe = true;
}
}
ToolsCapability? toolsCapability = options.Capabilities?.Tools;
if (ListToolsHandler is not null || CallToolHandler is not null)
{
toolsCapability = toolsCapability is null ?
new()
{
ListToolsHandler = ListToolsHandler,
CallToolHandler = CallToolHandler,
} :
toolsCapability with
{
ListToolsHandler = ListToolsHandler ?? toolsCapability.ListToolsHandler,
CallToolHandler = CallToolHandler ?? toolsCapability.CallToolHandler,
};
toolsCapability ??= new();
toolsCapability.ListToolsHandler = ListToolsHandler ?? toolsCapability.ListToolsHandler;
toolsCapability.CallToolHandler = CallToolHandler ?? toolsCapability.CallToolHandler;
}
LoggingCapability? loggingCapability = options.Capabilities?.Logging;
if (SetLoggingLevelHandler is not null)
{
loggingCapability = loggingCapability is null ?
new()
{
SetLoggingLevelHandler = SetLoggingLevelHandler,
} :
loggingCapability with
{
SetLoggingLevelHandler = SetLoggingLevelHandler ?? loggingCapability.SetLoggingLevelHandler,
};
loggingCapability ??= new();
loggingCapability.SetLoggingLevelHandler = SetLoggingLevelHandler;
}
options.Capabilities = options.Capabilities is null ?
new()
{
Prompts = promptsCapability,
Resources = resourcesCapability,
Tools = toolsCapability,
} :
options.Capabilities with
{
Prompts = promptsCapability,
Resources = resourcesCapability,
Tools = toolsCapability,
};
options.Capabilities ??= new();
options.Capabilities.Prompts = promptsCapability;
options.Capabilities.Resources = resourcesCapability;
options.Capabilities.Tools = toolsCapability;
options.GetCompletionHandler = GetCompletionHandler ?? options.GetCompletionHandler;
}

View file

@ -15,7 +15,6 @@ public class SseServerIntegrationTestFixture : IAsyncDisposable
private readonly DelegatingTestOutputHelper _delegatingTestOutputHelper = new();
private readonly ILoggerFactory _redirectingLoggerFactory;
public McpClientOptions DefaultOptions { get; }
public McpServerConfig DefaultConfig { get; }
public SseServerIntegrationTestFixture()
@ -26,11 +25,6 @@ public class SseServerIntegrationTestFixture : IAsyncDisposable
builder.AddProvider(new XunitLoggerProvider(_delegatingTestOutputHelper));
});
DefaultOptions = new()
{
ClientInfo = new() { Name = "IntegrationTestClient", Version = "1.0.0" },
};
DefaultConfig = new McpServerConfig
{
Id = "test_server",
@ -43,6 +37,11 @@ public class SseServerIntegrationTestFixture : IAsyncDisposable
_serverTask = Program.MainAsync([], _redirectingLoggerFactory, _stopCts.Token);
}
public static McpClientOptions CreateDefaultClientOptions() => new()
{
ClientInfo = new() { Name = "IntegrationTestClient", Version = "1.0.0" },
};
public void Initialize(ITestOutputHelper output)
{
_delegatingTestOutputHelper.CurrentTestOutputHelper = output;

View file

@ -19,7 +19,7 @@ public class SseServerIntegrationTests : LoggedTest, IClassFixture<SseServerInte
{
return McpClientFactory.CreateAsync(
_fixture.DefaultConfig,
options ?? _fixture.DefaultOptions,
options ?? SseServerIntegrationTestFixture.CreateDefaultClientOptions(),
loggerFactory: LoggerFactory);
}
@ -201,28 +201,22 @@ public class SseServerIntegrationTests : LoggedTest, IClassFixture<SseServerInte
// Set up the sampling handler
int samplingHandlerCalls = 0;
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
var options = _fixture.DefaultOptions with
var options = SseServerIntegrationTestFixture.CreateDefaultClientOptions();
options.Capabilities ??= new();
options.Capabilities.Sampling ??= new();
options.Capabilities.Sampling.SamplingHandler = async (_, _) =>
{
Capabilities = new()
samplingHandlerCalls++;
return new CreateMessageResult
{
Sampling = new()
Model = "test-model",
Role = "assistant",
Content = new Content
{
SamplingHandler = async (_, _) =>
{
samplingHandlerCalls++;
return new CreateMessageResult
{
Model = "test-model",
Role = "assistant",
Content = new Content
{
Type = "text",
Text = "Test response"
}
};
}
Type = "text",
Text = "Test response"
}
}
};
};
var client = await GetClientAsync(options);
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously