Add McpServerPrompt / McpClientPrompt and friends (#126)

This commit is contained in:
Stephen Toub 2025-03-31 09:54:40 -04:00 • committed by GitHub
commit 5d3fb65b11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,16 +3,18 @@ using ModelContextProtocol.Server;
using Microsoft.Extensions.Options;
using ModelContextProtocol.Utils;
namespace ModelContextProtocol.Configuration;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Configures the McpServerOptions using addition services from DI.
/// </summary>
/// <param name="serverHandlers">The server handlers configuration options.</param>
/// <param name="serverTools">Tools individually registered.</param>
/// <param name="serverPrompts">Prompts individually registered.</param>
internal sealed class McpServerOptionsSetup(
IOptions<McpServerHandlers> serverHandlers,
IEnumerable<McpServerTool> serverTools) : IConfigureOptions<McpServerOptions>
IEnumerable<McpServerTool> serverTools,
IEnumerable<McpServerPrompt> serverPrompts) : IConfigureOptions<McpServerOptions>
{
/// <summary>
/// Configures the given McpServerOptions instance by setting server information
@ -39,17 +41,34 @@ internal sealed class McpServerOptionsSetup(
// a collection, add to it, otherwise create a new one. We want to maintain the identity
// of an existing collection in case someone has provided their own derived type, wants
// change notifications, etc.
McpServerToolCollection toolsCollection = options.Capabilities?.Tools?.ToolCollection ?? [];
McpServerPrimitiveCollection<McpServerTool> toolCollection = options.Capabilities?.Tools?.ToolCollection ?? [];
foreach (var tool in serverTools)
{
toolsCollection.TryAdd(tool);
toolCollection.TryAdd(tool);
}
if (!toolsCollection.IsEmpty)
if (!toolCollection.IsEmpty)
{
options.Capabilities ??= new();
options.Capabilities.Tools ??= new();
options.Capabilities.Tools.ToolCollection = toolsCollection;
options.Capabilities.Tools.ToolCollection = toolCollection;
}
// Collect all of the provided prompts into a prompts collection. If the options already has
// a collection, add to it, otherwise create a new one. We want to maintain the identity
// of an existing collection in case someone has provided their own derived type, wants
// change notifications, etc.
McpServerPrimitiveCollection<McpServerPrompt> promptCollection = options.Capabilities?.Prompts?.PromptCollection ?? [];
foreach (var prompt in serverPrompts)
{
promptCollection.TryAdd(prompt);
}
if (!promptCollection.IsEmpty)
{
options.Capabilities ??= new();
options.Capabilities.Prompts ??= new();
options.Capabilities.Prompts.PromptCollection = promptCollection;
}
// Apply custom server handlers.