Skip to content

Conversation

@PederHP
Copy link
Member

@PederHP PederHP commented Nov 25, 2025

Make it possible to send request metadata (_meta) with Tool calls, by adding a WithMeta method similar to WithProgress. The copy semantics make it easy to have (outer) host context specific metadata injections.

Usage Example:

  var tool = tools.Single(t => t.Name == "my_tool");

  // Add metadata for tracing/logging
  var toolWithMeta = tool.WithMeta(new JsonObject
  {
      ["traceId"] = correlationId,
      ["userId"] = currentUserId
  });

  // Each IChatClient can have its own metadata
  var chatClient1Tools = tools.Select(t => t.WithMeta(new JsonObject { ["clientId"] = "client-1" }));
  var chatClient2Tools = tools.Select(t => t.WithMeta(new JsonObject { ["clientId"] = "client-2" }));

This is additive, so any existing (non-McpClientTool) metadata isn't overwritten.

@PederHP PederHP requested a review from Copilot November 25, 2025 13:46
Copilot finished reviewing on behalf of PederHP November 25, 2025 13:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a WithMetadata method to McpClientTool, enabling clients to include custom metadata with tool call requests. This follows the same copy-semantics pattern as the existing WithProgress method, making it easy to inject host-specific context (e.g., trace IDs, authentication tokens) into MCP tool calls without modifying the original tool instance.

Key changes:

  • Added WithMetadata(JsonObject) method to McpClientTool for attaching metadata to tool calls
  • Updated CallToolAsync methods to accept and properly handle metadata parameters
  • Metadata is correctly merged with protocol-level fields like progress tokens when both are used

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/ModelContextProtocol.Core/Client/McpClientTool.cs Added _metadata field, updated constructors to propagate metadata, and implemented WithMetadata method with comprehensive XML documentation
src/ModelContextProtocol.Core/Client/McpClient.Methods.cs Updated CallToolAsync to accept metadata parameter, properly clone metadata when combining with progress tokens to prevent mutation
src/ModelContextProtocol.Core/Client/McpClientExtensions.cs Added meta parameter to CallToolAsync extension method signature and XML documentation
tests/ModelContextProtocol.Tests/Client/McpClientToolTests.cs Added MetadataEchoTool test server tool and four comprehensive test cases covering metadata passing, instance creation, method chaining, and multiple instances with different metadata; also cleaned up trailing whitespace

@PederHP PederHP marked this pull request as ready for review November 25, 2025 13:53
IReadOnlyDictionary<string, object?>? arguments = null,
IProgress<ProgressNotificationValue>? progress = null,
JsonSerializerOptions? serializerOptions = null,
JsonObject? meta = null,
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this isn't going to conflict with @mikekistler's pending change. Let's get that one in first and then rebase this one on top of it.

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.

2 participants