-
Notifications
You must be signed in to change notification settings - Fork 545
Add McpMetaAttribute for attaching metadata to tools, prompts, and resources #857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,734
−3
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d003653
Initial plan
Copilot 034bd50
Add McpMetaAttribute and integrate with tools, prompts, and resources
Copilot 47e68b4
Add tests and sample usage for McpMetaAttribute
Copilot cf94e1b
Add Meta property to options classes and update logic to merge with a…
Copilot 8dc4a0a
Fix missing using directives and clarify McpMetaAttribute documentation
Copilot 62cfd2d
Make Name and Value constructor parameters in McpMetaAttribute and ch…
Copilot ce33408
Change Value type to object and use JsonSerializer.SerializeToNode, f…
Copilot 540eea4
Fix UnderlyingMethod property path, use SerializerOptions, and fix Js…
Copilot e9ef922
Simplify MethodInfo resolution to use function.UnderlyingMethod directly
Copilot 5ac543a
Fix IL2026 trimming warning by providing type info to JsonSerializer.…
Copilot c734f23
Fix remaining issues
stephentoub 8b801ed
Revert to string-based JSON values for McpMetaAttribute with StringSy…
Copilot d2a3444
Add StringSyntax attribute to Value property and fix sample code to u…
Copilot 3c597d6
Fix tests and a bit of cleanup
stephentoub 9e8de5c
Change approach and add lots of tests
stephentoub 4ce9664
Use JsonContext for serialization and fix floating point test comparison
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/ModelContextProtocol.Core/Server/McpMetaAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using ModelContextProtocol.Protocol; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
namespace ModelContextProtocol.Server; | ||
|
||
/// <summary> | ||
/// Used to specify metadata for an MCP server primitive (tool, prompt, or resource). | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// The metadata is used to populate the <see cref="Tool.Meta"/>, <see cref="Prompt.Meta"/>, | ||
/// or <see cref="Resource.Meta"/> property of the corresponding primitive. | ||
/// </para> | ||
/// <para> | ||
/// This attribute can be applied multiple times to a method to specify multiple key/value pairs | ||
/// of metadata. However, the same key should not be used more than once; doing so will result | ||
/// in undefined behavior. | ||
/// </para> | ||
/// <para> | ||
/// Metadata can be used to attach additional information to primitives, such as model preferences, | ||
/// version information, or other custom data that should be communicated to MCP clients. | ||
/// </para> | ||
/// </remarks> | ||
/// <example> | ||
/// <code> | ||
/// [McpServerTool] | ||
/// [McpMeta("model", "gpt-4o")] | ||
/// [McpMeta("version", "1.0")] | ||
/// [McpMeta("priority", 5.0)] | ||
/// [McpMeta("isBeta", true)] | ||
/// [McpMeta("tags", JsonValue = """["a","b"]""")] | ||
/// public string MyTool(string input) => $"Processed: {input}"; | ||
/// </code> | ||
/// </example> | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
public sealed class McpMetaAttribute : Attribute | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="McpMetaAttribute"/> class with a string value. | ||
/// </summary> | ||
/// <param name="name">The name (key) of the metadata entry.</param> | ||
/// <param name="value">The string value of the metadata entry. If null, the value will be serialized as JSON null.</param> | ||
public McpMetaAttribute(string name, string? value = null) | ||
{ | ||
Name = name; | ||
JsonValue = value is null ? "null" : JsonSerializer.Serialize(value, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(string))); | ||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="McpMetaAttribute"/> class with a double value. | ||
/// </summary> | ||
/// <param name="name">The name (key) of the metadata entry.</param> | ||
/// <param name="value">The double value of the metadata entry.</param> | ||
public McpMetaAttribute(string name, double value) | ||
{ | ||
Name = name; | ||
JsonValue = JsonSerializer.Serialize(value, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(double))); | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="McpMetaAttribute"/> class with a boolean value. | ||
/// </summary> | ||
/// <param name="name">The name (key) of the metadata entry.</param> | ||
/// <param name="value">The boolean value of the metadata entry.</param> | ||
public McpMetaAttribute(string name, bool value) | ||
{ | ||
Name = name; | ||
JsonValue = value ? "true" : "false"; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name (key) of the metadata entry. | ||
/// </summary> | ||
/// <remarks> | ||
/// This value is used as the key in the metadata object. It should be a unique identifier | ||
/// for this piece of metadata within the context of the primitive. | ||
/// </remarks> | ||
public string Name { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets the value of the metadata entry as a JSON string. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// This value must be well-formed JSON. It will be parsed and added to the metadata <see cref="JsonObject"/>. | ||
/// Simple values can be represented as JSON literals like <c>"\"my-string\""</c>, <c>"123"</c>, | ||
/// <c>"true"</c>, etc. Complex structures can be represented as JSON objects or arrays. | ||
/// </para> | ||
/// <para> | ||
/// Setting this property will override any value provided via the constructor. | ||
/// </para> | ||
/// <para> | ||
/// For programmatic scenarios where you want to construct complex metadata without dealing with | ||
/// JSON strings, use the <see cref="McpServerToolCreateOptions.Meta"/>, | ||
/// <see cref="McpServerPromptCreateOptions.Meta"/>, or <see cref="McpServerResourceCreateOptions.Meta"/> | ||
/// property to provide a JsonObject directly. | ||
/// </para> | ||
/// </remarks> | ||
[StringSyntax(StringSyntaxAttribute.Json)] | ||
public string JsonValue { get; set; } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.