Skip to content

Add an optional AIJsonSchemaCreateOptions to WithToolsFromAssembly.#1409

Open
fcourtial wants to merge 2 commits intomodelcontextprotocol:mainfrom
fcourtial:fcourtial/add-AIJsonSchemaCreateOptions-to-WithToolsFromAssembly
Open

Add an optional AIJsonSchemaCreateOptions to WithToolsFromAssembly.#1409
fcourtial wants to merge 2 commits intomodelcontextprotocol:mainfrom
fcourtial:fcourtial/add-AIJsonSchemaCreateOptions-to-WithToolsFromAssembly

Conversation

@fcourtial
Copy link

Add optional AIJsonSchemaCreateOptions parameter to tool registration methods

Summary

Add optional AIJsonSchemaCreateOptions parameter to WithTools and WithToolsFromAssembly extension methods, enabling developers to customize JSON schema generation for MCP tool parameters and outputs.

Motivation and Context

When registering MCP tools using WithTools or WithToolsFromAssembly, developers previously had no way to customize how JSON schemas were generated for tool parameters and return types. This limitation prevented use cases such as:

  • Custom schema transformations
  • Applying specific validation rules
  • Modifying schema nodes to meet specific client requirements

This change adds an optional AIJsonSchemaCreateOptions parameter to both methods, providing the same level of schema customization that's available when creating tools directly via McpServerTool.Create.

How Has This Been Tested?

  • Added three new unit tests in McpServerBuilderExtensionsToolsTests.cs:
    • Register_Static_Tools_With_Custom_Schema_Create_Options - Verifies static tool registration with custom schema options
    • Register_Instance_Tools_With_Custom_Schema_Create_Options - Verifies instance tool registration with custom schema options
    • WithToolsFromAssembly_With_Custom_Schema_Create_Options - Verifies assembly scanning with custom schema options
  • All tests verify that the TransformSchemaNode callback is properly applied to generated schemas
  • Existing tests continue to pass, confirming backward compatibility

Breaking Changes

None. The new parameter is optional and defaults to null, maintaining full backward compatibility with existing code.

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

Implementation details:

  • Added AIJsonSchemaCreateOptions? schemaCreateOptions = null parameter to both WithTools and WithToolsFromAssembly methods
  • The parameter is threaded through to McpServerTool.Create calls for both static and instance methods
  • Added using Microsoft.Extensions.AI; import to support the new parameter type
  • Updated XML documentation to describe the new parameter

/// </remarks>
[RequiresUnreferencedCode(WithToolsRequiresUnreferencedCodeMessage)]
public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnumerable<Type> toolTypes, JsonSerializerOptions? serializerOptions = null)
public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnumerable<Type> toolTypes, JsonSerializerOptions? serializerOptions = null, AIJsonSchemaCreateOptions? schemaCreateOptions = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Separate from whether we want to add this, point of order, this is a binary breaking change. Such functionality would need to be done via additional overloads.

Copy link
Author

Choose a reason for hiding this comment

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

Would that work?

I had to move around the params to avoid ambiguous overloads.

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.

2 participants