Skip to content

Commit 11dfada

Browse files
Copilotstephentoub
andcommitted
Fix UnderlyingMethod property path, use SerializerOptions, and fix JsonObject cref
Co-authored-by: stephentoub <[email protected]>
1 parent 2754d70 commit 11dfada

File tree

6 files changed

+15
-14
lines changed

6 files changed

+15
-14
lines changed

src/ModelContextProtocol.Core/Server/AIFunctionMcpServerPrompt.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
140140
};
141141

142142
// Populate Meta from options and/or McpMetaAttribute instances if a MethodInfo is available
143-
MethodInfo? method = options?.Metadata?.FirstOrDefault(m => m is MethodInfo) as MethodInfo ?? function.Metadata?.UnderlyingMethod;
143+
MethodInfo? method = options?.Metadata?.FirstOrDefault(m => m is MethodInfo) as MethodInfo ?? function.UnderlyingMethod;
144144
if (method is not null)
145145
{
146-
prompt.Meta = AIFunctionMcpServerTool.CreateMetaFromAttributes(method, options?.Meta);
146+
prompt.Meta = AIFunctionMcpServerTool.CreateMetaFromAttributes(method, options?.Meta, options?.SerializerOptions);
147147
}
148148
else if (options?.Meta is not null)
149149
{

src/ModelContextProtocol.Core/Server/AIFunctionMcpServerResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
213213
string name = options?.Name ?? function.Name;
214214

215215
// Populate Meta from options and/or McpMetaAttribute instances if a MethodInfo is available
216-
MethodInfo? method = options?.Metadata?.FirstOrDefault(m => m is MethodInfo) as MethodInfo ?? function.Metadata?.UnderlyingMethod;
216+
MethodInfo? method = options?.Metadata?.FirstOrDefault(m => m is MethodInfo) as MethodInfo ?? function.UnderlyingMethod;
217217
JsonObject? meta = null;
218218
if (method is not null)
219219
{
220-
meta = AIFunctionMcpServerTool.CreateMetaFromAttributes(method, options?.Meta);
220+
meta = AIFunctionMcpServerTool.CreateMetaFromAttributes(method, options?.Meta, options?.SerializerOptions);
221221
}
222222
else if (options?.Meta is not null)
223223
{

src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ options.OpenWorld is not null ||
145145
}
146146

147147
// Populate Meta from options and/or McpMetaAttribute instances if a MethodInfo is available
148-
MethodInfo? method = options.Metadata?.FirstOrDefault(m => m is MethodInfo) as MethodInfo ?? function.Metadata?.UnderlyingMethod;
148+
MethodInfo? method = options.Metadata?.FirstOrDefault(m => m is MethodInfo) as MethodInfo ?? function.UnderlyingMethod;
149149
if (method is not null)
150150
{
151-
tool.Meta = CreateMetaFromAttributes(method, options.Meta);
151+
tool.Meta = CreateMetaFromAttributes(method, options.Meta, options.SerializerOptions);
152152
}
153153
else if (options.Meta is not null)
154154
{
@@ -365,8 +365,9 @@ internal static IReadOnlyList<object> CreateMetadata(MethodInfo method)
365365
/// <summary>Creates a Meta JsonObject from McpMetaAttribute instances on the specified method.</summary>
366366
/// <param name="method">The method to extract McpMetaAttribute instances from.</param>
367367
/// <param name="seedMeta">Optional JsonObject to seed the Meta with. Properties from this object take precedence over attributes.</param>
368+
/// <param name="serializerOptions">Optional JsonSerializerOptions to use for serialization. Defaults to McpJsonUtilities.DefaultOptions if not provided.</param>
368369
/// <returns>A JsonObject with metadata, or null if no metadata is present.</returns>
369-
internal static JsonObject? CreateMetaFromAttributes(MethodInfo method, JsonObject? seedMeta = null)
370+
internal static JsonObject? CreateMetaFromAttributes(MethodInfo method, JsonObject? seedMeta = null, JsonSerializerOptions? serializerOptions = null)
370371
{
371372
// Get all McpMetaAttribute instances from the method
372373
var metaAttributes = method.GetCustomAttributes<McpMetaAttribute>();
@@ -378,7 +379,7 @@ internal static IReadOnlyList<object> CreateMetadata(MethodInfo method)
378379
// Only add the attribute property if it doesn't already exist in the seed
379380
if (!meta.ContainsKey(attr.Name))
380381
{
381-
meta[attr.Name] = JsonSerializer.SerializeToNode(attr.Value);
382+
meta[attr.Name] = JsonSerializer.SerializeToNode(attr.Value, serializerOptions ?? McpJsonUtilities.DefaultOptions);
382383
}
383384
}
384385

src/ModelContextProtocol.Core/Server/McpServerPromptCreateOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public sealed class McpServerPromptCreateOptions
9191
/// </summary>
9292
/// <remarks>
9393
/// <para>
94-
/// This JsonObject is used to seed the <see cref="Prompt.Meta"/> property. Any metadata from
94+
/// This <see cref="System.Text.Json.Nodes.JsonObject"/> is used to seed the <see cref="Prompt.Meta"/> property. Any metadata from
9595
/// <see cref="McpMetaAttribute"/> instances on the method will be added to this object, but
96-
/// properties already present in this JsonObject will not be overwritten.
96+
/// properties already present in this <see cref="System.Text.Json.Nodes.JsonObject"/> will not be overwritten.
9797
/// </para>
9898
/// <para>
9999
/// Implementations must not make assumptions about its contents.

src/ModelContextProtocol.Core/Server/McpServerResourceCreateOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ public sealed class McpServerResourceCreateOptions
106106
/// </summary>
107107
/// <remarks>
108108
/// <para>
109-
/// This JsonObject is used to seed the <see cref="Resource.Meta"/> property. Any metadata from
109+
/// This <see cref="System.Text.Json.Nodes.JsonObject"/> is used to seed the <see cref="Resource.Meta"/> property. Any metadata from
110110
/// <see cref="McpMetaAttribute"/> instances on the method will be added to this object, but
111-
/// properties already present in this JsonObject will not be overwritten.
111+
/// properties already present in this <see cref="System.Text.Json.Nodes.JsonObject"/> will not be overwritten.
112112
/// </para>
113113
/// <para>
114114
/// Implementations must not make assumptions about its contents.

src/ModelContextProtocol.Core/Server/McpServerToolCreateOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ public sealed class McpServerToolCreateOptions
177177
/// </summary>
178178
/// <remarks>
179179
/// <para>
180-
/// This JsonObject is used to seed the <see cref="Tool.Meta"/> property. Any metadata from
180+
/// This <see cref="System.Text.Json.Nodes.JsonObject"/> is used to seed the <see cref="Tool.Meta"/> property. Any metadata from
181181
/// <see cref="McpMetaAttribute"/> instances on the method will be added to this object, but
182-
/// properties already present in this JsonObject will not be overwritten.
182+
/// properties already present in this <see cref="System.Text.Json.Nodes.JsonObject"/> will not be overwritten.
183183
/// </para>
184184
/// <para>
185185
/// Implementations must not make assumptions about its contents.

0 commit comments

Comments
 (0)