Skip to content

Commit 10b4748

Browse files
Copilotstephentoub
andcommitted
Add AllowedValuesAttribute section with code samples to completions conceptual docs
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 90dd295 commit 10b4748

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

docs/concepts/completions/completions.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,42 @@ builder.Services.AddMcpServer()
8181
});
8282
```
8383

84+
### Automatic completions with AllowedValuesAttribute
85+
86+
For parameters with a known set of valid values, you can use `System.ComponentModel.DataAnnotations.AllowedValuesAttribute` on `string` parameters of prompts or resource templates. The server will automatically surface those values as completions without needing a custom completion handler.
87+
88+
#### Prompt parameters
89+
90+
```csharp
91+
[McpServerPromptType]
92+
public class MyPrompts
93+
{
94+
[McpServerPrompt, Description("Generates a code review prompt")]
95+
public static ChatMessage CodeReview(
96+
[Description("The programming language")]
97+
[AllowedValues("csharp", "python", "javascript", "typescript", "go", "rust")]
98+
string language,
99+
[Description("The code to review")] string code)
100+
=> new(ChatRole.User, $"Please review the following {language} code:\n\n```{language}\n{code}\n```");
101+
}
102+
```
103+
104+
#### Resource template parameters
105+
106+
```csharp
107+
[McpServerResourceType]
108+
public class MyResources
109+
{
110+
[McpServerResource("config://settings/{section}"), Description("Reads a configuration section")]
111+
public static string ReadConfig(
112+
[AllowedValues("general", "network", "security", "logging")]
113+
string section)
114+
=> GetConfig(section);
115+
}
116+
```
117+
118+
With these attributes in place, when a client sends a `completion/complete` request for the `language` or `section` argument, the server will automatically filter and return matching values based on what the user has typed so far. This approach can be combined with a custom completion handler registered via `WithCompleteHandler`; the handler's results are returned first, followed by any matching `AllowedValues`.
119+
84120
### Requesting completions on the client
85121

86122
Clients request completions using <xref:ModelContextProtocol.Client.McpClient.CompleteAsync*>. Provide a reference to the prompt or resource template, the argument name, and the current partial value:

0 commit comments

Comments
 (0)