File tree 6 files changed +71
-0
lines changed
src/ModelContextProtocol/Server
tests/ModelContextProtocol.Tests/Server
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(
130
130
return null ;
131
131
}
132
132
} ,
133
+ JsonSchemaCreateOptions = options ? . SchemaCreateOptions ,
133
134
} ;
134
135
135
136
/// <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(
151
151
return null ;
152
152
}
153
153
} ,
154
+ JsonSchemaCreateOptions = options ? . SchemaCreateOptions ,
154
155
} ;
155
156
156
157
/// <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 ;
1
2
using ModelContextProtocol . Utils . Json ;
2
3
using System . ComponentModel ;
3
4
using System . Text . Json ;
@@ -55,6 +56,14 @@ public sealed class McpServerPromptCreateOptions
55
56
/// </remarks>
56
57
public JsonSerializerOptions ? SerializerOptions { get ; set ; }
57
58
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
+
58
67
/// <summary>
59
68
/// Creates a shallow clone of the current <see cref="McpServerPromptCreateOptions"/> instance.
60
69
/// </summary>
@@ -65,5 +74,6 @@ internal McpServerPromptCreateOptions Clone() =>
65
74
Name = Name ,
66
75
Description = Description ,
67
76
SerializerOptions = SerializerOptions ,
77
+ SchemaCreateOptions = SchemaCreateOptions ,
68
78
} ;
69
79
}
Original file line number Diff line number Diff line change
1
+ using Microsoft . Extensions . AI ;
1
2
using ModelContextProtocol . Utils . Json ;
2
3
using System . ComponentModel ;
3
4
using System . Text . Json ;
@@ -132,6 +133,14 @@ public sealed class McpServerToolCreateOptions
132
133
/// </remarks>
133
134
public JsonSerializerOptions ? SerializerOptions { get ; set ; }
134
135
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
+
135
144
/// <summary>
136
145
/// Creates a shallow clone of the current <see cref="McpServerToolCreateOptions"/> instance.
137
146
/// </summary>
@@ -147,5 +156,6 @@ internal McpServerToolCreateOptions Clone() =>
147
156
OpenWorld = OpenWorld ,
148
157
ReadOnly = ReadOnly ,
149
158
SerializerOptions = SerializerOptions ,
159
+ SchemaCreateOptions = SchemaCreateOptions ,
150
160
} ;
151
161
}
Original file line number Diff line number Diff line change 3
3
using ModelContextProtocol . Protocol . Types ;
4
4
using ModelContextProtocol . Server ;
5
5
using Moq ;
6
+ using System . ComponentModel ;
6
7
using System . Reflection ;
8
+ using System . Text . Json ;
7
9
8
10
namespace ModelContextProtocol . Tests . Server ;
9
11
@@ -316,6 +318,30 @@ await Assert.ThrowsAsync<InvalidOperationException>(async () => await prompt.Get
316
318
TestContext . Current . CancellationToken ) ) ;
317
319
}
318
320
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
+
319
345
private sealed class MyService ;
320
346
321
347
private class DisposablePromptType : IDisposable
Original file line number Diff line number Diff line change @@ -355,6 +355,29 @@ public async Task CanReturnCallToolResponse()
355
355
Assert . Equal ( "image" , result . Content [ 1 ] . Type ) ;
356
356
}
357
357
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
+
358
381
private sealed class MyService ;
359
382
360
383
private class DisposableToolType : IDisposable
You can’t perform that action at this time.
0 commit comments