Skip to content

Commit 824d01d

Browse files
committed
Address more feedback and further cleanup
1 parent 3725859 commit 824d01d

File tree

25 files changed

+41
-128
lines changed

25 files changed

+41
-128
lines changed

samples/QuickstartWeatherServer/Tools/WeatherTools.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using ModelContextProtocol;
22
using ModelContextProtocol.Server;
33
using System.ComponentModel;
4-
using System.Net.Http.Json;
54
using System.Text.Json;
65

76
namespace QuickstartWeatherServer.Tools;

src/ModelContextProtocol/Client/McpClient.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
using ModelContextProtocol.Protocol.Types;
66
using ModelContextProtocol.Shared;
77
using ModelContextProtocol.Utils.Json;
8-
using System.Diagnostics;
9-
using System.Reflection;
108
using System.Text.Json;
119

1210
namespace ModelContextProtocol.Client;
1311

1412
/// <inheritdoc/>
1513
internal sealed class McpClient : McpEndpoint, IMcpClient
1614
{
17-
/// <summary>Cached naming information used for client name/version when none is specified.</summary>
18-
private static readonly AssemblyName s_asmName = (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).GetName();
15+
private static Implementation DefaultImplementation { get; } = new()
16+
{
17+
Name = DefaultAssemblyName.Name ?? nameof(McpClient),
18+
Version = DefaultAssemblyName.Version?.ToString() ?? "1.0.0",
19+
};
1920

2021
private readonly IClientTransport _clientTransport;
2122
private readonly McpClientOptions _options;
@@ -37,17 +38,9 @@ internal sealed class McpClient : McpEndpoint, IMcpClient
3738
public McpClient(IClientTransport clientTransport, McpClientOptions? options, McpServerConfig serverConfig, ILoggerFactory? loggerFactory)
3839
: base(loggerFactory)
3940
{
40-
_clientTransport = clientTransport;
41+
options ??= new();
4142

42-
if (options?.ClientInfo is null)
43-
{
44-
options = options?.Clone() ?? new();
45-
options.ClientInfo = new()
46-
{
47-
Name = s_asmName.Name ?? nameof(McpClient),
48-
Version = s_asmName.Version?.ToString() ?? "1.0.0",
49-
};
50-
}
43+
_clientTransport = clientTransport;
5144
_options = options;
5245

5346
EndpointName = $"Client ({serverConfig.Id}: {serverConfig.Name})";
@@ -122,14 +115,13 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
122115
try
123116
{
124117
// Send initialize request
125-
Debug.Assert(_options.ClientInfo is not null, "ClientInfo should be set by the constructor");
126118
var initializeResponse = await this.SendRequestAsync(
127119
RequestMethods.Initialize,
128120
new InitializeRequestParams
129121
{
130122
ProtocolVersion = _options.ProtocolVersion,
131123
Capabilities = _options.Capabilities ?? new ClientCapabilities(),
132-
ClientInfo = _options.ClientInfo!
124+
ClientInfo = _options.ClientInfo ?? DefaultImplementation,
133125
},
134126
McpJsonUtilities.JsonContext.Default.InitializeRequestParams,
135127
McpJsonUtilities.JsonContext.Default.InitializeResult,

src/ModelContextProtocol/Client/McpClientFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using ModelContextProtocol.Utils;
66
using Microsoft.Extensions.Logging;
77
using Microsoft.Extensions.Logging.Abstractions;
8-
using System.Reflection;
98

109
namespace ModelContextProtocol.Client;
1110

src/ModelContextProtocol/Client/McpClientOptions.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,4 @@ public class McpClientOptions
2828
/// Timeout for initialization sequence.
2929
/// </summary>
3030
public TimeSpan InitializationTimeout { get; set; } = TimeSpan.FromSeconds(60);
31-
32-
/// <summary>Creates a shallow clone of the options.</summary>
33-
internal McpClientOptions Clone() =>
34-
new()
35-
{
36-
ClientInfo = ClientInfo,
37-
Capabilities = Capabilities,
38-
ProtocolVersion = ProtocolVersion,
39-
InitializationTimeout = InitializationTimeout
40-
};
4131
}

src/ModelContextProtocol/Client/McpClientTool.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using ModelContextProtocol.Protocol.Types;
22
using ModelContextProtocol.Utils.Json;
3-
using ModelContextProtocol.Utils;
43
using Microsoft.Extensions.AI;
54
using System.Text.Json;
65

src/ModelContextProtocol/Configuration/McpServerOptionsSetup.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Reflection;
2-
using ModelContextProtocol.Server;
1+
using ModelContextProtocol.Server;
32
using Microsoft.Extensions.Options;
43
using ModelContextProtocol.Utils;
54

@@ -25,18 +24,6 @@ public void Configure(McpServerOptions options)
2524
{
2625
Throw.IfNull(options);
2726

28-
// Configure the option's server information based on the current process,
29-
// if it otherwise lacks server information.
30-
if (options.ServerInfo is not { } serverInfo)
31-
{
32-
var assemblyName = Assembly.GetEntryAssembly()?.GetName();
33-
options.ServerInfo = new()
34-
{
35-
Name = assemblyName?.Name ?? "McpServer",
36-
Version = assemblyName?.Version?.ToString() ?? "1.0.0",
37-
};
38-
}
39-
4027
// Collect all of the provided tools into a tools collection. If the options already has
4128
// a collection, add to it, otherwise create a new one. We want to maintain the identity
4229
// of an existing collection in case someone has provided their own derived type, wants

src/ModelContextProtocol/Protocol/Transport/StdioClientTransport.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.Extensions.Logging.Abstractions;
33
using ModelContextProtocol.Logging;
44
using ModelContextProtocol.Utils;
5-
using System.ComponentModel;
65
using System.Diagnostics;
76
using System.Text;
87

src/ModelContextProtocol/Protocol/Transport/StdioServerTransport.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ public StdioServerTransport(string serverName, ILoggerFactory? loggerFactory = n
7070
private static string GetServerName(McpServerOptions serverOptions)
7171
{
7272
Throw.IfNull(serverOptions);
73-
Throw.IfNull(serverOptions.ServerInfo);
74-
Throw.IfNull(serverOptions.ServerInfo.Name);
7573

76-
return serverOptions.ServerInfo.Name;
74+
return serverOptions.ServerInfo?.Name ?? McpServer.DefaultImplementation.Name;
7775
}
7876
}

src/ModelContextProtocol/Protocol/Types/ListRootsRequestParams.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using ModelContextProtocol.Protocol.Messages;
2-
3-
namespace ModelContextProtocol.Protocol.Types;
1+
namespace ModelContextProtocol.Protocol.Types;
42

53
/// <summary>
64
/// A request from the server to get a list of root URIs from the client.

src/ModelContextProtocol/Server/McpServer.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ namespace ModelContextProtocol.Server;
1111
/// <inheritdoc />
1212
internal sealed class McpServer : McpEndpoint, IMcpServer
1313
{
14+
internal static Implementation DefaultImplementation { get; } = new()
15+
{
16+
Name = DefaultAssemblyName.Name ?? nameof(McpServer),
17+
Version = DefaultAssemblyName.Version?.ToString() ?? "1.0.0",
18+
};
19+
1420
private readonly EventHandler? _toolsChangedDelegate;
1521
private readonly EventHandler? _promptsChangedDelegate;
1622

@@ -32,9 +38,11 @@ public McpServer(ITransport transport, McpServerOptions options, ILoggerFactory?
3238
Throw.IfNull(transport);
3339
Throw.IfNull(options);
3440

41+
options ??= new();
42+
3543
ServerOptions = options;
3644
Services = serviceProvider;
37-
_endpointName = $"Server ({options.ServerInfo.Name} {options.ServerInfo.Version})";
45+
_endpointName = $"Server ({options.ServerInfo?.Name ?? DefaultImplementation.Name} {options.ServerInfo?.Version ?? DefaultImplementation.Version})";
3846

3947
_toolsChangedDelegate = delegate
4048
{
@@ -158,7 +166,7 @@ private void SetInitializeHandler(McpServerOptions options)
158166
{
159167
ProtocolVersion = options.ProtocolVersion,
160168
Instructions = options.ServerInstructions,
161-
ServerInfo = options.ServerInfo,
169+
ServerInfo = options.ServerInfo ?? DefaultImplementation,
162170
Capabilities = ServerCapabilities ?? new(),
163171
});
164172
},

0 commit comments

Comments
 (0)