-
Notifications
You must be signed in to change notification settings - Fork 558
Add public constructors to McpClient types for reusing cached definitions #938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/add-mcpclienttool-constructor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+785
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
350f8e1
Initial plan
Copilot 44ea87c
Add public constructor to McpClientTool for reusing tool definitions
Copilot 7c6c554
Fix McpClientTool constructor per review feedback
Copilot 401e5da
Add public constructors for McpClientPrompt, McpClientResource, and M…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
tests/ModelContextProtocol.Tests/Client/McpClientPromptTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using ModelContextProtocol.Client; | ||
| using ModelContextProtocol.Protocol; | ||
| using ModelContextProtocol.Server; | ||
| using System.ComponentModel; | ||
|
|
||
| namespace ModelContextProtocol.Tests.Client; | ||
|
|
||
| public class McpClientPromptTests : ClientServerTestBase | ||
| { | ||
| public McpClientPromptTests(ITestOutputHelper outputHelper) | ||
| : base(outputHelper) | ||
| { | ||
| } | ||
|
|
||
| protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder) | ||
| { | ||
| mcpServerBuilder.WithPrompts<GreetingPrompts>(); | ||
| } | ||
|
|
||
| [McpServerPromptType] | ||
| private sealed class GreetingPrompts | ||
| { | ||
| [McpServerPrompt, Description("Generates a greeting prompt")] | ||
| public static ChatMessage Greeting([Description("The name to greet")] string name) => | ||
| new(ChatRole.User, $"Hello, {name}!"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Constructor_WithValidParameters_CreatesInstance() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| var prompts = await client.ListPromptsAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
| var originalPrompt = prompts.First(); | ||
| var promptDefinition = originalPrompt.ProtocolPrompt; | ||
|
|
||
| var newPrompt = new McpClientPrompt(client, promptDefinition); | ||
|
|
||
| Assert.NotNull(newPrompt); | ||
| Assert.Equal("greeting", newPrompt.Name); | ||
| Assert.Equal("Generates a greeting prompt", newPrompt.Description); | ||
| Assert.Same(promptDefinition, newPrompt.ProtocolPrompt); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Constructor_WithNullClient_ThrowsArgumentNullException() | ||
| { | ||
| var promptDefinition = new Prompt | ||
| { | ||
| Name = "test", | ||
| Description = "Test prompt" | ||
| }; | ||
|
|
||
| Assert.Throws<ArgumentNullException>("client", () => new McpClientPrompt(null!, promptDefinition)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Constructor_WithNullPrompt_ThrowsArgumentNullException() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| Assert.Throws<ArgumentNullException>("prompt", () => new McpClientPrompt(client, null!)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReusePromptDefinition_AcrossDifferentClients_InvokesSuccessfully() | ||
| { | ||
| Prompt promptDefinition; | ||
| { | ||
| await using McpClient client1 = await CreateMcpClientForServer(); | ||
| var prompts = await client1.ListPromptsAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
| var greetingPrompt = prompts.First(p => p.Name == "greeting"); | ||
| promptDefinition = greetingPrompt.ProtocolPrompt; | ||
| } | ||
|
|
||
| await using McpClient client2 = await CreateMcpClientForServer(); | ||
|
|
||
| var reusedPrompt = new McpClientPrompt(client2, promptDefinition); | ||
|
|
||
| var result = await reusedPrompt.GetAsync( | ||
| new Dictionary<string, object?> { ["name"] = "World" }, | ||
| cancellationToken: TestContext.Current.CancellationToken); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.NotNull(result.Messages); | ||
| var message = result.Messages.First(); | ||
| Assert.NotNull(message.Content); | ||
| var textContent = message.Content as TextContentBlock; | ||
| Assert.NotNull(textContent); | ||
| Assert.Equal("Hello, World!", textContent.Text); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReusePromptDefinition_PreservesPromptMetadata() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| var prompts = await client.ListPromptsAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
| var originalPrompt = prompts.First(); | ||
| var promptDefinition = originalPrompt.ProtocolPrompt; | ||
|
|
||
| var reusedPrompt = new McpClientPrompt(client, promptDefinition); | ||
|
|
||
| Assert.Equal(originalPrompt.Name, reusedPrompt.Name); | ||
| Assert.Equal(originalPrompt.Description, reusedPrompt.Description); | ||
| Assert.Equal(originalPrompt.ProtocolPrompt.Name, reusedPrompt.ProtocolPrompt.Name); | ||
| Assert.Equal(originalPrompt.ProtocolPrompt.Description, reusedPrompt.ProtocolPrompt.Description); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ManuallyConstructedPrompt_CanBeInvoked() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| var manualPrompt = new Prompt | ||
| { | ||
| Name = "greeting", | ||
| Description = "Generates a greeting prompt" | ||
| }; | ||
|
|
||
| var clientPrompt = new McpClientPrompt(client, manualPrompt); | ||
|
|
||
| var result = await clientPrompt.GetAsync( | ||
| new Dictionary<string, object?> { ["name"] = "Test" }, | ||
| cancellationToken: TestContext.Current.CancellationToken); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.NotNull(result.Messages); | ||
| var message = result.Messages.First(); | ||
| Assert.NotNull(message.Content); | ||
| var textContent = message.Content as TextContentBlock; | ||
| Assert.NotNull(textContent); | ||
| Assert.Equal("Hello, Test!", textContent.Text); | ||
| } | ||
| } |
103 changes: 103 additions & 0 deletions
103
tests/ModelContextProtocol.Tests/Client/McpClientResourceTemplateConstructorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using ModelContextProtocol.Client; | ||
| using ModelContextProtocol.Protocol; | ||
| using ModelContextProtocol.Server; | ||
| using System.ComponentModel; | ||
|
|
||
| namespace ModelContextProtocol.Tests.Client; | ||
|
|
||
| public class McpClientResourceTemplateConstructorTests : ClientServerTestBase | ||
| { | ||
| public McpClientResourceTemplateConstructorTests(ITestOutputHelper outputHelper) | ||
| : base(outputHelper) | ||
| { | ||
| } | ||
|
|
||
| protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder) | ||
| { | ||
| mcpServerBuilder.WithResources<FileTemplateResources>(); | ||
| } | ||
|
|
||
| [McpServerResourceType] | ||
| private sealed class FileTemplateResources | ||
| { | ||
| [McpServerResource, Description("A file template")] | ||
| public static string FileTemplate([Description("The file path")] string path) => $"Content for {path}"; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Constructor_WithValidParameters_CreatesInstance() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| var templates = await client.ListResourceTemplatesAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
| var originalTemplate = templates.First(); | ||
| var templateDefinition = originalTemplate.ProtocolResourceTemplate; | ||
|
|
||
| var newTemplate = new McpClientResourceTemplate(client, templateDefinition); | ||
|
|
||
| Assert.NotNull(newTemplate); | ||
| Assert.Equal("file_template", newTemplate.Name); | ||
| Assert.Equal("A file template", newTemplate.Description); | ||
| Assert.Same(templateDefinition, newTemplate.ProtocolResourceTemplate); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Constructor_WithNullClient_ThrowsArgumentNullException() | ||
| { | ||
| var templateDefinition = new ResourceTemplate | ||
| { | ||
| UriTemplate = "file:///{path}", | ||
| Name = "test", | ||
| Description = "Test template" | ||
| }; | ||
|
|
||
| Assert.Throws<ArgumentNullException>("client", () => new McpClientResourceTemplate(null!, templateDefinition)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Constructor_WithNullResourceTemplate_ThrowsArgumentNullException() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| Assert.Throws<ArgumentNullException>("resourceTemplate", () => new McpClientResourceTemplate(client, null!)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReuseResourceTemplateDefinition_PreservesTemplateMetadata() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| var templates = await client.ListResourceTemplatesAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
| var originalTemplate = templates.First(); | ||
| var templateDefinition = originalTemplate.ProtocolResourceTemplate; | ||
|
|
||
| var reusedTemplate = new McpClientResourceTemplate(client, templateDefinition); | ||
|
|
||
| Assert.Equal(originalTemplate.Name, reusedTemplate.Name); | ||
| Assert.Equal(originalTemplate.Description, reusedTemplate.Description); | ||
| Assert.Equal(originalTemplate.UriTemplate, reusedTemplate.UriTemplate); | ||
| Assert.Equal(originalTemplate.ProtocolResourceTemplate.Name, reusedTemplate.ProtocolResourceTemplate.Name); | ||
| Assert.Equal(originalTemplate.ProtocolResourceTemplate.Description, reusedTemplate.ProtocolResourceTemplate.Description); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ManuallyConstructedResourceTemplate_CreatesValidInstance() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| var manualTemplate = new ResourceTemplate | ||
| { | ||
| UriTemplate = "file:///{path}", | ||
| Name = "file_template", | ||
| Description = "A file template" | ||
| }; | ||
|
|
||
| var clientTemplate = new McpClientResourceTemplate(client, manualTemplate); | ||
|
|
||
| Assert.NotNull(clientTemplate); | ||
| Assert.Equal("file_template", clientTemplate.Name); | ||
| Assert.Equal("A file template", clientTemplate.Description); | ||
| Assert.Equal("file:///{path}", clientTemplate.UriTemplate); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.