Documentation Index
Fetch the complete documentation index at: https://mintlify.com/modelcontextprotocol/csharp-sdk/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The MCP C# SDK uses attributes to declaratively define tools, prompts, and resources. Methods marked with these attributes are automatically discovered and registered when using the builder’sWithTools, WithPrompts, or WithResources methods.
McpServerToolAttribute
Purpose
Indicates that a method should be exposed as an MCP tool that clients can invoke.Syntax
Properties
The name of the tool. If null, the method name is used.
A human-readable title for the tool that can be displayed to users. Unlike the tool name (which follows programmatic naming conventions), the title can include spaces and special characters.
Indicates whether the tool might perform destructive updates to its environment. Set to
false if the tool performs only additive updates.Indicates whether calling the tool repeatedly with the same arguments has no additional effect on its environment.
Indicates whether this tool can interact with an “open world” of external entities (like web search). Set to
false if the tool’s domain is closed and well-defined (like memory access).Indicates whether this tool does not modify its environment. Read-only tools only perform read operations without changing state.
When enabled, the tool will populate the
Tool.OutputSchema and provide structured content in the CallToolResult.StructuredContent property.The source URI for the tool’s icon. Can be an HTTP/HTTPS URL or a data URI with base64-encoded image data. For advanced icon configuration (multiple icons, MIME types, sizes), use
McpServerToolCreateOptions.Icons.Configures how the tool supports task-based invocation:
Forbidden: Clients must not invoke the tool as a taskOptional: Clients may invoke as a task or normal requestRequired: Clients must invoke as a task
Parameter Binding
Tool method parameters are bound from various sources: Automatically Bound (not in JSON schema):CancellationToken- Bound to request cancellation token (respects client cancellation notifications)IServiceProvider- Bound from the request contextMcpServer- Bound to the server instanceIProgress<ProgressNotificationValue>- For reporting progress to the client- Services from DI (checked via
IServiceProviderIsService) - Parameters with
[FromKeyedServices]attribute
- All other parameters are deserialized from
CallToolRequestParams.Argumentsdictionary using JSON
Data from client arguments should be considered unvalidated and untrusted. Always validate input within your tool method. Data annotations like
[Required] and [MaxLength] influence the JSON schema but are not enforced at runtime.Return Value Handling
Tool return values are automatically converted toCallToolResult:
| Return Type | Result |
|---|---|
null | Empty Content list |
string | Single TextContentBlock |
AIContent | Converted to ContentBlock |
ContentBlock | Single-item list |
IEnumerable<string> | Multiple TextContentBlock items |
IEnumerable<AIContent> | Multiple ContentBlock items |
IEnumerable<ContentBlock> | Returned as-is |
CallToolResult | Returned directly |
| Other types | Serialized to JSON as text content |
Error Handling
- Throw
McpExceptionto return an error with a specific message to the client - Throw any other exception to return a generic error (prevents leaking sensitive information)
- Return
CallToolResultwithIsError = truefor full control over error responses
Examples
McpServerPromptAttribute
Purpose
Indicates that a method should be exposed as an MCP prompt that clients can retrieve.Syntax
Properties
The name of the prompt. If null, the method name is used.
The title of the prompt for display purposes.
The source URI for the prompt’s icon. Can be an HTTP/HTTPS URL or data URI. For advanced configuration, use
McpServerPromptCreateOptions.Icons.Parameter Binding
Automatically Bound:CancellationTokenIServiceProviderMcpServerIProgress<ProgressNotificationValue>- Services from DI
- Parameters with
[FromKeyedServices]
- Deserialized from
GetPromptRequestParams.Argumentsdictionary
Parameters of type
string decorated with [AllowedValues] automatically get their allowed values surfaced as completions in response to completion/complete requests.Return Value Handling
| Return Type | Result |
|---|---|
string | Single PromptMessage with the string as content |
PromptMessage | Single-item list |
IEnumerable<PromptMessage> | Returned as-is |
ChatMessage | Converted to PromptMessage list |
IEnumerable<ChatMessage> | Converted to PromptMessage list |
| Other types | Throws InvalidOperationException |
Examples
McpServerResourceAttribute
Purpose
Indicates that a method or property should be exposed as an MCP resource that clients can read.Syntax
Properties
The URI template of the resource (RFC 6570 format). If null, derived from
Name and method parameters.- With parameters (e.g.,
"test://template/resource/{id}") - Resource template listed withresources/templates/list - Without parameters (e.g.,
"test://direct/resource") - Direct resource listed withresources/list
The name of the resource. If null, the method name is used.
The title of the resource for display purposes.
The MIME (media) type of the resource (e.g.,
"text/plain", "application/json").The source URI for the resource’s icon. For advanced configuration, use
McpServerResourceCreateOptions.Icons.Parameter Binding
Automatically Bound:CancellationTokenIServiceProviderMcpServerIProgress<ProgressNotificationValue>- Services from DI
- Parameters with
[FromKeyedServices]
- All other parameters are bound from the URI using the template pattern
Parameters of type
string decorated with [AllowedValues] automatically surface completions for completion/complete requests.Return Value Handling
| Return Type | Result |
|---|---|
ResourceContents | Single-item list |
TextContentBlock | Converted to TextResourceContents |
DataContent | Converted to BlobResourceContents |
string | Converted to TextResourceContents |
IEnumerable<ResourceContents> | Returned as-is |
IEnumerable<AIContent> | Converted to appropriate ResourceContents types |
IEnumerable<string> | Each string as TextResourceContents |
| Other types | Throws InvalidOperationException |
Examples
Type Attributes
These attributes mark classes for automatic discovery when using assembly scanning methods.McpServerToolTypeAttribute
[McpServerTool]. Used with WithToolsFromAssembly().
McpServerPromptTypeAttribute
[McpServerPrompt]. Used with WithPromptsFromAssembly().
McpServerResourceTypeAttribute
[McpServerResource]. Used with WithResourcesFromAssembly().
Best Practices
Descriptions
Always provide clear[Description] attributes on methods and parameters. These descriptions are surfaced to AI models and help them understand when and how to use your tools.
Validation
Validate all client-provided input explicitly:Security
Use dependency injection to provide trusted data rather than accepting it from client arguments:See Also
- IMcpServerBuilder - Builder methods that discover these attributes
- Hosting and DI - How to register attributed types
- Protocol Types - The Tool, Prompt, and Resource types created from attributed methods