Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions src/ModelContextProtocol/McpServerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json;
using Microsoft.Extensions.AI;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -125,6 +126,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
/// <param name="builder">The builder instance.</param>
/// <param name="toolTypes">Types with <see cref="McpServerToolAttribute"/>-attributed methods to add as tools to the server.</param>
/// <param name="serializerOptions">The serializer options governing tool parameter marshalling.</param>
/// <param name="schemaCreateOptions">The schema creation options governing tool parameter/output schema generation.</param>
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="builder"/> or <paramref name="toolTypes"/> is <see langword="null"/>.</exception>
/// <remarks>
Expand All @@ -133,7 +135,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
/// instance for each. For instance methods, an instance is constructed for each invocation of the tool.
/// </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
Copy Markdown
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
Copy Markdown
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.

{
Throw.IfNull(builder);
Throw.IfNull(toolTypes);
Expand All @@ -147,8 +149,8 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
if (toolMethod.GetCustomAttribute<McpServerToolAttribute>() is not null)
{
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
services => McpServerTool.Create(toolMethod, options: new() { Services = services, SerializerOptions = serializerOptions }) :
services => McpServerTool.Create(toolMethod, r => CreateTarget(r.Services, toolType), new() { Services = services, SerializerOptions = serializerOptions })));
services => McpServerTool.Create(toolMethod, options: new() { Services = services, SerializerOptions = serializerOptions, SchemaCreateOptions = schemaCreateOptions }) :
services => McpServerTool.Create(toolMethod, r => CreateTarget(r.Services, toolType), new() { Services = services, SerializerOptions = serializerOptions, SchemaCreateOptions = schemaCreateOptions })));
}
}
}
Expand All @@ -163,6 +165,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
/// <param name="builder">The builder instance.</param>
/// <param name="serializerOptions">The serializer options governing tool parameter marshalling.</param>
/// <param name="toolAssembly">The assembly to load the types from. If <see langword="null"/>, the calling assembly is used.</param>
/// <param name="schemaCreateOptions">The schema creation options governing tool parameter/output schema generation.</param>
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
/// <remarks>
Expand All @@ -186,7 +189,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
/// </para>
/// </remarks>
[RequiresUnreferencedCode(WithToolsRequiresUnreferencedCodeMessage)]
public static IMcpServerBuilder WithToolsFromAssembly(this IMcpServerBuilder builder, Assembly? toolAssembly = null, JsonSerializerOptions? serializerOptions = null)
public static IMcpServerBuilder WithToolsFromAssembly(this IMcpServerBuilder builder, Assembly? toolAssembly = null, JsonSerializerOptions? serializerOptions = null, AIJsonSchemaCreateOptions? schemaCreateOptions = null)
{
Throw.IfNull(builder);

Expand All @@ -196,7 +199,8 @@ public static IMcpServerBuilder WithToolsFromAssembly(this IMcpServerBuilder bui
from t in toolAssembly.GetTypes()
where t.GetCustomAttribute<McpServerToolTypeAttribute>() is not null
select t,
serializerOptions);
serializerOptions,
schemaCreateOptions);
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public void WithTools_InvalidArgs_Throws()
IMcpServerBuilder builder = new ServiceCollection().AddMcpServer();

Assert.Throws<ArgumentNullException>("tools", () => builder.WithTools((IEnumerable<McpServerTool>)null!));
Assert.Throws<ArgumentNullException>("toolTypes", () => builder.WithTools((IEnumerable<Type>)null!));
Assert.Throws<ArgumentNullException>("toolTypes", () => builder.WithTools(toolTypes: (IEnumerable<Type>)null!));
Assert.Throws<ArgumentNullException>("target", () => builder.WithTools<object>(target: null!));

IMcpServerBuilder nullBuilder = null!;
Expand Down Expand Up @@ -664,6 +664,57 @@ public void Register_Tools_From_Multiple_Sources()
Assert.Contains(services.GetServices<McpServerTool>(), t => t.ProtocolTool.Name == "method_d");
Assert.Contains(services.GetServices<McpServerTool>(), t => t.ProtocolTool.Name == "Returns42");
}

[Fact]
public void Register_Static_Tools_With_Custom_Schema_Create_Options()
{
var jsonString = "{\"value\":42}";
var schemaCreateOptions = new AIJsonSchemaCreateOptions
{
TransformSchemaNode = (context, node) => JsonNode.Parse(jsonString)!
};

ServiceCollection sc = new();
sc.AddMcpServer().WithTools([typeof(ToolTypeWithSchemaCreateOptions)], schemaCreateOptions: schemaCreateOptions);
IServiceProvider services = sc.BuildServiceProvider();

McpServerTool tool = services.GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "static_tool");
Assert.Contains("42", tool.ProtocolTool.InputSchema.GetProperty("properties").GetProperty("input").GetProperty("value").ToString());
}

[Fact]
public void Register_Instance_Tools_With_Custom_Schema_Create_Options()
{
var jsonString = "{\"value\":42}";
var schemaCreateOptions = new AIJsonSchemaCreateOptions
{
TransformSchemaNode = (context, node) => JsonNode.Parse(jsonString)!
};

ServiceCollection sc = new();
sc.AddMcpServer().WithTools([typeof(ToolTypeWithSchemaCreateOptions)], schemaCreateOptions: schemaCreateOptions);
IServiceProvider services = sc.BuildServiceProvider();

McpServerTool tool = services.GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "instance_tool");
Assert.Contains("42", tool.ProtocolTool.InputSchema.GetProperty("properties").GetProperty("input").GetProperty("value").ToString());
}

[Fact]
public void WithToolsFromAssembly_With_Custom_Schema_Create_Options()
{
var jsonString = "{\"value\":42}";
var schemaCreateOptions = new AIJsonSchemaCreateOptions
{
TransformSchemaNode = (context, node) => JsonNode.Parse(jsonString)!
};

ServiceCollection sc = new();
sc.AddMcpServer().WithToolsFromAssembly(schemaCreateOptions: schemaCreateOptions);
IServiceProvider services = sc.BuildServiceProvider();

McpServerTool tool = services.GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "instance_tool");
Assert.Contains("42", tool.ProtocolTool.InputSchema.GetProperty("properties").GetProperty("input").GetProperty("value").ToString());
}

[Fact]
public void Create_ExtractsToolAnnotations_AllSet()
Expand Down Expand Up @@ -951,6 +1002,17 @@ public class ComplexObject
public string? Name { get; set; }
public int Age { get; set; }
}

[McpServerToolType]
internal class ToolTypeWithSchemaCreateOptions
{
[McpServerTool]
public static string StaticTool(string input) => input;

[McpServerTool]
public string InstanceTool(string input) => input;
}


[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(int))]
Expand Down