Skip to content

Commit 2754d70

Browse files
Copilotstephentoub
andcommitted
Change Value type to object and use JsonSerializer.SerializeToNode, fix nullable warnings
Co-authored-by: stephentoub <[email protected]>
1 parent 3ab5541 commit 2754d70

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ internal static IReadOnlyList<object> CreateMetadata(MethodInfo method)
378378
// Only add the attribute property if it doesn't already exist in the seed
379379
if (!meta.ContainsKey(attr.Name))
380380
{
381-
meta[attr.Name] = attr.Value;
381+
meta[attr.Name] = JsonSerializer.SerializeToNode(attr.Value);
382382
}
383383
}
384384

src/ModelContextProtocol.Core/Server/McpMetaAttribute.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public sealed class McpMetaAttribute : Attribute
3434
/// Initializes a new instance of the <see cref="McpMetaAttribute"/> class.
3535
/// </summary>
3636
/// <param name="name">The name (key) of the metadata entry.</param>
37-
/// <param name="value">The value of the metadata entry.</param>
38-
public McpMetaAttribute(string name, string value)
37+
/// <param name="value">The value of the metadata entry. This can be any value that can be encoded in .NET metadata.</param>
38+
public McpMetaAttribute(string name, object? value)
3939
{
4040
Name = name;
4141
Value = value;
@@ -54,9 +54,16 @@ public McpMetaAttribute(string name, string value)
5454
/// Gets the value of the metadata entry.
5555
/// </summary>
5656
/// <remarks>
57-
/// This value is stored as a string in the metadata object. For complex values, use the
57+
/// <para>
58+
/// This value can be any object that can be encoded in .NET metadata (strings, numbers, booleans, etc.).
59+
/// The value will be serialized to JSON using <see cref="System.Text.Json.JsonSerializer"/> when
60+
/// populating the metadata JsonObject.
61+
/// </para>
62+
/// <para>
63+
/// For complex JSON structures that cannot be represented as .NET metadata, use the
5864
/// <see cref="McpServerToolCreateOptions.Meta"/>, <see cref="McpServerPromptCreateOptions.Meta"/>,
5965
/// or <see cref="McpServerResourceCreateOptions.Meta"/> property to provide a JsonObject directly.
66+
/// </para>
6067
/// </remarks>
61-
public string Value { get; }
68+
public object? Value { get; }
6269
}

tests/ModelContextProtocol.Tests/Server/McpMetaAttributeTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void McpMetaAttribute_OnTool_PopulatesMeta()
1414
var method = typeof(TestToolClass).GetMethod(nameof(TestToolClass.ToolWithMeta))!;
1515

1616
// Act
17-
var tool = McpServerTool.Create(method, null);
17+
var tool = McpServerTool.Create(method, target: null);
1818

1919
// Assert
2020
Assert.NotNull(tool.ProtocolTool.Meta);
@@ -29,7 +29,7 @@ public void McpMetaAttribute_OnPrompt_PopulatesMeta()
2929
var method = typeof(TestPromptClass).GetMethod(nameof(TestPromptClass.PromptWithMeta))!;
3030

3131
// Act
32-
var prompt = McpServerPrompt.Create(method, null);
32+
var prompt = McpServerPrompt.Create(method, target: null);
3333

3434
// Assert
3535
Assert.NotNull(prompt.ProtocolPrompt.Meta);
@@ -44,7 +44,7 @@ public void McpMetaAttribute_OnResource_PopulatesMeta()
4444
var method = typeof(TestResourceClass).GetMethod(nameof(TestResourceClass.ResourceWithMeta))!;
4545

4646
// Act
47-
var resource = McpServerResource.Create(method, null);
47+
var resource = McpServerResource.Create(method, target: null);
4848

4949
// Assert
5050
Assert.NotNull(resource.ProtocolResource.Meta);
@@ -59,7 +59,7 @@ public void McpMetaAttribute_WithoutAttributes_ReturnsNull()
5959
var method = typeof(TestToolClass).GetMethod(nameof(TestToolClass.ToolWithoutMeta))!;
6060

6161
// Act
62-
var tool = McpServerTool.Create(method, null);
62+
var tool = McpServerTool.Create(method, target: null);
6363

6464
// Assert
6565
Assert.Null(tool.ProtocolTool.Meta);
@@ -72,7 +72,7 @@ public void McpMetaAttribute_SingleAttribute_PopulatesMeta()
7272
var method = typeof(TestToolClass).GetMethod(nameof(TestToolClass.ToolWithSingleMeta))!;
7373

7474
// Act
75-
var tool = McpServerTool.Create(method, null);
75+
var tool = McpServerTool.Create(method, target: null);
7676

7777
// Assert
7878
Assert.NotNull(tool.ProtocolTool.Meta);
@@ -93,7 +93,7 @@ public void McpMetaAttribute_OptionsMetaTakesPrecedence()
9393
var options = new McpServerToolCreateOptions { Meta = seedMeta };
9494

9595
// Act
96-
var tool = McpServerTool.Create(method, options);
96+
var tool = McpServerTool.Create(method, target: null, options: options);
9797

9898
// Assert
9999
Assert.NotNull(tool.ProtocolTool.Meta);
@@ -117,7 +117,7 @@ public void McpMetaAttribute_OptionsMetaOnly_NoAttributes()
117117
var options = new McpServerToolCreateOptions { Meta = seedMeta };
118118

119119
// Act
120-
var tool = McpServerTool.Create(method, options);
120+
var tool = McpServerTool.Create(method, target: null, options: options);
121121

122122
// Assert
123123
Assert.NotNull(tool.ProtocolTool.Meta);
@@ -138,7 +138,7 @@ public void McpMetaAttribute_PromptOptionsMetaTakesPrecedence()
138138
var options = new McpServerPromptCreateOptions { Meta = seedMeta };
139139

140140
// Act
141-
var prompt = McpServerPrompt.Create(method, options);
141+
var prompt = McpServerPrompt.Create(method, target: null, options: options);
142142

143143
// Assert
144144
Assert.NotNull(prompt.ProtocolPrompt.Meta);
@@ -163,7 +163,7 @@ public void McpMetaAttribute_ResourceOptionsMetaTakesPrecedence()
163163
var options = new McpServerResourceCreateOptions { Meta = seedMeta };
164164

165165
// Act
166-
var resource = McpServerResource.Create(method, options);
166+
var resource = McpServerResource.Create(method, target: null, options: options);
167167

168168
// Assert
169169
Assert.NotNull(resource.ProtocolResource.Meta);

0 commit comments

Comments
 (0)