File tree Expand file tree Collapse file tree 6 files changed +71
-0
lines changed
src/ModelContextProtocol/Server
tests/ModelContextProtocol.Tests/Server Expand file tree Collapse file tree 6 files changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -130,6 +130,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
130130 return null ;
131131 }
132132 } ,
133+ JsonSchemaCreateOptions = options ? . SchemaCreateOptions ,
133134 } ;
134135
135136 /// <summary>Creates an <see cref="McpServerPrompt"/> that wraps the specified <see cref="AIFunction"/>.</summary>
Original file line number Diff line number Diff line change @@ -151,6 +151,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
151151 return null ;
152152 }
153153 } ,
154+ JsonSchemaCreateOptions = options ? . SchemaCreateOptions ,
154155 } ;
155156
156157 /// <summary>Creates an <see cref="McpServerTool"/> that wraps the specified <see cref="AIFunction"/>.</summary>
Original file line number Diff line number Diff line change 1+ using Microsoft . Extensions . AI ;
12using ModelContextProtocol . Utils . Json ;
23using System . ComponentModel ;
34using System . Text . Json ;
@@ -55,6 +56,14 @@ public sealed class McpServerPromptCreateOptions
5556 /// </remarks>
5657 public JsonSerializerOptions ? SerializerOptions { get ; set ; }
5758
59+ /// <summary>
60+ /// Gets or sets the JSON schema options when creating <see cref="AIFunction"/> from a method.
61+ /// </summary>
62+ /// <remarks>
63+ /// Defaults to <see cref="AIJsonSchemaCreateOptions.Default"/> if left unspecified.
64+ /// </remarks>
65+ public AIJsonSchemaCreateOptions ? SchemaCreateOptions { get ; set ; }
66+
5867 /// <summary>
5968 /// Creates a shallow clone of the current <see cref="McpServerPromptCreateOptions"/> instance.
6069 /// </summary>
@@ -65,5 +74,6 @@ internal McpServerPromptCreateOptions Clone() =>
6574 Name = Name ,
6675 Description = Description ,
6776 SerializerOptions = SerializerOptions ,
77+ SchemaCreateOptions = SchemaCreateOptions ,
6878 } ;
6979}
Original file line number Diff line number Diff line change 1+ using Microsoft . Extensions . AI ;
12using ModelContextProtocol . Utils . Json ;
23using System . ComponentModel ;
34using System . Text . Json ;
@@ -132,6 +133,14 @@ public sealed class McpServerToolCreateOptions
132133 /// </remarks>
133134 public JsonSerializerOptions ? SerializerOptions { get ; set ; }
134135
136+ /// <summary>
137+ /// Gets or sets the JSON schema options when creating <see cref="AIFunction"/> from a method.
138+ /// </summary>
139+ /// <remarks>
140+ /// Defaults to <see cref="AIJsonSchemaCreateOptions.Default"/> if left unspecified.
141+ /// </remarks>
142+ public AIJsonSchemaCreateOptions ? SchemaCreateOptions { get ; set ; }
143+
135144 /// <summary>
136145 /// Creates a shallow clone of the current <see cref="McpServerToolCreateOptions"/> instance.
137146 /// </summary>
@@ -147,5 +156,6 @@ internal McpServerToolCreateOptions Clone() =>
147156 OpenWorld = OpenWorld ,
148157 ReadOnly = ReadOnly ,
149158 SerializerOptions = SerializerOptions ,
159+ SchemaCreateOptions = SchemaCreateOptions ,
150160 } ;
151161}
Original file line number Diff line number Diff line change 33using ModelContextProtocol . Protocol . Types ;
44using ModelContextProtocol . Server ;
55using Moq ;
6+ using System . ComponentModel ;
67using System . Reflection ;
8+ using System . Text . Json ;
79
810namespace ModelContextProtocol . Tests . Server ;
911
@@ -316,6 +318,30 @@ await Assert.ThrowsAsync<InvalidOperationException>(async () => await prompt.Get
316318 TestContext . Current . CancellationToken ) ) ;
317319 }
318320
321+ [ Fact ]
322+ public async Task SupportsSchemaCreateOptions ( )
323+ {
324+ AIJsonSchemaCreateOptions schemaCreateOptions = new ( )
325+ {
326+ TransformSchemaNode = ( context , node ) =>
327+ {
328+ node [ "description" ] = "1234" ;
329+ return node ;
330+ }
331+ } ;
332+
333+ McpServerPrompt prompt = McpServerPrompt . Create ( ( [ Description ( "argument1" ) ] int num , [ Description ( "argument2" ) ] string str ) =>
334+ {
335+ return new ChatMessage ( ChatRole . User , "Hello" ) ;
336+ } , new ( ) { SchemaCreateOptions = schemaCreateOptions } ) ;
337+
338+ Assert . NotNull ( prompt . ProtocolPrompt . Arguments ) ;
339+ Assert . All (
340+ prompt . ProtocolPrompt . Arguments ,
341+ x => Assert . Equal ( "1234" , x . Description )
342+ ) ;
343+ }
344+
319345 private sealed class MyService ;
320346
321347 private class DisposablePromptType : IDisposable
Original file line number Diff line number Diff line change @@ -355,6 +355,29 @@ public async Task CanReturnCallToolResponse()
355355 Assert . Equal ( "image" , result . Content [ 1 ] . Type ) ;
356356 }
357357
358+ [ Fact ]
359+ public async Task SupportsSchemaCreateOptions ( )
360+ {
361+ AIJsonSchemaCreateOptions schemaCreateOptions = new ( )
362+ {
363+ TransformSchemaNode = ( context , node ) =>
364+ {
365+ node [ "text" ] = "1234" ;
366+ return node ;
367+ } ,
368+ } ;
369+
370+ McpServerTool tool = McpServerTool . Create ( ( int num , string str ) =>
371+ {
372+ return "42" ;
373+ } , new ( ) { SchemaCreateOptions = schemaCreateOptions } ) ;
374+
375+ Assert . All (
376+ tool . ProtocolTool . InputSchema . GetProperty ( "properties" ) . EnumerateObject ( ) ,
377+ x => Assert . True ( x . Value . TryGetProperty ( "text" , out JsonElement value ) && value . ToString ( ) == "1234" )
378+ ) ;
379+ }
380+
358381 private sealed class MyService ;
359382
360383 private class DisposableToolType : IDisposable
You can’t perform that action at this time.
0 commit comments