Skip to content

Commit 6fa39b1

Browse files
committed
Fix tests and a bit of cleanup
1 parent 468c7c7 commit 6fa39b1

File tree

9 files changed

+34
-27
lines changed

9 files changed

+34
-27
lines changed

src/ModelContextProtocol.Core/Protocol/ClientCapabilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public sealed class ClientCapabilities
4747
/// </para>
4848
/// <para>
4949
/// The server can use <see cref="McpServer.RequestRootsAsync"/> to request the list of
50-
/// available roots from the client, which will trigger the client's <see cref="ModelContextProtocol.Client.McpClientHandlers.RootsHandler"/>.
50+
/// available roots from the client, which will trigger the client's <see cref="McpClientHandlers.RootsHandler"/>.
5151
/// </para>
5252
/// </remarks>
5353
[JsonPropertyName("roots")]

src/ModelContextProtocol.Core/Protocol/ElicitationCapability.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace ModelContextProtocol.Protocol;
1313
/// </para>
1414
/// <para>
1515
/// When this capability is enabled, an MCP server can request the client to provide additional information
16-
/// during interactions. The client must set a <see cref="ModelContextProtocol.Client.McpClientHandlers.ElicitationHandler"/> to process these requests.
16+
/// during interactions. The client must set a <see cref="McpClientHandlers.ElicitationHandler"/> to process these requests.
1717
/// </para>
1818
/// <para>
1919
/// This class is intentionally empty as the Model Context Protocol specification does not

src/ModelContextProtocol.Core/Protocol/SamplingCapability.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace ModelContextProtocol.Protocol;
1414
/// </para>
1515
/// <para>
1616
/// When this capability is enabled, an MCP server can request the client to generate content
17-
/// using an AI model. The client must set a <see cref="ModelContextProtocol.Client.McpClientHandlers.SamplingHandler"/> to process these requests.
17+
/// using an AI model. The client must set a <see cref="McpClientHandlers.SamplingHandler"/> to process these requests.
1818
/// </para>
1919
/// <para>
2020
/// This class is intentionally empty as the Model Context Protocol specification does not

src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,12 @@ internal static IReadOnlyList<object> CreateMetadata(MethodInfo method)
364364
/// <returns>A <see cref="JsonObject"/> with metadata, or null if no metadata is present.</returns>
365365
internal static JsonObject? CreateMetaFromAttributes(MethodInfo method, JsonObject? meta = null, JsonSerializerOptions? serializerOptions = null)
366366
{
367-
// Get all McpMetaAttribute instances from the method.
368-
var metaAttributes = method.GetCustomAttributes<McpMetaAttribute>();
369-
370-
foreach (var attr in metaAttributes)
367+
// Transfer all McpMetaAttribute instances to the Meta JsonObject, ignoring any that would overwrite existing properties.
368+
foreach (var attr in method.GetCustomAttributes<McpMetaAttribute>())
371369
{
372-
meta ??= [];
373-
if (!meta.ContainsKey(attr.Name))
370+
if (meta?.ContainsKey(attr.Name) is not true)
374371
{
375-
// Parse the JSON string value into a JsonNode
376-
meta[attr.Name] = JsonNode.Parse(attr.Value);
372+
(meta ??= [])[attr.Name] = JsonNode.Parse(attr.Value);
377373
}
378374
}
379375

src/ModelContextProtocol.Core/Server/McpMetaAttribute.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ namespace ModelContextProtocol.Server;
88
/// </summary>
99
/// <remarks>
1010
/// <para>
11-
/// This attribute can be applied multiple times to a method to specify multiple key/value pairs
12-
/// of metadata. The metadata is used to populate the <see cref="Tool.Meta"/>, <see cref="Prompt.Meta"/>,
11+
/// The metadata is used to populate the <see cref="Tool.Meta"/>, <see cref="Prompt.Meta"/>,
1312
/// or <see cref="Resource.Meta"/> property of the corresponding primitive.
1413
/// </para>
1514
/// <para>
15+
/// This attribute can be applied multiple times to a method to specify multiple key/value pairs
16+
/// of metadata. However, the same key should not be used more than once; doing so will result
17+
/// in undefined behavior.
18+
/// </para>
19+
/// <para>
1620
/// Metadata can be used to attach additional information to primitives, such as model preferences,
1721
/// version information, or other custom data that should be communicated to MCP clients.
1822
/// </para>
@@ -22,10 +26,7 @@ namespace ModelContextProtocol.Server;
2226
/// [McpMeta("model", "\"gpt-4o\"")]
2327
/// [McpMeta("version", "\"1.0\"")]
2428
/// [McpMeta("priority", "5")]
25-
/// public string MyTool(string input)
26-
/// {
27-
/// return $"Processed: {input}";
28-
/// }
29+
/// public string MyTool(string input) => $"Processed: {input}";
2930
/// </code>
3031
/// </example>
3132
/// </remarks>

src/ModelContextProtocol.Core/Server/McpServerPromptCreateOptions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using ModelContextProtocol.Protocol;
33
using System.ComponentModel;
44
using System.Text.Json;
5+
using System.Text.Json.Nodes;
56

67
namespace ModelContextProtocol.Server;
78

@@ -91,15 +92,15 @@ public sealed class McpServerPromptCreateOptions
9192
/// </summary>
9293
/// <remarks>
9394
/// <para>
94-
/// This <see cref="System.Text.Json.Nodes.JsonObject"/> is used to seed the <see cref="Prompt.Meta"/> property. Any metadata from
95+
/// This <see cref="JsonObject"/> is used to seed the <see cref="Prompt.Meta"/> property. Any metadata from
9596
/// <see cref="McpMetaAttribute"/> instances on the method will be added to this object, but
96-
/// properties already present in this <see cref="System.Text.Json.Nodes.JsonObject"/> will not be overwritten.
97+
/// properties already present in this <see cref="JsonObject"/> will not be overwritten.
9798
/// </para>
9899
/// <para>
99100
/// Implementations must not make assumptions about its contents.
100101
/// </para>
101102
/// </remarks>
102-
public System.Text.Json.Nodes.JsonObject? Meta { get; set; }
103+
public JsonObject? Meta { get; set; }
103104

104105
/// <summary>
105106
/// Creates a shallow clone of the current <see cref="McpServerPromptCreateOptions"/> instance.

src/ModelContextProtocol.Core/Server/McpServerResourceCreateOptions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using ModelContextProtocol.Protocol;
33
using System.ComponentModel;
44
using System.Text.Json;
5+
using System.Text.Json.Nodes;
56

67
namespace ModelContextProtocol.Server;
78

@@ -106,15 +107,15 @@ public sealed class McpServerResourceCreateOptions
106107
/// </summary>
107108
/// <remarks>
108109
/// <para>
109-
/// This <see cref="System.Text.Json.Nodes.JsonObject"/> is used to seed the <see cref="Resource.Meta"/> property. Any metadata from
110+
/// This <see cref="JsonObject"/> is used to seed the <see cref="Resource.Meta"/> property. Any metadata from
110111
/// <see cref="McpMetaAttribute"/> instances on the method will be added to this object, but
111-
/// properties already present in this <see cref="System.Text.Json.Nodes.JsonObject"/> will not be overwritten.
112+
/// properties already present in this <see cref="JsonObject"/> will not be overwritten.
112113
/// </para>
113114
/// <para>
114115
/// Implementations must not make assumptions about its contents.
115116
/// </para>
116117
/// </remarks>
117-
public System.Text.Json.Nodes.JsonObject? Meta { get; set; }
118+
public JsonObject? Meta { get; set; }
118119

119120
/// <summary>
120121
/// Creates a shallow clone of the current <see cref="McpServerResourceCreateOptions"/> instance.

src/ModelContextProtocol.Core/Server/McpServerToolCreateOptions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using ModelContextProtocol.Protocol;
33
using System.ComponentModel;
44
using System.Text.Json;
5+
using System.Text.Json.Nodes;
56

67
namespace ModelContextProtocol.Server;
78

@@ -177,15 +178,15 @@ public sealed class McpServerToolCreateOptions
177178
/// </summary>
178179
/// <remarks>
179180
/// <para>
180-
/// This <see cref="System.Text.Json.Nodes.JsonObject"/> is used to seed the <see cref="Tool.Meta"/> property. Any metadata from
181+
/// This <see cref="JsonObject"/> is used to seed the <see cref="Tool.Meta"/> property. Any metadata from
181182
/// <see cref="McpMetaAttribute"/> instances on the method will be added to this object, but
182-
/// properties already present in this <see cref="System.Text.Json.Nodes.JsonObject"/> will not be overwritten.
183+
/// properties already present in this <see cref="JsonObject"/> will not be overwritten.
183184
/// </para>
184185
/// <para>
185186
/// Implementations must not make assumptions about its contents.
186187
/// </para>
187188
/// </remarks>
188-
public System.Text.Json.Nodes.JsonObject? Meta { get; set; }
189+
public JsonObject? Meta { get; set; }
189190

190191
/// <summary>
191192
/// Creates a shallow clone of the current <see cref="McpServerToolCreateOptions"/> instance.

tests/ModelContextProtocol.Tests/Server/McpMetaAttributeTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ModelContextProtocol.Server;
2+
using System.Text.Json;
23
using System.Text.Json.Nodes;
34

45
namespace ModelContextProtocol.Tests.Server;
@@ -193,9 +194,15 @@ public void McpMetaAttribute_NonStringValues_Serialized()
193194
var method = typeof(TestToolNonStringMetaClass).GetMethod(nameof(TestToolNonStringMetaClass.ToolWithNonStringMeta))!;
194195
var tool = McpServerTool.Create(method, target: null);
195196
Assert.NotNull(tool.ProtocolTool.Meta);
197+
196198
Assert.Equal("42", tool.ProtocolTool.Meta["intValue"]?.ToString());
197-
Assert.Equal("True", tool.ProtocolTool.Meta["boolValue"]?.ToString());
199+
Assert.Equal(JsonValueKind.Number, tool.ProtocolTool.Meta["intValue"]?.GetValueKind());
200+
201+
Assert.Equal("true", tool.ProtocolTool.Meta["boolValue"]?.ToString());
202+
Assert.Equal(JsonValueKind.True, tool.ProtocolTool.Meta["boolValue"]?.GetValueKind());
203+
198204
Assert.Equal("1", tool.ProtocolTool.Meta["enumValue"]?.ToString());
205+
Assert.Equal(JsonValueKind.Number, tool.ProtocolTool.Meta["enumValue"]?.GetValueKind());
199206
}
200207

201208
[Fact]

0 commit comments

Comments
 (0)