Skip to content

Conversation

@mikekistler
Copy link
Contributor

Motivation and Context

This PR adds the ability to set fields of the _meta parameter in requests with the high-level request methods.

It also refactored these methods to move several parameters common in request methods -- JsonSerializerOptions, ProgressToken, and the new Meta field -- to an options bag to make these signatures more concise.

How Has This Been Tested?

All tests pass with the revised method signatures.

Breaking Changes

This will be breaking (as currently implemented) for users that specify any of these (uncommonly used) parameters.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Some new features in the 2025-11-25 version of the MCP spec require setting and reading fields of _meta, e.g. SEP-1686: Tasks. This change will make it easier for users to access these new features.

Name = toolName,
Arguments = ToArgumentsDictionary(arguments, serializerOptions),
Meta = meta,
// Must set ProgressToken after Meta
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems like a footgun that maybe we should find a way to avoid.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the order in which properties are set should never affect behavior.

Why does the order matter?

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 introduces a RequestOptions class to consolidate common optional parameters (Meta, JsonSerializerOptions, and ProgressToken) across MCP client and server request methods. This refactoring reduces method signature complexity and enables setting the _meta field in requests, which is needed for new MCP spec features like SEP-1686 (Tasks).

Key Changes

  • Added new RequestOptions class to hold optional request parameters
  • Updated all client and server request method signatures to accept RequestOptions instead of individual parameters
  • Created PingRequestParams protocol class for structured ping requests
  • Updated tests to use the new method signatures with named parameters or null for options

Reviewed Changes

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

Show a summary per file
File Description
src/ModelContextProtocol.Core/RequestOptions.cs New options bag class with Meta, JsonSerializerOptions, and ProgressToken properties
src/ModelContextProtocol.Core/Protocol/PingRequestParams.cs New protocol parameter class for ping requests
src/ModelContextProtocol.Core/Client/McpClient.Methods.cs Updated all client methods to accept RequestOptions and properly set Meta in request params
src/ModelContextProtocol.Core/Server/McpServer.Methods.cs Updated SampleAsync and RequestRootsAsync to accept RequestOptions
src/ModelContextProtocol.Core/Client/McpClientExtensions.cs Updated extension methods to wrap individual parameters in RequestOptions
src/ModelContextProtocol.Core/Server/McpServerExtensions.cs Updated extension methods to forward calls with named parameters
src/ModelContextProtocol.Core/Client/McpClient*.cs Updated client wrapper classes to construct RequestOptions when calling client methods
src/ModelContextProtocol.Core/McpJsonUtilities.cs Added PingRequestParams to source generation context
tests/**/*.cs Updated all test call sites to use new signatures with null or named parameters
samples/**/*.cs Updated sample code to use named parameters for cancellationToken

IMcpClient client = mockClient.Object;

var result = await client.ReadResourceAsync("mcp://resource/1", TestContext.Current.CancellationToken);
var result = await client.ReadResourceAsync("mcp://resource/1", cancellationToken: TestContext.Current.CancellationToken);
Copy link

Copilot AI Nov 17, 2025

Choose a reason for hiding this comment

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

Call to obsolete method ReadResourceAsync.

Suggested change
var result = await client.ReadResourceAsync("mcp://resource/1", cancellationToken: TestContext.Current.CancellationToken);
var result = await ((McpClient)client).ReadResourceAsync("mcp://resource/1", cancellationToken: TestContext.Current.CancellationToken);

Copilot uses AI. Check for mistakes.

var tool = (await client.ListToolsAsync(options, TestContext.Current.CancellationToken)).First();
var tool = (await client.ListToolsAsync(new RequestOptions { JsonSerializerOptions = options }, TestContext.Current.CancellationToken)).First();
var originalName = tool.Name;
Copy link

Copilot AI Nov 17, 2025

Choose a reason for hiding this comment

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

Variable tool may be null at this access as suggested by this null check.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@mikekistler mikekistler requested a review from halter73 November 17, 2025 22:01
ProtocolTool.Name,
arguments,
progress,
new RequestOptions
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nice to avoid allocating the RequestOptions when serializerOptions is null (which is expected to be the common case)

Comment on lines +39 to +59
/// <summary>
/// Initializes a new instance of the <see cref="RequestOptions"/> class with the specified metadata.
/// </summary>
/// <param name="meta">Optional metadata to include in the request.</param>
public RequestOptions(JsonObject? meta)
{
Meta = meta;
}

/// <summary>
/// Initializes a new instance of the <see cref="RequestOptions"/> class with the specified options.
/// </summary>
/// <param name="meta">Optional metadata to include in the request.</param>
/// <param name="jsonSerializerOptions">The JSON serializer options to use.</param>
/// <param name="progressToken">The progress token for tracking operations.</param>
public RequestOptions(JsonObject? meta = null, JsonSerializerOptions? jsonSerializerOptions = null, ProgressToken? progressToken = null)
{
Meta = meta;
JsonSerializerOptions = jsonSerializerOptions;
ProgressToken = progressToken;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are these constructors necessary?

/// <summary>
/// Gets a default instance with all properties set to null.
/// </summary>
public static RequestOptions Default { get; } = new();
Copy link
Contributor

Choose a reason for hiding this comment

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

Any code in the process could do:

RequestOptions.Default.Meta = new();

or the like with any of the properties, such that the properties would no longer be null.

This property should be removed.

(Alternatively, you could modify the other properties to all throw if ReferenceEquals(this, Default), but I don't think it's worthwhile. Default should never be needed by consumers, as they should always be able to just pass in null for the options.)

{
ThrowIfRootsUnsupported();

if (options?.Meta is not null)
Copy link
Contributor

Choose a reason for hiding this comment

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

If this kind of merging will need to be done in more than one place, we should have a helper for 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.

3 participants