Add server "completions" capability (#232)
* Split apart Capabilities.cs No code changes, just moving each type to its own file. * Add server completions capability
This commit is contained in:
parent
ac0bc3a1f1
commit
82510d5629
3 changed files with 321 additions and 272 deletions
|
|
@ -175,11 +175,18 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
|
|||
|
||||
private void SetCompletionHandler(McpServerOptions options)
|
||||
{
|
||||
if (options.Capabilities?.Completions is not { } completionsCapability)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var completeHandler = completionsCapability.CompleteHandler ??
|
||||
throw new McpException("Completions capability was enabled, but Complete handler was not specified.");
|
||||
|
||||
// This capability is not optional, so return an empty result if there is no handler.
|
||||
RequestHandlers.Set(RequestMethods.CompletionComplete,
|
||||
options.GetCompletionHandler is { } handler ?
|
||||
(request, cancellationToken) => handler(new(this, request), cancellationToken) :
|
||||
(request, cancellationToken) => Task.FromResult(new CompleteResult() { Completion = new() { Values = [], Total = 0, HasMore = false } }),
|
||||
RequestHandlers.Set(
|
||||
RequestMethods.CompletionComplete,
|
||||
(request, cancellationToken) => completeHandler(new(this, request), cancellationToken),
|
||||
McpJsonUtilities.JsonContext.Default.CompleteRequestParams,
|
||||
McpJsonUtilities.JsonContext.Default.CompleteResult);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,9 +43,9 @@ public sealed class McpServerHandlers
|
|||
public Func<RequestContext<ReadResourceRequestParams>, CancellationToken, Task<ReadResourceResult>>? ReadResourceHandler { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the handler for get completion requests.
|
||||
/// Gets or sets the handler for completion complete requests.
|
||||
/// </summary>
|
||||
public Func<RequestContext<CompleteRequestParams>, CancellationToken, Task<CompleteResult>>? GetCompletionHandler { get; set; }
|
||||
public Func<RequestContext<CompleteRequestParams>, CancellationToken, Task<CompleteResult>>? CompleteHandler { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the handler for subscribe to resources messages.
|
||||
|
|
@ -109,12 +109,18 @@ public sealed class McpServerHandlers
|
|||
loggingCapability.SetLoggingLevelHandler = SetLoggingLevelHandler;
|
||||
}
|
||||
|
||||
CompletionsCapability? completionsCapability = options.Capabilities?.Completions;
|
||||
if (CompleteHandler is not null)
|
||||
{
|
||||
completionsCapability ??= new();
|
||||
completionsCapability.CompleteHandler = CompleteHandler;
|
||||
}
|
||||
|
||||
options.Capabilities ??= new();
|
||||
options.Capabilities.Prompts = promptsCapability;
|
||||
options.Capabilities.Resources = resourcesCapability;
|
||||
options.Capabilities.Tools = toolsCapability;
|
||||
options.Capabilities.Logging = loggingCapability;
|
||||
|
||||
options.GetCompletionHandler = GetCompletionHandler ?? options.GetCompletionHandler;
|
||||
options.Capabilities.Completions = completionsCapability;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,10 +35,4 @@ public class McpServerOptions
|
|||
/// Optional server instructions to send to clients
|
||||
/// </summary>
|
||||
public string ServerInstructions { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the handler for get completion requests.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public Func<RequestContext<CompleteRequestParams>, CancellationToken, Task<CompleteResult>>? GetCompletionHandler { get; set; }
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue