Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 3, 2025

Enables caching tool, prompt, resource, and resource template definitions to avoid redundant List*Async calls on reconnect scenarios.

Changes

API Additions

  • Added public McpClientTool(McpClient client, Tool tool, JsonSerializerOptions? serializerOptions = null) constructor
  • Added public McpClientPrompt(McpClient client, Prompt prompt) constructor
  • Added public McpClientResource(McpClient client, Resource resource) constructor
  • Added public McpClientResourceTemplate(McpClient client, ResourceTemplate resourceTemplate) constructor
  • All constructors validate non-null parameters
  • Tool constructor defaults serializer options to McpJsonUtilities.DefaultOptions

Tests

  • Added 12 test cases for McpClientTool covering constructor validation, tool reuse across client instances, manually constructed Tool objects, and AIFunction integration
  • Added 6 test cases for McpClientPrompt covering constructor validation, prompt reuse, and metadata preservation
  • Added 6 test cases for McpClientResource covering constructor validation, resource reuse, and metadata preservation
  • Added 5 test cases for McpClientResourceTemplate covering constructor validation, template reuse, and metadata preservation

Usage

// Cache tool definition from initial connection
var tools = await client1.ListToolsAsync();
var toolDef = tools[0].ProtocolTool;

// Reuse on reconnect without ListToolsAsync round-trip
var client2 = await McpClient.CreateAsync(transport2);
var tool = new McpClientTool(client2, toolDef);
var result = await tool.CallAsync(args);

Similar patterns work for prompts, resources, and resource templates using their respective constructors and protocol definitions.

Original prompt

This section details on the original issue you should resolve

<issue_title>Reuse McpClientTool across different McpClient instances</issue_title>
<issue_description>Sometimes it makes sense to cache tool definitions and not do a ListToolsAsync on every reconnect.

Adding a WithMcpClient() that creates a new instance with the provided client to McpClientTool would enable this pattern.
</issue_description>

<agent_instructions>Add a public ctor to McpClientTool that accepts McpClient client, Tool tool, JsonSerializerOptions? serializerOptions = null, defaulting the serializer options to McpJsonUtilities.DefaultOptions if none is specified. Add tests that validate resulting McpClientTool instances can be used to successfully invoke manually constructed Tool instances representing a tool available on a particular McpClient.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Nov 3, 2025
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Add constructor for McpClientTool with McpClient instance Add public constructor to McpClientTool for reusing cached tool definitions Nov 3, 2025
Copilot AI requested a review from stephentoub November 3, 2025 23:30
Copy link
Contributor

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, please add a similar ctor (and associated tests) for McpClientPrompt, McpClientResource, and McpClientResourceTemplate. When there's already a relevant test file (as there is for McpClientResourceTemplate, add to the existing file rather than creating a new one).

@stephentoub stephentoub marked this pull request as ready for review November 4, 2025 01:45
Copilot AI and others added 2 commits November 4, 2025 01:53
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…cpClientResourceTemplate

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 4, 2025

@copilot, please add a similar ctor (and associated tests) for McpClientPrompt, McpClientResource, and McpClientResourceTemplate. When there's already a relevant test file (as there is for McpClientRe...

Added public constructors and tests for McpClientPrompt, McpClientResource, and McpClientResourceTemplate in commit 401e5da. All 29 tests are passing.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: curl -I -sSL --retry 5 --retry-delay 2 --connect-timeout 15 REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Add public constructor to McpClientTool for reusing cached tool definitions Add public constructors to McpClient types for reusing cached definitions Nov 4, 2025
Copilot AI requested a review from stephentoub November 4, 2025 02:13
Copilot finished work on behalf of stephentoub November 4, 2025 02:13

_client = client;
ProtocolTool = tool;
JsonSerializerOptions = serializerOptions ?? McpJsonUtilities.DefaultOptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the options from the source McpClientTool?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks but I'm not following... from where specifically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, my bad. I should've noticed it's Tool and not McpClientTool. Makes perfect sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reuse McpClientTool across different McpClient instances

3 participants