Skip to content

Commit e634cc7

Browse files
Copilotstephentoub
andcommitted
Address PR review comments: fix completion filtering, JSON guards, and add AllowedValues docs
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 3693966 commit e634cc7

5 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/ModelContextProtocol.Core/Server/McpServerImpl.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,12 @@ private void ConfigureCompletion(McpServerOptions options)
295295
if (allowedValues is not null)
296296
{
297297
string partialValue = request.Params!.Argument.Value;
298-
var filtered = Array.FindAll(allowedValues, v => v.StartsWith(partialValue, StringComparison.OrdinalIgnoreCase));
299-
300-
foreach (var v in filtered)
298+
foreach (var v in allowedValues)
301299
{
302-
result.Completion.Values.Add(v);
300+
if (v.StartsWith(partialValue, StringComparison.OrdinalIgnoreCase))
301+
{
302+
result.Completion.Values.Add(v);
303+
}
303304
}
304305

305306
result.Completion.Total = result.Completion.Values.Count;
@@ -352,18 +353,19 @@ private void ConfigureCompletion(McpServerOptions options)
352353
continue;
353354
}
354355

355-
if (schema.TryGetProperty("properties", out JsonElement properties))
356+
if (schema.TryGetProperty("properties", out JsonElement properties) &&
357+
properties.ValueKind is JsonValueKind.Object)
356358
{
357359
Dictionary<string, string[]>? paramValues = null;
358360
foreach (var param in properties.EnumerateObject())
359361
{
360362
if (param.Value.TryGetProperty("enum", out JsonElement enumValues) &&
361-
enumValues.ValueKind == JsonValueKind.Array)
363+
enumValues.ValueKind is JsonValueKind.Array)
362364
{
363365
List<string>? values = null;
364366
foreach (var item in enumValues.EnumerateArray())
365367
{
366-
if (item.GetString() is { } str)
368+
if (item.ValueKind is JsonValueKind.String && item.GetString() is { } str)
367369
{
368370
values ??= [];
369371
values.Add(str);

src/ModelContextProtocol.Core/Server/McpServerPrompt.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ namespace ModelContextProtocol.Server;
116116
/// <para>
117117
/// Other returned types will result in an <see cref="InvalidOperationException"/> being thrown.
118118
/// </para>
119+
/// <para>
120+
/// Parameters of type <see cref="string"/> that are decorated with <c>AllowedValuesAttribute</c>
121+
/// will automatically have their allowed values surfaced as completions in response to <c>completion/complete</c> requests from clients,
122+
/// without requiring a custom <see cref="McpServerHandlers.CompleteHandler"/> to be configured.
123+
/// </para>
119124
/// </remarks>
120125
public abstract class McpServerPrompt : IMcpServerPrimitive
121126
{

src/ModelContextProtocol.Core/Server/McpServerPromptAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ namespace ModelContextProtocol.Server;
103103
/// <para>
104104
/// Other returned types will result in an <see cref="InvalidOperationException"/> being thrown.
105105
/// </para>
106+
/// <para>
107+
/// Parameters of type <see cref="string"/> that are decorated with <c>AllowedValuesAttribute</c>
108+
/// will automatically have their allowed values surfaced as completions in response to <c>completion/complete</c> requests from clients,
109+
/// without requiring a custom <see cref="McpServerHandlers.CompleteHandler"/> to be configured.
110+
/// </para>
106111
/// </remarks>
107112
[AttributeUsage(AttributeTargets.Method)]
108113
public sealed class McpServerPromptAttribute : Attribute

src/ModelContextProtocol.Core/Server/McpServerResource.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ namespace ModelContextProtocol.Server;
121121
/// <para>
122122
/// Other returned types will result in an <see cref="InvalidOperationException"/> being thrown.
123123
/// </para>
124+
/// <para>
125+
/// Parameters of type <see cref="string"/> that are decorated with <c>AllowedValuesAttribute</c>
126+
/// will automatically have their allowed values surfaced as completions in response to <c>completion/complete</c> requests from clients,
127+
/// without requiring a custom <see cref="McpServerHandlers.CompleteHandler"/> to be configured.
128+
/// </para>
124129
/// </remarks>
125130
public abstract class McpServerResource : IMcpServerPrimitive
126131
{

src/ModelContextProtocol.Core/Server/McpServerResourceAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ namespace ModelContextProtocol.Server;
105105
/// <para>
106106
/// Other returned types will result in an <see cref="InvalidOperationException"/> being thrown.
107107
/// </para>
108+
/// <para>
109+
/// Parameters of type <see cref="string"/> that are decorated with <c>AllowedValuesAttribute</c>
110+
/// will automatically have their allowed values surfaced as completions in response to <c>completion/complete</c> requests from clients,
111+
/// without requiring a custom <see cref="McpServerHandlers.CompleteHandler"/> to be configured.
112+
/// </para>
108113
/// </remarks>
109114
[AttributeUsage(AttributeTargets.Method)]
110115
public sealed class McpServerResourceAttribute : Attribute

0 commit comments

Comments
 (0)