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