Add ToolAnnotations support (#124)
* Add ToolAnnotations support * Address feedback
This commit is contained in:
parent
2c1672cc1a
commit
62e92fa154
70 changed files with 645 additions and 394 deletions
|
|
@ -10,7 +10,7 @@ namespace TestServerWithHosting.Tools;
|
|||
[McpServerToolType]
|
||||
public static class SampleLlmTool
|
||||
{
|
||||
[McpServerTool("sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
|
||||
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
|
||||
public static async Task<string> SampleLLM(
|
||||
IMcpServer thisServer,
|
||||
[Description("The prompt to send to the LLM")] string prompt,
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ namespace TestServerWithHosting.Tools;
|
|||
[McpServerToolType]
|
||||
public static class SampleLlmTool
|
||||
{
|
||||
[McpServerTool("sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
|
||||
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
|
||||
public static async Task<string> SampleLLM(
|
||||
IMcpServer thisServer,
|
||||
[Description("The prompt to send to the LLM")] string prompt,
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ public static class AIContentExtensions
|
|||
Throw.IfNull(content);
|
||||
|
||||
AIContent ac;
|
||||
if (content is { Type: "image", MimeType: not null, Data: not null })
|
||||
if (content is { Type: "image" or "audio", MimeType: not null, Data: not null })
|
||||
{
|
||||
ac = new DataContent(Convert.FromBase64String(content.Data), content.MimeType);
|
||||
}
|
||||
|
|
@ -112,6 +112,7 @@ public static class AIContentExtensions
|
|||
Text = textContent.Text,
|
||||
Type = "text",
|
||||
},
|
||||
|
||||
DataContent dataContent => new()
|
||||
{
|
||||
Data = dataContent.GetBase64Data(),
|
||||
|
|
@ -121,6 +122,7 @@ public static class AIContentExtensions
|
|||
dataContent.HasTopLevelMediaType("audio") ? "audio" :
|
||||
"resource",
|
||||
},
|
||||
|
||||
_ => new()
|
||||
{
|
||||
Text = JsonSerializer.Serialize(content, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object))),
|
||||
|
|
|
|||
|
|
@ -469,7 +469,7 @@ public static class McpClientExtensions
|
|||
{
|
||||
message.Contents.Add(new TextContent(sm.Content.Text));
|
||||
}
|
||||
else if (sm.Content is { Type: "image", MimeType: not null, Data: not null })
|
||||
else if (sm.Content is { Type: "image" or "audio", MimeType: not null, Data: not null })
|
||||
{
|
||||
message.Contents.Add(new DataContent(Convert.FromBase64String(sm.Content.Data), sm.Content.MimeType));
|
||||
}
|
||||
|
|
@ -512,11 +512,11 @@ public static class McpClientExtensions
|
|||
{
|
||||
foreach (var lmc in lastMessage.Contents)
|
||||
{
|
||||
if (lmc is DataContent dc && dc.HasTopLevelMediaType("image"))
|
||||
if (lmc is DataContent dc && (dc.HasTopLevelMediaType("image") || dc.HasTopLevelMediaType("audio")))
|
||||
{
|
||||
content = new()
|
||||
{
|
||||
Type = "image",
|
||||
Type = dc.HasTopLevelMediaType("image") ? "image" : "audio",
|
||||
MimeType = dc.MediaType,
|
||||
Data = dc.GetBase64Data(),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,22 +9,24 @@ namespace ModelContextProtocol.Client;
|
|||
public sealed class McpClientTool : AIFunction
|
||||
{
|
||||
private readonly IMcpClient _client;
|
||||
private readonly Tool _tool;
|
||||
|
||||
internal McpClientTool(IMcpClient client, Tool tool)
|
||||
{
|
||||
_client = client;
|
||||
_tool = tool;
|
||||
ProtocolTool = tool;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string Name => _tool.Name;
|
||||
/// <summary>Gets the protocol <see cref="Tool"/> type for this instance.</summary>
|
||||
public Tool ProtocolTool { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string Description => _tool.Description ?? string.Empty;
|
||||
public override string Name => ProtocolTool.Name;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override JsonElement JsonSchema => _tool.InputSchema;
|
||||
public override string Description => ProtocolTool.Description ?? string.Empty;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override JsonElement JsonSchema => ProtocolTool.InputSchema;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override JsonSerializerOptions JsonSerializerOptions => McpJsonUtilities.DefaultOptions;
|
||||
|
|
@ -37,7 +39,7 @@ public sealed class McpClientTool : AIFunction
|
|||
arguments as IReadOnlyDictionary<string, object?> ??
|
||||
arguments.ToDictionary();
|
||||
|
||||
CallToolResponse result = await _client.CallToolAsync(_tool.Name, argDict, cancellationToken).ConfigureAwait(false);
|
||||
CallToolResponse result = await _client.CallToolAsync(ProtocolTool.Name, argDict, cancellationToken).ConfigureAwait(false);
|
||||
return JsonSerializer.SerializeToElement(result, McpJsonUtilities.JsonContext.Default.CallToolResponse);
|
||||
}
|
||||
}
|
||||
|
|
@ -37,14 +37,9 @@ public static partial class McpServerBuilderExtensions
|
|||
{
|
||||
if (toolMethod.GetCustomAttribute<McpServerToolAttribute>() is not null)
|
||||
{
|
||||
if (toolMethod.IsStatic)
|
||||
{
|
||||
builder.Services.AddSingleton(services => McpServerTool.Create(toolMethod, services: services));
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Services.AddSingleton(services => McpServerTool.Create(toolMethod, typeof(TTool), services: services));
|
||||
}
|
||||
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
|
||||
services => McpServerTool.Create(toolMethod, new McpServerToolCreateOptions() { Services = services }) :
|
||||
services => McpServerTool.Create(toolMethod, typeof(TTool), new() { Services = services })));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -71,18 +66,13 @@ public static partial class McpServerBuilderExtensions
|
|||
{
|
||||
if (toolType is not null)
|
||||
{
|
||||
foreach (var method in toolType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
|
||||
foreach (var toolMethod in toolType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
|
||||
{
|
||||
if (method.GetCustomAttribute<McpServerToolAttribute>() is not null)
|
||||
if (toolMethod.GetCustomAttribute<McpServerToolAttribute>() is not null)
|
||||
{
|
||||
if (method.IsStatic)
|
||||
{
|
||||
builder.Services.AddSingleton(services => McpServerTool.Create(method, services: services));
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Services.AddSingleton(services => McpServerTool.Create(method, toolType, services: services));
|
||||
}
|
||||
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
|
||||
services => McpServerTool.Create(toolMethod, new McpServerToolCreateOptions() { Services = services }) :
|
||||
services => McpServerTool.Create(toolMethod, toolType, new() { Services = services })));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ public sealed class StdioServerTransport : TransportBase, IServerTransport
|
|||
private readonly TextReader _stdInReader;
|
||||
private readonly Stream _stdOutStream;
|
||||
|
||||
private SemaphoreSlim _sendLock = new(1, 1);
|
||||
private readonly SemaphoreSlim _sendLock = new(1, 1);
|
||||
private Task? _readTask;
|
||||
private CancellationTokenSource? _shutdownCts;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
|
||||
/// <summary>
|
||||
/// Base for objects that include optional annotations for the client. The client can use annotations to inform how objects are used or displayed.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public abstract record Annotated
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
|
||||
/// <summary>
|
||||
/// Represents annotations that can be attached to content.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public record Annotations
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
|
||||
/// <summary>
|
||||
/// Used for completion requests to provide additional context for the completion options.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class Argument
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
/// <summary>
|
||||
/// Used by the client to invoke a tool provided by the server.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class CallToolRequestParams : RequestParams
|
||||
{
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
/// However, any errors in _finding_ the tool, an error indicating that the
|
||||
/// server does not support tool calls, or any other exceptional conditions,
|
||||
/// should be reported as an MCP error response.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class CallToolResponse
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
|
||||
/// <summary>
|
||||
/// Represents the capabilities that a client may support.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class ClientCapabilities
|
||||
{
|
||||
|
|
@ -30,7 +30,7 @@ public class ClientCapabilities
|
|||
|
||||
/// <summary>
|
||||
/// Represents the roots capability configuration.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class RootsCapability
|
||||
{
|
||||
|
|
@ -47,7 +47,7 @@ public class RootsCapability
|
|||
|
||||
/// <summary>
|
||||
/// Represents the sampling capability configuration.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class SamplingCapability
|
||||
{
|
||||
|
|
@ -60,7 +60,7 @@ public class SamplingCapability
|
|||
|
||||
/// <summary>
|
||||
/// Represents the logging capability configuration.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class LoggingCapability
|
||||
{
|
||||
|
|
@ -76,7 +76,7 @@ public class LoggingCapability
|
|||
|
||||
/// <summary>
|
||||
/// Represents the prompts capability configuration.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class PromptsCapability
|
||||
{
|
||||
|
|
@ -101,7 +101,7 @@ public class PromptsCapability
|
|||
|
||||
/// <summary>
|
||||
/// Represents the resources capability configuration.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class ResourcesCapability
|
||||
{
|
||||
|
|
@ -150,7 +150,7 @@ public class ResourcesCapability
|
|||
|
||||
/// <summary>
|
||||
/// Represents the tools capability configuration.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class ToolsCapability
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
/// <summary>
|
||||
/// A request from the client to the server, to ask for completion options.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class CompleteRequestParams : RequestParams
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
|
||||
/// <summary>
|
||||
/// The server's response to a completion/complete request
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class CompleteResult
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
|
||||
/// <summary>
|
||||
/// Represents a completion object in the server's response
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class Completion
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,13 +4,13 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
|
||||
/// <summary>
|
||||
/// Represents the content of a tool response.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// There are multiple subtypes of content, depending on the "type" field, these are represented as separate classes.
|
||||
/// </summary>
|
||||
public class Content
|
||||
{
|
||||
/// <summary>
|
||||
/// The type of content. This determines the structure of the content object. Can be "image", "text", "resource".
|
||||
/// The type of content. This determines the structure of the content object. Can be "image", "audio", "text", "resource".
|
||||
/// </summary>
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
|
||||
/// <summary>
|
||||
/// A request to include context from one or more MCP servers (including the caller), to be attached to the prompt.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(JsonStringEnumConverter<ContextInclusion>))]
|
||||
public enum ContextInclusion
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
///
|
||||
/// While these align with the protocol specification,
|
||||
/// clients have full discretion over model selection and should inform users before sampling.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class CreateMessageRequestParams : RequestParams
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
/// The client's response to a sampling/create_message request from the server.
|
||||
/// The client should inform the user before returning the sampled message, to allow them to inspect the response (human in the loop)
|
||||
/// and decide whether to allow the server to see it.
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
|
||||
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
|
||||
/// </summary>
|
||||
public class CreateMessageResult
|
||||
{
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue