Skip to content

Commit e9877e1

Browse files
mikekistlerstephentoub
authored andcommitted
Fix client conformance test elicitation-sep1034-client-defaults
1 parent 7d08ec2 commit e9877e1

1 file changed

Lines changed: 77 additions & 10 deletions

File tree

  • tests/ModelContextProtocol.ConformanceClient

tests/ModelContextProtocol.ConformanceClient/Program.cs

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Net;
22
using System.Net.Sockets;
3-
using System.Text;
3+
using System.Text.Json;
44
using System.Web;
55
using Microsoft.Extensions.Logging;
66
using ModelContextProtocol.Client;
7+
using ModelContextProtocol.Protocol;
78

89
// This program expects the following command-line arguments:
910
// 1. The client conformance test scenario to run (e.g., "tools_call")
@@ -18,15 +19,6 @@
1819
var scenario = args[0];
1920
var endpoint = args[1];
2021

21-
McpClientOptions options = new()
22-
{
23-
ClientInfo = new()
24-
{
25-
Name = "ConformanceClient",
26-
Version = "1.0.0"
27-
}
28-
};
29-
3022
var consoleLoggerFactory = LoggerFactory.Create(builder =>
3123
{
3224
builder.AddConsole();
@@ -67,6 +59,31 @@
6759
}
6860
}, loggerFactory: consoleLoggerFactory);
6961

62+
// Wrapper delegate pattern: allows setting elicitation handler after client creation
63+
// This allows the actual handler to be set dynamically based on scenario
64+
Func<ElicitRequestParams?, CancellationToken, ValueTask<ElicitResult>>? elicitationHandler = null;
65+
66+
McpClientOptions options = new()
67+
{
68+
ClientInfo = new()
69+
{
70+
Name = "ConformanceClient",
71+
Version = "1.0.0"
72+
},
73+
Handlers = new()
74+
{
75+
ElicitationHandler = (request, cancellationToken) =>
76+
{
77+
if (elicitationHandler is not null)
78+
{
79+
return elicitationHandler(request, cancellationToken);
80+
}
81+
Console.WriteLine("No elicitation handler set, rejecting by default");
82+
return ValueTask.FromResult(new ElicitResult()); // default - reject
83+
}
84+
}
85+
};
86+
7087
await using var mcpClient = await McpClient.CreateAsync(clientTransport, options, loggerFactory: consoleLoggerFactory);
7188

7289
bool success = true;
@@ -105,6 +122,56 @@
105122
success &= !(result.IsError == true);
106123
break;
107124
}
125+
case "elicitation-sep1034-client-defaults":
126+
{
127+
// In this test scenario, an elicitation request will be made that includes default values in the schema.
128+
// The client should apply these defaults to demonstrate that it received and processed them correctly.
129+
130+
// Set the elicitation handler dynamically for this scenario
131+
elicitationHandler = (request, cancellationToken) =>
132+
{
133+
Console.WriteLine($"Received elicitation request: {request?.Message}");
134+
135+
// Apply default values from the schema
136+
var content = new Dictionary<string, JsonElement>();
137+
138+
if (request?.RequestedSchema?.Properties is not null)
139+
{
140+
foreach (var (key, schema) in request.RequestedSchema.Properties)
141+
{
142+
switch (schema)
143+
{
144+
case ElicitRequestParams.StringSchema stringSchema when stringSchema.Default is not null:
145+
content[key] = JsonSerializer.SerializeToElement(stringSchema.Default);
146+
break;
147+
case ElicitRequestParams.NumberSchema numberSchema when numberSchema.Default.HasValue:
148+
content[key] = JsonSerializer.SerializeToElement(numberSchema.Default.Value);
149+
break;
150+
case ElicitRequestParams.BooleanSchema booleanSchema when booleanSchema.Default.HasValue:
151+
content[key] = JsonSerializer.SerializeToElement(booleanSchema.Default.Value);
152+
break;
153+
case ElicitRequestParams.UntitledSingleSelectEnumSchema enumSchema when enumSchema.Default is not null:
154+
content[key] = JsonSerializer.SerializeToElement(enumSchema.Default);
155+
break;
156+
case ElicitRequestParams.TitledSingleSelectEnumSchema titledEnumSchema when titledEnumSchema.Default is not null:
157+
content[key] = JsonSerializer.SerializeToElement(titledEnumSchema.Default);
158+
break;
159+
}
160+
}
161+
}
162+
163+
return new ValueTask<ElicitResult>(new ElicitResult { Action = "accept", Content = content });
164+
};
165+
166+
// Call the test_client_elicitation_defaults tool
167+
var testToolName = "test_client_elicitation_defaults";
168+
Console.WriteLine($"Calling tool: {testToolName}");
169+
var result = await mcpClient.CallToolAsync(toolName: testToolName, arguments: new Dictionary<string, object?>());
170+
Console.WriteLine($"Tool result: {result}");
171+
success &= !(result.IsError == true);
172+
173+
break;
174+
}
108175
default:
109176
// No extra processing for other scenarios
110177
break;

0 commit comments

Comments
 (0)