Skip to content

Commit 15d1271

Browse files
mehrandvdeiriktsarpalisstephentoub
authored
Add ElicitAsync<T> (#630) (#715)
* Add ElicitAsync<T> (#630) - Refactored `ElicitResult` to a generic class `ElicitResult<T>` for typed content. - Added `ElicitAsync<T>` method in `McpServerExtensions.cs` to request user input and construct schemas based on type `T`. - Implemented schema building logic to handle primitive types and enums, ignoring unsupported types. - Introduced `ElicitationTypedTests.cs` for testing the new elicitation functionality with typed forms. - Verified naming policies in tests to ensure correct serialization casing. - Defined `SampleForm` and `CamelForm` classes for expected input shapes, including unsupported properties for schema testing. - Created JSON serialization contexts for both forms using source generation for improved performance. * Fix the enum issue. for ElicitAsync<T>. * Use AIJsonUtilities.CreateJsonSchema to create PrimitiveSchemaDefinition. #630 Renamed `BuildRequestSchemaFor<T>` to `BuildRequestSchema<T>`. Updated the implementation to use `CreatePrimitiveSchema` and enhanced type checks for supported primitives with `AIJsonUtilities.CreateJsonSchema`. Streamlined handling of unsupported types by consolidating return statements. * Simplify ElicitAsync for schema validation. #630 Simplified the `ElicitAsync` method in `McpServerExtensions.cs` by removing unnecessary JSON object construction and directly deserializing `raw.Content`. Updated `CreatePrimitiveSchema` to use `JsonTypeInfo` for type checking and adjusted return logic for unsupported types. In `ElicitationTypedTests.cs`, modified the `Can_Elicit_Typed_Information` test to account for the new `Created` property in `SampleForm`, increasing the expected property count from 5 to 6. Added assertions for the type and format of the `Created` property and included its deserialization in the test setup. * Add error handling for unsupported elicitation types #630 Introduce exception handling in `McpServerExtensions.cs` for unsupported types. Add a test case in `ElicitationTypedTests.cs` to verify exception throwing for unsupported types. Define a new `UnsupportedForm` class with nested properties and include JSON serialization attributes for proper handling. * Validate generic types in BuildRequestSchema #630 Introduce validation to ensure only object types are supported for elicitation requests in the `BuildRequestSchema` method. An exception is thrown for non-object types. Update the test suite with a new test case to verify this behavior, ensuring that an exception is raised when eliciting a non-object generic type (e.g., string) and that the elicitation handler is not invoked in this scenario. * Move nullable types handling logic to ElicitationRequestParams.Coverter. #630 * Add schema validation for elicitation requests Introduce `TryValidateElicitationPrimitiveSchema` method to validate JSON schemas for elicitation requests. This method checks for object type, verifies the "type" property, and ensures compliance with allowed properties based on primitive types (string, boolean, number). Integrate this validation in the `BuildRequestSchema` method to ensure generated schemas are valid before further processing. * Refactor nullable type pattern matching. #630 * Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs Co-authored-by: Eirik Tsarpalis <[email protected]> * Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs Co-authored-by: Eirik Tsarpalis <[email protected]> * Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs Co-authored-by: Eirik Tsarpalis <[email protected]> * Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs Co-authored-by: Eirik Tsarpalis <[email protected]> * Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs Co-authored-by: Eirik Tsarpalis <[email protected]> * Add ElicitResultSchemaCache. #630 * Prepopulate elicit schema validation logic. #630 - Introduced `LazyElicitAllowedProperties` to cache allowed property names for primitive types, replacing hardcoded logic. - Streamlined `TryValidateElicitationPrimitiveSchema` method by consolidating type checks using pattern matching. - Removed manual addition of allowed properties and now retrieve them directly from the new dictionary. - Updated handling of "integer" type to be treated as "number" for consistency. * Use Nullable.GetUnderlyingType to handle nullable types on elicitation. #630 - Update `ElicitRequestParams.cs` to throw a `JsonException` for non-string "type" properties, simplifying error handling. - Modify `CreatePrimitiveSchema` in `McpServerExtensions.cs` to better handle nullable types and improve error messages. - Revise `TryValidateElicitationPrimitiveSchema` to accept schema as the first parameter, enhancing clarity in error reporting. - Simplify validation logic for the "type" keyword by directly retrieving string values, ensuring unsupported "type" arrays are flagged as errors. * Rename static field. Co-authored-by: Eirik Tsarpalis <[email protected]> * Fix static field renamings. #630 * Make BuildRequestSchema non-generic. #630 * Avoid closure allocation for serializerOptions on netcore #630 Updated the `GetOrAdd` method to use a generic type parameter for .NET, allowing for better handling of `JsonSerializerOptions`. Retained the original implementation for other frameworks to ensure compatibility. * Refactor ElicitRequestParams and McpServerExtensions. #630 - Simplified "type" property handling in Converter. - Changed s_lazyElicitAllowedProperties to nullable type. - Updated ElicitAsync to use static lambda for clarity. - Refactored CreatePrimitiveSchema to handle nullable types. - Initialized allowed properties in TryValidateElicitationPrimitiveSchema. - Updated ElicitationTypedTests for naming conventions and added tests for nullable properties. - Enforced required properties in SampleForm and introduced NullablePropertyForm. - Added JSON source generation context for NullablePropertyForm. * Remove reduntant checks. #630 Refactor JSON schema type handling and add integer support This commit simplifies the validation logic for the `type` property in the JSON schema by removing unnecessary conditional checks. It directly retrieves the string value from `typeProperty` and assigns it to `typeKeyword`. Additionally, it enhances the `s_lazyElicitAllowedProperties` dictionary to include support for the `integer` type, allowing it to be recognized as a valid type with its corresponding allowed properties. * Add IsAccepted property and update ElicitAsync return type - Introduced `IsAccepted` property in `ElicitResult` and `ElicitResult<T>` to indicate if the action was accepted. - Changed `ElicitAsync<T>` to return `ValueTask<ElicitResult<T>>` instead of `ValueTask<ElicitResult<T?>>`, ensuring non-nullable results. - Updated return statements in `ElicitAsync<T>` to reflect the new return type, treating `Content` as non-nullable. * Rename to s_elicitAllowedProperties Co-authored-by: Eirik Tsarpalis <[email protected]> * Fix renaming s_elicitAllowedProperties. #630 * Remove unnecessary json attributes. #630 * Improve xml comment Co-authored-by: Eirik Tsarpalis <[email protected]> * Remove extra IsAccepted property. #630 * Use IsAccepted for checks. Co-authored-by: Stephen Toub <[email protected]> * Add IsAccepted to non-generic ElicitResult. --------- Co-authored-by: Eirik Tsarpalis <[email protected]> Co-authored-by: Stephen Toub <[email protected]>
1 parent d344c65 commit 15d1271

File tree

3 files changed

+608
-0
lines changed

3 files changed

+608
-0
lines changed

src/ModelContextProtocol.Core/Protocol/ElicitResult.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ public sealed class ElicitResult : Result
3333
[JsonPropertyName("action")]
3434
public string Action { get; set; } = "cancel";
3535

36+
/// <summary>
37+
/// Convenience indicator for whether the elicitation was accepted by the user.
38+
/// </summary>
39+
/// <remarks>
40+
/// Indicates that the elicitation request completed successfully and value of <see cref="Content"/> has been populated with a value.
41+
/// </remarks>
42+
[JsonIgnore]
43+
public bool IsAccepted => string.Equals(Action, "accept", StringComparison.OrdinalIgnoreCase);
44+
3645
/// <summary>
3746
/// Gets or sets the submitted form data.
3847
/// </summary>
@@ -48,3 +57,28 @@ public sealed class ElicitResult : Result
4857
[JsonPropertyName("content")]
4958
public IDictionary<string, JsonElement>? Content { get; set; }
5059
}
60+
61+
/// <summary>
62+
/// Represents the client's response to an elicitation request, with typed content payload.
63+
/// </summary>
64+
/// <typeparam name="T">The type of the expected content payload.</typeparam>
65+
public sealed class ElicitResult<T> : Result
66+
{
67+
/// <summary>
68+
/// Gets or sets the user action in response to the elicitation.
69+
/// </summary>
70+
public string Action { get; set; } = "cancel";
71+
72+
/// <summary>
73+
/// Convenience indicator for whether the elicitation was accepted by the user.
74+
/// </summary>
75+
/// <remarks>
76+
/// Indicates that the elicitation request completed successfully and value of <see cref="Content"/> has been populated with a value.
77+
/// </remarks>
78+
public bool IsAccepted => string.Equals(Action, "accept", StringComparison.OrdinalIgnoreCase);
79+
80+
/// <summary>
81+
/// Gets or sets the submitted form data as a typed value.
82+
/// </summary>
83+
public T? Content { get; set; }
84+
}

src/ModelContextProtocol.Core/Server/McpServerExtensions.cs

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
using Microsoft.Extensions.AI;
22
using Microsoft.Extensions.Logging;
33
using ModelContextProtocol.Protocol;
4+
using System.Collections.Concurrent;
5+
using System.Diagnostics.CodeAnalysis;
46
using System.Runtime.CompilerServices;
57
using System.Text;
68
using System.Text.Json;
9+
using System.Text.Json.Nodes;
10+
using System.Text.Json.Serialization.Metadata;
711

812
namespace ModelContextProtocol.Server;
913

@@ -12,6 +16,13 @@ namespace ModelContextProtocol.Server;
1216
/// </summary>
1317
public static class McpServerExtensions
1418
{
19+
/// <summary>
20+
/// Caches request schemas for elicitation requests based on the type and serializer options.
21+
/// </summary>
22+
private static readonly ConditionalWeakTable<JsonSerializerOptions, ConcurrentDictionary<Type, ElicitRequestParams.RequestSchema>> s_elicitResultSchemaCache = new();
23+
24+
private static Dictionary<string, HashSet<string>>? s_elicitAllowedProperties = null;
25+
1526
/// <summary>
1627
/// Requests to sample an LLM via the client using the specified request parameters.
1728
/// </summary>
@@ -234,6 +245,190 @@ public static ValueTask<ElicitResult> ElicitAsync(
234245
cancellationToken: cancellationToken);
235246
}
236247

248+
/// <summary>
249+
/// Requests additional information from the user via the client, constructing a request schema from the
250+
/// public serializable properties of <typeparamref name="T"/> and deserializing the response into <typeparamref name="T"/>.
251+
/// </summary>
252+
/// <typeparam name="T">The type describing the expected input shape. Only primitive members are supported (string, number, boolean, enum).</typeparam>
253+
/// <param name="server">The server initiating the request.</param>
254+
/// <param name="message">The message to present to the user.</param>
255+
/// <param name="serializerOptions">Serializer options that influence property naming and deserialization.</param>
256+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param>
257+
/// <returns>An <see cref="ElicitResult{T}"/> with the user's response, if accepted.</returns>
258+
/// <remarks>
259+
/// Elicitation uses a constrained subset of JSON Schema and only supports strings, numbers/integers, booleans and string enums.
260+
/// Unsupported member types are ignored when constructing the schema.
261+
/// </remarks>
262+
public static async ValueTask<ElicitResult<T>> ElicitAsync<T>(
263+
this IMcpServer server,
264+
string message,
265+
JsonSerializerOptions? serializerOptions = null,
266+
CancellationToken cancellationToken = default)
267+
{
268+
Throw.IfNull(server);
269+
ThrowIfElicitationUnsupported(server);
270+
271+
serializerOptions ??= McpJsonUtilities.DefaultOptions;
272+
serializerOptions.MakeReadOnly();
273+
274+
var dict = s_elicitResultSchemaCache.GetValue(serializerOptions, _ => new());
275+
276+
#if NET
277+
var schema = dict.GetOrAdd(typeof(T), static (t, s) => BuildRequestSchema(t, s), serializerOptions);
278+
#else
279+
var schema = dict.GetOrAdd(typeof(T), type => BuildRequestSchema(type, serializerOptions));
280+
#endif
281+
282+
var request = new ElicitRequestParams
283+
{
284+
Message = message,
285+
RequestedSchema = schema,
286+
};
287+
288+
var raw = await server.ElicitAsync(request, cancellationToken).ConfigureAwait(false);
289+
290+
if (!raw.IsAccepted || raw.Content is null)
291+
{
292+
return new ElicitResult<T> { Action = raw.Action, Content = default };
293+
}
294+
295+
var obj = new JsonObject();
296+
foreach (var kvp in raw.Content)
297+
{
298+
obj[kvp.Key] = JsonNode.Parse(kvp.Value.GetRawText());
299+
}
300+
301+
T? typed = JsonSerializer.Deserialize(obj, serializerOptions.GetTypeInfo<T>());
302+
return new ElicitResult<T> { Action = raw.Action, Content = typed };
303+
}
304+
305+
/// <summary>
306+
/// Builds a request schema for elicitation based on the public serializable properties of <paramref name="type"/>.
307+
/// </summary>
308+
/// <param name="type">The type of the schema being built.</param>
309+
/// <param name="serializerOptions">The serializer options to use.</param>
310+
/// <returns>The built request schema.</returns>
311+
/// <exception cref="McpException"></exception>
312+
private static ElicitRequestParams.RequestSchema BuildRequestSchema(Type type, JsonSerializerOptions serializerOptions)
313+
{
314+
var schema = new ElicitRequestParams.RequestSchema();
315+
var props = schema.Properties;
316+
317+
JsonTypeInfo typeInfo = serializerOptions.GetTypeInfo(type);
318+
319+
if (typeInfo.Kind != JsonTypeInfoKind.Object)
320+
{
321+
throw new McpException($"Type '{type.FullName}' is not supported for elicitation requests.");
322+
}
323+
324+
foreach (JsonPropertyInfo pi in typeInfo.Properties)
325+
{
326+
var def = CreatePrimitiveSchema(pi.PropertyType, serializerOptions);
327+
props[pi.Name] = def;
328+
}
329+
330+
return schema;
331+
}
332+
333+
/// <summary>
334+
/// Creates a primitive schema definition for the specified type, if supported.
335+
/// </summary>
336+
/// <param name="type">The type to create the schema for.</param>
337+
/// <param name="serializerOptions">The serializer options to use.</param>
338+
/// <returns>The created primitive schema definition.</returns>
339+
/// <exception cref="McpException">Thrown when the type is not supported.</exception>
340+
private static ElicitRequestParams.PrimitiveSchemaDefinition CreatePrimitiveSchema(Type type, JsonSerializerOptions serializerOptions)
341+
{
342+
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
343+
{
344+
throw new McpException($"Type '{type.FullName}' is not a supported property type for elicitation requests. Nullable types are not supported.");
345+
}
346+
347+
var typeInfo = serializerOptions.GetTypeInfo(type);
348+
349+
if (typeInfo.Kind != JsonTypeInfoKind.None)
350+
{
351+
throw new McpException($"Type '{type.FullName}' is not a supported property type for elicitation requests.");
352+
}
353+
354+
var jsonElement = AIJsonUtilities.CreateJsonSchema(type, serializerOptions: serializerOptions);
355+
356+
if (!TryValidateElicitationPrimitiveSchema(jsonElement, type, out var error))
357+
{
358+
throw new McpException(error);
359+
}
360+
361+
var primitiveSchemaDefinition =
362+
jsonElement.Deserialize(McpJsonUtilities.JsonContext.Default.PrimitiveSchemaDefinition);
363+
364+
if (primitiveSchemaDefinition is null)
365+
throw new McpException($"Type '{type.FullName}' is not a supported property type for elicitation requests.");
366+
367+
return primitiveSchemaDefinition;
368+
}
369+
370+
/// <summary>
371+
/// Validate the produced schema strictly to the subset we support. We only accept an object schema
372+
/// with a supported primitive type keyword and no additional unsupported keywords.Reject things like
373+
/// {}, 'true', or schemas that include unrelated keywords(e.g.items, properties, patternProperties, etc.).
374+
/// </summary>
375+
/// <param name="schema">The schema to validate.</param>
376+
/// <param name="type">The type of the schema being validated, just for reporting errors.</param>
377+
/// <param name="error">The error message, if validation fails.</param>
378+
/// <returns></returns>
379+
private static bool TryValidateElicitationPrimitiveSchema(JsonElement schema, Type type,
380+
[NotNullWhen(false)] out string? error)
381+
{
382+
if (schema.ValueKind is not JsonValueKind.Object)
383+
{
384+
error = $"Schema generated for type '{type.FullName}' is invalid: expected an object schema.";
385+
return false;
386+
}
387+
388+
if (!schema.TryGetProperty("type", out JsonElement typeProperty)
389+
|| typeProperty.ValueKind is not JsonValueKind.String)
390+
{
391+
error = $"Schema generated for type '{type.FullName}' is invalid: missing or invalid 'type' keyword.";
392+
return false;
393+
}
394+
395+
var typeKeyword = typeProperty.GetString();
396+
397+
if (string.IsNullOrEmpty(typeKeyword))
398+
{
399+
error = $"Schema generated for type '{type.FullName}' is invalid: empty 'type' value.";
400+
return false;
401+
}
402+
403+
if (typeKeyword is not ("string" or "number" or "integer" or "boolean"))
404+
{
405+
error = $"Schema generated for type '{type.FullName}' is invalid: unsupported primitive type '{typeKeyword}'.";
406+
return false;
407+
}
408+
409+
s_elicitAllowedProperties ??= new()
410+
{
411+
["string"] = ["type", "title", "description", "minLength", "maxLength", "format", "enum", "enumNames"],
412+
["number"] = ["type", "title", "description", "minimum", "maximum"],
413+
["integer"] = ["type", "title", "description", "minimum", "maximum"],
414+
["boolean"] = ["type", "title", "description", "default"]
415+
};
416+
417+
var allowed = s_elicitAllowedProperties[typeKeyword];
418+
419+
foreach (JsonProperty prop in schema.EnumerateObject())
420+
{
421+
if (!allowed.Contains(prop.Name))
422+
{
423+
error = $"The property '{type.FullName}.{prop.Name}' is not supported for elicitation.";
424+
return false;
425+
}
426+
}
427+
428+
error = string.Empty;
429+
return true;
430+
}
431+
237432
private static void ThrowIfSamplingUnsupported(IMcpServer server)
238433
{
239434
if (server.ClientCapabilities?.Sampling is null)

0 commit comments

Comments
 (0)