Enable .NET SDK code analyzers (#41)

This commit is contained in:
Stephen Toub 2025-03-08 11:18:09 -05:00 • committed by GitHub
commit 587e05f057
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 301 additions and 151 deletions

18
.editorconfig Normal file
View file

@ -0,0 +1,18 @@
# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true
# C# files
[*.cs]
# Code Analysis
dotnet_diagnostic.CA1002.severity = none # CA1002: Do not expose generic lists
dotnet_diagnostic.CA1031.severity = none # CA1031: Do not catch general exception types
dotnet_diagnostic.CA1054.severity = none # CA1054: URI-like parameters should not be strings
dotnet_diagnostic.CA1056.severity = none # CA1056: URI-like properties should not be strings
dotnet_diagnostic.CA1062.severity = suggestion # CA1062: Validate arguments of public methods
dotnet_diagnostic.CA1510.severity = suggestion # CA1510: Use ArgumentNullException throw helper
dotnet_diagnostic.CA1849.severity = suggestion # CA1849: Call async methods when in an async method
dotnet_diagnostic.CA2000.severity = suggestion # CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2227.severity = suggestion # CA2227: Collection properties should be read only
dotnet_diagnostic.CA2249.severity = suggestion # CA2249: Use 'string.Contains' instead of 'string.IndexOf'
dotnet_diagnostic.CA1819.severity = suggestion # CA1819: Properties should not return arrays

View file

@ -5,7 +5,7 @@ namespace TestServerWithHosting.Tools;
[McpToolType]
public static class EchoTool
{
[McpTool(Description = "Echoes the input back to the client.")]
[McpTool(description: "Echoes the input back to the client.")]
public static string Echo([McpParameter(true)] string message)
{
return "hello " + message;

View file

@ -16,8 +16,11 @@ public class SampleLlmTool
_server = server ?? throw new ArgumentNullException(nameof(server));
}
[McpTool(Name = "sampleLLM", Description = "Samples from an LLM using MCP's sampling feature")]
public async Task<string> SampleLLM([McpParameter(true, Description = "The prompt to send to the LLM")] string prompt, [McpParameter(true, Description = "Maximum number of tokens to generate")] int maxTokens, CancellationToken cancellationToken)
[McpTool(name: "sampleLLM", description: "Samples from an LLM using MCP's sampling feature")]
public async Task<string> SampleLLM(
[McpParameter(true, description: "The prompt to send to the LLM")] string prompt,
[McpParameter(true, description: "Maximum number of tokens to generate")] int maxTokens,
CancellationToken cancellationToken)
{
var samplingParams = CreateRequestSamplingParams(prompt ?? string.Empty, "sampleLLM", maxTokens);
var sampleResult = await _server.RequestSamplingAsync(samplingParams, cancellationToken);

View file

@ -2,10 +2,17 @@
using System.Text.Json;
using System.Text.Json.Nodes;
namespace McpDotNet;
public static class McpToolExtensions
{
public static IList<Anthropic.SDK.Common.Tool> ToAnthropicTools(this IEnumerable<McpDotNet.Protocol.Types.Tool> tools)
{
if (tools is null)
{
throw new ArgumentNullException(nameof(tools));
}
List<Anthropic.SDK.Common.Tool> result = [];
foreach (var tool in tools)
{

View file

@ -1,6 +1,7 @@
using Anthropic.SDK;
using Anthropic.SDK.Constants;
using Anthropic.SDK.Messaging;
using McpDotNet;
using McpDotNet.Client;
using McpDotNet.Configuration;
using McpDotNet.Protocol.Transport;

View file

@ -29,6 +29,11 @@ public class McpAIFunction : AIFunction
protected async override Task<object?> InvokeCoreAsync(IEnumerable<KeyValuePair<string, object?>> arguments, CancellationToken cancellationToken)
{
if (arguments is null)
{
throw new ArgumentNullException(nameof(arguments));
}
// Convert arguments to dictionary format expected by mcpdotnet
Dictionary<string, object> argDict = [];
foreach (var arg in arguments)

View file

@ -34,6 +34,11 @@ public class McpAIFunction : AIFunction
/// <inheritdoc/>
protected async override Task<object?> InvokeCoreAsync(IEnumerable<KeyValuePair<string, object?>> arguments, CancellationToken cancellationToken)
{
if (arguments is null)
{
throw new ArgumentNullException(nameof(arguments));
}
// Convert arguments to dictionary format expected by mcpdotnet
Dictionary<string, object> argDict = [];
foreach (var arg in arguments)
@ -45,7 +50,7 @@ public class McpAIFunction : AIFunction
}
// Call the tool through mcpdotnet
var result = await _client.CallToolAsync(_tool.Name, argDict, cancellationToken);
var result = await _client.CallToolAsync(_tool.Name, argDict, cancellationToken).ConfigureAwait(false);
// Extract the text content from the result.
return string.Join("\n", result.Content

View file

@ -5,6 +5,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>Latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisMode>All</AnalysisMode>
</PropertyGroup>
<PropertyGroup>

View file

@ -12,7 +12,7 @@ namespace McpDotNet.Extensions.AI;
///
/// Disposing the scope will dispose all clients and tools, and close all connections.
/// </summary>
public class McpSessionScope : IAsyncDisposable
public sealed class McpSessionScope : IAsyncDisposable
{
private readonly List<IMcpClient> _clients = [];
private bool _disposed;
@ -39,9 +39,14 @@ public class McpSessionScope : IAsyncDisposable
McpClientOptions? options = null,
ILoggerFactory? loggerFactory = null)
{
if (serverConfig is null)
{
throw new ArgumentNullException(nameof(serverConfig));
}
var scope = new McpSessionScope();
var client = await scope.AddClientAsync(serverConfig, options, loggerFactory);
var tools = await client.ListToolsAsync();
var client = await scope.AddClientAsync(serverConfig, options, loggerFactory).ConfigureAwait(false);
var tools = await client.ListToolsAsync().ConfigureAwait(false);
scope.Tools = tools.Tools.Select(t => new McpAIFunction(t, client)).ToList<AITool>();
if (!string.IsNullOrEmpty(client.ServerInstructions))
{
@ -61,11 +66,16 @@ public class McpSessionScope : IAsyncDisposable
McpClientOptions? options = null,
ILoggerFactory? loggerFactory = null)
{
if (serverConfigs is null)
{
throw new ArgumentNullException(nameof(serverConfigs));
}
var scope = new McpSessionScope();
foreach (var config in serverConfigs)
{
var client = await scope.AddClientAsync(config, options, loggerFactory);
var tools = await client.ListToolsAsync();
var client = await scope.AddClientAsync(config, options, loggerFactory).ConfigureAwait(false);
var tools = await client.ListToolsAsync().ConfigureAwait(false);
scope.Tools = scope.Tools.Concat(tools.Tools.Select(t => new McpAIFunction(t, client))).ToList();
}
scope.ServerInstructions = scope._clients.Select(c => c.ServerInstructions).Where(s => !string.IsNullOrEmpty(s)).ToList()!;
@ -79,7 +89,7 @@ public class McpSessionScope : IAsyncDisposable
var factory = new McpClientFactory([config],
options ?? new() { ClientInfo = new() { Name = "AnonymousClient", Version = "1.0.0.0" } },
loggerFactory ?? NullLoggerFactory.Instance);
var client = await factory.GetClientAsync(config.Id);
var client = await factory.GetClientAsync(config.Id).ConfigureAwait(false);
_clients.Add(client);
return client;
}
@ -92,7 +102,7 @@ public class McpSessionScope : IAsyncDisposable
foreach (var client in _clients)
{
await client.DisposeAsync();
await client.DisposeAsync().ConfigureAwait(false);
}
_clients.Clear();
Tools = [];

View file

@ -1,8 +1,8 @@
namespace McpDotNet.Client;
using McpDotNet.Protocol.Messages;
using McpDotNet.Protocol.Messages;
using McpDotNet.Protocol.Types;
namespace McpDotNet.Client;
/// <summary>
/// Represents an instance of the MCP client connecting to a specific server.
/// Use PingAsync, ListToolsAsync and CallToolAsync instead of SendRequestAsync for common operations.

View file

@ -55,7 +55,7 @@ internal class McpClient : McpJsonRpcEndpoint, IMcpClient
throw new McpClientException("Sampling handler not configured");
}
return await SamplingHandler(request, CancellationTokenSource?.Token ?? CancellationToken.None);
return await SamplingHandler(request, CancellationTokenSource?.Token ?? CancellationToken.None).ConfigureAwait(false);
});
}
if (options.Capabilities?.Roots != null)
@ -69,7 +69,7 @@ internal class McpClient : McpJsonRpcEndpoint, IMcpClient
_logger.RootsHandlerNotConfigured(EndpointName);
throw new McpClientException("Roots handler not configured");
}
return await RootsHandler(request, CancellationTokenSource?.Token ?? CancellationToken.None);
return await RootsHandler(request, CancellationTokenSource?.Token ?? CancellationToken.None).ConfigureAwait(false);
});
}
}

View file

@ -10,6 +10,13 @@ public class McpClientException : Exception
/// </summary>
public int? ErrorCode { get; }
/// <summary>
/// Initializes a new instance of the <see cref="McpClientException"/> class.
/// </summary>
public McpClientException()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="McpClientException"/> class with a specified error message.
/// </summary>

View file

@ -81,7 +81,7 @@ public class McpClientFactory
var transport = _transportFactoryMethod(config);
var client = _clientFactoryMethod(transport, config, _clientOptions);
await client.ConnectAsync(cancellationToken);
await client.ConnectAsync(cancellationToken).ConfigureAwait(false);
_logger.ClientCreated(endpointName);
@ -97,29 +97,36 @@ public class McpClientFactory
var options = string.Join(", ", config.TransportOptions?.Select(kv => $"{kv.Key}={kv.Value}") ?? []);
_logger.CreatingTransport(endpointName, config.TransportType, options);
return config.TransportType.ToLowerInvariant() switch
if (string.Equals(config.TransportType, TransportTypes.StdIo, StringComparison.OrdinalIgnoreCase))
{
TransportTypes.StdIo => new StdioClientTransport(new StdioClientTransportOptions
return new StdioClientTransport(new StdioClientTransportOptions
{
Command = GetCommand(config),
Arguments = config.TransportOptions?.GetValueOrDefault("arguments")?.Split(' '),
WorkingDirectory = config.TransportOptions?.GetValueOrDefault("workingDirectory"),
EnvironmentVariables = config.TransportOptions?
.Where(kv => kv.Key.StartsWith("env:"))
.Where(kv => kv.Key.StartsWith("env:", StringComparison.Ordinal))
.ToDictionary(kv => kv.Key.Substring(4), kv => kv.Value)
}, config, _loggerFactory),
TransportTypes.Sse or "http" => new SseClientTransport(
}, config, _loggerFactory);
}
if (string.Equals(config.TransportType, TransportTypes.Sse, StringComparison.OrdinalIgnoreCase) ||
string.Equals(config.TransportType, "http", StringComparison.OrdinalIgnoreCase))
{
return new SseClientTransport(
new SseClientTransportOptions
{
ConnectionTimeout = TimeSpan.FromSeconds(ParseOrDefault(config.TransportOptions, "connectionTimeout", 30)),
MaxReconnectAttempts = ParseOrDefault(config.TransportOptions, "maxReconnectAttempts", 3),
ReconnectDelay = TimeSpan.FromSeconds(ParseOrDefault(config.TransportOptions, "reconnectDelay", 5)),
AdditionalHeaders = config.TransportOptions?
.Where(kv => kv.Key.StartsWith("header."))
.Where(kv => kv.Key.StartsWith("header.", StringComparison.Ordinal))
.ToDictionary(kv => kv.Key.Substring(7), kv => kv.Value)
}, config, _loggerFactory),
_ => throw new ArgumentException($"Unsupported transport type '{config.TransportType}'.", nameof(config))
};
}, config, _loggerFactory);
}
throw new ArgumentException($"Unsupported transport type '{config.TransportType}'.", nameof(config));
}
private static int ParseOrDefault(Dictionary<string, string>? options, string key, int defaultValue)

View file

@ -17,6 +17,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithListToolsHandler(this IMcpServerBuilder builder, Func<RequestContext<ListToolsRequestParams>, CancellationToken, Task<ListToolsResult>> handler)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<McpServerDelegates>(s => s.ListToolsHandler = handler);
return builder;
}
@ -28,6 +33,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithCallToolHandler(this IMcpServerBuilder builder, Func<RequestContext<CallToolRequestParams>, CancellationToken, Task<CallToolResponse>> handler)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<McpServerDelegates>(s => s.CallToolHandler = handler);
return builder;
}
@ -39,6 +49,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithListPromptsHandler(this IMcpServerBuilder builder, Func<RequestContext<ListPromptsRequestParams>, CancellationToken, Task<ListPromptsResult>> handler)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<McpServerDelegates>(s => s.ListPromptsHandler = handler);
return builder;
}
@ -50,6 +65,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithGetPromptHandler(this IMcpServerBuilder builder, Func<RequestContext<GetPromptRequestParams>, CancellationToken, Task<GetPromptResult>> handler)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<McpServerDelegates>(s => s.GetPromptHandler = handler);
return builder;
}
@ -61,6 +81,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithListResourcesHandler(this IMcpServerBuilder builder, Func<RequestContext<ListResourcesRequestParams>, CancellationToken, Task<ListResourcesResult>> handler)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<McpServerDelegates>(s => s.ListResourcesHandler = handler);
return builder;
}
@ -72,6 +97,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithReadResourceHandler(this IMcpServerBuilder builder, Func<RequestContext<ReadResourceRequestParams>, CancellationToken, Task<ReadResourceResult>> handler)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<McpServerDelegates>(s => s.ReadResourceHandler = handler);
return builder;
}
@ -83,6 +113,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithGetCompletionHandler(this IMcpServerBuilder builder, Func<RequestContext<CompleteRequestParams>, CancellationToken, Task<CompleteResult>> handler)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<McpServerDelegates>(s => s.GetCompletionHandler = handler);
return builder;
}
@ -94,6 +129,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithSubscribeToResourcesHandler(this IMcpServerBuilder builder, Func<RequestContext<string>, CancellationToken, Task> handler)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<McpServerDelegates>(s => s.SubscribeToResourcesHandler = handler);
return builder;
}
@ -105,6 +145,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithUnsubscribeFromResourcesHandler(this IMcpServerBuilder builder, Func<RequestContext<string>, CancellationToken, Task> handler)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<McpServerDelegates>(s => s.UnsubscribeFromResourcesHandler = handler);
return builder;
}

View file

@ -1,4 +1,5 @@
using System.Reflection;
using System.Globalization;
using System.Reflection;
using System.Text.Json;
using McpDotNet.Configuration;
using McpDotNet.Protocol.Types;
@ -37,6 +38,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="toolTypes">Types with marked methods to add as tools to the server.</param>
public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, params Type[] toolTypes)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (toolTypes is null || toolTypes.Length == 0)
throw new ArgumentException("At least one tool type must be provided.", nameof(toolTypes));
@ -54,7 +60,7 @@ public static partial class McpServerBuilderExtensions
var tool = CreateTool(method, attribute);
tools.Add(tool);
callbacks.Add(tool.Name, async (request, cancellationToken) => await CallTool(request, method, cancellationToken));
callbacks.Add(tool.Name, async (request, cancellationToken) => await CallTool(request, method, cancellationToken).ConfigureAwait(false));
// register type because method is not static and so we need an instance
if (!method.IsStatic)
@ -68,7 +74,7 @@ public static partial class McpServerBuilderExtensions
builder.WithCallToolHandler(async (request, cancellationToken) =>
{
if (request.Params != null && callbacks.TryGetValue(request.Params.Name, out var callback))
return await callback(request, cancellationToken);
return await callback(request, cancellationToken).ConfigureAwait(false);
throw new McpServerException($"Unknown tool: {request.Params?.Name}");
});
@ -219,7 +225,7 @@ public static partial class McpServerBuilderExtensions
if (value is JsonElement element)
value = JsonSerializer.Deserialize(element.GetRawText(), parameter.ParameterType);
parameters.Add(Convert.ChangeType(value, parameter.ParameterType));
parameters.Add(Convert.ChangeType(value, parameter.ParameterType, CultureInfo.InvariantCulture));
}
else
{

View file

@ -15,6 +15,11 @@ public static partial class McpServerBuilderExtensions
/// <param name="builder">The builder instance.</param>
public static IMcpServerBuilder WithStdioServerTransport(this IMcpServerBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.AddSingleton<IServerTransport, StdioServerTransport>();
return builder;
}

View file

@ -23,6 +23,6 @@ public class McpServerHostedService : BackgroundService
/// <inheritdoc />
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await _server.StartAsync(stoppingToken);
await _server.StartAsync(stoppingToken).ConfigureAwait(false);
}
}

View file

@ -1,6 +1,6 @@
namespace McpDotNet.Protocol.Messages;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;
namespace McpDotNet.Protocol.Messages;
/// <summary>
/// An error response message in the JSON-RPC protocol.

View file

@ -1,6 +1,6 @@
namespace McpDotNet.Protocol.Messages;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;
namespace McpDotNet.Protocol.Messages;
/// <summary>
/// Detailed error information for JSON-RPC error responses.

View file

@ -1,6 +1,6 @@
namespace McpDotNet.Protocol.Messages;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;
namespace McpDotNet.Protocol.Messages;
/// <summary>
/// A request message in the JSON-RPC protocol.

Some files were not shown because too many files have changed in this diff Show more