Skip to content

Commit c8fb356

Browse files
committed
Add RequestOptions with meta to high-level client methods
1 parent b230b17 commit c8fb356

32 files changed

+284
-182
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: 76 additions & 48 deletions
Large diffs are not rendered by default.

src/ModelContextProtocol.Core/Client/McpClientExtensions.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static class McpClientExtensions
3939
[Obsolete($"Use {nameof(McpClient)}.{nameof(McpClient.PingAsync)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774
4040
[EditorBrowsable(EditorBrowsableState.Never)]
4141
public static Task PingAsync(this IMcpClient client, CancellationToken cancellationToken = default)
42-
=> AsClientOrThrow(client).PingAsync(cancellationToken);
42+
=> AsClientOrThrow(client).PingAsync(null, cancellationToken).AsTask();
4343

4444
/// <summary>
4545
/// Retrieves a list of available tools from the server.
@@ -86,7 +86,7 @@ public static ValueTask<IList<McpClientTool>> ListToolsAsync(
8686
this IMcpClient client,
8787
JsonSerializerOptions? serializerOptions = null,
8888
CancellationToken cancellationToken = default)
89-
=> AsClientOrThrow(client).ListToolsAsync(serializerOptions, cancellationToken);
89+
=> AsClientOrThrow(client).ListToolsAsync(new RequestOptions { JsonSerializerOptions = serializerOptions }, cancellationToken);
9090

9191
/// <summary>
9292
/// Creates an enumerable for asynchronously enumerating all available tools from the server.
@@ -126,7 +126,7 @@ public static IAsyncEnumerable<McpClientTool> EnumerateToolsAsync(
126126
this IMcpClient client,
127127
JsonSerializerOptions? serializerOptions = null,
128128
CancellationToken cancellationToken = default)
129-
=> AsClientOrThrow(client).EnumerateToolsAsync(serializerOptions, cancellationToken);
129+
=> AsClientOrThrow(client).EnumerateToolsAsync(new RequestOptions { JsonSerializerOptions = serializerOptions }, cancellationToken);
130130

131131
/// <summary>
132132
/// Retrieves a list of available prompts from the server.
@@ -149,7 +149,7 @@ public static IAsyncEnumerable<McpClientTool> EnumerateToolsAsync(
149149
[EditorBrowsable(EditorBrowsableState.Never)]
150150
public static ValueTask<IList<McpClientPrompt>> ListPromptsAsync(
151151
this IMcpClient client, CancellationToken cancellationToken = default)
152-
=> AsClientOrThrow(client).ListPromptsAsync(cancellationToken);
152+
=> AsClientOrThrow(client).ListPromptsAsync(null, cancellationToken);
153153

154154
/// <summary>
155155
/// Creates an enumerable for asynchronously enumerating all available prompts from the server.
@@ -182,7 +182,7 @@ public static ValueTask<IList<McpClientPrompt>> ListPromptsAsync(
182182
[EditorBrowsable(EditorBrowsableState.Never)]
183183
public static IAsyncEnumerable<McpClientPrompt> EnumeratePromptsAsync(
184184
this IMcpClient client, CancellationToken cancellationToken = default)
185-
=> AsClientOrThrow(client).EnumeratePromptsAsync(cancellationToken);
185+
=> AsClientOrThrow(client).EnumeratePromptsAsync(null, cancellationToken);
186186

187187
/// <summary>
188188
/// Retrieves a specific prompt from the MCP server.
@@ -217,7 +217,7 @@ public static ValueTask<GetPromptResult> GetPromptAsync(
217217
IReadOnlyDictionary<string, object?>? arguments = null,
218218
JsonSerializerOptions? serializerOptions = null,
219219
CancellationToken cancellationToken = default)
220-
=> AsClientOrThrow(client).GetPromptAsync(name, arguments, serializerOptions, cancellationToken);
220+
=> AsClientOrThrow(client).GetPromptAsync(name, arguments, new RequestOptions { JsonSerializerOptions = serializerOptions }, cancellationToken);
221221

222222
/// <summary>
223223
/// Retrieves a list of available resource templates from the server.
@@ -240,7 +240,7 @@ public static ValueTask<GetPromptResult> GetPromptAsync(
240240
[EditorBrowsable(EditorBrowsableState.Never)]
241241
public static ValueTask<IList<McpClientResourceTemplate>> ListResourceTemplatesAsync(
242242
this IMcpClient client, CancellationToken cancellationToken = default)
243-
=> AsClientOrThrow(client).ListResourceTemplatesAsync(cancellationToken);
243+
=> AsClientOrThrow(client).ListResourceTemplatesAsync(null, cancellationToken);
244244

245245
/// <summary>
246246
/// Creates an enumerable for asynchronously enumerating all available resource templates from the server.
@@ -273,7 +273,7 @@ public static ValueTask<IList<McpClientResourceTemplate>> ListResourceTemplatesA
273273
[EditorBrowsable(EditorBrowsableState.Never)]
274274
public static IAsyncEnumerable<McpClientResourceTemplate> EnumerateResourceTemplatesAsync(
275275
this IMcpClient client, CancellationToken cancellationToken = default)
276-
=> AsClientOrThrow(client).EnumerateResourceTemplatesAsync(cancellationToken);
276+
=> AsClientOrThrow(client).EnumerateResourceTemplatesAsync(null, cancellationToken);
277277

278278
/// <summary>
279279
/// Retrieves a list of available resources from the server.
@@ -308,7 +308,7 @@ public static IAsyncEnumerable<McpClientResourceTemplate> EnumerateResourceTempl
308308
[EditorBrowsable(EditorBrowsableState.Never)]
309309
public static ValueTask<IList<McpClientResource>> ListResourcesAsync(
310310
this IMcpClient client, CancellationToken cancellationToken = default)
311-
=> AsClientOrThrow(client).ListResourcesAsync(cancellationToken);
311+
=> AsClientOrThrow(client).ListResourcesAsync(null, cancellationToken);
312312

313313
/// <summary>
314314
/// Creates an enumerable for asynchronously enumerating all available resources from the server.
@@ -341,7 +341,7 @@ public static ValueTask<IList<McpClientResource>> ListResourcesAsync(
341341
[EditorBrowsable(EditorBrowsableState.Never)]
342342
public static IAsyncEnumerable<McpClientResource> EnumerateResourcesAsync(
343343
this IMcpClient client, CancellationToken cancellationToken = default)
344-
=> AsClientOrThrow(client).EnumerateResourcesAsync(cancellationToken);
344+
=> AsClientOrThrow(client).EnumerateResourcesAsync(null, cancellationToken);
345345

346346
/// <summary>
347347
/// Reads a resource from the server.
@@ -356,7 +356,7 @@ public static IAsyncEnumerable<McpClientResource> EnumerateResourcesAsync(
356356
[EditorBrowsable(EditorBrowsableState.Never)]
357357
public static ValueTask<ReadResourceResult> ReadResourceAsync(
358358
this IMcpClient client, string uri, CancellationToken cancellationToken = default)
359-
=> AsClientOrThrow(client).ReadResourceAsync(uri, cancellationToken);
359+
=> AsClientOrThrow(client).ReadResourceAsync(uri, null, cancellationToken);
360360

361361
/// <summary>
362362
/// Reads a resource from the server.
@@ -370,7 +370,7 @@ public static ValueTask<ReadResourceResult> ReadResourceAsync(
370370
[EditorBrowsable(EditorBrowsableState.Never)]
371371
public static ValueTask<ReadResourceResult> ReadResourceAsync(
372372
this IMcpClient client, Uri uri, CancellationToken cancellationToken = default)
373-
=> AsClientOrThrow(client).ReadResourceAsync(uri, cancellationToken);
373+
=> AsClientOrThrow(client).ReadResourceAsync(uri, null, cancellationToken);
374374

375375
/// <summary>
376376
/// Reads a resource from the server.
@@ -386,7 +386,7 @@ public static ValueTask<ReadResourceResult> ReadResourceAsync(
386386
[EditorBrowsable(EditorBrowsableState.Never)]
387387
public static ValueTask<ReadResourceResult> ReadResourceAsync(
388388
this IMcpClient client, string uriTemplate, IReadOnlyDictionary<string, object?> arguments, CancellationToken cancellationToken = default)
389-
=> AsClientOrThrow(client).ReadResourceAsync(uriTemplate, arguments, cancellationToken);
389+
=> AsClientOrThrow(client).ReadResourceAsync(uriTemplate, arguments, null, cancellationToken);
390390

391391
/// <summary>
392392
/// Requests completion suggestions for a prompt argument or resource reference.
@@ -450,7 +450,7 @@ public static ValueTask<CompleteResult> CompleteAsync(this IMcpClient client, Re
450450
[Obsolete($"Use {nameof(McpClient)}.{nameof(McpClient.SubscribeToResourceAsync)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774
451451
[EditorBrowsable(EditorBrowsableState.Never)]
452452
public static Task SubscribeToResourceAsync(this IMcpClient client, string uri, CancellationToken cancellationToken = default)
453-
=> AsClientOrThrow(client).SubscribeToResourceAsync(uri, cancellationToken);
453+
=> AsClientOrThrow(client).SubscribeToResourceAsync(uri, null, cancellationToken);
454454

455455
/// <summary>
456456
/// Subscribes to a resource on the server to receive notifications when it changes.
@@ -479,7 +479,7 @@ public static Task SubscribeToResourceAsync(this IMcpClient client, string uri,
479479
[Obsolete($"Use {nameof(McpClient)}.{nameof(McpClient.SubscribeToResourceAsync)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774
480480
[EditorBrowsable(EditorBrowsableState.Never)]
481481
public static Task SubscribeToResourceAsync(this IMcpClient client, Uri uri, CancellationToken cancellationToken = default)
482-
=> AsClientOrThrow(client).SubscribeToResourceAsync(uri, cancellationToken);
482+
=> AsClientOrThrow(client).SubscribeToResourceAsync(uri, null, cancellationToken);
483483

484484
/// <summary>
485485
/// Unsubscribes from a resource on the server to stop receiving notifications about its changes.
@@ -508,7 +508,7 @@ public static Task SubscribeToResourceAsync(this IMcpClient client, Uri uri, Can
508508
[Obsolete($"Use {nameof(McpClient)}.{nameof(McpClient.UnsubscribeFromResourceAsync)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774
509509
[EditorBrowsable(EditorBrowsableState.Never)]
510510
public static Task UnsubscribeFromResourceAsync(this IMcpClient client, string uri, CancellationToken cancellationToken = default)
511-
=> AsClientOrThrow(client).UnsubscribeFromResourceAsync(uri, cancellationToken);
511+
=> AsClientOrThrow(client).UnsubscribeFromResourceAsync(uri, null, cancellationToken);
512512

513513
/// <summary>
514514
/// Unsubscribes from a resource on the server to stop receiving notifications about its changes.
@@ -536,7 +536,7 @@ public static Task UnsubscribeFromResourceAsync(this IMcpClient client, string u
536536
[Obsolete($"Use {nameof(McpClient)}.{nameof(McpClient.UnsubscribeFromResourceAsync)} instead. This member will be removed in a subsequent release.")] // See: https://github.com/modelcontextprotocol/csharp-sdk/issues/774
537537
[EditorBrowsable(EditorBrowsableState.Never)]
538538
public static Task UnsubscribeFromResourceAsync(this IMcpClient client, Uri uri, CancellationToken cancellationToken = default)
539-
=> AsClientOrThrow(client).UnsubscribeFromResourceAsync(uri, cancellationToken);
539+
=> AsClientOrThrow(client).UnsubscribeFromResourceAsync(uri, null, cancellationToken);
540540

541541
/// <summary>
542542
/// Invokes a tool on the server.
@@ -582,7 +582,7 @@ public static ValueTask<CallToolResult> CallToolAsync(
582582
IProgress<ProgressNotificationValue>? progress = null,
583583
JsonSerializerOptions? serializerOptions = null,
584584
CancellationToken cancellationToken = default)
585-
=> AsClientOrThrow(client).CallToolAsync(toolName, arguments, progress, serializerOptions, cancellationToken);
585+
=> AsClientOrThrow(client).CallToolAsync(toolName, arguments, progress, new RequestOptions { JsonSerializerOptions = serializerOptions }, cancellationToken);
586586

587587
[MethodImpl(MethodImplOptions.AggressiveInlining)]
588588
#pragma warning disable CS0618 // Type or member is obsolete

src/ModelContextProtocol.Core/Client/McpClientPrompt.cs

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

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

src/ModelContextProtocol.Core/Client/McpClientResource.cs

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

src/ModelContextProtocol.Core/Client/McpClientResourceTemplate.cs

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

src/ModelContextProtocol.Core/Client/McpClientTool.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,15 @@ public ValueTask<CallToolResult> CallAsync(
193193
IProgress<ProgressNotificationValue>? progress = null,
194194
JsonSerializerOptions? serializerOptions = null,
195195
CancellationToken cancellationToken = default) =>
196-
_client.CallToolAsync(ProtocolTool.Name, arguments, progress, serializerOptions, cancellationToken);
196+
_client.CallToolAsync(
197+
ProtocolTool.Name,
198+
arguments,
199+
progress,
200+
serializerOptions is null ? null : new RequestOptions
201+
{
202+
JsonSerializerOptions = serializerOptions
203+
},
204+
cancellationToken);
197205

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

src/ModelContextProtocol.Core/McpEndpointExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static Task NotifyProgressAsync(
126126
ProgressToken progressToken,
127127
ProgressNotificationValue progress,
128128
CancellationToken cancellationToken = default)
129-
=> AsSessionOrThrow(endpoint).NotifyProgressAsync(progressToken, progress, cancellationToken);
129+
=> AsSessionOrThrow(endpoint).NotifyProgressAsync(progressToken, progress, cancellationToken: cancellationToken);
130130

131131
[MethodImpl(MethodImplOptions.AggressiveInlining)]
132132
#pragma warning disable CS0618 // Type or member is obsolete

0 commit comments

Comments
 (0)