Skip to content

Conversation

mehrandvd
Copy link

  • Added 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.

Motivation and Context

It resolves #630

How Has This Been Tested?

I've added proper unit tests for it.

Breaking Changes

No breaking changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

No additional notes.

mehrandvd and others added 2 commits August 19, 2025 02:39
- 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.
@mehrandvd
Copy link
Author

@stephentoub Could you review this, please?

…ion. modelcontextprotocol#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.
@mehrandvd
Copy link
Author

@eiriktsarpalis I've made the changes based on your suggestions, utilizing AIJsonUtilities.CreateJsonSchema.

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.
@mehrandvd
Copy link
Author

@eiriktsarpalis I reviewed the MCP specification and realized I was mistaken. It does support datetime, but it’s converted into a string type with 'date-time' formatting, consistent with AIJsonUtilities.CreateJsonSchema. I've updated the code to reflect this.

mehrandvd and others added 5 commits August 27, 2025 20:27
…ocol#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.
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.
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.
@mehrandvd
Copy link
Author

@eiriktsarpalis I think I've addressed all your feedback. Would you mind reviewing it again?

@mehrandvd
Copy link
Author

@eiriktsarpalis Now it supports both nullable types like ["boolean", "null"] or ["null", "boolean"].

mehrandvd and others added 4 commits September 6, 2025 03:03
Co-authored-by: Eirik Tsarpalis <[email protected]>
…tprotocol#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.
@mehrandvd
Copy link
Author

@eiriktsarpalis All done!

mehrandvd and others added 3 commits September 8, 2025 20:26
…tocol#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.
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.
mehrandvd and others added 2 commits September 9, 2025 00:58
- 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.
@mehrandvd
Copy link
Author

@eiriktsarpalis All done.

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

ThrowIfElicitationUnsupported(server);

serializerOptions ??= McpJsonUtilities.DefaultOptions;
serializerOptions.MakeReadOnly();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this MakeReadOnly call necessary?


var raw = await server.ElicitAsync(request, cancellationToken).ConfigureAwait(false);

if (!string.Equals(raw.Action, "accept", StringComparison.OrdinalIgnoreCase) || raw.Content is null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!string.Equals(raw.Action, "accept", StringComparison.OrdinalIgnoreCase) || raw.Content is null)
if (!raw.IsAccepted || raw.Content is null)

RequestedSchema = schema,
};

var raw = await server.ElicitAsync(request, cancellationToken).ConfigureAwait(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using this helper that's deserializing the response as a non-generic ElicitResponse, and then needing to manually copy all the content into a JsonObject and deserialize that, could this instead just write out the additional few lines to call the underlying helper with the the generic ElicitResponse<T>?

obj[kvp.Key] = JsonNode.Parse(kvp.Value.GetRawText());
}

T? typed = JsonSerializer.Deserialize(obj, serializerOptions.GetTypeInfo<T>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eiriktsarpalis, unrelated to this PR, at one point we'd talked about fixing JsonSerializer.Deserialize overloads that take a JsonSerializerOptions to be on the same plan as what we use in MCP and M.E.AI, having them implicitly call GetTypeInfo and not raise linker and AOT warnings. Are we still considering that? Do we have a tracking issue somewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an ElicitAsync<T> overload
4 participants