Skip to content

Commit c447973

Browse files
mikekistlerstephentoubCopilot
authored
Add request options bag to high level requests and include Meta (#970)
Co-authored-by: Stephen Toub <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 627de76 commit c447973

33 files changed

+882
-164
lines changed

samples/EverythingServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ await ctx.Server.SampleAsync([
7575
new ChatMessage(ChatRole.System, "You are a helpful test server"),
7676
new ChatMessage(ChatRole.User, $"Resource {uri}, context: A new subscription was started"),
7777
],
78-
options: new ChatOptions
78+
chatOptions: new ChatOptions
7979
{
8080
MaxOutputTokens = 100,
8181
Temperature = 0.7f,

samples/EverythingServer/Tools/SampleLlmTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static async Task<string> SampleLLM(
1515
CancellationToken cancellationToken)
1616
{
1717
var samplingParams = CreateRequestSamplingParams(prompt ?? string.Empty, "sampleLLM", maxTokens);
18-
var sampleResult = await server.SampleAsync(samplingParams, cancellationToken);
18+
var sampleResult = await server.SampleAsync(samplingParams, cancellationToken: cancellationToken);
1919

2020
return $"LLM sampling result: {sampleResult.Content.OfType<TextContentBlock>().FirstOrDefault()?.Text}";
2121
}

samples/TestServerWithHosting/Tools/SampleLlmTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static async Task<string> SampleLLM(
1818
CancellationToken cancellationToken)
1919
{
2020
var samplingParams = CreateRequestSamplingParams(prompt ?? string.Empty, "sampleLLM", maxTokens);
21-
var sampleResult = await thisServer.SampleAsync(samplingParams, cancellationToken);
21+
var sampleResult = await thisServer.SampleAsync(samplingParams, cancellationToken: cancellationToken);
2222

2323
return $"LLM sampling result: {sampleResult.Content.OfType<TextContentBlock>().FirstOrDefault()?.Text}";
2424
}

src/ModelContextProtocol.Core/Client/McpClient.Methods.cs

Lines changed: 63 additions & 45 deletions
Large diffs are not rendered by default.

src/ModelContextProtocol.Core/Client/McpClientPrompt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ public async ValueTask<GetPromptResult> GetAsync(
9696
arguments as IReadOnlyDictionary<string, object?> ??
9797
arguments?.ToDictionary();
9898

99-
return await _client.GetPromptAsync(ProtocolPrompt.Name, argDict, serializerOptions, cancellationToken: cancellationToken).ConfigureAwait(false);
99+
return await _client.GetPromptAsync(ProtocolPrompt.Name, argDict, new RequestOptions() { JsonSerializerOptions = serializerOptions }, cancellationToken).ConfigureAwait(false);
100100
}
101101
}

src/ModelContextProtocol.Core/Client/McpClientResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ public McpClientResource(McpClient client, Resource resource)
7878
/// <returns>A <see cref="ValueTask{ReadResourceResult}"/> containing the resource's result with content and messages.</returns>
7979
/// <remarks>
8080
/// <para>
81-
/// This is a convenience method that internally calls <see cref="McpClient.ReadResourceAsync(string, CancellationToken)"/>.
81+
/// This is a convenience method that internally calls <see cref="McpClient.ReadResourceAsync(string, RequestOptions, CancellationToken)"/>.
8282
/// </para>
8383
/// </remarks>
8484
public ValueTask<ReadResourceResult> ReadAsync(
8585
CancellationToken cancellationToken = default) =>
86-
_client.ReadResourceAsync(Uri, cancellationToken);
86+
_client.ReadResourceAsync(Uri, cancellationToken: cancellationToken);
8787
}

src/ModelContextProtocol.Core/Client/McpClientResourceTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ public McpClientResourceTemplate(McpClient client, ResourceTemplate resourceTemp
8383
public ValueTask<ReadResourceResult> ReadAsync(
8484
IReadOnlyDictionary<string, object?> arguments,
8585
CancellationToken cancellationToken = default) =>
86-
_client.ReadResourceAsync(UriTemplate, arguments, cancellationToken);
86+
_client.ReadResourceAsync(UriTemplate, arguments, cancellationToken: cancellationToken);
8787
}

src/ModelContextProtocol.Core/Client/McpClientTool.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ internal McpClientTool(
128128
protected async override ValueTask<object?> InvokeCoreAsync(
129129
AIFunctionArguments arguments, CancellationToken cancellationToken)
130130
{
131-
CallToolResult result = await CallAsync(arguments, _progress, JsonSerializerOptions, cancellationToken).ConfigureAwait(false);
131+
var options = JsonSerializerOptions is null ? null : new RequestOptions()
132+
{
133+
JsonSerializerOptions = JsonSerializerOptions,
134+
};
135+
CallToolResult result = await CallAsync(arguments, _progress, options, cancellationToken).ConfigureAwait(false);
132136

133137
// We want to translate the result content into AIContent, using AIContent as the exchange types, so
134138
// that downstream IChatClients can specialize handling based on the content (e.g. sending image content
@@ -163,8 +167,8 @@ result.StructuredContent is null &&
163167
/// value will result in a progress token being included in the call, and any resulting progress notifications during the operation
164168
/// routed to this instance.
165169
/// </param>
166-
/// <param name="serializerOptions">
167-
/// The JSON serialization options governing argument serialization. If <see langword="null"/>, the default serialization options are used.
170+
/// <param name="options">
171+
/// Optional request options including metadata, serialization settings, and progress tracking.
168172
/// </param>
169173
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
170174
/// <returns>
@@ -191,9 +195,16 @@ result.StructuredContent is null &&
191195
public ValueTask<CallToolResult> CallAsync(
192196
IReadOnlyDictionary<string, object?>? arguments = null,
193197
IProgress<ProgressNotificationValue>? progress = null,
194-
JsonSerializerOptions? serializerOptions = null,
198+
RequestOptions? options = null,
195199
CancellationToken cancellationToken = default) =>
196-
_client.CallToolAsync(ProtocolTool.Name, arguments, progress, serializerOptions, cancellationToken);
200+
_client.CallToolAsync(
201+
ProtocolTool.Name,
202+
arguments,
203+
progress,
204+
options ?? new RequestOptions() {
205+
JsonSerializerOptions = JsonSerializerOptions
206+
},
207+
cancellationToken);
197208

198209
/// <summary>
199210
/// Creates a new instance of the tool but modified to return the specified name from its <see cref="Name"/> property.

src/ModelContextProtocol.Core/McpJsonUtilities.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ internal static bool IsValidMcpToolSchema(JsonElement element)
132132
[JsonSerializable(typeof(ListRootsResult))]
133133
[JsonSerializable(typeof(ListToolsRequestParams))]
134134
[JsonSerializable(typeof(ListToolsResult))]
135+
[JsonSerializable(typeof(PingRequestParams))]
135136
[JsonSerializable(typeof(PingResult))]
136137
[JsonSerializable(typeof(ReadResourceRequestParams))]
137138
[JsonSerializable(typeof(ReadResourceResult))]

src/ModelContextProtocol.Core/McpSession.Methods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ internal Task SendNotificationAsync<TParameters>(
150150
/// </summary>
151151
/// <param name="progressToken">The token that identifies the operation for which progress is being reported.</param>
152152
/// <param name="progress">The progress update to send, containing information such as percentage complete or status message.</param>
153+
/// <param name="options">Optional request options including metadata, serialization settings, and progress tracking.</param>
153154
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
154155
/// <returns>A task representing the completion of the notification operation (not the operation being tracked).</returns>
155156
/// <exception cref="ArgumentNullException">The current session instance is <see langword="null"/>.</exception>
@@ -166,6 +167,7 @@ internal Task SendNotificationAsync<TParameters>(
166167
public Task NotifyProgressAsync(
167168
ProgressToken progressToken,
168169
ProgressNotificationValue progress,
170+
RequestOptions? options = null,
169171
CancellationToken cancellationToken = default)
170172
{
171173
return SendNotificationAsync(
@@ -174,6 +176,7 @@ public Task NotifyProgressAsync(
174176
{
175177
ProgressToken = progressToken,
176178
Progress = progress,
179+
Meta = options?.Meta,
177180
},
178181
McpJsonUtilities.JsonContext.Default.ProgressNotificationParams,
179182
cancellationToken);

0 commit comments

Comments
 (0)