-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathConformanceTools.cs
More file actions
445 lines (408 loc) · 15.9 KB
/
ConformanceTools.cs
File metadata and controls
445 lines (408 loc) · 15.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using ModelContextProtocol;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using System.ComponentModel;
using System.Text.Json;
namespace ConformanceServer.Tools;
[McpServerToolType]
public class ConformanceTools
{
// Sample base64 encoded 1x1 red PNG pixel for testing
private const string TestImageBase64 =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==";
// Sample base64 encoded minimal WAV file for testing
private const string TestAudioBase64 =
"UklGRiYAAABXQVZFZm10IBAAAAABAAEAQB8AAAB9AAACABAAZGF0YQIAAAA=";
/// <summary>
/// Simple text tool - returns simple text content for testing
/// </summary>
[McpServerTool(Name = "test_simple_text")]
[Description("Tests simple text content response")]
public static string SimpleText()
{
return "This is a simple text response for testing.";
}
/// <summary>
/// Image content tool - returns base64-encoded image content
/// </summary>
[McpServerTool(Name = "test_image_content")]
[Description("Tests image content response")]
public static ImageContentBlock ImageContent()
{
return new ImageContentBlock
{
Data = System.Text.Encoding.UTF8.GetBytes(TestImageBase64),
MimeType = "image/png"
};
}
/// <summary>
/// Audio content tool - returns base64-encoded audio content
/// </summary>
[McpServerTool(Name = "test_audio_content")]
[Description("Tests audio content response")]
public static AudioContentBlock AudioContent()
{
return new AudioContentBlock
{
Data = System.Text.Encoding.UTF8.GetBytes(TestAudioBase64),
MimeType = "audio/wav"
};
}
/// <summary>
/// Embedded resource tool - returns embedded resource content
/// </summary>
[McpServerTool(Name = "test_embedded_resource")]
[Description("Tests embedded resource content response")]
public static EmbeddedResourceBlock EmbeddedResource()
{
return new EmbeddedResourceBlock
{
Resource = new TextResourceContents
{
Uri = "test://embedded-resource",
MimeType = "text/plain",
Text = "This is an embedded resource content."
}
};
}
/// <summary>
/// Multiple content types tool - returns mixed content types (text, image, resource)
/// </summary>
[McpServerTool(Name = "test_multiple_content_types")]
[Description("Tests response with multiple content types (text, image, resource)")]
public static ContentBlock[] MultipleContentTypes()
{
return
[
new TextContentBlock { Text = "Multiple content types test:" },
new ImageContentBlock { Data = System.Text.Encoding.UTF8.GetBytes(TestImageBase64), MimeType = "image/png" },
new EmbeddedResourceBlock
{
Resource = new TextResourceContents
{
Uri = "test://mixed-content-resource",
MimeType = "application/json",
Text = "{ \"test\" = \"data\", \"value\" = 123 }"
}
}
];
}
/// <summary>
/// Tool with logging - emits log messages during execution
/// </summary>
[McpServerTool(Name = "test_tool_with_logging")]
[Description("Tests tool that emits log messages during execution")]
public static async Task<string> ToolWithLogging(
RequestContext<CallToolRequestParams> context,
CancellationToken cancellationToken)
{
var server = context.Server;
// Use ILogger for logging (will be forwarded to client if supported)
ILoggerProvider loggerProvider = server.AsClientLoggerProvider();
ILogger logger = loggerProvider.CreateLogger("ConformanceTools");
logger.LogInformation("Tool execution started");
await Task.Delay(50, cancellationToken);
logger.LogInformation("Tool processing data");
await Task.Delay(50, cancellationToken);
logger.LogInformation("Tool execution completed");
return "Tool with logging executed successfully";
}
/// <summary>
/// Tool with progress - reports progress notifications
/// </summary>
[McpServerTool(Name = "test_tool_with_progress")]
[Description("Tests tool that reports progress notifications")]
public static async Task<string> ToolWithProgress(
McpServer server,
RequestContext<CallToolRequestParams> context,
CancellationToken cancellationToken)
{
var progressToken = context.Params.ProgressToken;
if (progressToken is not null)
{
await server.NotifyProgressAsync(progressToken.Value, new ProgressNotificationValue
{
Progress = 0,
Total = 100,
}, cancellationToken: cancellationToken);
await Task.Delay(50, cancellationToken);
await server.NotifyProgressAsync(progressToken.Value, new ProgressNotificationValue
{
Progress = 50,
Total = 100,
}, cancellationToken: cancellationToken);
await Task.Delay(50, cancellationToken);
await server.NotifyProgressAsync(progressToken.Value, new ProgressNotificationValue
{
Progress = 100,
Total = 100,
}, cancellationToken: cancellationToken);
}
return progressToken?.ToString() ?? "No progress token provided";
}
/// <summary>
/// Error handling tool - intentionally throws an error for testing
/// </summary>
[McpServerTool(Name = "test_error_handling")]
[Description("Tests error response handling")]
public static string ErrorHandling()
{
throw new Exception("This tool intentionally returns an error for testing");
}
/// <summary>
/// Sampling tool - requests LLM completion from client
/// </summary>
[McpServerTool(Name = "test_sampling")]
[Description("Tests server-initiated sampling (LLM completion request)")]
public static async Task<string> Sampling(
McpServer server,
[Description("The prompt to send to the LLM")] string prompt,
CancellationToken cancellationToken)
{
try
{
var samplingParams = new CreateMessageRequestParams
{
Messages = [new SamplingMessage
{
Role = Role.User,
Content = [new TextContentBlock { Text = prompt }],
}],
MaxTokens = 100,
Temperature = 0.7f
};
var result = await server.SampleAsync(samplingParams, cancellationToken);
return $"Sampling result: {(result.Content.FirstOrDefault() as TextContentBlock)?.Text ?? "No text content"}";
}
catch (Exception ex)
{
return $"Sampling not supported or error: {ex.Message}";
}
}
/// <summary>
/// Elicitation tool - requests user input from client
/// </summary>
[McpServerTool(Name = "test_elicitation")]
[Description("Tests elicitation (user input request from client)")]
public static async Task<string> Elicitation(
McpServer server,
[Description("Message to show to the user")] string message,
CancellationToken cancellationToken)
{
try
{
var schema = new ElicitRequestParams.RequestSchema
{
Properties =
{
["response"] = new ElicitRequestParams.StringSchema()
{
Description = "User's response to the message"
}
}
};
var result = await server.ElicitAsync(new ElicitRequestParams
{
Message = message,
RequestedSchema = schema
}, cancellationToken);
if (result.Action == "accept" && result.Content != null)
{
return $"User responded: {result.Content["response"].GetString()}";
}
else
{
return $"Elicitation {result.Action}";
}
}
catch (Exception ex)
{
return $"Elicitation not supported or error: {ex.Message}";
}
}
/// <summary>
/// SEP-1034: Elicitation with default values for all primitive types
/// </summary>
[McpServerTool(Name = "test_elicitation_sep1034_defaults")]
[Description("Tests elicitation with default values per SEP-1034")]
public static async Task<string> ElicitationSep1034Defaults(
McpServer server,
CancellationToken cancellationToken)
{
try
{
var schema = new ElicitRequestParams.RequestSchema
{
Properties =
{
["name"] = new ElicitRequestParams.StringSchema()
{
Description = "Name",
Default = "John Doe"
},
["age"] = new ElicitRequestParams.NumberSchema()
{
Type = "integer",
Description = "Age",
Default = 30
},
["score"] = new ElicitRequestParams.NumberSchema()
{
Description = "Score",
Default = 95.5
},
["status"] = new ElicitRequestParams.UntitledSingleSelectEnumSchema()
{
Description = "Status",
Enum = ["active", "inactive", "pending"],
Default = "active"
},
["verified"] = new ElicitRequestParams.BooleanSchema()
{
Description = "Verified",
Default = true
}
}
};
var result = await server.ElicitAsync(new ElicitRequestParams
{
Message = "Test elicitation with default values for primitive types",
RequestedSchema = schema
}, cancellationToken);
if (result.Action == "accept" && result.Content != null)
{
return $"Accepted with values: string={result.Content["stringField"].GetString()}, " +
$"number={result.Content["numberField"].GetInt32()}, " +
$"boolean={result.Content["booleanField"].GetBoolean()}";
}
else
{
return $"Elicitation {result.Action}";
}
}
catch (Exception ex)
{
return $"Elicitation not supported or error: {ex.Message}";
}
}
/// <summary>
/// SEP-1330: Elicitation with enum schema improvements
/// </summary>
[McpServerTool(Name = "test_elicitation_sep1330_enums")]
[Description("Tests elicitation with enum schema improvements per SEP-1330")]
public static async Task<string> ElicitationSep1330Enums(
McpServer server,
CancellationToken cancellationToken)
{
try
{
var schema = new ElicitRequestParams.RequestSchema
{
Properties =
{
["untitledSingle"] = new ElicitRequestParams.UntitledSingleSelectEnumSchema()
{
Description = "Choose an option",
Enum = ["option1", "option2", "option3"]
},
["titledSingle"] = new ElicitRequestParams.TitledSingleSelectEnumSchema()
{
Description = "Choose a titled option",
OneOf =
[
new() { Const = "value1", Title = "First Option" },
new() { Const = "value2", Title = "Second Option" },
new() { Const = "value3", Title = "Third Option" }
]
},
#pragma warning disable MCP9001
["legacyEnum"] = new ElicitRequestParams.LegacyTitledEnumSchema()
{
Description = "Choose a legacy option",
Enum = ["opt1", "opt2", "opt3"],
EnumNames = ["Option One", "Option Two", "Option Three"]
},
#pragma warning restore MCP9001
["untitledMulti"] = new ElicitRequestParams.UntitledMultiSelectEnumSchema()
{
Description = "Choose multiple options",
Items = new ElicitRequestParams.UntitledEnumItemsSchema
{
Enum = ["option1", "option2", "option3"]
}
},
["titledMulti"] = new ElicitRequestParams.TitledMultiSelectEnumSchema()
{
Description = "Choose multiple titled options",
Items = new ElicitRequestParams.TitledEnumItemsSchema
{
AnyOf =
[
new() { Const = "value1", Title = "First Choice" },
new() { Const = "value2", Title = "Second Choice" },
new() { Const = "value3", Title = "Third Choice" }
]
}
}
}
};
var result = await server.ElicitAsync(new ElicitRequestParams
{
Message = "Test elicitation with enum schema",
RequestedSchema = schema
}, cancellationToken);
if (result.Action == "accept" && result.Content != null)
{
return $"Elicitation completed: action={result.Action}, content={result.Content}";
}
else
{
return $"Elicitation {result.Action}";
}
}
catch (Exception ex)
{
return $"Elicitation not supported or error: {ex.Message}";
}
}
/// <summary>Create the json_schema_2020_12_tool with a raw JSON Schema 2020-12 inputSchema.</summary>
public static McpServerTool CreateJsonSchema202012Tool()
{
var tool = McpServerTool.Create(
() => "JSON Schema 2020-12 tool executed successfully",
new()
{
Name = "json_schema_2020_12_tool",
Description = "Tool with JSON Schema 2020-12 features"
});
tool.ProtocolTool.InputSchema = JsonElement.Parse("""
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"$defs": {
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" }
}
}
},
"properties": {
"name": { "type": "string" },
"address": { "$ref": "#/$defs/address" }
},
"additionalProperties": false
}
""");
return tool;
}
[McpServerTool(Name = "test_reconnection")]
[Description("Tests SSE stream reconnection by closing the stream mid-call")]
public static string TestReconnection()
{
// This tool doesn't need to do anything - the call filter will close the stream after this tool runs,
// and the client must reconnect to get the result.
return "Reconnection test completed successfully";
}
}