Skip to content

Add SchemaCreateOptions in McpServerToolCreateOptions/McpServerPromptCreateOptions #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
return null;
}
},
JsonSchemaCreateOptions = options?.SchemaCreateOptions,
};

/// <summary>Creates an <see cref="McpServerPrompt"/> that wraps the specified <see cref="AIFunction"/>.</summary>
Expand Down
1 change: 1 addition & 0 deletions src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
return null;
}
},
JsonSchemaCreateOptions = options?.SchemaCreateOptions,
};

/// <summary>Creates an <see cref="McpServerTool"/> that wraps the specified <see cref="AIFunction"/>.</summary>
Expand Down
10 changes: 10 additions & 0 deletions src/ModelContextProtocol/Server/McpServerPromptCreateOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.AI;
using ModelContextProtocol.Utils.Json;
using System.ComponentModel;
using System.Text.Json;
Expand Down Expand Up @@ -55,6 +56,14 @@ public sealed class McpServerPromptCreateOptions
/// </remarks>
public JsonSerializerOptions? SerializerOptions { get; set; }

/// <summary>
/// Gets or sets the JSON schema options when creating <see cref="AIFunction"/> from a method.
/// </summary>
/// <remarks>
/// Defaults to <see cref="AIJsonSchemaCreateOptions.Default"/> if left unspecified.
/// </remarks>
public AIJsonSchemaCreateOptions? SchemaCreateOptions { get; set; }

/// <summary>
/// Creates a shallow clone of the current <see cref="McpServerPromptCreateOptions"/> instance.
/// </summary>
Expand All @@ -65,5 +74,6 @@ internal McpServerPromptCreateOptions Clone() =>
Name = Name,
Description = Description,
SerializerOptions = SerializerOptions,
SchemaCreateOptions = SchemaCreateOptions,
};
}
10 changes: 10 additions & 0 deletions src/ModelContextProtocol/Server/McpServerToolCreateOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.AI;
using ModelContextProtocol.Utils.Json;
using System.ComponentModel;
using System.Text.Json;
Expand Down Expand Up @@ -132,6 +133,14 @@ public sealed class McpServerToolCreateOptions
/// </remarks>
public JsonSerializerOptions? SerializerOptions { get; set; }

/// <summary>
/// Gets or sets the JSON schema options when creating <see cref="AIFunction"/> from a method.
/// </summary>
/// <remarks>
/// Defaults to <see cref="AIJsonSchemaCreateOptions.Default"/> if left unspecified.
/// </remarks>
public AIJsonSchemaCreateOptions? SchemaCreateOptions { get; set; }

/// <summary>
/// Creates a shallow clone of the current <see cref="McpServerToolCreateOptions"/> instance.
/// </summary>
Expand All @@ -147,5 +156,6 @@ internal McpServerToolCreateOptions Clone() =>
OpenWorld = OpenWorld,
ReadOnly = ReadOnly,
SerializerOptions = SerializerOptions,
SchemaCreateOptions = SchemaCreateOptions,
};
}
26 changes: 26 additions & 0 deletions tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using ModelContextProtocol.Protocol.Types;
using ModelContextProtocol.Server;
using Moq;
using System.ComponentModel;
using System.Reflection;
using System.Text.Json;

namespace ModelContextProtocol.Tests.Server;

Expand Down Expand Up @@ -316,6 +318,30 @@ await Assert.ThrowsAsync<InvalidOperationException>(async () => await prompt.Get
TestContext.Current.CancellationToken));
}

[Fact]
public async Task SupportsSchemaCreateOptions()
{
AIJsonSchemaCreateOptions schemaCreateOptions = new()
{
TransformSchemaNode = (context, node) =>
{
node["description"] = "1234";
return node;
}
};

McpServerPrompt prompt = McpServerPrompt.Create(([Description("argument1")] int num, [Description("argument2")] string str) =>
{
return new ChatMessage(ChatRole.User, "Hello");
}, new() { SchemaCreateOptions = schemaCreateOptions });

Assert.NotNull(prompt.ProtocolPrompt.Arguments);
Assert.All(
prompt.ProtocolPrompt.Arguments,
x => Assert.Equal("1234", x.Description)
);
}

private sealed class MyService;

private class DisposablePromptType : IDisposable
Expand Down
23 changes: 23 additions & 0 deletions tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,29 @@ public async Task CanReturnCallToolResponse()
Assert.Equal("image", result.Content[1].Type);
}

[Fact]
public async Task SupportsSchemaCreateOptions()
{
AIJsonSchemaCreateOptions schemaCreateOptions = new ()
{
TransformSchemaNode = (context, node) =>
{
node["text"] = "1234";
return node;
},
};

McpServerTool tool = McpServerTool.Create((int num, string str) =>
{
return "42";
}, new() { SchemaCreateOptions = schemaCreateOptions });

Assert.All(
tool.ProtocolTool.InputSchema.GetProperty("properties").EnumerateObject(),
x => Assert.True(x.Value.TryGetProperty("text", out JsonElement value) && value.ToString() == "1234")
);
}

private sealed class MyService;

private class DisposableToolType : IDisposable
Expand Down