Address some static analysis and TODOs (#12)

This commit is contained in:
Stephen Toub 2025-03-20 23:09:38 -04:00 • committed by GitHub
commit 33b9582199
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 132 additions and 123 deletions

View file

@ -45,7 +45,7 @@ internal static class Program
GetCompletionHandler = ConfigureCompletion(),
};
var loggerFactory = CreateLoggerFactory();
using var loggerFactory = CreateLoggerFactory();
await using IMcpServer server = McpServerFactory.Create(new StdioServerTransport("TestServer", loggerFactory), options, loggerFactory);
Log.Logger.Information("Server initialized.");
@ -266,7 +266,7 @@ internal static class Program
};
}
private static HashSet<string> _subscribedResources = new();
private static readonly HashSet<string> _subscribedResources = new();
private static readonly object _subscribedResourcesLock = new();
private static ResourcesCapability ConfigureResources()

View file

@ -29,7 +29,7 @@ internal class Program
{
Console.WriteLine("Starting server...");
McpServerOptions options = new McpServerOptions()
McpServerOptions options = new()
{
ServerInfo = new Implementation() { Name = "TestServer", Version = "1.0.0" },
Capabilities = new ServerCapabilities()
@ -236,11 +236,10 @@ internal class Program
{
throw new McpServerException("Missing required argument 'uri'");
}
ResourceContents? contents = resourceContents.FirstOrDefault(r => r.Uri == request.Params.Uri);
if (contents is null)
{
ResourceContents? contents = resourceContents.FirstOrDefault(r => r.Uri == request.Params.Uri) ??
throw new McpServerException("Resource not found");
}
return Task.FromResult(new ReadResourceResult()
{
Contents = [contents]
@ -347,7 +346,7 @@ internal class Program
},
};
var loggerFactory = CreateLoggerFactory();
using var loggerFactory = CreateLoggerFactory();
server = McpServerFactory.Create(new HttpListenerSseServerTransport("TestServer", 3001, loggerFactory), options, loggerFactory);
Console.WriteLine("Server initialized.");
@ -357,10 +356,7 @@ internal class Program
Console.WriteLine("Server started.");
// Run until process is stopped by the client (parent process)
while (true)
{
await Task.Delay(1000);
}
await Task.Delay(Timeout.Infinite);
}
const string MCP_TINY_IMAGE =

View file

@ -220,18 +220,17 @@ public class McpServerBuilderExtensionsToolsTests
Assert.Contains("'NotRegisteredTool'", exception.Message);
}
// TODO: https://github.com/dotnet/extensions/issues/6124
//[Fact]
//public async Task Throws_Exception_Missing_Parameter()
//{
// _builder.Object.WithTools(typeof(EchoTool));
[Fact(Skip = "https://github.com/dotnet/extensions/issues/6124")]
public async Task Throws_Exception_Missing_Parameter()
{
_builder.Object.WithTools(typeof(EchoTool));
// var serviceProvider = _services.BuildServiceProvider();
// var options = serviceProvider.GetRequiredService<IOptions<McpServerHandlers>>().Value;
var serviceProvider = _services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<McpServerHandlers>>().Value;
// var exception = await Assert.ThrowsAsync<McpServerException>(async () => await options.CallToolHandler!(new(Mock.Of<IMcpServer>(), new() { Name = "Echo" }), CancellationToken.None));
// Assert.Equal("Missing required argument 'message'.", exception.Message);
//}
var exception = await Assert.ThrowsAsync<McpServerException>(async () => await options.CallToolHandler!(new(Mock.Of<IMcpServer>(), new() { Name = "Echo" }), CancellationToken.None));
Assert.Equal("Missing required argument 'message'.", exception.Message);
}
[Fact]
public void Throws_Exception_For_Null_Types()
@ -357,26 +356,6 @@ public class McpServerBuilderExtensionsToolsTests
}
}
public interface IDependendService
{
}
private class EchoToolWithDi
{
public EchoToolWithDi(IDependendService service)
{
}
[McpTool, Description("Echoes the input back to the client.")]
#pragma warning disable CA1822 // Mark members as static
public Task<string> Echo(string message)
#pragma warning restore CA1822 // Mark members as static
{
return Task.FromResult("hello " + message);
}
}
public class ComplexObject
{
public string? Name { get; set; }

View file

@ -4,8 +4,8 @@ namespace ModelContextProtocol.Tests;
public class EverythingSseServerFixture : IAsyncDisposable
{
private int _port;
private string _containerName;
private readonly int _port;
private readonly string _containerName;
public static bool IsDockerAvailable => _isDockerAvailable ??= CheckIsDockerAvailable();
private static bool? _isDockerAvailable;

View file

@ -44,9 +44,11 @@ public static class ProtocolTypeTests
public static void ToolInputSchema_AcceptsValidSchemaDocuments(string validSchema)
{
using var document = JsonDocument.Parse(validSchema);
var tool = new Tool();
Tool tool = new()
{
InputSchema = document.RootElement
};
tool.InputSchema = document.RootElement;
Assert.True(JsonElement.DeepEquals(document.RootElement, tool.InputSchema));
}
}

View file

@ -64,7 +64,7 @@ public class StdioServerTransportTests
TextWriter oldOut = Console.Out;
try
{
var output = new StringWriter();
using var output = new StringWriter();
Console.SetIn(new StringReader(""));
Console.SetOut(output);

View file

@ -8,14 +8,12 @@ public class MockHttpHandler : HttpMessageHandler
{
if (RequestHandler == null)
throw new InvalidOperationException($"No {nameof(RequestHandler)} was set! Please set handler first and make request afterwards.");
if (cancellationToken.IsCancellationRequested)
throw new OperationCanceledException(cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
var result = await RequestHandler.Invoke(request);
if (cancellationToken.IsCancellationRequested)
throw new OperationCanceledException(cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
return result;
}