Add McpClient/Session methods using .Protocol types (#1063)

This commit is contained in:
Stephen Toub 2025-12-05 13:45:12 -05:00 • committed by GitHub
commit 4a84412331
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 1116 additions and 639 deletions

View file

@ -14,7 +14,7 @@ public sealed class InteractiveTools
[McpServerTool, Description("A simple game where the user has to guess a number between 1 and 10.")]
public async Task<string> GuessTheNumber(
McpServer server, // Get the McpServer from DI container
CancellationToken token
CancellationToken cancellationToken
)
{
// Check if the client supports elicitation
@ -37,7 +37,7 @@ public sealed class InteractiveTools
{
Message = "Do you want to play a game?",
RequestedSchema = playSchema
}, token);
}, cancellationToken);
// Check if user wants to play
if (playResponse.Action != "accept" || playResponse.Content?["Answer"].ValueKind != JsonValueKind.True)
@ -64,7 +64,7 @@ public sealed class InteractiveTools
{
Message = "What is your name?",
RequestedSchema = nameSchema
}, token);
}, cancellationToken);
if (nameResponse.Action != "accept")
{
@ -100,7 +100,7 @@ public sealed class InteractiveTools
{
Message = message,
RequestedSchema = guessSchema
}, token);
}, cancellationToken);
if (guessResponse.Action != "accept")
{
@ -128,7 +128,7 @@ public sealed class InteractiveTools
[McpServerTool, Description("Example tool demonstrating various enum schema types")]
public async Task<string> EnumExamples(
McpServer server,
CancellationToken token
CancellationToken cancellationToken
)
{
// Example 1: UntitledSingleSelectEnumSchema - Simple enum without display titles
@ -150,7 +150,7 @@ public sealed class InteractiveTools
{
Message = "Select a priority level:",
RequestedSchema = prioritySchema
}, token);
}, cancellationToken);
if (priorityResponse.Action != "accept")
{
@ -184,7 +184,7 @@ public sealed class InteractiveTools
{
Message = "Select the issue severity:",
RequestedSchema = severitySchema
}, token);
}, cancellationToken);
if (severityResponse.Action != "accept")
{
@ -218,7 +218,7 @@ public sealed class InteractiveTools
{
Message = "Select up to 3 tags:",
RequestedSchema = tagsSchema
}, token);
}, cancellationToken);
if (tagsResponse.Action != "accept")
{
@ -257,7 +257,7 @@ public sealed class InteractiveTools
{
Message = "Select desired features:",
RequestedSchema = featuresSchema
}, token);
}, cancellationToken);
if (featuresResponse.Action != "accept")
{

View file

@ -69,7 +69,7 @@ Clients should check if the server supports logging by checking the <xref:ModelC
[!code-csharp[](samples/client/Program.cs?name=snippet_LoggingCapabilities)]
If the server supports logging, the client should set the level of log messages it wishes to receive with
the <xref:ModelContextProtocol.Client.McpClient.SetLoggingLevel*> method on <xref:ModelContextProtocol.Client.McpClient>. If the client does not set a logging level, the server might choose
the <xref:ModelContextProtocol.Client.McpClient.SetLoggingLevelAsync*> method on <xref:ModelContextProtocol.Client.McpClient>. If the client does not set a logging level, the server might choose
to send all log messages or none&mdash;this is not specified in the protocol. So it's important that the client
sets a logging level to ensure it receives the desired log messages and only those messages.

View file

@ -30,7 +30,7 @@ if (firstArgument is not null)
if (Enum.TryParse<LoggingLevel>(firstArgument, true, out var loggingLevel))
{
// <snippet_LoggingLevel>
await mcpClient.SetLoggingLevel(loggingLevel);
await mcpClient.SetLoggingLevelAsync(loggingLevel);
// </snippet_LoggingLevel>
}
else