Skip to content

Commit e190844

Browse files
authored
Add SchemaCreateOptions in McpServerToolCreateOptions/McpServerPromptCreateOptions (#354)
* Add SchemaCreateOptions in McpServerToolCreateOptions * Use TransformSchemaNode for SchemaCreateOptions testing * Add SchemaCreateOptions in McpServerPromptCreateOptions also
1 parent ea296f5 commit e190844

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs

+1
Original file line numberDiff line numberDiff 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>

src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs

+1
Original file line numberDiff line numberDiff 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>

src/ModelContextProtocol/Server/McpServerPromptCreateOptions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.AI;
12
using ModelContextProtocol.Utils.Json;
23
using System.ComponentModel;
34
using 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
}

src/ModelContextProtocol/Server/McpServerToolCreateOptions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.AI;
12
using ModelContextProtocol.Utils.Json;
23
using System.ComponentModel;
34
using 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
}

tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs

+26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using ModelContextProtocol.Protocol.Types;
44
using ModelContextProtocol.Server;
55
using Moq;
6+
using System.ComponentModel;
67
using System.Reflection;
8+
using System.Text.Json;
79

810
namespace 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

tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs

+23
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)