Add support for URL mode elicitation (#1021)
This commit is contained in:
parent
c447973972
commit
ae17ba0b4b
1 changed files with 1241 additions and 92 deletions
|
|
@ -240,7 +240,8 @@ public abstract partial class McpServer : McpSession
|
|||
public ValueTask<ElicitResult> ElicitAsync(
|
||||
ElicitRequestParams request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ThrowIfElicitationUnsupported();
|
||||
Throw.IfNull(request);
|
||||
ThrowIfElicitationUnsupported(request);
|
||||
|
||||
return SendRequestAsync(
|
||||
RequestMethods.ElicitationCreate,
|
||||
|
|
@ -268,8 +269,6 @@ public abstract partial class McpServer : McpSession
|
|||
JsonSerializerOptions? serializerOptions = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ThrowIfElicitationUnsupported();
|
||||
|
||||
serializerOptions ??= McpJsonUtilities.DefaultOptions;
|
||||
serializerOptions.MakeReadOnly();
|
||||
|
||||
|
|
@ -287,6 +286,8 @@ public abstract partial class McpServer : McpSession
|
|||
RequestedSchema = schema,
|
||||
};
|
||||
|
||||
ThrowIfElicitationUnsupported(request);
|
||||
|
||||
var raw = await ElicitAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (!raw.IsAccepted || raw.Content is null)
|
||||
|
|
@ -453,16 +454,47 @@ public abstract partial class McpServer : McpSession
|
|||
}
|
||||
}
|
||||
|
||||
private void ThrowIfElicitationUnsupported()
|
||||
private void ThrowIfElicitationUnsupported(ElicitRequestParams request)
|
||||
{
|
||||
if (ClientCapabilities?.Elicitation is null)
|
||||
if (ClientCapabilities is null)
|
||||
{
|
||||
if (ClientCapabilities is null)
|
||||
throw new InvalidOperationException("Elicitation is not supported in stateless mode.");
|
||||
}
|
||||
|
||||
var elicitationCapability = ClientCapabilities.Elicitation;
|
||||
if (elicitationCapability is null)
|
||||
{
|
||||
throw new InvalidOperationException("Client does not support elicitation requests.");
|
||||
}
|
||||
|
||||
if (string.Equals(request.Mode, "form", StringComparison.Ordinal) && elicitationCapability.Form is null)
|
||||
{
|
||||
if (request.RequestedSchema is null)
|
||||
{
|
||||
throw new InvalidOperationException("Elicitation is not supported in stateless mode.");
|
||||
throw new ArgumentException("Form mode elicitation requests require a requested schema.");
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Client does not support elicitation requests.");
|
||||
if (elicitationCapability.Form is null)
|
||||
{
|
||||
throw new InvalidOperationException("Client does not support form mode elicitation requests.");
|
||||
}
|
||||
}
|
||||
else if (string.Equals(request.Mode, "url", StringComparison.Ordinal))
|
||||
{
|
||||
if (request.Url is null)
|
||||
{
|
||||
throw new ArgumentException("URL mode elicitation requests require a URL.");
|
||||
}
|
||||
|
||||
if (request.ElicitationId is null)
|
||||
{
|
||||
throw new ArgumentException("URL mode elicitation requests require an elicitation ID.");
|
||||
}
|
||||
|
||||
if (elicitationCapability.Url is null)
|
||||
{
|
||||
throw new InvalidOperationException("Client does not support URL mode elicitation requests.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue