diff --git a/docs/concepts/filters.md b/docs/concepts/filters.md index 9f63dd962..32f5c6757 100644 --- a/docs/concepts/filters.md +++ b/docs/concepts/filters.md @@ -317,7 +317,7 @@ Execution flow: `filter1 -> filter2 -> filter3 -> baseHandler -> filter3 -> filt { var logger = context.Services?.GetService>(); - logger?.LogInformation($"Processing request from {context.Params?.ProgressToken}"); + logger?.LogInformation($"Processing request from {context.Params.ProgressToken}"); var result = await next(context, cancellationToken); logger?.LogInformation($"Returning {result.Tools?.Count ?? 0} tools"); return result; @@ -339,7 +339,7 @@ Execution flow: `filter1 -> filter2 -> filter3 -> baseHandler -> filter3 -> filt catch (Exception ex) { var logger = context.Services?.GetService>(); - logger?.LogError(ex, "Error while processing CallTool request for {ProgressToken}", context.Params?.ProgressToken); + logger?.LogError(ex, "Error while processing CallTool request for {ProgressToken}", context.Params.ProgressToken); return new CallToolResult { diff --git a/docs/concepts/logging/samples/server/Tools/LoggingTools.cs b/docs/concepts/logging/samples/server/Tools/LoggingTools.cs index 33fa3c040..8ddb9e4df 100644 --- a/docs/concepts/logging/samples/server/Tools/LoggingTools.cs +++ b/docs/concepts/logging/samples/server/Tools/LoggingTools.cs @@ -13,7 +13,7 @@ public static async Task LoggingTool( int duration = 10, int steps = 10) { - var progressToken = context.Params?.ProgressToken; + var progressToken = context.Params.ProgressToken; var stepDuration = duration / steps; // diff --git a/docs/concepts/pagination/pagination.md b/docs/concepts/pagination/pagination.md index 1249acbf5..f019fd1fe 100644 --- a/docs/concepts/pagination/pagination.md +++ b/docs/concepts/pagination/pagination.md @@ -77,7 +77,7 @@ builder.Services.AddMcpServer() int startIndex = 0; // Parse cursor to determine starting position - if (ctx.Params?.Cursor is { } cursor) + if (ctx.Params.Cursor is { } cursor) { startIndex = int.Parse(cursor); } diff --git a/docs/concepts/progress/samples/server/Tools/LongRunningTools.cs b/docs/concepts/progress/samples/server/Tools/LongRunningTools.cs index 7fcd1244a..e02889a05 100644 --- a/docs/concepts/progress/samples/server/Tools/LongRunningTools.cs +++ b/docs/concepts/progress/samples/server/Tools/LongRunningTools.cs @@ -15,7 +15,7 @@ public static async Task LongRunningTool( int duration = 10, int steps = 5) { - var progressToken = context.Params?.ProgressToken; + var progressToken = context.Params.ProgressToken; var stepDuration = duration / steps; for (int i = 1; i <= steps; i++) diff --git a/docs/concepts/resources/resources.md b/docs/concepts/resources/resources.md index c7e713c51..5598d96b2 100644 --- a/docs/concepts/resources/resources.md +++ b/docs/concepts/resources/resources.md @@ -212,7 +212,7 @@ builder.Services.AddMcpServer() .WithResources() .WithSubscribeToResourcesHandler(async (ctx, ct) => { - if (ctx.Params?.Uri is { } uri) + if (ctx.Params.Uri is { } uri) { // Track the subscription (e.g., in a concurrent dictionary) subscriptions[ctx.Server.SessionId].TryAdd(uri, 0); @@ -221,7 +221,7 @@ builder.Services.AddMcpServer() }) .WithUnsubscribeFromResourcesHandler(async (ctx, ct) => { - if (ctx.Params?.Uri is { } uri) + if (ctx.Params.Uri is { } uri) { subscriptions[ctx.Server.SessionId].TryRemove(uri, out _); } diff --git a/docs/list-of-diagnostics.md b/docs/list-of-diagnostics.md index 59666d518..16f17f7ed 100644 --- a/docs/list-of-diagnostics.md +++ b/docs/list-of-diagnostics.md @@ -36,3 +36,4 @@ When APIs are marked as obsolete, a diagnostic is emitted to warn users that the | :------------ | :----- | :---------- | | `MCP9001` | In place | The `EnumSchema` and `LegacyTitledEnumSchema` APIs are deprecated as of specification version 2025-11-25. Use the current schema APIs instead. | | `MCP9002` | Removed | The `AddXxxFilter` extension methods on `IMcpServerBuilder` (e.g., `AddListToolsFilter`, `AddCallToolFilter`, `AddIncomingMessageFilter`) were superseded by `WithRequestFilters()` and `WithMessageFilters()`. | +| `MCP9003` | In place | The `RequestContext(McpServer, JsonRpcRequest)` constructor is obsolete. Use the overload that accepts a `parameters` argument: `RequestContext(McpServer, JsonRpcRequest, TParams)`. | diff --git a/samples/EverythingServer/Program.cs b/samples/EverythingServer/Program.cs index 99d7759bd..1807fa8de 100644 --- a/samples/EverythingServer/Program.cs +++ b/samples/EverythingServer/Program.cs @@ -130,7 +130,7 @@ { throw new McpException("Cannot add subscription for server with null SessionId"); } - if (ctx.Params?.Uri is { } uri) + if (ctx.Params.Uri is { } uri) { subscriptions[ctx.Server.SessionId].TryAdd(uri, 0); @@ -154,7 +154,7 @@ await ctx.Server.SampleAsync([ { throw new McpException("Cannot remove subscription for server with null SessionId"); } - if (ctx.Params?.Uri is { } uri) + if (ctx.Params.Uri is { } uri) { subscriptions[ctx.Server.SessionId].TryRemove(uri, out _); } @@ -212,11 +212,6 @@ await ctx.Server.SampleAsync([ }) .WithSetLoggingLevelHandler(async (ctx, ct) => { - if (ctx.Params?.Level is null) - { - throw new McpProtocolException("Missing required argument 'level'", McpErrorCode.InvalidParams); - } - // The SDK updates the LoggingLevel field of the IMcpServer await ctx.Server.SendNotificationAsync("notifications/message", new diff --git a/samples/EverythingServer/Resources/SimpleResourceType.cs b/samples/EverythingServer/Resources/SimpleResourceType.cs index eafc6c66d..cc1aed715 100644 --- a/samples/EverythingServer/Resources/SimpleResourceType.cs +++ b/samples/EverythingServer/Resources/SimpleResourceType.cs @@ -19,7 +19,7 @@ public static ResourceContents TemplateResource(RequestContext= ResourceGenerator.Resources.Count) { - throw new NotSupportedException($"Unknown resource: {requestContext.Params?.Uri}"); + throw new NotSupportedException($"Unknown resource: {requestContext.Params.Uri}"); } var resource = ResourceGenerator.Resources[index]; diff --git a/samples/EverythingServer/Tools/LongRunningTool.cs b/samples/EverythingServer/Tools/LongRunningTool.cs index 405b5e823..3dc42b5cf 100644 --- a/samples/EverythingServer/Tools/LongRunningTool.cs +++ b/samples/EverythingServer/Tools/LongRunningTool.cs @@ -15,7 +15,7 @@ public static async Task LongRunningOperation( int duration = 10, int steps = 5) { - var progressToken = context.Params?.ProgressToken; + var progressToken = context.Params.ProgressToken; var stepDuration = duration / steps; for (int i = 1; i <= steps + 1; i++) diff --git a/src/Common/Obsoletions.cs b/src/Common/Obsoletions.cs index bbc7bffb4..cc850d126 100644 --- a/src/Common/Obsoletions.cs +++ b/src/Common/Obsoletions.cs @@ -25,4 +25,8 @@ internal static class Obsoletions // MCP9002 was used for the AddXxxFilter extension methods on IMcpServerBuilder that were superseded by // WithMessageFilters() and WithRequestFilters(). The APIs were removed; do not reuse this diagnostic ID. + + public const string RequestContextParamsConstructor_DiagnosticId = "MCP9003"; + public const string RequestContextParamsConstructor_Message = "Use the constructor overload that accepts a parameters argument."; + public const string RequestContextParamsConstructor_Url = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#mcp9003"; } diff --git a/src/ModelContextProtocol.Core/RequestHandlers.cs b/src/ModelContextProtocol.Core/RequestHandlers.cs index fd95751d9..97e8b95df 100644 --- a/src/ModelContextProtocol.Core/RequestHandlers.cs +++ b/src/ModelContextProtocol.Core/RequestHandlers.cs @@ -29,7 +29,7 @@ internal sealed class RequestHandlers : Dictionary public void Set( string method, - Func> handler, + Func> handler, JsonTypeInfo requestTypeInfo, JsonTypeInfo responseTypeInfo) { @@ -40,7 +40,7 @@ public void Set( this[method] = async (request, cancellationToken) => { - TParams? typedRequest = JsonSerializer.Deserialize(request.Params, requestTypeInfo); + TParams typedRequest = JsonSerializer.Deserialize(request.Params, requestTypeInfo)!; object? result = await handler(typedRequest, request, cancellationToken).ConfigureAwait(false); return JsonSerializer.SerializeToNode(result, responseTypeInfo); }; diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerResource.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerResource.cs index 3413d7038..7f0f29140 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerResource.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerResource.cs @@ -386,14 +386,14 @@ public override async ValueTask ReadAsync( TextContent tc => new() { - Contents = [new TextResourceContents { Uri = request.Params!.Uri, MimeType = ProtocolResourceTemplate.MimeType, Text = tc.Text }], + Contents = [new TextResourceContents { Uri = request.Params.Uri, MimeType = ProtocolResourceTemplate.MimeType, Text = tc.Text }], }, DataContent dc => new() { Contents = [new BlobResourceContents { - Uri = request.Params!.Uri, + Uri = request.Params.Uri, MimeType = dc.MediaType, Blob = EncodingUtilities.GetUtf8Bytes(dc.Base64Data.Span) }], @@ -401,7 +401,7 @@ public override async ValueTask ReadAsync( string text => new() { - Contents = [new TextResourceContents { Uri = request.Params!.Uri, MimeType = ProtocolResourceTemplate.MimeType, Text = text }], + Contents = [new TextResourceContents { Uri = request.Params.Uri, MimeType = ProtocolResourceTemplate.MimeType, Text = text }], }, IEnumerable contents => new() @@ -416,14 +416,14 @@ public override async ValueTask ReadAsync( { TextContent tc => new TextResourceContents { - Uri = request.Params!.Uri, + Uri = request.Params.Uri, MimeType = ProtocolResourceTemplate.MimeType, Text = tc.Text }, DataContent dc => new BlobResourceContents { - Uri = request.Params!.Uri, + Uri = request.Params.Uri, MimeType = dc.MediaType, Blob = EncodingUtilities.GetUtf8Bytes(dc.Base64Data.Span) }, @@ -436,7 +436,7 @@ public override async ValueTask ReadAsync( { Contents = strings.Select(text => new TextResourceContents { - Uri = request.Params!.Uri, + Uri = request.Params.Uri, MimeType = ProtocolResourceTemplate.MimeType, Text = text }).ToList(), diff --git a/src/ModelContextProtocol.Core/Server/McpServerImpl.cs b/src/ModelContextProtocol.Core/Server/McpServerImpl.cs index 753d91667..c9a6f764c 100644 --- a/src/ModelContextProtocol.Core/Server/McpServerImpl.cs +++ b/src/ModelContextProtocol.Core/Server/McpServerImpl.cs @@ -954,7 +954,7 @@ private void ConfigureLogging(McpServerOptions options) // If a handler was provided, now delegate to it. if (setLoggingLevelHandler is not null) { - return InvokeHandlerAsync(setLoggingLevelHandler, request, jsonRpcRequest, cancellationToken); + return InvokeHandlerAsync(setLoggingLevelHandler, request!, jsonRpcRequest, cancellationToken); } // Otherwise, consider it handled. @@ -966,17 +966,17 @@ private void ConfigureLogging(McpServerOptions options) private ValueTask InvokeHandlerAsync( McpRequestHandler handler, - TParams? args, + TParams args, JsonRpcRequest jsonRpcRequest, CancellationToken cancellationToken = default) { return _servicesScopePerRequest ? InvokeScopedAsync(handler, args, jsonRpcRequest, cancellationToken) : - handler(new(new DestinationBoundMcpServer(this, jsonRpcRequest.Context?.RelatedTransport), jsonRpcRequest) { Params = args }, cancellationToken); + handler(new(new DestinationBoundMcpServer(this, jsonRpcRequest.Context?.RelatedTransport), jsonRpcRequest, args), cancellationToken); async ValueTask InvokeScopedAsync( McpRequestHandler handler, - TParams? args, + TParams args, JsonRpcRequest jsonRpcRequest, CancellationToken cancellationToken) { @@ -984,10 +984,9 @@ async ValueTask InvokeScopedAsync( try { return await handler( - new RequestContext(new DestinationBoundMcpServer(this, jsonRpcRequest.Context?.RelatedTransport), jsonRpcRequest) + new RequestContext(new DestinationBoundMcpServer(this, jsonRpcRequest.Context?.RelatedTransport), jsonRpcRequest, args) { Services = scope?.ServiceProvider ?? Services, - Params = args }, cancellationToken).ConfigureAwait(false); } diff --git a/src/ModelContextProtocol.Core/Server/RequestContext.cs b/src/ModelContextProtocol.Core/Server/RequestContext.cs index 959bb07da..596712eea 100644 --- a/src/ModelContextProtocol.Core/Server/RequestContext.cs +++ b/src/ModelContextProtocol.Core/Server/RequestContext.cs @@ -13,19 +13,34 @@ namespace ModelContextProtocol.Server; /// public sealed class RequestContext : MessageContext { + /// + /// Initializes a new instance of the class with the specified server, JSON-RPC request, and request parameters. + /// + /// The server with which this instance is associated. + /// The JSON-RPC request associated with this context. + /// The parameters associated with this request. + /// or is . + public RequestContext(McpServer server, JsonRpcRequest jsonRpcRequest, TParams parameters) + : base(server, jsonRpcRequest) + { + Params = parameters; + } + /// /// Initializes a new instance of the class with the specified server and JSON-RPC request. /// /// The server with which this instance is associated. /// The JSON-RPC request associated with this context. /// or is . + [Obsolete(Obsoletions.RequestContextParamsConstructor_Message, DiagnosticId = Obsoletions.RequestContextParamsConstructor_DiagnosticId, UrlFormat = Obsoletions.RequestContextParamsConstructor_Url)] public RequestContext(McpServer server, JsonRpcRequest jsonRpcRequest) : base(server, jsonRpcRequest) { + Params = default!; } /// Gets or sets the parameters associated with this request. - public TParams? Params { get; set; } + public TParams Params { get; set; } /// /// Gets or sets the primitive that matched the request. diff --git a/tests/ModelContextProtocol.ConformanceServer/Program.cs b/tests/ModelContextProtocol.ConformanceServer/Program.cs index 94c62727b..017ec235f 100644 --- a/tests/ModelContextProtocol.ConformanceServer/Program.cs +++ b/tests/ModelContextProtocol.ConformanceServer/Program.cs @@ -39,7 +39,7 @@ public static async Task MainAsync(string[] args, ILoggerProvider? loggerProvide // For the test_reconnection tool, enable polling mode after the tool runs. // This stores the result and closes the SSE stream, so the client // must reconnect via GET with Last-Event-ID to retrieve the result. - if (request.Params?.Name == "test_reconnection") + if (request.Params.Name == "test_reconnection") { await request.EnablePollingAsync(TimeSpan.FromMilliseconds(500), cancellationToken); } @@ -54,7 +54,7 @@ public static async Task MainAsync(string[] args, ILoggerProvider? loggerProvide { throw new McpException("Cannot add subscription for server with null SessionId"); } - if (ctx.Params?.Uri is { } uri) + if (ctx.Params.Uri is { } uri) { var sessionSubscriptions = subscriptions.GetOrAdd(ctx.Server.SessionId, _ => new()); sessionSubscriptions.TryAdd(uri, 0); @@ -68,7 +68,7 @@ public static async Task MainAsync(string[] args, ILoggerProvider? loggerProvide { throw new McpException("Cannot remove subscription for server with null SessionId"); } - if (ctx.Params?.Uri is { } uri) + if (ctx.Params.Uri is { } uri) { subscriptions[ctx.Server.SessionId].TryRemove(uri, out _); } @@ -91,11 +91,6 @@ public static async Task MainAsync(string[] args, ILoggerProvider? loggerProvide }) .WithSetLoggingLevelHandler(async (ctx, ct) => { - if (ctx.Params?.Level is null) - { - throw new McpProtocolException("Missing required argument 'level'", McpErrorCode.InvalidParams); - } - // The SDK updates the LoggingLevel field of the McpServer // Send a log notification to confirm the level was set await ctx.Server.SendNotificationAsync("notifications/message", new LoggingMessageNotificationParams diff --git a/tests/ModelContextProtocol.ConformanceServer/Tools/ConformanceTools.cs b/tests/ModelContextProtocol.ConformanceServer/Tools/ConformanceTools.cs index 0a1f7ecfb..d6db6f626 100644 --- a/tests/ModelContextProtocol.ConformanceServer/Tools/ConformanceTools.cs +++ b/tests/ModelContextProtocol.ConformanceServer/Tools/ConformanceTools.cs @@ -132,7 +132,7 @@ public static async Task ToolWithProgress( RequestContext context, CancellationToken cancellationToken) { - var progressToken = context.Params?.ProgressToken; + var progressToken = context.Params.ProgressToken; if (progressToken is not null) { diff --git a/tests/ModelContextProtocol.TestServer/Program.cs b/tests/ModelContextProtocol.TestServer/Program.cs index 89ad9987d..9cb963a96 100644 --- a/tests/ModelContextProtocol.TestServer/Program.cs +++ b/tests/ModelContextProtocol.TestServer/Program.cs @@ -204,9 +204,9 @@ private static void ConfigureTools(McpServerOptions options, string? cliArg) }; options.Handlers.CallToolHandler = async (request, cancellationToken) => { - if (request.Params?.Name == "echo") + if (request.Params.Name == "echo") { - if (request.Params?.Arguments is null || !request.Params.Arguments.TryGetValue("message", out var message)) + if (request.Params.Arguments is null || !request.Params.Arguments.TryGetValue("message", out var message)) { throw new McpProtocolException("Missing required argument 'message'", McpErrorCode.InvalidParams); } @@ -215,16 +215,16 @@ private static void ConfigureTools(McpServerOptions options, string? cliArg) Content = [new TextContentBlock { Text = $"Echo: {message}" }] }; } - else if (request.Params?.Name == "echoSessionId") + else if (request.Params.Name == "echoSessionId") { return new CallToolResult { Content = [new TextContentBlock { Text = request.Server.SessionId ?? string.Empty }] }; } - else if (request.Params?.Name == "trigger-sampling-request") + else if (request.Params.Name == "trigger-sampling-request") { - if (request.Params?.Arguments is null || + if (request.Params.Arguments is null || !request.Params.Arguments.TryGetValue("prompt", out var prompt) || !request.Params.Arguments.TryGetValue("maxTokens", out var maxTokens)) { @@ -238,16 +238,16 @@ private static void ConfigureTools(McpServerOptions options, string? cliArg) Content = [new TextContentBlock { Text = $"LLM sampling result: {sampleResult.Content.OfType().FirstOrDefault()?.Text}" }] }; } - else if (request.Params?.Name == "echoCliArg") + else if (request.Params.Name == "echoCliArg") { return new CallToolResult { Content = [new TextContentBlock { Text = cliArg ?? "null" }] }; } - else if (request.Params?.Name == "longRunning") + else if (request.Params.Name == "longRunning") { - if (request.Params?.Arguments is null || !request.Params.Arguments.TryGetValue("durationMs", out var durationMsValue)) + if (request.Params.Arguments is null || !request.Params.Arguments.TryGetValue("durationMs", out var durationMsValue)) { throw new McpProtocolException("Missing required argument 'durationMs'", McpErrorCode.InvalidParams); } @@ -258,9 +258,9 @@ private static void ConfigureTools(McpServerOptions options, string? cliArg) Content = [new TextContentBlock { Text = $"Long-running operation completed after {durationMs}ms" }] }; } - else if (request.Params?.Name == "crash") + else if (request.Params.Name == "crash") { - if (request.Params?.Arguments is null || !request.Params.Arguments.TryGetValue("exitCode", out var exitCodeValue)) + if (request.Params.Arguments is null || !request.Params.Arguments.TryGetValue("exitCode", out var exitCodeValue)) { throw new McpProtocolException("Missing required argument 'exitCode'", McpErrorCode.InvalidParams); } @@ -271,7 +271,7 @@ private static void ConfigureTools(McpServerOptions options, string? cliArg) } else { - throw new McpProtocolException($"Unknown tool: {request.Params?.Name}", McpErrorCode.InvalidParams); + throw new McpProtocolException($"Unknown tool: {request.Params.Name}", McpErrorCode.InvalidParams); } }; } @@ -335,7 +335,7 @@ private static void ConfigurePrompts(McpServerOptions options) options.Handlers.GetPromptHandler = async (request, cancellationToken) => { List messages = []; - if (request.Params?.Name == "simple-prompt") + if (request.Params.Name == "simple-prompt") { messages.Add(new PromptMessage { @@ -343,7 +343,7 @@ private static void ConfigurePrompts(McpServerOptions options) Content = new TextContentBlock { Text = "This is a simple prompt without arguments." }, }); } - else if (request.Params?.Name == "args-prompt") + else if (request.Params.Name == "args-prompt") { string city = request.Params.Arguments?["city"].ToString() ?? "unknown"; string state = request.Params.Arguments?["state"].ToString() ?? ""; @@ -354,7 +354,7 @@ private static void ConfigurePrompts(McpServerOptions options) Content = new TextContentBlock { Text = $"What's weather in {location}?" }, }); } - else if (request.Params?.Name == "completable-prompt") + else if (request.Params.Name == "completable-prompt") { string department = request.Params.Arguments?["department"].ToString() ?? "unknown"; string name = request.Params.Arguments?["name"].ToString() ?? "unknown"; @@ -366,7 +366,7 @@ private static void ConfigurePrompts(McpServerOptions options) } else { - throw new McpProtocolException($"Unknown prompt: {request.Params?.Name}", McpErrorCode.InvalidParams); + throw new McpProtocolException($"Unknown prompt: {request.Params.Name}", McpErrorCode.InvalidParams); } return new GetPromptResult @@ -382,11 +382,6 @@ private static void ConfigureLogging(McpServerOptions options) { options.Handlers.SetLoggingLevelHandler = async (request, cancellationToken) => { - if (request.Params?.Level is null) - { - throw new McpProtocolException("Missing required argument 'level'", McpErrorCode.InvalidParams); - } - _minimumLoggingLevel = request.Params.Level; return new EmptyResult(); @@ -452,7 +447,7 @@ private static void ConfigureResources(McpServerOptions options) options.Handlers.ListResourcesHandler = async (request, cancellationToken) => { int startIndex = 0; - if (request.Params?.Cursor is not null) + if (request.Params.Cursor is not null) { try { @@ -481,7 +476,7 @@ private static void ConfigureResources(McpServerOptions options) options.Handlers.ReadResourceHandler = async (request, cancellationToken) => { - if (request.Params?.Uri is null) + if (request.Params.Uri is null) { throw new McpProtocolException("Missing required argument 'uri'", McpErrorCode.InvalidParams); } @@ -518,7 +513,7 @@ private static void ConfigureResources(McpServerOptions options) options.Handlers.SubscribeToResourcesHandler = async (request, cancellationToken) => { - if (request?.Params?.Uri is null) + if (request?.Params.Uri is null) { throw new McpProtocolException("Missing required argument 'uri'", McpErrorCode.InvalidParams); } @@ -535,7 +530,7 @@ private static void ConfigureResources(McpServerOptions options) options.Handlers.UnsubscribeFromResourcesHandler = async (request, cancellationToken) => { - if (request?.Params?.Uri is null) + if (request?.Params.Uri is null) { throw new McpProtocolException("Missing required argument 'uri'", McpErrorCode.InvalidParams); } @@ -563,7 +558,7 @@ private static void ConfigureCompletions(McpServerOptions options) options.Handlers.CompleteHandler = async (request, cancellationToken) => { string[]? values; - switch (request.Params?.Ref) + switch (request.Params.Ref) { case ResourceTemplateReference rtr: var resourceId = rtr.Uri?.Split('/').LastOrDefault(); @@ -571,7 +566,7 @@ private static void ConfigureCompletions(McpServerOptions options) return new CompleteResult { Completion = new() { Values = [] } }; // Filter resource IDs that start with the input value - values = sampleResourceIds.Where(id => id.StartsWith(request.Params!.Argument.Value)).ToArray(); + values = sampleResourceIds.Where(id => id.StartsWith(request.Params.Argument.Value)).ToArray(); return new CompleteResult { Completion = new() { Values = values, HasMore = false, Total = values.Length } }; case PromptReference pr: @@ -583,7 +578,7 @@ private static void ConfigureCompletions(McpServerOptions options) return new CompleteResult { Completion = new() { Values = values, HasMore = false, Total = values.Length } }; default: - throw new McpProtocolException($"Unknown reference type: '{request.Params?.Ref.Type}'", McpErrorCode.InvalidParams); + throw new McpProtocolException($"Unknown reference type: '{request.Params.Ref.Type}'", McpErrorCode.InvalidParams); } }; } diff --git a/tests/ModelContextProtocol.TestSseServer/Program.cs b/tests/ModelContextProtocol.TestSseServer/Program.cs index 1a27c0c15..3c42e290a 100644 --- a/tests/ModelContextProtocol.TestSseServer/Program.cs +++ b/tests/ModelContextProtocol.TestSseServer/Program.cs @@ -280,7 +280,7 @@ static CreateMessageRequestParams CreateRequestSamplingParams(string context, st ReadResourceHandler = async (request, cancellationToken) => { - if (request.Params?.Uri is null) + if (request.Params.Uri is null) { throw new McpProtocolException("Missing required argument 'uri'", McpErrorCode.InvalidParams); } diff --git a/tests/ModelContextProtocol.Tests/Client/McpClientMetaTests.cs b/tests/ModelContextProtocol.Tests/Client/McpClientMetaTests.cs index ddb30b720..863d8e671 100644 --- a/tests/ModelContextProtocol.Tests/Client/McpClientMetaTests.cs +++ b/tests/ModelContextProtocol.Tests/Client/McpClientMetaTests.cs @@ -37,7 +37,7 @@ public async Task ToolCallWithMetaFields() async (RequestContext context) => { // Access the foo property of _meta field from the request parameters - var metaFoo = context.Params?.Meta?["foo"]?.ToString(); + var metaFoo = context.Params.Meta?["foo"]?.ToString(); // Assert that the meta foo is correctly passed Assert.NotNull(metaFoo); @@ -73,7 +73,7 @@ public async Task ResourceReadWithMetaFields() (RequestContext context) => { // Access the foo property of _meta field from the request parameters - var metaFoo = context.Params?.Meta?["foo"]?.ToString(); + var metaFoo = context.Params.Meta?["foo"]?.ToString(); // Assert that the meta foo is correctly passed Assert.NotNull(metaFoo); @@ -109,7 +109,7 @@ public async Task PromptGetWithMetaFields() (RequestContext context) => { // Access the foo property of _meta field from the request parameters - var metaFoo = context.Params?.Meta?["foo"]?.ToString(); + var metaFoo = context.Params.Meta?["foo"]?.ToString(); // Assert that the meta foo is correctly passed Assert.NotNull(metaFoo); diff --git a/tests/ModelContextProtocol.Tests/Client/McpClientPromptTests.cs b/tests/ModelContextProtocol.Tests/Client/McpClientPromptTests.cs index ec9f5f561..e84b98733 100644 --- a/tests/ModelContextProtocol.Tests/Client/McpClientPromptTests.cs +++ b/tests/ModelContextProtocol.Tests/Client/McpClientPromptTests.cs @@ -29,7 +29,7 @@ public static ChatMessage Greeting([Description("The name to greet")] string nam [McpServerPrompt, Description("Echoes back the metadata it receives")] public static ChatMessage MetadataEcho(RequestContext context) => - new(ChatRole.User, context.Params?.Meta?.ToJsonString() ?? "{}"); + new(ChatRole.User, context.Params.Meta?.ToJsonString() ?? "{}"); } [Fact] diff --git a/tests/ModelContextProtocol.Tests/Client/McpClientResourceTemplateConstructorTests.cs b/tests/ModelContextProtocol.Tests/Client/McpClientResourceTemplateConstructorTests.cs index efaa647aa..6a415dd5e 100644 --- a/tests/ModelContextProtocol.Tests/Client/McpClientResourceTemplateConstructorTests.cs +++ b/tests/ModelContextProtocol.Tests/Client/McpClientResourceTemplateConstructorTests.cs @@ -27,7 +27,7 @@ private sealed class FileTemplateResources [McpServerResource, Description("Echoes back the metadata it receives")] public static string MetadataEcho(RequestContext context, [Description("An ID")] string id) => - context.Params?.Meta?.ToJsonString() ?? "{}"; + context.Params.Meta?.ToJsonString() ?? "{}"; } [Fact] diff --git a/tests/ModelContextProtocol.Tests/Client/McpClientResourceTemplateTests.cs b/tests/ModelContextProtocol.Tests/Client/McpClientResourceTemplateTests.cs index 693dab282..9bafd8ea4 100644 --- a/tests/ModelContextProtocol.Tests/Client/McpClientResourceTemplateTests.cs +++ b/tests/ModelContextProtocol.Tests/Client/McpClientResourceTemplateTests.cs @@ -17,7 +17,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer mcpServerBuilder.WithReadResourceHandler((request, cancellationToken) => new ValueTask(new ReadResourceResult { - Contents = [new TextResourceContents { Text = request.Params?.Uri ?? "", Uri = request.Params?.Uri ?? "" }] + Contents = [new TextResourceContents { Text = request.Params.Uri ?? "", Uri = request.Params.Uri ?? "" }] })); } diff --git a/tests/ModelContextProtocol.Tests/Client/McpClientResourceTests.cs b/tests/ModelContextProtocol.Tests/Client/McpClientResourceTests.cs index 52fe54b63..26e5f1d27 100644 --- a/tests/ModelContextProtocol.Tests/Client/McpClientResourceTests.cs +++ b/tests/ModelContextProtocol.Tests/Client/McpClientResourceTests.cs @@ -27,7 +27,7 @@ private sealed class SampleResources [McpServerResource, Description("Echoes back the metadata it receives")] public static string MetadataEcho(RequestContext context) => - context.Params?.Meta?.ToJsonString() ?? "{}"; + context.Params.Meta?.ToJsonString() ?? "{}"; } [Fact] diff --git a/tests/ModelContextProtocol.Tests/Client/McpClientToolTests.cs b/tests/ModelContextProtocol.Tests/Client/McpClientToolTests.cs index e17fb6bc9..4ec64dae1 100644 --- a/tests/ModelContextProtocol.Tests/Client/McpClientToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Client/McpClientToolTests.cs @@ -161,7 +161,7 @@ public static EmbeddedResourceBlock BinaryResourceTool() => [McpServerTool] public static TextContentBlock MetadataEchoTool(RequestContext context) { - var meta = context.Params?.Meta; + var meta = context.Params.Meta; var metaJson = meta?.ToJsonString() ?? "{}"; return new TextContentBlock { Text = metaJson }; } diff --git a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsMessageFilterTests.cs b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsMessageFilterTests.cs index ec396c5be..6883944b6 100644 --- a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsMessageFilterTests.cs +++ b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsMessageFilterTests.cs @@ -687,7 +687,7 @@ public static async Task ReportProgress( RequestContext context, CancellationToken cancellationToken) { - if (context.Params?.ProgressToken is { } token) + if (context.Params.ProgressToken is { } token) { await server.NotifyProgressAsync(token, new ProgressNotificationValue { diff --git a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsPromptsTests.cs b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsPromptsTests.cs index 69405e16c..209469e7b 100644 --- a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsPromptsTests.cs +++ b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsPromptsTests.cs @@ -26,7 +26,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer mcpServerBuilder .WithListPromptsHandler(async (request, cancellationToken) => { - var cursor = request.Params?.Cursor; + var cursor = request.Params.Cursor; switch (cursor) { case null: @@ -68,7 +68,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer }) .WithGetPromptHandler(async (request, cancellationToken) => { - switch (request.Params?.Name) + switch (request.Params.Name) { case "FirstCustomPrompt": case "SecondCustomPrompt": @@ -79,7 +79,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer }; default: - throw new McpProtocolException($"Unknown prompt '{request.Params?.Name}'", McpErrorCode.InvalidParams); + throw new McpProtocolException($"Unknown prompt '{request.Params.Name}'", McpErrorCode.InvalidParams); } }) .WithPrompts(); @@ -314,17 +314,14 @@ public async Task WithPrompts_TargetInstance_UsesTarget() sc.AddMcpServer().WithPrompts(target); McpServerPrompt prompt = sc.BuildServiceProvider().GetServices().First(t => t.ProtocolPrompt.Name == "returns_string"); - var result = await prompt.GetAsync(new RequestContext(new Mock().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }) + var result = await prompt.GetAsync(new RequestContext(new Mock().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }, new GetPromptRequestParams { - Params = new GetPromptRequestParams + Name = "returns_string", + Arguments = new Dictionary { - Name = "returns_string", - Arguments = new Dictionary - { - ["message"] = JsonSerializer.SerializeToElement("hello", AIJsonUtilities.DefaultOptions), - } + ["message"] = JsonSerializer.SerializeToElement("hello", AIJsonUtilities.DefaultOptions), } - }, TestContext.Current.CancellationToken); + }), TestContext.Current.CancellationToken); Assert.Equal(target.ReturnsString("hello"), (result.Messages[0].Content as TextContentBlock)?.Text); } diff --git a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsResourcesTests.cs b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsResourcesTests.cs index 545384a7c..4b03cadb2 100644 --- a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsResourcesTests.cs +++ b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsResourcesTests.cs @@ -25,7 +25,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer mcpServerBuilder .WithListResourcesHandler(async (request, cancellationToken) => { - var cursor = request.Params?.Cursor; + var cursor = request.Params.Cursor; switch (cursor) { case null: @@ -67,7 +67,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer }) .WithListResourceTemplatesHandler(async (request, cancellationToken) => { - var cursor = request.Params?.Cursor; + var cursor = request.Params.Cursor; switch (cursor) { case null: @@ -96,7 +96,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer }) .WithReadResourceHandler(async (request, cancellationToken) => { - switch (request.Params?.Uri) + switch (request.Params.Uri) { case "test://Resource1": case "test://Resource2": @@ -105,11 +105,11 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer case "test://ResourceTemplate2": return new ReadResourceResult { - Contents = [new TextResourceContents { Text = request.Params?.Uri ?? "(null)", Uri = request.Params?.Uri ?? "(null)" }] + Contents = [new TextResourceContents { Text = request.Params.Uri ?? "(null)", Uri = request.Params.Uri ?? "(null)" }] }; } - throw new McpProtocolException($"Resource not found: {request.Params?.Uri}", McpErrorCode.ResourceNotFound); + throw new McpProtocolException($"Resource not found: {request.Params.Uri}", McpErrorCode.ResourceNotFound); }) .WithResources(); } @@ -345,13 +345,10 @@ public async Task WithResources_TargetInstance_UsesTarget() sc.AddMcpServer().WithResources(target); McpServerResource resource = sc.BuildServiceProvider().GetServices().First(t => t.ProtocolResource?.Name == "returns_string"); - var result = await resource.ReadAsync(new RequestContext(new Mock().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }) + var result = await resource.ReadAsync(new RequestContext(new Mock().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }, new() { - Params = new() - { - Uri = "returns://string" - } - }, TestContext.Current.CancellationToken); + Uri = "returns://string" + }), TestContext.Current.CancellationToken); Assert.Equal(target.ReturnsString(), (result?.Contents[0] as TextResourceContents)?.Text); } diff --git a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs index 518b70f00..d2db4c62c 100644 --- a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs +++ b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs @@ -30,7 +30,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer mcpServerBuilder .WithListToolsHandler(async (request, cancellationToken) => { - var cursor = request.Params?.Cursor; + var cursor = request.Params.Cursor; switch (cursor) { case null: @@ -93,7 +93,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer }) .WithCallToolHandler(async (request, cancellationToken) => { - switch (request.Params?.Name) + switch (request.Params.Name) { case "FirstCustomTool": case "SecondCustomTool": @@ -104,7 +104,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer }; default: - throw new McpProtocolException($"Unknown tool: '{request.Params?.Name}'", McpErrorCode.InvalidParams); + throw new McpProtocolException($"Unknown tool: '{request.Params.Name}'", McpErrorCode.InvalidParams); } }) .WithTools(serializerOptions: BuilderToolsJsonContext.Default.Options); @@ -594,7 +594,7 @@ public async Task WithTools_TargetInstance_UsesTarget() sc.AddMcpServer().WithTools(target, BuilderToolsJsonContext.Default.Options); McpServerTool tool = sc.BuildServiceProvider().GetServices().First(t => t.ProtocolTool.Name == "get_ctor_parameter"); - var result = await tool.InvokeAsync(new RequestContext(new Mock().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }), TestContext.Current.CancellationToken); + var result = await tool.InvokeAsync(new RequestContext(new Mock().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }, new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal(target.GetCtorParameter(), (result.Content[0] as TextContentBlock)?.Text); } diff --git a/tests/ModelContextProtocol.Tests/McpProtocolExceptionDataTests.cs b/tests/ModelContextProtocol.Tests/McpProtocolExceptionDataTests.cs index 6816f5534..7d50a3044 100644 --- a/tests/ModelContextProtocol.Tests/McpProtocolExceptionDataTests.cs +++ b/tests/ModelContextProtocol.Tests/McpProtocolExceptionDataTests.cs @@ -28,7 +28,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer { mcpServerBuilder.WithCallToolHandler((request, cancellationToken) => { - var toolName = request.Params?.Name; + var toolName = request.Params.Name; switch (toolName) { diff --git a/tests/ModelContextProtocol.Tests/Protocol/ElicitationTypedTests.cs b/tests/ModelContextProtocol.Tests/Protocol/ElicitationTypedTests.cs index 6f0988e48..7b55b738a 100644 --- a/tests/ModelContextProtocol.Tests/Protocol/ElicitationTypedTests.cs +++ b/tests/ModelContextProtocol.Tests/Protocol/ElicitationTypedTests.cs @@ -19,7 +19,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer { Assert.NotNull(request.Params); - if (request.Params!.Name == "TestElicitationTyped") + if (request.Params.Name == "TestElicitationTyped") { var result = await request.Server.ElicitAsync( message: "Please provide more information.", @@ -34,7 +34,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer Assert.Equal(SampleRole.Admin, result.Content!.Role); Assert.Equal(99.5, result.Content!.Score); } - else if (request.Params!.Name == "TestElicitationCamelForm") + else if (request.Params.Name == "TestElicitationCamelForm") { var result = await request.Server.ElicitAsync( message: "Please provide more information.", @@ -47,7 +47,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer Assert.Equal(90210, result.Content!.ZipCode); Assert.False(result.Content!.IsAdmin); } - else if (request.Params!.Name == "TestElicitationNullablePropertyForm") + else if (request.Params.Name == "TestElicitationNullablePropertyForm") { var result = await request.Server.ElicitAsync( message: "Please provide more information.", @@ -60,7 +60,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer Content = [new TextContentBlock { Text = "unexpected" }], }; } - else if (request.Params!.Name == "TestElicitationUnsupportedType") + else if (request.Params.Name == "TestElicitationUnsupportedType") { await request.Server.ElicitAsync( message: "Please provide more information.", @@ -73,7 +73,7 @@ await request.Server.ElicitAsync( Content = [new TextContentBlock { Text = "unexpected" }], }; } - else if (request.Params!.Name == "TestElicitationNonObjectGenericType") + else if (request.Params.Name == "TestElicitationNonObjectGenericType") { // This should throw because T is not an object type with properties (string primitive) await request.Server.ElicitAsync( @@ -86,7 +86,7 @@ await request.Server.ElicitAsync( Content = [new TextContentBlock { Text = "unexpected" }], }; } - else if (request.Params!.Name == "TestElicitationWithDefaults") + else if (request.Params.Name == "TestElicitationWithDefaults") { var result = await request.Server.ElicitAsync( message: "Please provide information.", @@ -101,7 +101,7 @@ await request.Server.ElicitAsync( } else { - Assert.Fail($"Unexpected tool name: {request.Params!.Name}"); + Assert.Fail($"Unexpected tool name: {request.Params.Name}"); } return new CallToolResult diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs index b463514f9..dec16d6b2 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs @@ -54,7 +54,7 @@ public async Task SupportsMcpServer() Assert.DoesNotContain("server", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []); var result = await prompt.GetAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.NotNull(result.Messages); @@ -83,7 +83,7 @@ public async Task SupportsCtorInjection() }, new() { Services = services }); var result = await prompt.GetAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.NotNull(result.Messages); @@ -133,11 +133,11 @@ public async Task SupportsServiceFromDI() Assert.DoesNotContain("actualMyService", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []); await Assert.ThrowsAnyAsync(async () => await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken)); var result = await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Services = services }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }) { Services = services }, TestContext.Current.CancellationToken); Assert.Equal("Hello", Assert.IsType(result.Messages[0].Content).Text); } @@ -158,7 +158,7 @@ public async Task SupportsOptionalServiceFromDI() }, new() { Services = services }); var result = await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal("Hello", Assert.IsType(result.Messages[0].Content).Text); } @@ -171,7 +171,7 @@ public async Task SupportsDisposingInstantiatedDisposableTargets() _ => new DisposablePromptType()); var result = await prompt1.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal("disposals:1", Assert.IsType(result.Messages[0].Content).Text); } @@ -184,7 +184,7 @@ public async Task SupportsAsyncDisposingInstantiatedAsyncDisposableTargets() _ => new AsyncDisposablePromptType()); var result = await prompt1.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal("asyncDisposals:1", Assert.IsType(result.Messages[0].Content).Text); } @@ -197,7 +197,7 @@ public async Task SupportsAsyncDisposingInstantiatedAsyncDisposableAndDisposable _ => new AsyncDisposableAndDisposablePromptType()); var result = await prompt1.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal("disposals:0, asyncDisposals:1", Assert.IsType(result.Messages[0].Content).Text); } @@ -213,7 +213,7 @@ public async Task CanReturnGetPromptResult() }); var actual = await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Same(expected, actual); @@ -230,7 +230,7 @@ public async Task CanReturnText() }); var actual = await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.NotNull(actual); @@ -256,7 +256,7 @@ public async Task CanReturnPromptMessage() }); var actual = await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.NotNull(actual); @@ -288,7 +288,7 @@ public async Task CanReturnPromptMessages() }); var actual = await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.NotNull(actual); @@ -315,7 +315,7 @@ public async Task CanReturnChatMessage() }); var actual = await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.NotNull(actual); @@ -347,7 +347,7 @@ public async Task CanReturnChatMessages() }); var actual = await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.NotNull(actual); @@ -368,7 +368,7 @@ public async Task ThrowsForNullReturn() }); await Assert.ThrowsAsync(async () => await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken)); } @@ -381,7 +381,7 @@ public async Task ThrowsForUnexpectedTypeReturn() }); await Assert.ThrowsAsync(async () => await prompt.GetAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken)); } diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerResourceTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerResourceTests.cs index 695bbf39d..dfcc0bd4f 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerResourceTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerResourceTests.cs @@ -51,7 +51,7 @@ public void CanCreateServerWithResource() { Contents = [new TextResourceContents { - Uri = ctx.Params!.Uri!, + Uri = ctx.Params.Uri!, Text = "Static Resource", MimeType = "text/plain", }] @@ -87,7 +87,7 @@ public void CanCreateServerWithResourceTemplates() { Contents = [new TextResourceContents { - Uri = ctx.Params!.Uri!, + Uri = ctx.Params.Uri!, Text = "Static Resource", MimeType = "text/plain", }] @@ -111,7 +111,7 @@ public void CreatingReadHandlerWithNoListHandlerSucceeds() { Contents = [new TextResourceContents { - Uri = ctx.Params!.Uri!, + Uri = ctx.Params.Uri!, Text = "Static Resource", MimeType = "text/plain", }] @@ -148,7 +148,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create(() => "42", new() { Name = Name }); Assert.Equal("resource://mcp/Hello", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Hello" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("42", ((TextResourceContents)result.Contents[0]).Text); @@ -156,7 +156,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((McpServer server) => "42", new() { Name = Name }); Assert.Equal("resource://mcp/Hello", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Hello" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("42", ((TextResourceContents)result.Contents[0]).Text); @@ -164,7 +164,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((string arg1) => arg1, new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?arg1}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?arg1=wOrLd" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?arg1=wOrLd" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("wOrLd", ((TextResourceContents)result.Contents[0]).Text); @@ -172,7 +172,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((string arg1, string? arg2 = null) => arg1 + arg2, new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?arg1,arg2}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?arg1=wo&arg2=rld" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?arg1=wo&arg2=rld" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("world", ((TextResourceContents)result.Contents[0]).Text); @@ -180,7 +180,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((object a1, bool a2, char a3, byte a4, sbyte a5) => a1.ToString() + a2 + a3 + a4 + a5, new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a1,a2,a3,a4,a5}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=hi&a2=true&a3=s&a4=12&a5=34" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a1=hi&a2=true&a3=s&a4=12&a5=34" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("hiTrues1234", ((TextResourceContents)result.Contents[0]).Text); @@ -188,7 +188,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((ushort a1, short a2, uint a3, int a4, ulong a5) => (a1 + a2 + a3 + a4 + (long)a5).ToString(), new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a1,a2,a3,a4,a5}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=10&a2=20&a3=30&a4=40&a5=50" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a1=10&a2=20&a3=30&a4=40&a5=50" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("150", ((TextResourceContents)result.Contents[0]).Text); @@ -196,7 +196,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((long a1, float a2, double a3, decimal a4, TimeSpan a5) => a5.ToString(), new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a1,a2,a3,a4,a5}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=1&a2=2&a3=3&a4=4&a5=5" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a1=1&a2=2&a3=3&a4=4&a5=5" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("5.00:00:00", ((TextResourceContents)result.Contents[0]).Text); @@ -204,7 +204,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((DateTime a1, DateTimeOffset a2, Uri a3, Guid a4, Version a5) => a4.ToString("N") + a5, new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a1,a2,a3,a4,a5}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1={DateTime.UtcNow:r}&a2={DateTimeOffset.UtcNow:r}&a3=http%3A%2F%2Ftest&a4=14e5f43d-0d41-47d6-8207-8249cf669e41&a5=1.2.3.4" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a1={DateTime.UtcNow:r}&a2={DateTimeOffset.UtcNow:r}&a3=http%3A%2F%2Ftest&a4=14e5f43d-0d41-47d6-8207-8249cf669e41&a5=1.2.3.4" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("14e5f43d0d4147d682078249cf669e411.2.3.4", ((TextResourceContents)result.Contents[0]).Text); @@ -213,7 +213,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((Half a2, Int128 a3, UInt128 a4, IntPtr a5) => (a3 + (Int128)a4 + a5).ToString(), new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a2,a3,a4,a5}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a2=1.0&a3=3&a4=4&a5=5" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a2=1.0&a3=3&a4=4&a5=5" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("12", ((TextResourceContents)result.Contents[0]).Text); @@ -221,7 +221,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((UIntPtr a1, DateOnly a2, TimeOnly a3) => a1.ToString(), new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a1,a2,a3}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=123&a2=0001-02-03&a3=01%3A02%3A03" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a1=123&a2=0001-02-03&a3=01%3A02%3A03" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("123", ((TextResourceContents)result.Contents[0]).Text); @@ -230,7 +230,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((bool? a2, char? a3, byte? a4, sbyte? a5) => a2?.ToString() + a3 + a4 + a5, new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a2,a3,a4,a5}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a2=true&a3=s&a4=12&a5=34" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a2=true&a3=s&a4=12&a5=34" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("Trues1234", ((TextResourceContents)result.Contents[0]).Text); @@ -238,7 +238,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((ushort? a1, short? a2, uint? a3, int? a4, ulong? a5) => (a1 + a2 + a3 + a4 + (long?)a5).ToString(), new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a1,a2,a3,a4,a5}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=10&a2=20&a3=30&a4=40&a5=50" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a1=10&a2=20&a3=30&a4=40&a5=50" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("150", ((TextResourceContents)result.Contents[0]).Text); @@ -246,7 +246,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((long? a1, float? a2, double? a3, decimal? a4, TimeSpan? a5) => a5?.ToString(), new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a1,a2,a3,a4,a5}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=1&a2=2&a3=3&a4=4&a5=5" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a1=1&a2=2&a3=3&a4=4&a5=5" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("5.00:00:00", ((TextResourceContents)result.Contents[0]).Text); @@ -254,7 +254,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((DateTime? a1, DateTimeOffset? a2, Guid? a4) => a4?.ToString("N"), new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a1,a2,a4}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1={DateTime.UtcNow:r}&a2={DateTimeOffset.UtcNow:r}&a4=14e5f43d-0d41-47d6-8207-8249cf669e41" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a1={DateTime.UtcNow:r}&a2={DateTimeOffset.UtcNow:r}&a4=14e5f43d-0d41-47d6-8207-8249cf669e41" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("14e5f43d0d4147d682078249cf669e41", ((TextResourceContents)result.Contents[0]).Text); @@ -263,7 +263,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((Half? a2, Int128? a3, UInt128? a4, IntPtr? a5) => (a3 + (Int128?)a4 + a5).ToString(), new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a2,a3,a4,a5}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a2=1.0&a3=3&a4=4&a5=5" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a2=1.0&a3=3&a4=4&a5=5" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("12", ((TextResourceContents)result.Contents[0]).Text); @@ -271,7 +271,7 @@ public async Task UriTemplate_CreatedFromParameters_LotsOfTypesSupported() t = McpServerResource.Create((UIntPtr? a1, DateOnly? a2, TimeOnly? a3) => a1?.ToString(), new() { Name = Name }); Assert.Equal($"resource://mcp/Hello{{?a1,a2,a3}}", t.ProtocolResourceTemplate.UriTemplate); result = await t.ReadAsync( - new RequestContext(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=123&a2=0001-02-03&a3=01%3A02%3A03" } }, + new RequestContext(server, CreateTestJsonRpcRequest(), new() { Uri = $"resource://mcp/Hello?a1=123&a2=0001-02-03&a3=01%3A02%3A03" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("123", ((TextResourceContents)result.Contents[0]).Text); @@ -288,7 +288,7 @@ public async Task UriTemplate_NonMatchingUri_DoesNotMatch(string uri) Assert.Equal("resource://mcp/Hello{?arg1}", t.ProtocolResourceTemplate.UriTemplate); Assert.False(t.IsMatch(uri)); await Assert.ThrowsAsync(async () => await t.ReadAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = uri } }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Uri = uri }), TestContext.Current.CancellationToken)); } @@ -299,7 +299,7 @@ public async Task UriTemplate_IsHostCaseInsensitive(string actualUri, string que { McpServerResource t = McpServerResource.Create(() => "resource", new() { UriTemplate = actualUri }); Assert.NotNull(await t.ReadAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = queriedUri } }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Uri = queriedUri }), TestContext.Current.CancellationToken)); } @@ -328,7 +328,7 @@ public async Task UriTemplate_MissingParameter_Throws(string uri) McpServerResource t = McpServerResource.Create((string arg1, int arg2) => arg1, new() { Name = "Hello" }); Assert.Equal("resource://mcp/Hello{?arg1,arg2}", t.ProtocolResourceTemplate.UriTemplate); await Assert.ThrowsAsync(async () => await t.ReadAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = uri } }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Uri = uri }), TestContext.Current.CancellationToken)); } @@ -341,25 +341,25 @@ public async Task UriTemplate_MissingOptionalParameter_Succeeds() ReadResourceResult result; result = await t.ReadAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello" } }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Hello" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("", ((TextResourceContents)result.Contents[0]).Text); result = await t.ReadAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello?arg1=first" } }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Hello?arg1=first" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("first", ((TextResourceContents)result.Contents[0]).Text); result = await t.ReadAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello?arg2=42" } }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Hello?arg2=42" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("42", ((TextResourceContents)result.Contents[0]).Text); result = await t.ReadAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello?arg1=first&arg2=42" } }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Hello?arg1=first&arg2=42" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("first42", ((TextResourceContents)result.Contents[0]).Text); @@ -377,7 +377,7 @@ public async Task SupportsMcpServer() }, new() { Name = "Test" }); var result = await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("42", ((TextResourceContents)result.Contents[0]).Text); @@ -404,7 +404,7 @@ public async Task SupportsCtorInjection() }, new() { Services = services }); var result = await tool.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "https://something" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "https://something" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.NotNull(result.Contents); @@ -481,11 +481,11 @@ public async Task SupportsServiceFromDI(ServiceLifetime injectedArgumentLifetime Mock mockServer = new(); await Assert.ThrowsAnyAsync(async () => await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken)); var result = await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Services = services, Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }) { Services = services }, TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("42", ((TextResourceContents)result.Contents[0]).Text); @@ -507,7 +507,7 @@ public async Task SupportsOptionalServiceFromDI() }, new() { Services = services, Name = "Test" }); var result = await resource.ReadAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("42", ((TextResourceContents)result.Contents[0]).Text); @@ -523,7 +523,7 @@ public async Task SupportsDisposingInstantiatedDisposableTargets() _ => new DisposableResourceType()); var result = await resource1.ReadAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "test://static/resource/instanceMethod" } }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Uri = "test://static/resource/instanceMethod" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal("0", ((TextResourceContents)result.Contents[0]).Text); @@ -541,7 +541,7 @@ public async Task CanReturnReadResult() return new ReadResourceResult { Contents = [new TextResourceContents { Text = "hello", Uri = "" }] }; }, new() { Name = "Test" }); var result = await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Single(result.Contents); @@ -558,7 +558,7 @@ public async Task CanReturnResourceContents() return new TextResourceContents { Text = "hello", Uri = "" }; }, new() { Name = "Test", SerializerOptions = JsonContext6.Default.Options }); var result = await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Single(result.Contents); @@ -579,7 +579,7 @@ public async Task CanReturnCollectionOfResourceContents() ]; }, new() { Name = "Test" }); var result = await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal(2, result.Contents.Count); @@ -597,7 +597,7 @@ public async Task CanReturnString() return "42"; }, new() { Name = "Test" }); var result = await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Single(result.Contents); @@ -614,7 +614,7 @@ public async Task CanReturnCollectionOfStrings() return new List { "42", "43" }; }, new() { Name = "Test", SerializerOptions = JsonContext6.Default.Options }); var result = await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal(2, result.Contents.Count); @@ -632,7 +632,7 @@ public async Task CanReturnDataContent() return new DataContent(new byte[] { 0, 1, 2 }, "application/octet-stream"); }, new() { Name = "Test" }); var result = await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Single(result.Contents); @@ -654,7 +654,7 @@ public async Task CanReturnCollectionOfAIContent() }; }, new() { Name = "Test", SerializerOptions = JsonContext6.Default.Options }); var result = await resource.ReadAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.Equal(2, result.Contents.Count); diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index aa20a851c..808ba7efe 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -62,7 +62,7 @@ public async Task SupportsMcpServer() Assert.DoesNotContain("server", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema, McpJsonUtilities.DefaultOptions)); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal("42", (result.Content[0] as TextContentBlock)?.Text); } @@ -88,7 +88,7 @@ public async Task SupportsCtorInjection() }, new() { Services = services }); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.NotNull(result); Assert.NotNull(result.Content); @@ -166,13 +166,13 @@ public async Task SupportsServiceFromDI(ServiceLifetime injectedArgumentLifetime Mock mockServer = new(); var ex = await Assert.ThrowsAsync(async () => await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken)); mockServer.SetupGet(s => s.Services).Returns(services); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) { Services = services }, + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }) { Services = services }, TestContext.Current.CancellationToken); Assert.Equal("42", (result.Content[0] as TextContentBlock)?.Text); } @@ -193,7 +193,7 @@ public async Task SupportsOptionalServiceFromDI() }, new() { Services = services }); var result = await tool.InvokeAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal("42", (result.Content[0] as TextContentBlock)?.Text); } @@ -208,7 +208,7 @@ public async Task SupportsDisposingInstantiatedDisposableTargets() options); var result = await tool1.InvokeAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal("""{"disposals":1}""", (result.Content[0] as TextContentBlock)?.Text); } @@ -223,7 +223,7 @@ public async Task SupportsAsyncDisposingInstantiatedAsyncDisposableTargets() options); var result = await tool1.InvokeAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()), + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal("""{"asyncDisposals":1}""", (result.Content[0] as TextContentBlock)?.Text); } @@ -242,7 +242,7 @@ public async Task SupportsAsyncDisposingInstantiatedAsyncDisposableAndDisposable options); var result = await tool1.InvokeAsync( - new RequestContext(new Mock().Object, CreateTestJsonRpcRequest()) { Services = services }, + new RequestContext(new Mock().Object, CreateTestJsonRpcRequest(), new() { Name = "" }) { Services = services }, TestContext.Current.CancellationToken); Assert.Equal("""{"asyncDisposals":1,"disposals":0}""", (result.Content[0] as TextContentBlock)?.Text); } @@ -263,7 +263,7 @@ public async Task CanReturnCollectionOfAIContent() }, new() { SerializerOptions = JsonContext2.Default.Options }); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal(3, result.Content.Count); @@ -297,7 +297,7 @@ public async Task CanReturnSingleAIContent(string data, string type) }); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Single(result.Content); @@ -333,7 +333,7 @@ public async Task CanReturnNullAIContent() return (string?)null; }); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Empty(result.Content); } @@ -348,7 +348,7 @@ public async Task CanReturnString() return "42"; }); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Single(result.Content); Assert.Equal("42", Assert.IsType(result.Content[0]).Text); @@ -364,7 +364,7 @@ public async Task CanReturnCollectionOfStrings() return new List { "42", "43" }; }, new() { SerializerOptions = JsonContext2.Default.Options }); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Single(result.Content); Assert.Equal("""["42","43"]""", Assert.IsType(result.Content[0]).Text); @@ -380,7 +380,7 @@ public async Task CanReturnMcpContent() return new TextContentBlock { Text = "42" }; }); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Single(result.Content); Assert.Equal("42", Assert.IsType(result.Content[0]).Text); @@ -401,7 +401,7 @@ public async Task CanReturnCollectionOfMcpContent() ]; }); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Equal(2, result.Content.Count); Assert.Equal("42", Assert.IsType(result.Content[0]).Text); @@ -424,7 +424,7 @@ public async Task CanReturnCallToolResult() return response; }); var result = await tool.InvokeAsync( - new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()), + new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }), TestContext.Current.CancellationToken); Assert.Same(response, result); @@ -464,10 +464,7 @@ public async Task StructuredOutput_Enabled_ReturnsExpectedSchema(T value) JsonSerializerOptions options = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() }; McpServerTool tool = McpServerTool.Create(() => value, new() { Name = "tool", UseStructuredContent = true, SerializerOptions = options }); var mockServer = new Mock(); - var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) - { - Params = new CallToolRequestParams { Name = "tool" }, - }; + var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" }); var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); @@ -482,10 +479,7 @@ public async Task StructuredOutput_Enabled_VoidReturningTools_ReturnsExpectedSch { McpServerTool tool = McpServerTool.Create(() => { }); var mockServer = new Mock(); - var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) - { - Params = new CallToolRequestParams { Name = "tool" }, - }; + var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" }); var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); @@ -493,10 +487,7 @@ public async Task StructuredOutput_Enabled_VoidReturningTools_ReturnsExpectedSch Assert.Null(result.StructuredContent); tool = McpServerTool.Create(() => Task.CompletedTask); - request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) - { - Params = new CallToolRequestParams { Name = "tool" }, - }; + request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" }); result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); @@ -504,10 +495,7 @@ public async Task StructuredOutput_Enabled_VoidReturningTools_ReturnsExpectedSch Assert.Null(result.StructuredContent); tool = McpServerTool.Create(() => default(ValueTask)); - request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) - { - Params = new CallToolRequestParams { Name = "tool" }, - }; + request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" }); result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); @@ -522,10 +510,7 @@ public async Task StructuredOutput_Disabled_ReturnsExpectedSchema(T value) JsonSerializerOptions options = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() }; McpServerTool tool = McpServerTool.Create(() => value, new() { UseStructuredContent = false, SerializerOptions = options }); var mockServer = new Mock(); - var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) - { - Params = new CallToolRequestParams { Name = "tool" }, - }; + var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" }); var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); @@ -584,10 +569,7 @@ public async Task OutputSchema_Options_CallToolResult_PreservesStructuredContent OutputSchema = outputSchema, }); var mockServer = new Mock(); - var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) - { - Params = new CallToolRequestParams { Name = "tool" }, - }; + var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" }); var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); @@ -665,10 +647,7 @@ public async Task OutputSchema_Attribute_CallToolResult_PreservesStructuredConte Assert.Equal("object", tool.ProtocolTool.OutputSchema.Value.GetProperty("type").GetString()); var mockServer = new Mock(); - var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) - { - Params = new CallToolRequestParams { Name = "tool" }, - }; + var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" }); var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); @@ -742,10 +721,7 @@ public async Task OutputSchema_Options_PersonType_WithCallToolResult() SerializerOptions = serializerOptions, }); var mockServer = new Mock(); - var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) - { - Params = new CallToolRequestParams { Name = "tool" }, - }; + var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" }); var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); @@ -768,10 +744,7 @@ public async Task OutputSchema_Options_OverridesReturnTypeSchema_InvokeAndValida SerializerOptions = serializerOptions, }); var mockServer = new Mock(); - var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest()) - { - Params = new CallToolRequestParams { Name = "tool" }, - }; + var request = new RequestContext(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" }); var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); @@ -1097,7 +1070,7 @@ public async Task EnablePollingAsync_ThrowsInvalidOperationException_WhenTranspo var jsonRpcRequest = CreateTestJsonRpcRequest(); // The JsonRpcRequest has no Context, so RelatedTransport will be null - var requestContext = new RequestContext(mockServer.Object, jsonRpcRequest); + var requestContext = new RequestContext(mockServer.Object, jsonRpcRequest, new() { Name = "" }); // Act & Assert var exception = await Assert.ThrowsAsync(