Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions samples/AspNetCoreMcpServer/Tools/WeatherTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public WeatherTools(IHttpClientFactory httpClientFactory)
}

[McpServerTool, Description("Get weather alerts for a US state.")]
[McpMeta("category", "weather")]
[McpMeta("dataSource", "weather.gov")]
public async Task<string> GetAlerts(
[Description("The US state to get alerts for. Use the 2 letter abbreviation for the state (e.g. NY).")] string state)
{
Expand Down Expand Up @@ -46,6 +48,8 @@ public async Task<string> GetAlerts(
}

[McpServerTool, Description("Get weather forecast for a location.")]
[McpMeta("category", "weather")]
[McpMeta("recommendedModel", "gpt-4")]
public async Task<string> GetForecast(
[Description("Latitude of the location.")] double latitude,
[Description("Longitude of the location.")] double longitude)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public sealed class ClientCapabilities
/// </para>
/// <para>
/// The server can use <see cref="McpServer.RequestRootsAsync"/> to request the list of
/// available roots from the client, which will trigger the client's <see cref="ModelContextProtocol.Client.McpClientHandlers.RootsHandler"/>.
/// available roots from the client, which will trigger the client's <see cref="McpClientHandlers.RootsHandler"/>.
/// </para>
/// </remarks>
[JsonPropertyName("roots")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ModelContextProtocol.Protocol;
/// </para>
/// <para>
/// When this capability is enabled, an MCP server can request the client to provide additional information
/// during interactions. The client must set a <see cref="ModelContextProtocol.Client.McpClientHandlers.ElicitationHandler"/> to process these requests.
/// during interactions. The client must set a <see cref="McpClientHandlers.ElicitationHandler"/> to process these requests.
/// </para>
/// <para>
/// This class is intentionally empty as the Model Context Protocol specification does not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ModelContextProtocol.Protocol;
/// </para>
/// <para>
/// When this capability is enabled, an MCP server can request the client to generate content
/// using an AI model. The client must set a <see cref="ModelContextProtocol.Client.McpClientHandlers.SamplingHandler"/> to process these requests.
/// using an AI model. The client must set a <see cref="McpClientHandlers.SamplingHandler"/> to process these requests.
/// </para>
/// <para>
/// This class is intentionally empty as the Model Context Protocol specification does not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace ModelContextProtocol.Server;

Expand Down Expand Up @@ -138,6 +139,11 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
Icons = options?.Icons,
};

// Populate Meta from options and/or McpMetaAttribute instances if a MethodInfo is available
prompt.Meta = function.UnderlyingMethod is not null ?
AIFunctionMcpServerTool.CreateMetaFromAttributes(function.UnderlyingMethod, options?.Meta, options?.SerializerOptions) :
options?.Meta;

return new AIFunctionMcpServerPrompt(function, prompt, options?.Metadata ?? []);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;

namespace ModelContextProtocol.Server;
Expand Down Expand Up @@ -219,6 +220,9 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
Description = options?.Description,
MimeType = options?.MimeType ?? "application/octet-stream",
Icons = options?.Icons,
Meta = function.UnderlyingMethod is not null ?
AIFunctionMcpServerTool.CreateMetaFromAttributes(function.UnderlyingMethod, options?.Meta, options?.SerializerOptions) :
options?.Meta,
};

return new AIFunctionMcpServerResource(function, resource, options?.Metadata ?? []);
Expand Down
25 changes: 25 additions & 0 deletions src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization.Metadata;
using System.Text.RegularExpressions;

namespace ModelContextProtocol.Server;
Expand Down Expand Up @@ -143,6 +144,11 @@ options.OpenWorld is not null ||
ReadOnlyHint = options.ReadOnly,
};
}

// Populate Meta from options and/or McpMetaAttribute instances if a MethodInfo is available
tool.Meta = function.UnderlyingMethod is not null ?
CreateMetaFromAttributes(function.UnderlyingMethod, options.Meta, options.SerializerOptions) :
options.Meta;
}

return new AIFunctionMcpServerTool(function, tool, options?.Services, structuredOutputRequiresWrapping, options?.Metadata ?? []);
Expand Down Expand Up @@ -351,6 +357,25 @@ internal static IReadOnlyList<object> CreateMetadata(MethodInfo method)
return metadata.AsReadOnly();
}

/// <summary>Creates a Meta <see cref="JsonObject"/> from <see cref="McpMetaAttribute"/> instances on the specified method.</summary>
/// <param name="method">The method to extract <see cref="McpMetaAttribute"/> instances from.</param>
/// <param name="meta">Optional <see cref="JsonObject"/> to seed the Meta with. Properties from this object take precedence over attributes.</param>
/// <param name="serializerOptions">Optional <see cref="JsonSerializerOptions"/> to use for serialization. This parameter is ignored when parsing JSON strings from attributes.</param>
/// <returns>A <see cref="JsonObject"/> with metadata, or null if no metadata is present.</returns>
internal static JsonObject? CreateMetaFromAttributes(MethodInfo method, JsonObject? meta = null, JsonSerializerOptions? serializerOptions = null)
{
// Transfer all McpMetaAttribute instances to the Meta JsonObject, ignoring any that would overwrite existing properties.
foreach (var attr in method.GetCustomAttributes<McpMetaAttribute>())
{
if (meta?.ContainsKey(attr.Name) is not true)
{
(meta ??= [])[attr.Name] = JsonNode.Parse(attr.JsonValue);
}
}

return meta;
}

/// <summary>Regex that flags runs of characters other than ASCII digits or letters.</summary>
#if NET
[GeneratedRegex("[^0-9A-Za-z]+")]
Expand Down
103 changes: 103 additions & 0 deletions src/ModelContextProtocol.Core/Server/McpMetaAttribute.cs
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
{
/// <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.JsonContext.Default.String);
}

/// <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)));
}

/// <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 = JsonSerializer.Serialize(value, McpJsonUtilities.JsonContext.Default.Boolean);
}

/// <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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ModelContextProtocol.Protocol;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace ModelContextProtocol.Server;

Expand Down Expand Up @@ -86,6 +87,21 @@ public sealed class McpServerPromptCreateOptions
/// </remarks>
public IList<Icon>? Icons { get; set; }

/// <summary>
/// Gets or sets metadata reserved by MCP for protocol-level metadata.
/// </summary>
/// <remarks>
/// <para>
/// This <see cref="JsonObject"/> is used to seed the <see cref="Prompt.Meta"/> property. Any metadata from
/// <see cref="McpMetaAttribute"/> instances on the method will be added to this object, but
/// properties already present in this <see cref="JsonObject"/> will not be overwritten.
/// </para>
/// <para>
/// Implementations must not make assumptions about its contents.
/// </para>
/// </remarks>
public JsonObject? Meta { get; set; }

/// <summary>
/// Creates a shallow clone of the current <see cref="McpServerPromptCreateOptions"/> instance.
/// </summary>
Expand All @@ -100,5 +116,6 @@ internal McpServerPromptCreateOptions Clone() =>
SchemaCreateOptions = SchemaCreateOptions,
Metadata = Metadata,
Icons = Icons,
Meta = Meta,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ModelContextProtocol.Protocol;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace ModelContextProtocol.Server;

Expand Down Expand Up @@ -101,6 +102,21 @@ public sealed class McpServerResourceCreateOptions
/// </remarks>
public IList<Icon>? Icons { get; set; }

/// <summary>
/// Gets or sets metadata reserved by MCP for protocol-level metadata.
/// </summary>
/// <remarks>
/// <para>
/// This <see cref="JsonObject"/> is used to seed the <see cref="Resource.Meta"/> property. Any metadata from
/// <see cref="McpMetaAttribute"/> instances on the method will be added to this object, but
/// properties already present in this <see cref="JsonObject"/> will not be overwritten.
/// </para>
/// <para>
/// Implementations must not make assumptions about its contents.
/// </para>
/// </remarks>
public JsonObject? Meta { get; set; }

/// <summary>
/// Creates a shallow clone of the current <see cref="McpServerResourceCreateOptions"/> instance.
/// </summary>
Expand All @@ -117,5 +133,6 @@ internal McpServerResourceCreateOptions Clone() =>
SchemaCreateOptions = SchemaCreateOptions,
Metadata = Metadata,
Icons = Icons,
Meta = Meta,
};
}
17 changes: 17 additions & 0 deletions src/ModelContextProtocol.Core/Server/McpServerToolCreateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ModelContextProtocol.Protocol;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace ModelContextProtocol.Server;

Expand Down Expand Up @@ -172,6 +173,21 @@ public sealed class McpServerToolCreateOptions
/// </remarks>
public IList<Icon>? Icons { get; set; }

/// <summary>
/// Gets or sets metadata reserved by MCP for protocol-level metadata.
/// </summary>
/// <remarks>
/// <para>
/// This <see cref="JsonObject"/> is used to seed the <see cref="Tool.Meta"/> property. Any metadata from
/// <see cref="McpMetaAttribute"/> instances on the method will be added to this object, but
/// properties already present in this <see cref="JsonObject"/> will not be overwritten.
/// </para>
/// <para>
/// Implementations must not make assumptions about its contents.
/// </para>
/// </remarks>
public JsonObject? Meta { get; set; }

/// <summary>
/// Creates a shallow clone of the current <see cref="McpServerToolCreateOptions"/> instance.
/// </summary>
Expand All @@ -191,5 +207,6 @@ internal McpServerToolCreateOptions Clone() =>
SchemaCreateOptions = SchemaCreateOptions,
Metadata = Metadata,
Icons = Icons,
Meta = Meta,
};
}
Loading
Loading