8
8
using ModelContextProtocol . Utils ;
9
9
using System . Diagnostics . CodeAnalysis ;
10
10
using System . Reflection ;
11
+ using System . Text . Json ;
11
12
12
13
namespace Microsoft . Extensions . DependencyInjection ;
13
14
@@ -24,6 +25,7 @@ public static partial class McpServerBuilderExtensions
24
25
/// <summary>Adds <see cref="McpServerTool"/> instances to the service collection backing <paramref name="builder"/>.</summary>
25
26
/// <typeparam name="TToolType">The tool type.</typeparam>
26
27
/// <param name="builder">The builder instance.</param>
28
+ /// <param name="serializerOptions">The serializer options governing tool parameter marshalling.</param>
27
29
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
28
30
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
29
31
/// <remarks>
@@ -35,7 +37,8 @@ public static partial class McpServerBuilderExtensions
35
37
DynamicallyAccessedMemberTypes . PublicMethods |
36
38
DynamicallyAccessedMemberTypes . NonPublicMethods |
37
39
DynamicallyAccessedMemberTypes . PublicConstructors ) ] TToolType > (
38
- this IMcpServerBuilder builder )
40
+ this IMcpServerBuilder builder ,
41
+ JsonSerializerOptions ? serializerOptions = null )
39
42
{
40
43
Throw . IfNull ( builder ) ;
41
44
@@ -44,8 +47,8 @@ public static partial class McpServerBuilderExtensions
44
47
if ( toolMethod . GetCustomAttribute < McpServerToolAttribute > ( ) is not null )
45
48
{
46
49
builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerTool > ) ( toolMethod . IsStatic ?
47
- services => McpServerTool . Create ( toolMethod , options : new ( ) { Services = services } ) :
48
- services => McpServerTool . Create ( toolMethod , typeof ( TToolType ) , new ( ) { Services = services } ) ) ) ;
50
+ services => McpServerTool . Create ( toolMethod , options : new ( ) { Services = services , SerializerOptions = serializerOptions } ) :
51
+ services => McpServerTool . Create ( toolMethod , typeof ( TToolType ) , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
49
52
}
50
53
}
51
54
@@ -55,6 +58,7 @@ public static partial class McpServerBuilderExtensions
55
58
/// <summary>Adds <see cref="McpServerTool"/> instances to the service collection backing <paramref name="builder"/>.</summary>
56
59
/// <param name="builder">The builder instance.</param>
57
60
/// <param name="toolTypes">Types with marked methods to add as tools to the server.</param>
61
+ /// <param name="serializerOptions">The serializer options governing tool parameter marshalling.</param>
58
62
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
59
63
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
60
64
/// <exception cref="ArgumentNullException"><paramref name="toolTypes"/> is <see langword="null"/>.</exception>
@@ -64,7 +68,7 @@ public static partial class McpServerBuilderExtensions
64
68
/// instance for each. For instance methods, an instance will be constructed for each invocation of the tool.
65
69
/// </remarks>
66
70
[ RequiresUnreferencedCode ( WithToolsRequiresUnreferencedCodeMessage ) ]
67
- public static IMcpServerBuilder WithTools ( this IMcpServerBuilder builder , params IEnumerable < Type > toolTypes )
71
+ public static IMcpServerBuilder WithTools ( this IMcpServerBuilder builder , IEnumerable < Type > toolTypes , JsonSerializerOptions ? serializerOptions = null )
68
72
{
69
73
Throw . IfNull ( builder ) ;
70
74
Throw . IfNull ( toolTypes ) ;
@@ -78,8 +82,8 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, params
78
82
if ( toolMethod . GetCustomAttribute < McpServerToolAttribute > ( ) is not null )
79
83
{
80
84
builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerTool > ) ( toolMethod . IsStatic ?
81
- services => McpServerTool . Create ( toolMethod , options : new ( ) { Services = services } ) :
82
- services => McpServerTool . Create ( toolMethod , toolType , new ( ) { Services = services } ) ) ) ;
85
+ services => McpServerTool . Create ( toolMethod , options : new ( ) { Services = services , SerializerOptions = serializerOptions } ) :
86
+ services => McpServerTool . Create ( toolMethod , toolType , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
83
87
}
84
88
}
85
89
}
@@ -92,6 +96,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, params
92
96
/// Adds types marked with the <see cref="McpServerToolTypeAttribute"/> attribute from the given assembly as tools to the server.
93
97
/// </summary>
94
98
/// <param name="builder">The builder instance.</param>
99
+ /// <param name="serializerOptions">The serializer options governing tool parameter marshalling.</param>
95
100
/// <param name="toolAssembly">The assembly to load the types from. If <see langword="null"/>, the calling assembly will be used.</param>
96
101
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
97
102
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
@@ -116,7 +121,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, params
116
121
/// </para>
117
122
/// </remarks>
118
123
[ RequiresUnreferencedCode ( WithToolsRequiresUnreferencedCodeMessage ) ]
119
- public static IMcpServerBuilder WithToolsFromAssembly ( this IMcpServerBuilder builder , Assembly ? toolAssembly = null )
124
+ public static IMcpServerBuilder WithToolsFromAssembly ( this IMcpServerBuilder builder , Assembly ? toolAssembly = null , JsonSerializerOptions ? serializerOptions = null )
120
125
{
121
126
Throw . IfNull ( builder ) ;
122
127
@@ -125,7 +130,8 @@ public static IMcpServerBuilder WithToolsFromAssembly(this IMcpServerBuilder bui
125
130
return builder . WithTools (
126
131
from t in toolAssembly . GetTypes ( )
127
132
where t . GetCustomAttribute < McpServerToolTypeAttribute > ( ) is not null
128
- select t ) ;
133
+ select t ,
134
+ serializerOptions ) ;
129
135
}
130
136
#endregion
131
137
0 commit comments