Add 3-arg RequestContext constructor and obsolete 2-arg to eliminate null-forgiving operator usage (#1462)
This commit is contained in:
parent
afc40d1e2e
commit
7dcdbafe9d
19 changed files with 190 additions and 219 deletions
|
|
@ -39,7 +39,7 @@ public class Program
|
|||
// 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 class Program
|
|||
{
|
||||
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 class Program
|
|||
{
|
||||
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 class Program
|
|||
})
|
||||
.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
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ public class ConformanceTools
|
|||
RequestContext<CallToolRequestParams> context,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var progressToken = context.Params?.ProgressToken;
|
||||
var progressToken = context.Params.ProgressToken;
|
||||
|
||||
if (progressToken is not null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -204,9 +204,9 @@ internal static class Program
|
|||
};
|
||||
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 @@ internal static class Program
|
|||
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 @@ internal static class Program
|
|||
Content = [new TextContentBlock { Text = $"LLM sampling result: {sampleResult.Content.OfType<TextContentBlock>().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 @@ internal static class Program
|
|||
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 @@ internal static class Program
|
|||
}
|
||||
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 @@ internal static class Program
|
|||
options.Handlers.GetPromptHandler = async (request, cancellationToken) =>
|
||||
{
|
||||
List<PromptMessage> messages = [];
|
||||
if (request.Params?.Name == "simple-prompt")
|
||||
if (request.Params.Name == "simple-prompt")
|
||||
{
|
||||
messages.Add(new PromptMessage
|
||||
{
|
||||
|
|
@ -343,7 +343,7 @@ internal static class Program
|
|||
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 @@ internal static class Program
|
|||
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 @@ internal static class Program
|
|||
}
|
||||
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 @@ internal static class Program
|
|||
{
|
||||
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 @@ internal static class Program
|
|||
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 @@ internal static class Program
|
|||
|
||||
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 @@ internal static class Program
|
|||
|
||||
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 @@ internal static class Program
|
|||
|
||||
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 @@ internal static class Program
|
|||
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 @@ internal static class Program
|
|||
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 @@ internal static class Program
|
|||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -280,7 +280,7 @@ public class Program
|
|||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ public class McpClientMetaTests : ClientServerTestBase
|
|||
async (RequestContext<CallToolRequestParams> 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 class McpClientMetaTests : ClientServerTestBase
|
|||
(RequestContext<ReadResourceRequestParams> 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 class McpClientMetaTests : ClientServerTestBase
|
|||
(RequestContext<GetPromptRequestParams> 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);
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ public class McpClientPromptTests : ClientServerTestBase
|
|||
|
||||
[McpServerPrompt, Description("Echoes back the metadata it receives")]
|
||||
public static ChatMessage MetadataEcho(RequestContext<GetPromptRequestParams> context) =>
|
||||
new(ChatRole.User, context.Params?.Meta?.ToJsonString() ?? "{}");
|
||||
new(ChatRole.User, context.Params.Meta?.ToJsonString() ?? "{}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public class McpClientResourceTemplateConstructorTests : ClientServerTestBase
|
|||
|
||||
[McpServerResource, Description("Echoes back the metadata it receives")]
|
||||
public static string MetadataEcho(RequestContext<ReadResourceRequestParams> context, [Description("An ID")] string id) =>
|
||||
context.Params?.Meta?.ToJsonString() ?? "{}";
|
||||
context.Params.Meta?.ToJsonString() ?? "{}";
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public partial class McpClientResourceTemplateTests : ClientServerTestBase
|
|||
mcpServerBuilder.WithReadResourceHandler((request, cancellationToken) =>
|
||||
new ValueTask<ReadResourceResult>(new ReadResourceResult
|
||||
{
|
||||
Contents = [new TextResourceContents { Text = request.Params?.Uri ?? "", Uri = request.Params?.Uri ?? "" }]
|
||||
Contents = [new TextResourceContents { Text = request.Params.Uri ?? "", Uri = request.Params.Uri ?? "" }]
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public class McpClientResourceTests : ClientServerTestBase
|
|||
|
||||
[McpServerResource, Description("Echoes back the metadata it receives")]
|
||||
public static string MetadataEcho(RequestContext<ReadResourceRequestParams> context) =>
|
||||
context.Params?.Meta?.ToJsonString() ?? "{}";
|
||||
context.Params.Meta?.ToJsonString() ?? "{}";
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
|
|
@ -161,7 +161,7 @@ public class McpClientToolTests : ClientServerTestBase
|
|||
[McpServerTool]
|
||||
public static TextContentBlock MetadataEchoTool(RequestContext<CallToolRequestParams> context)
|
||||
{
|
||||
var meta = context.Params?.Meta;
|
||||
var meta = context.Params.Meta;
|
||||
var metaJson = meta?.ToJsonString() ?? "{}";
|
||||
return new TextContentBlock { Text = metaJson };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -826,7 +826,7 @@ public class McpServerBuilderExtensionsMessageFilterTests(ITestOutputHelper test
|
|||
RequestContext<CallToolRequestParams> context,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (context.Params?.ProgressToken is { } token)
|
||||
if (context.Params.ProgressToken is { } token)
|
||||
{
|
||||
await server.NotifyProgressAsync(token, new ProgressNotificationValue
|
||||
{
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
|
|||
mcpServerBuilder
|
||||
.WithListPromptsHandler(async (request, cancellationToken) =>
|
||||
{
|
||||
var cursor = request.Params?.Cursor;
|
||||
var cursor = request.Params.Cursor;
|
||||
switch (cursor)
|
||||
{
|
||||
case null:
|
||||
|
|
@ -68,7 +68,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
|
|||
})
|
||||
.WithGetPromptHandler(async (request, cancellationToken) =>
|
||||
{
|
||||
switch (request.Params?.Name)
|
||||
switch (request.Params.Name)
|
||||
{
|
||||
case "FirstCustomPrompt":
|
||||
case "SecondCustomPrompt":
|
||||
|
|
@ -79,7 +79,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
|
|||
};
|
||||
|
||||
default:
|
||||
throw new McpProtocolException($"Unknown prompt '{request.Params?.Name}'", McpErrorCode.InvalidParams);
|
||||
throw new McpProtocolException($"Unknown prompt '{request.Params.Name}'", McpErrorCode.InvalidParams);
|
||||
}
|
||||
})
|
||||
.WithPrompts<SimplePrompts>();
|
||||
|
|
@ -314,17 +314,14 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
|
|||
sc.AddMcpServer().WithPrompts(target);
|
||||
|
||||
McpServerPrompt prompt = sc.BuildServiceProvider().GetServices<McpServerPrompt>().First(t => t.ProtocolPrompt.Name == "returns_string");
|
||||
var result = await prompt.GetAsync(new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") })
|
||||
var result = await prompt.GetAsync(new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }, new GetPromptRequestParams
|
||||
{
|
||||
Params = new GetPromptRequestParams
|
||||
Name = "returns_string",
|
||||
Arguments = new Dictionary<string, JsonElement>
|
||||
{
|
||||
Name = "returns_string",
|
||||
Arguments = new Dictionary<string, JsonElement>
|
||||
{
|
||||
["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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
|
|||
mcpServerBuilder
|
||||
.WithListResourcesHandler(async (request, cancellationToken) =>
|
||||
{
|
||||
var cursor = request.Params?.Cursor;
|
||||
var cursor = request.Params.Cursor;
|
||||
switch (cursor)
|
||||
{
|
||||
case null:
|
||||
|
|
@ -67,7 +67,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
|
|||
})
|
||||
.WithListResourceTemplatesHandler(async (request, cancellationToken) =>
|
||||
{
|
||||
var cursor = request.Params?.Cursor;
|
||||
var cursor = request.Params.Cursor;
|
||||
switch (cursor)
|
||||
{
|
||||
case null:
|
||||
|
|
@ -96,7 +96,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
|
|||
})
|
||||
.WithReadResourceHandler(async (request, cancellationToken) =>
|
||||
{
|
||||
switch (request.Params?.Uri)
|
||||
switch (request.Params.Uri)
|
||||
{
|
||||
case "test://Resource1":
|
||||
case "test://Resource2":
|
||||
|
|
@ -105,11 +105,11 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
|
|||
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<SimpleResources>();
|
||||
}
|
||||
|
|
@ -345,13 +345,10 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
|
|||
sc.AddMcpServer().WithResources(target);
|
||||
|
||||
McpServerResource resource = sc.BuildServiceProvider().GetServices<McpServerResource>().First(t => t.ProtocolResource?.Name == "returns_string");
|
||||
var result = await resource.ReadAsync(new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") })
|
||||
var result = await resource.ReadAsync(new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
|
|||
mcpServerBuilder
|
||||
.WithListToolsHandler(async (request, cancellationToken) =>
|
||||
{
|
||||
var cursor = request.Params?.Cursor;
|
||||
var cursor = request.Params.Cursor;
|
||||
switch (cursor)
|
||||
{
|
||||
case null:
|
||||
|
|
@ -93,7 +93,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
|
|||
})
|
||||
.WithCallToolHandler(async (request, cancellationToken) =>
|
||||
{
|
||||
switch (request.Params?.Name)
|
||||
switch (request.Params.Name)
|
||||
{
|
||||
case "FirstCustomTool":
|
||||
case "SecondCustomTool":
|
||||
|
|
@ -104,7 +104,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
|
|||
};
|
||||
|
||||
default:
|
||||
throw new McpProtocolException($"Unknown tool: '{request.Params?.Name}'", McpErrorCode.InvalidParams);
|
||||
throw new McpProtocolException($"Unknown tool: '{request.Params.Name}'", McpErrorCode.InvalidParams);
|
||||
}
|
||||
})
|
||||
.WithTools<EchoTool>(serializerOptions: BuilderToolsJsonContext.Default.Options);
|
||||
|
|
@ -594,7 +594,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
|
|||
sc.AddMcpServer().WithTools(target, BuilderToolsJsonContext.Default.Options);
|
||||
|
||||
McpServerTool tool = sc.BuildServiceProvider().GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "get_ctor_parameter");
|
||||
var result = await tool.InvokeAsync(new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }), TestContext.Current.CancellationToken);
|
||||
var result = await tool.InvokeAsync(new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }, new() { Name = "" }), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(target.GetCtorParameter(), (result.Content[0] as TextContentBlock)?.Text);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public class McpProtocolExceptionDataTests : ClientServerTestBase
|
|||
{
|
||||
mcpServerBuilder.WithCallToolHandler((request, cancellationToken) =>
|
||||
{
|
||||
var toolName = request.Params?.Name;
|
||||
var toolName = request.Params.Name;
|
||||
|
||||
switch (toolName)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public partial class ElicitationTypedTests : ClientServerTestBase
|
|||
{
|
||||
Assert.NotNull(request.Params);
|
||||
|
||||
if (request.Params!.Name == "TestElicitationTyped")
|
||||
if (request.Params.Name == "TestElicitationTyped")
|
||||
{
|
||||
var result = await request.Server.ElicitAsync<SampleForm>(
|
||||
message: "Please provide more information.",
|
||||
|
|
@ -34,7 +34,7 @@ public partial class ElicitationTypedTests : ClientServerTestBase
|
|||
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<CamelForm>(
|
||||
message: "Please provide more information.",
|
||||
|
|
@ -47,7 +47,7 @@ public partial class ElicitationTypedTests : ClientServerTestBase
|
|||
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<NullablePropertyForm>(
|
||||
message: "Please provide more information.",
|
||||
|
|
@ -60,7 +60,7 @@ public partial class ElicitationTypedTests : ClientServerTestBase
|
|||
Content = [new TextContentBlock { Text = "unexpected" }],
|
||||
};
|
||||
}
|
||||
else if (request.Params!.Name == "TestElicitationUnsupportedType")
|
||||
else if (request.Params.Name == "TestElicitationUnsupportedType")
|
||||
{
|
||||
await request.Server.ElicitAsync<UnsupportedForm>(
|
||||
message: "Please provide more information.",
|
||||
|
|
@ -73,7 +73,7 @@ public partial class ElicitationTypedTests : ClientServerTestBase
|
|||
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<string>(
|
||||
|
|
@ -86,7 +86,7 @@ public partial class ElicitationTypedTests : ClientServerTestBase
|
|||
Content = [new TextContentBlock { Text = "unexpected" }],
|
||||
};
|
||||
}
|
||||
else if (request.Params!.Name == "TestElicitationWithDefaults")
|
||||
else if (request.Params.Name == "TestElicitationWithDefaults")
|
||||
{
|
||||
var result = await request.Server.ElicitAsync<FormWithDefaults>(
|
||||
message: "Please provide information.",
|
||||
|
|
@ -101,7 +101,7 @@ public partial class ElicitationTypedTests : ClientServerTestBase
|
|||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Unexpected tool name: {request.Params!.Name}");
|
||||
Assert.Fail($"Unexpected tool name: {request.Params.Name}");
|
||||
}
|
||||
|
||||
return new CallToolResult
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ public class McpServerPromptTests
|
|||
Assert.DoesNotContain("server", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []);
|
||||
|
||||
var result = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.Messages);
|
||||
|
|
@ -83,7 +83,7 @@ public class McpServerPromptTests
|
|||
}, new() { Services = services });
|
||||
|
||||
var result = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.Messages);
|
||||
|
|
@ -133,11 +133,11 @@ public class McpServerPromptTests
|
|||
Assert.DoesNotContain("actualMyService", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []);
|
||||
|
||||
await Assert.ThrowsAnyAsync<ArgumentException>(async () => await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken));
|
||||
|
||||
var result = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Services = services },
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }) { Services = services },
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("Hello", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -158,7 +158,7 @@ public class McpServerPromptTests
|
|||
}, new() { Services = services });
|
||||
|
||||
var result = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("Hello", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -171,7 +171,7 @@ public class McpServerPromptTests
|
|||
_ => new DisposablePromptType());
|
||||
|
||||
var result = await prompt1.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("disposals:1", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -184,7 +184,7 @@ public class McpServerPromptTests
|
|||
_ => new AsyncDisposablePromptType());
|
||||
|
||||
var result = await prompt1.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("asyncDisposals:1", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -197,7 +197,7 @@ public class McpServerPromptTests
|
|||
_ => new AsyncDisposableAndDisposablePromptType());
|
||||
|
||||
var result = await prompt1.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("disposals:0, asyncDisposals:1", Assert.IsType<TextContentBlock>(result.Messages[0].Content).Text);
|
||||
}
|
||||
|
|
@ -213,7 +213,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Same(expected, actual);
|
||||
|
|
@ -230,7 +230,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -256,7 +256,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -288,7 +288,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -315,7 +315,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -347,7 +347,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
var actual = await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
|
|
@ -368,7 +368,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
|
|
@ -381,7 +381,7 @@ public class McpServerPromptTests
|
|||
});
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await prompt.GetAsync(
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ public partial class McpServerResourceTests
|
|||
{
|
||||
Contents = [new TextResourceContents
|
||||
{
|
||||
Uri = ctx.Params!.Uri!,
|
||||
Uri = ctx.Params.Uri!,
|
||||
Text = "Static Resource",
|
||||
MimeType = "text/plain",
|
||||
}]
|
||||
|
|
@ -87,7 +87,7 @@ public partial class McpServerResourceTests
|
|||
{
|
||||
Contents = [new TextResourceContents
|
||||
{
|
||||
Uri = ctx.Params!.Uri!,
|
||||
Uri = ctx.Params.Uri!,
|
||||
Text = "Static Resource",
|
||||
MimeType = "text/plain",
|
||||
}]
|
||||
|
|
@ -111,7 +111,7 @@ public partial class McpServerResourceTests
|
|||
{
|
||||
Contents = [new TextResourceContents
|
||||
{
|
||||
Uri = ctx.Params!.Uri!,
|
||||
Uri = ctx.Params.Uri!,
|
||||
Text = "Static Resource",
|
||||
MimeType = "text/plain",
|
||||
}]
|
||||
|
|
@ -148,7 +148,7 @@ public partial class McpServerResourceTests
|
|||
t = McpServerResource.Create(() => "42", new() { Name = Name });
|
||||
Assert.Equal("resource://mcp/Hello", t.ProtocolResourceTemplate.UriTemplate);
|
||||
result = await t.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
t = McpServerResource.Create((McpServer server) => "42", new() { Name = Name });
|
||||
Assert.Equal("resource://mcp/Hello", t.ProtocolResourceTemplate.UriTemplate);
|
||||
result = await t.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
t = McpServerResource.Create((string arg1) => arg1, new() { Name = Name });
|
||||
Assert.Equal($"resource://mcp/Hello{{?arg1}}", t.ProtocolResourceTemplate.UriTemplate);
|
||||
result = await t.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?arg1=wOrLd" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?arg1=wo&arg2=rld" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=hi&a2=true&a3=s&a4=12&a5=34" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=10&a2=20&a3=30&a4=40&a5=50" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=1&a2=2&a3=3&a4=4&a5=5" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(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<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a2=1.0&a3=3&a4=4&a5=5" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=123&a2=0001-02-03&a3=01%3A02%3A03" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a2=true&a3=s&a4=12&a5=34" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=10&a2=20&a3=30&a4=40&a5=50" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=1&a2=2&a3=3&a4=4&a5=5" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1={DateTime.UtcNow:r}&a2={DateTimeOffset.UtcNow:r}&a4=14e5f43d-0d41-47d6-8207-8249cf669e41" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a2=1.0&a3=3&a4=4&a5=5" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
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<ReadResourceRequestParams>(server, CreateTestJsonRpcRequest()) { Params = new() { Uri = $"resource://mcp/Hello?a1=123&a2=0001-02-03&a3=01%3A02%3A03" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
Assert.Equal("resource://mcp/Hello{?arg1}", t.ProtocolResourceTemplate.UriTemplate);
|
||||
Assert.False(t.IsMatch(uri));
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await t.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = uri } },
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Uri = uri }),
|
||||
TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
|
|
@ -299,7 +299,7 @@ public partial class McpServerResourceTests
|
|||
{
|
||||
McpServerResource t = McpServerResource.Create(() => "resource", new() { UriTemplate = actualUri });
|
||||
Assert.NotNull(await t.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = queriedUri } },
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Uri = queriedUri }),
|
||||
TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
|
|
@ -328,7 +328,7 @@ public partial class McpServerResourceTests
|
|||
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<ArgumentException>(async () => await t.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = uri } },
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Uri = uri }),
|
||||
TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
|
|
@ -341,25 +341,25 @@ public partial class McpServerResourceTests
|
|||
ReadResourceResult result;
|
||||
|
||||
result = await t.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello" } },
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().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<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello?arg1=first" } },
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().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<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello?arg2=42" } },
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().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<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Hello?arg1=first&arg2=42" } },
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().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 partial class McpServerResourceTests
|
|||
}, new() { Name = "Test" });
|
||||
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
}, new() { Services = services });
|
||||
|
||||
var result = await tool.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "https://something" } },
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "https://something" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.Contents);
|
||||
|
|
@ -481,11 +481,11 @@ public partial class McpServerResourceTests
|
|||
Mock<McpServer> mockServer = new();
|
||||
|
||||
await Assert.ThrowsAnyAsync<ArgumentException>(async () => await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }),
|
||||
TestContext.Current.CancellationToken));
|
||||
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Services = services, Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
}, new() { Services = services, Name = "Test" });
|
||||
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().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 partial class McpServerResourceTests
|
|||
_ => new DisposableResourceType());
|
||||
|
||||
var result = await resource1.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "test://static/resource/instanceMethod" } },
|
||||
new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().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 partial class McpServerResourceTests
|
|||
return new ReadResourceResult { Contents = [new TextResourceContents { Text = "hello", Uri = "" }] };
|
||||
}, new() { Name = "Test" });
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.Contents);
|
||||
|
|
@ -558,7 +558,7 @@ public partial class McpServerResourceTests
|
|||
return new TextResourceContents { Text = "hello", Uri = "" };
|
||||
}, new() { Name = "Test", SerializerOptions = JsonContext6.Default.Options });
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.Contents);
|
||||
|
|
@ -579,7 +579,7 @@ public partial class McpServerResourceTests
|
|||
];
|
||||
}, new() { Name = "Test" });
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
return "42";
|
||||
}, new() { Name = "Test" });
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.Contents);
|
||||
|
|
@ -614,7 +614,7 @@ public partial class McpServerResourceTests
|
|||
return new List<string> { "42", "43" };
|
||||
}, new() { Name = "Test", SerializerOptions = JsonContext6.Default.Options });
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(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 partial class McpServerResourceTests
|
|||
return new DataContent(new byte[] { 0, 1, 2 }, "application/octet-stream");
|
||||
}, new() { Name = "Test" });
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.Contents);
|
||||
|
|
@ -654,7 +654,7 @@ public partial class McpServerResourceTests
|
|||
};
|
||||
}, new() { Name = "Test", SerializerOptions = JsonContext6.Default.Options });
|
||||
var result = await resource.ReadAsync(
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Params = new() { Uri = "resource://mcp/Test" } },
|
||||
new RequestContext<ReadResourceRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Uri = "resource://mcp/Test" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Contents.Count);
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ public partial class McpServerToolTests
|
|||
Assert.DoesNotContain("server", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema, McpJsonUtilities.DefaultOptions));
|
||||
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("42", (result.Content[0] as TextContentBlock)?.Text);
|
||||
}
|
||||
|
|
@ -88,7 +88,7 @@ public partial class McpServerToolTests
|
|||
}, new() { Services = services });
|
||||
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.Content);
|
||||
|
|
@ -166,13 +166,13 @@ public partial class McpServerToolTests
|
|||
Mock<McpServer> mockServer = new();
|
||||
|
||||
var ex = await Assert.ThrowsAsync<ArgumentException>(async () => await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken));
|
||||
|
||||
mockServer.SetupGet(s => s.Services).Returns(services);
|
||||
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()) { Services = services },
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }) { Services = services },
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("42", (result.Content[0] as TextContentBlock)?.Text);
|
||||
}
|
||||
|
|
@ -193,7 +193,7 @@ public partial class McpServerToolTests
|
|||
}, new() { Services = services });
|
||||
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("42", (result.Content[0] as TextContentBlock)?.Text);
|
||||
}
|
||||
|
|
@ -208,7 +208,7 @@ public partial class McpServerToolTests
|
|||
options);
|
||||
|
||||
var result = await tool1.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("""{"disposals":1}""", (result.Content[0] as TextContentBlock)?.Text);
|
||||
}
|
||||
|
|
@ -223,7 +223,7 @@ public partial class McpServerToolTests
|
|||
options);
|
||||
|
||||
var result = await tool1.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal("""{"asyncDisposals":1}""", (result.Content[0] as TextContentBlock)?.Text);
|
||||
}
|
||||
|
|
@ -242,7 +242,7 @@ public partial class McpServerToolTests
|
|||
options);
|
||||
|
||||
var result = await tool1.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, CreateTestJsonRpcRequest()) { Services = services },
|
||||
new RequestContext<CallToolRequestParams>(new Mock<McpServer>().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 partial class McpServerToolTests
|
|||
}, new() { SerializerOptions = JsonContext2.Default.Options });
|
||||
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(3, result.Content.Count);
|
||||
|
|
@ -297,7 +297,7 @@ public partial class McpServerToolTests
|
|||
});
|
||||
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Single(result.Content);
|
||||
|
|
@ -333,7 +333,7 @@ public partial class McpServerToolTests
|
|||
return (string?)null;
|
||||
});
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Empty(result.Content);
|
||||
}
|
||||
|
|
@ -348,7 +348,7 @@ public partial class McpServerToolTests
|
|||
return "42";
|
||||
});
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Single(result.Content);
|
||||
Assert.Equal("42", Assert.IsType<TextContentBlock>(result.Content[0]).Text);
|
||||
|
|
@ -364,7 +364,7 @@ public partial class McpServerToolTests
|
|||
return new List<string> { "42", "43" };
|
||||
}, new() { SerializerOptions = JsonContext2.Default.Options });
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Single(result.Content);
|
||||
Assert.Equal("""["42","43"]""", Assert.IsType<TextContentBlock>(result.Content[0]).Text);
|
||||
|
|
@ -380,7 +380,7 @@ public partial class McpServerToolTests
|
|||
return new TextContentBlock { Text = "42" };
|
||||
});
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Single(result.Content);
|
||||
Assert.Equal("42", Assert.IsType<TextContentBlock>(result.Content[0]).Text);
|
||||
|
|
@ -401,7 +401,7 @@ public partial class McpServerToolTests
|
|||
];
|
||||
});
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
Assert.Equal(2, result.Content.Count);
|
||||
Assert.Equal("42", Assert.IsType<TextContentBlock>(result.Content[0]).Text);
|
||||
|
|
@ -424,7 +424,7 @@ public partial class McpServerToolTests
|
|||
return response;
|
||||
});
|
||||
var result = await tool.InvokeAsync(
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest()),
|
||||
new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new() { Name = "" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Same(response, result);
|
||||
|
|
@ -464,10 +464,7 @@ public partial class McpServerToolTests
|
|||
JsonSerializerOptions options = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() };
|
||||
McpServerTool tool = McpServerTool.Create(() => value, new() { Name = "tool", UseStructuredContent = true, SerializerOptions = options });
|
||||
var mockServer = new Mock<McpServer>();
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest())
|
||||
{
|
||||
Params = new CallToolRequestParams { Name = "tool" },
|
||||
};
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" });
|
||||
|
||||
var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
@ -482,10 +479,7 @@ public partial class McpServerToolTests
|
|||
{
|
||||
McpServerTool tool = McpServerTool.Create(() => { });
|
||||
var mockServer = new Mock<McpServer>();
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest())
|
||||
{
|
||||
Params = new CallToolRequestParams { Name = "tool" },
|
||||
};
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" });
|
||||
|
||||
var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
@ -493,10 +487,7 @@ public partial class McpServerToolTests
|
|||
Assert.Null(result.StructuredContent);
|
||||
|
||||
tool = McpServerTool.Create(() => Task.CompletedTask);
|
||||
request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest())
|
||||
{
|
||||
Params = new CallToolRequestParams { Name = "tool" },
|
||||
};
|
||||
request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" });
|
||||
|
||||
result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
@ -504,10 +495,7 @@ public partial class McpServerToolTests
|
|||
Assert.Null(result.StructuredContent);
|
||||
|
||||
tool = McpServerTool.Create(() => default(ValueTask));
|
||||
request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest())
|
||||
{
|
||||
Params = new CallToolRequestParams { Name = "tool" },
|
||||
};
|
||||
request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" });
|
||||
|
||||
result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
@ -522,10 +510,7 @@ public partial class McpServerToolTests
|
|||
JsonSerializerOptions options = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() };
|
||||
McpServerTool tool = McpServerTool.Create(() => value, new() { UseStructuredContent = false, SerializerOptions = options });
|
||||
var mockServer = new Mock<McpServer>();
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest())
|
||||
{
|
||||
Params = new CallToolRequestParams { Name = "tool" },
|
||||
};
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" });
|
||||
|
||||
var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
@ -584,10 +569,7 @@ public partial class McpServerToolTests
|
|||
OutputSchema = outputSchema,
|
||||
});
|
||||
var mockServer = new Mock<McpServer>();
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest())
|
||||
{
|
||||
Params = new CallToolRequestParams { Name = "tool" },
|
||||
};
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" });
|
||||
|
||||
var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
@ -665,10 +647,7 @@ public partial class McpServerToolTests
|
|||
Assert.Equal("object", tool.ProtocolTool.OutputSchema.Value.GetProperty("type").GetString());
|
||||
|
||||
var mockServer = new Mock<McpServer>();
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest())
|
||||
{
|
||||
Params = new CallToolRequestParams { Name = "tool" },
|
||||
};
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" });
|
||||
|
||||
var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
@ -742,10 +721,7 @@ public partial class McpServerToolTests
|
|||
SerializerOptions = serializerOptions,
|
||||
});
|
||||
var mockServer = new Mock<McpServer>();
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest())
|
||||
{
|
||||
Params = new CallToolRequestParams { Name = "tool" },
|
||||
};
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" });
|
||||
|
||||
var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
@ -768,10 +744,7 @@ public partial class McpServerToolTests
|
|||
SerializerOptions = serializerOptions,
|
||||
});
|
||||
var mockServer = new Mock<McpServer>();
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest())
|
||||
{
|
||||
Params = new CallToolRequestParams { Name = "tool" },
|
||||
};
|
||||
var request = new RequestContext<CallToolRequestParams>(mockServer.Object, CreateTestJsonRpcRequest(), new CallToolRequestParams { Name = "tool" });
|
||||
|
||||
var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
@ -1097,7 +1070,7 @@ public partial class McpServerToolTests
|
|||
var jsonRpcRequest = CreateTestJsonRpcRequest();
|
||||
|
||||
// The JsonRpcRequest has no Context, so RelatedTransport will be null
|
||||
var requestContext = new RequestContext<CallToolRequestParams>(mockServer.Object, jsonRpcRequest);
|
||||
var requestContext = new RequestContext<CallToolRequestParams>(mockServer.Object, jsonRpcRequest, new() { Name = "" });
|
||||
|
||||
// Act & Assert
|
||||
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
|
|
|
|||
Loading…
Reference in a new issue