Skip to content

Commit 95a017a

Browse files
authored
Make options types fully mutable (#107)
1 parent b61cdef commit 95a017a

File tree

9 files changed

+97
-172
lines changed

9 files changed

+97
-172
lines changed

src/ModelContextProtocol/Client/McpClientOptions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ namespace ModelContextProtocol.Client;
77
/// protocol version.
88
/// <see href="https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle/">See the protocol specification for details on capability negotiation</see>
99
/// </summary>
10-
public record McpClientOptions
10+
public class McpClientOptions
1111
{
1212
/// <summary>
1313
/// Information about this client implementation.
1414
/// </summary>
15-
public required Implementation ClientInfo { get; init; }
15+
public required Implementation ClientInfo { get; set; }
1616

1717
/// <summary>
1818
/// Client capabilities to advertise to the server.
1919
/// </summary>
20-
public ClientCapabilities? Capabilities { get; init; }
20+
public ClientCapabilities? Capabilities { get; set; }
2121

2222
/// <summary>
2323
/// Protocol version to request from the server.
2424
/// </summary>
25-
public string ProtocolVersion { get; init; } = "2024-11-05";
25+
public string ProtocolVersion { get; set; } = "2024-11-05";
2626

2727
/// <summary>
2828
/// Timeout for initialization sequence.
2929
/// </summary>
30-
public TimeSpan InitializationTimeout { get; init; } = TimeSpan.FromSeconds(60);
30+
public TimeSpan InitializationTimeout { get; set; } = TimeSpan.FromSeconds(60);
3131
}

src/ModelContextProtocol/Configuration/McpServerOptionsSetup.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,14 @@ public void Configure(McpServerOptions options)
2525

2626
// Configure the option's server information based on the current process,
2727
// if it otherwise lacks server information.
28-
var assemblyName = (Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).GetName();
29-
if (options.ServerInfo is not { } serverInfo ||
30-
serverInfo.Name is null ||
31-
serverInfo.Version is null)
28+
if (options.ServerInfo is not { } serverInfo)
3229
{
33-
options.ServerInfo = options.ServerInfo is null ?
34-
new()
35-
{
36-
Name = assemblyName.Name ?? "McpServer",
37-
Version = assemblyName.Version?.ToString() ?? "1.0.0",
38-
} :
39-
options.ServerInfo with
40-
{
41-
Name = options.ServerInfo.Name ?? assemblyName.Name ?? "McpServer",
42-
Version = options.ServerInfo.Version ?? assemblyName.Version?.ToString() ?? "1.0.0",
43-
};
30+
var assemblyName = (Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).GetName();
31+
options.ServerInfo = new()
32+
{
33+
Name = assemblyName.Name ?? "McpServer",
34+
Version = assemblyName.Version?.ToString() ?? "1.0.0",
35+
};
4436
}
4537

4638
// Collect all of the provided tools into a tools collection. If the options already has
@@ -55,14 +47,9 @@ options.ServerInfo with
5547

5648
if (!toolsCollection.IsEmpty)
5749
{
58-
options.Capabilities = options.Capabilities is null ?
59-
new() { Tools = new() { ToolCollection = toolsCollection } } :
60-
options.Capabilities with
61-
{
62-
Tools = options.Capabilities.Tools is null ?
63-
new() { ToolCollection = toolsCollection } :
64-
options.Capabilities.Tools with { ToolCollection = toolsCollection },
65-
};
50+
options.Capabilities ??= new();
51+
options.Capabilities.Tools ??= new();
52+
options.Capabilities.Tools.ToolCollection = toolsCollection;
6653
}
6754

6855
// Apply custom server handlers.

src/ModelContextProtocol/Protocol/Types/Capabilities.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,62 @@ namespace ModelContextProtocol.Protocol.Types;
77
/// Represents the capabilities that a client may support.
88
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
99
/// </summary>
10-
public record ClientCapabilities
10+
public class ClientCapabilities
1111
{
1212
/// <summary>
1313
/// Experimental, non-standard capabilities that the client supports.
1414
/// </summary>
1515
[JsonPropertyName("experimental")]
16-
public Dictionary<string, object>? Experimental { get; init; }
16+
public Dictionary<string, object>? Experimental { get; set; }
1717

1818
/// <summary>
1919
/// Present if the client supports listing roots.
2020
/// </summary>
2121
[JsonPropertyName("roots")]
22-
public RootsCapability? Roots { get; init; }
22+
public RootsCapability? Roots { get; set; }
2323

2424
/// <summary>
2525
/// Present if the client supports sampling from an LLM.
2626
/// </summary>
2727
[JsonPropertyName("sampling")]
28-
public SamplingCapability? Sampling { get; init; }
28+
public SamplingCapability? Sampling { get; set; }
2929
}
3030

3131
/// <summary>
3232
/// Represents the roots capability configuration.
3333
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
3434
/// </summary>
35-
public record RootsCapability
35+
public class RootsCapability
3636
{
3737
/// <summary>
3838
/// Whether the client supports notifications for changes to the roots list.
3939
/// </summary>
4040
[JsonPropertyName("listChanged")]
41-
public bool? ListChanged { get; init; }
41+
public bool? ListChanged { get; set; }
4242

4343
/// <summary>Gets or sets the handler for sampling requests.</summary>
4444
[JsonIgnore]
45-
public Func<ListRootsRequestParams?, CancellationToken, Task<ListRootsResult>>? RootsHandler { get; init; }
45+
public Func<ListRootsRequestParams?, CancellationToken, Task<ListRootsResult>>? RootsHandler { get; set; }
4646
}
4747

4848
/// <summary>
4949
/// Represents the sampling capability configuration.
5050
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
5151
/// </summary>
52-
public record SamplingCapability
52+
public class SamplingCapability
5353
{
5454
// Currently empty in the spec, but may be extended in the future
5555

5656
/// <summary>Gets or sets the handler for sampling requests.</summary>
5757
[JsonIgnore]
58-
public Func<CreateMessageRequestParams?, CancellationToken, Task<CreateMessageResult>>? SamplingHandler { get; init; }
58+
public Func<CreateMessageRequestParams?, CancellationToken, Task<CreateMessageResult>>? SamplingHandler { get; set; }
5959
}
6060

6161
/// <summary>
6262
/// Represents the logging capability configuration.
6363
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
6464
/// </summary>
65-
public record LoggingCapability
65+
public class LoggingCapability
6666
{
6767
// Currently empty in the spec, but may be extended in the future
6868

@@ -71,106 +71,106 @@ public record LoggingCapability
7171
/// Gets or sets the handler for set logging level requests.
7272
/// </summary>
7373
[JsonIgnore]
74-
public Func<RequestContext<SetLevelRequestParams>, CancellationToken, Task<EmptyResult>>? SetLoggingLevelHandler { get; init; }
74+
public Func<RequestContext<SetLevelRequestParams>, CancellationToken, Task<EmptyResult>>? SetLoggingLevelHandler { get; set; }
7575
}
7676

7777
/// <summary>
7878
/// Represents the prompts capability configuration.
7979
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
8080
/// </summary>
81-
public record PromptsCapability
81+
public class PromptsCapability
8282
{
8383
/// <summary>
8484
/// Whether this server supports notifications for changes to the prompt list.
8585
/// </summary>
8686
[JsonPropertyName("listChanged")]
87-
public bool? ListChanged { get; init; }
87+
public bool? ListChanged { get; set; }
8888

8989
/// <summary>
9090
/// Gets or sets the handler for list prompts requests.
9191
/// </summary>
9292
[JsonIgnore]
93-
public Func<RequestContext<ListPromptsRequestParams>, CancellationToken, Task<ListPromptsResult>>? ListPromptsHandler { get; init; }
93+
public Func<RequestContext<ListPromptsRequestParams>, CancellationToken, Task<ListPromptsResult>>? ListPromptsHandler { get; set; }
9494

9595
/// <summary>
9696
/// Gets or sets the handler for get prompt requests.
9797
/// </summary>
9898
[JsonIgnore]
99-
public Func<RequestContext<GetPromptRequestParams>, CancellationToken, Task<GetPromptResult>>? GetPromptHandler { get; init; }
99+
public Func<RequestContext<GetPromptRequestParams>, CancellationToken, Task<GetPromptResult>>? GetPromptHandler { get; set; }
100100
}
101101

102102
/// <summary>
103103
/// Represents the resources capability configuration.
104104
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
105105
/// </summary>
106-
public record ResourcesCapability
106+
public class ResourcesCapability
107107
{
108108
/// <summary>
109109
/// Whether this server supports subscribing to resource updates.
110110
/// </summary>
111111
[JsonPropertyName("subscribe")]
112-
public bool? Subscribe { get; init; }
112+
public bool? Subscribe { get; set; }
113113

114114
/// <summary>
115115
/// Whether this server supports notifications for changes to the resource list.
116116
/// </summary>
117117
[JsonPropertyName("listChanged")]
118-
public bool? ListChanged { get; init; }
118+
public bool? ListChanged { get; set; }
119119

120120
/// <summary>
121121
/// Gets or sets the handler for list resource templates requests.
122122
/// </summary>
123123
[JsonIgnore]
124-
public Func<RequestContext<ListResourceTemplatesRequestParams>, CancellationToken, Task<ListResourceTemplatesResult>>? ListResourceTemplatesHandler { get; init; }
124+
public Func<RequestContext<ListResourceTemplatesRequestParams>, CancellationToken, Task<ListResourceTemplatesResult>>? ListResourceTemplatesHandler { get; set; }
125125

126126
/// <summary>
127127
/// Gets or sets the handler for list resources requests.
128128
/// </summary>
129129
[JsonIgnore]
130-
public Func<RequestContext<ListResourcesRequestParams>, CancellationToken, Task<ListResourcesResult>>? ListResourcesHandler { get; init; }
130+
public Func<RequestContext<ListResourcesRequestParams>, CancellationToken, Task<ListResourcesResult>>? ListResourcesHandler { get; set; }
131131

132132
/// <summary>
133133
/// Gets or sets the handler for read resources requests.
134134
/// </summary>
135135
[JsonIgnore]
136-
public Func<RequestContext<ReadResourceRequestParams>, CancellationToken, Task<ReadResourceResult>>? ReadResourceHandler { get; init; }
136+
public Func<RequestContext<ReadResourceRequestParams>, CancellationToken, Task<ReadResourceResult>>? ReadResourceHandler { get; set; }
137137

138138
/// <summary>
139139
/// Gets or sets the handler for subscribe to resources messages.
140140
/// </summary>
141141
[JsonIgnore]
142-
public Func<RequestContext<SubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? SubscribeToResourcesHandler { get; init; }
142+
public Func<RequestContext<SubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? SubscribeToResourcesHandler { get; set; }
143143

144144
/// <summary>
145145
/// Gets or sets the handler for unsubscribe from resources messages.
146146
/// </summary>
147147
[JsonIgnore]
148-
public Func<RequestContext<UnsubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? UnsubscribeFromResourcesHandler { get; init; }
148+
public Func<RequestContext<UnsubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? UnsubscribeFromResourcesHandler { get; set; }
149149
}
150150

151151
/// <summary>
152152
/// Represents the tools capability configuration.
153153
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
154154
/// </summary>
155-
public record ToolsCapability
155+
public class ToolsCapability
156156
{
157157
/// <summary>
158158
/// Gets or sets whether this server supports notifications for changes to the tool list.
159159
/// </summary>
160160
[JsonPropertyName("listChanged")]
161-
public bool? ListChanged { get; init; }
161+
public bool? ListChanged { get; set; }
162162

163163
/// <summary>
164164
/// Gets or sets the handler for list tools requests.
165165
/// </summary>
166166
[JsonIgnore]
167-
public Func<RequestContext<ListToolsRequestParams>, CancellationToken, Task<ListToolsResult>>? ListToolsHandler { get; init; }
167+
public Func<RequestContext<ListToolsRequestParams>, CancellationToken, Task<ListToolsResult>>? ListToolsHandler { get; set; }
168168

169169
/// <summary>
170170
/// Gets or sets the handler for call tool requests.
171171
/// </summary>
172172
[JsonIgnore]
173-
public Func<RequestContext<CallToolRequestParams>, CancellationToken, Task<CallToolResponse>>? CallToolHandler { get; init; }
173+
public Func<RequestContext<CallToolRequestParams>, CancellationToken, Task<CallToolResponse>>? CallToolHandler { get; set; }
174174

175175
/// <summary>Gets or sets a collection of tools served by the server.</summary>
176176
/// <remarks>
@@ -182,5 +182,5 @@ public record ToolsCapability
182182
/// will be invoked as a fallback.
183183
/// </remarks>
184184
[JsonIgnore]
185-
public McpServerToolCollection? ToolCollection { get; init; }
185+
public McpServerToolCollection? ToolCollection { get; set; }
186186
}

src/ModelContextProtocol/Protocol/Types/Implementation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ namespace ModelContextProtocol.Protocol.Types;
66
/// Describes the name and version of an MCP implementation.
77
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
88
/// </summary>
9-
public record Implementation
9+
public class Implementation
1010
{
1111
/// <summary>
1212
/// Name of the implementation.
1313
/// </summary>
1414
[JsonPropertyName("name")]
15-
public required string Name { get; init; }
15+
public required string Name { get; set; }
1616

1717
/// <summary>
1818
/// Version of the implementation.
1919
/// </summary>
2020
[JsonPropertyName("version")]
21-
public required string Version { get; init; }
21+
public required string Version { get; set; }
2222
}

src/ModelContextProtocol/Protocol/Types/ServerCapabilities.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@ namespace ModelContextProtocol.Protocol.Types;
66
/// Represents the capabilities that a server may support.
77
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
88
/// </summary>
9-
public record ServerCapabilities
9+
public class ServerCapabilities
1010
{
1111
/// <summary>
1212
/// Experimental, non-standard capabilities that the server supports.
1313
/// </summary>
1414
[JsonPropertyName("experimental")]
15-
public Dictionary<string, object>? Experimental { get; init; }
15+
public Dictionary<string, object>? Experimental { get; set; }
1616

1717
/// <summary>
1818
/// Present if the server supports sending log messages to the client.
1919
/// </summary>
2020
[JsonPropertyName("logging")]
21-
public LoggingCapability? Logging { get; init; }
21+
public LoggingCapability? Logging { get; set; }
2222

2323
/// <summary>
2424
/// Present if the server offers any prompt templates.
2525
/// </summary>
2626
[JsonPropertyName("prompts")]
27-
public PromptsCapability? Prompts { get; init; }
27+
public PromptsCapability? Prompts { get; set; }
2828

2929
/// <summary>
3030
/// Present if the server offers any resources to read.
3131
/// </summary>
3232
[JsonPropertyName("resources")]
33-
public ResourcesCapability? Resources { get; init; }
33+
public ResourcesCapability? Resources { get; set; }
3434

3535
/// <summary>
3636
/// Present if the server offers any tools to call.
3737
/// </summary>
3838
[JsonPropertyName("tools")]
39-
public ToolsCapability? Tools { get; init; }
39+
public ToolsCapability? Tools { get; set; }
4040
}

src/ModelContextProtocol/Server/McpServer.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,14 @@ private void SetToolsHandler(ref McpServerOptions options)
263263
return tool.InvokeAsync(request, cancellationToken);
264264
};
265265

266-
toolsCapability = toolsCapability is null ?
267-
new()
268-
{
269-
CallToolHandler = callToolHandler,
270-
ListToolsHandler = listToolsHandler,
271-
ToolCollection = tools,
272-
ListChanged = true,
273-
} :
274-
toolsCapability with
275-
{
276-
CallToolHandler = callToolHandler,
277-
ListToolsHandler = listToolsHandler,
278-
ToolCollection = tools,
279-
ListChanged = true,
280-
};
281-
282-
options.Capabilities = options.Capabilities is null ?
283-
new() { Tools = toolsCapability } :
284-
options.Capabilities with { Tools = toolsCapability };
266+
toolsCapability ??= new();
267+
toolsCapability.CallToolHandler = callToolHandler;
268+
toolsCapability.ListToolsHandler = listToolsHandler;
269+
toolsCapability.ToolCollection = tools;
270+
toolsCapability.ListChanged = true;
271+
272+
options.Capabilities ??= new();
273+
options.Capabilities.Tools = toolsCapability;
285274

286275
tools.Changed += delegate
287276
{

0 commit comments

Comments
 (0)