|
| 1 | +using System.Text.Json; |
| 2 | +using ModelContextProtocol.Client; |
| 3 | +using ModelContextProtocol.Protocol; |
| 4 | + |
| 5 | +var endpoint = Environment.GetEnvironmentVariable("ENDPOINT") ?? "http://localhost:3001"; |
| 6 | + |
| 7 | +var clientTransport = new SseClientTransport(new() |
| 8 | +{ |
| 9 | + Endpoint = new Uri(endpoint), |
| 10 | + TransportMode = HttpTransportMode.StreamableHttp, |
| 11 | +}); |
| 12 | + |
| 13 | +// <snippet_McpInitialize> |
| 14 | +McpClientOptions options = new() |
| 15 | +{ |
| 16 | + ClientInfo = new() |
| 17 | + { |
| 18 | + Name = "ElicitationClient", |
| 19 | + Version = "1.0.0" |
| 20 | + }, |
| 21 | + Capabilities = new() |
| 22 | + { |
| 23 | + Elicitation = new() |
| 24 | + { |
| 25 | + ElicitationHandler = HandleElicitationAsync |
| 26 | + } |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport, options); |
| 31 | +// </snippet_McpInitialize> |
| 32 | + |
| 33 | +var tools = await mcpClient.ListToolsAsync(); |
| 34 | +foreach (var tool in tools) |
| 35 | +{ |
| 36 | + Console.WriteLine($"Connected to server with tools: {tool.Name}"); |
| 37 | +} |
| 38 | + |
| 39 | +Console.WriteLine($"Calling tool: {tools.First().Name}"); |
| 40 | + |
| 41 | +var result = await mcpClient.CallToolAsync(toolName: tools.First().Name); |
| 42 | + |
| 43 | +foreach (var block in result.Content) |
| 44 | +{ |
| 45 | + if (block is TextContentBlock textBlock) |
| 46 | + { |
| 47 | + Console.WriteLine(textBlock.Text); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + Console.WriteLine($"Received unexpected result content of type {block.GetType()}"); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// <snippet_ElicitationHandler> |
| 56 | +async ValueTask<ElicitResult> HandleElicitationAsync(ElicitRequestParams? requestParams, CancellationToken token) |
| 57 | +{ |
| 58 | + // Bail out if the requestParams is null or if the requested schema has no properties |
| 59 | + if (requestParams?.RequestedSchema?.Properties == null) |
| 60 | + { |
| 61 | + return new ElicitResult(); |
| 62 | + } |
| 63 | + |
| 64 | + // Process the elicitation request |
| 65 | + if (requestParams?.Message is not null) |
| 66 | + { |
| 67 | + Console.WriteLine(requestParams.Message); |
| 68 | + } |
| 69 | + |
| 70 | + var content = new Dictionary<string, JsonElement>(); |
| 71 | + |
| 72 | + // Loop through requestParams.requestSchema.Properties dictionary requesting values for each property |
| 73 | + foreach (var property in requestParams.RequestedSchema.Properties) |
| 74 | + { |
| 75 | + if (property.Value is ElicitRequestParams.BooleanSchema booleanSchema) |
| 76 | + { |
| 77 | + Console.Write($"{booleanSchema.Description}: "); |
| 78 | + var clientInput = Console.ReadLine(); |
| 79 | + bool parsedBool; |
| 80 | + |
| 81 | + // Try standard boolean parsing first |
| 82 | + if (bool.TryParse(clientInput, out parsedBool)) |
| 83 | + { |
| 84 | + content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(parsedBool)); |
| 85 | + } |
| 86 | + // Also accept "yes"/"no" as valid boolean inputs |
| 87 | + else if (string.Equals(clientInput?.Trim(), "yes", StringComparison.OrdinalIgnoreCase)) |
| 88 | + { |
| 89 | + content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(true)); |
| 90 | + } |
| 91 | + else if (string.Equals(clientInput?.Trim(), "no", StringComparison.OrdinalIgnoreCase)) |
| 92 | + { |
| 93 | + content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(false)); |
| 94 | + } |
| 95 | + } |
| 96 | + else if (property.Value is ElicitRequestParams.NumberSchema numberSchema) |
| 97 | + { |
| 98 | + Console.Write($"{numberSchema.Description}: "); |
| 99 | + var clientInput = Console.ReadLine(); |
| 100 | + double parsedNumber; |
| 101 | + if (double.TryParse(clientInput, out parsedNumber)) |
| 102 | + { |
| 103 | + content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(parsedNumber)); |
| 104 | + } |
| 105 | + } |
| 106 | + else if (property.Value is ElicitRequestParams.StringSchema stringSchema) |
| 107 | + { |
| 108 | + Console.Write($"{stringSchema.Description}: "); |
| 109 | + var clientInput = Console.ReadLine(); |
| 110 | + content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(clientInput)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Return the user's input |
| 115 | + return new ElicitResult |
| 116 | + { |
| 117 | + Action = "accept", |
| 118 | + Content = content |
| 119 | + }; |
| 120 | +} |
| 121 | +// </snippet_ElicitationHandler> |
0 commit comments