Skip to content

Commit ec4c732

Browse files
Copilotstephentoub
andauthored
Add validation and error signaling guidance to MCP tool XML docs (#1275)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 841e810 commit ec4c732

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

src/ModelContextProtocol.Core/McpException.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ namespace ModelContextProtocol;
1212
///
1313
/// This exception type can be thrown by MCP tools or tool call filters to propagate detailed error messages
1414
/// from <see cref="Exception.Message"/> when a tool execution fails via a <see cref="CallToolResult"/>.
15+
/// This includes input validation errors, business logic errors, or any other failure that the model should
16+
/// be informed about. For example, if a required field is missing or a value is out of range, throwing an
17+
/// <see cref="McpException"/> with a descriptive message allows the model to understand the issue and
18+
/// potentially self-correct in a subsequent request.
19+
///
1520
/// For non-tool calls, this exception controls the message propagated via a <see cref="JsonRpcError"/>.
1621
///
1722
/// <see cref="McpProtocolException"/> is a derived type that can be used to also specify the

src/ModelContextProtocol.Core/Protocol/CallToolResult.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ namespace ModelContextProtocol.Protocol;
1414
/// and potentially self-correct in subsequent requests.
1515
/// </para>
1616
/// <para>
17+
/// To return a validation or business-logic error from a tool method, either throw an <see cref="McpException"/>
18+
/// (whose <see cref="Exception.Message"/> will be included in the error result), or declare the tool's return type
19+
/// as <see cref="CallToolResult"/> so it can be returned directly with <see cref="IsError"/> set to <see langword="true"/>
20+
/// and details in <see cref="Content"/>. Using <see cref="CallToolResult"/> as the return type gives the tool full control
21+
/// over both success and error responses.
22+
/// </para>
23+
/// <para>
1724
/// Protocol-level errors (such as unknown tool names, malformed requests that fail schema validation,
1825
/// or server errors) should be reported as MCP protocol error responses using <see cref="McpErrorCode"/>.
1926
/// </para>

src/ModelContextProtocol.Core/Protocol/Tool.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ public sealed class Tool : IBaseMetadata
3131
/// </para>
3232
/// <para>
3333
/// The description is typically presented to AI models to help them determine when
34-
/// and how to use the tool based on user requests.
34+
/// and how to use the tool based on user requests. A well-written description significantly
35+
/// reduces incorrect tool invocations. Include information about what the tool does, any
36+
/// constraints or prerequisites, and what it returns.
37+
/// </para>
38+
/// <para>
39+
/// Similarly, individual parameter descriptions (provided via <see cref="System.ComponentModel.DescriptionAttribute"/>
40+
/// on tool method parameters) are important for guiding the model to supply correct argument values.
41+
/// Descriptions should document expected formats, valid value ranges, and any other constraints
42+
/// the model should be aware of.
3543
/// </para>
3644
/// </remarks>
3745
[JsonPropertyName("description")]

src/ModelContextProtocol.Core/Server/McpServerTool.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ namespace ModelContextProtocol.Server;
9090
/// to provide data to the method.
9191
/// </para>
9292
/// <para>
93+
/// The tool method is responsible for validating its own input arguments (e.g., checking required fields, value ranges, string lengths, or
94+
/// any other business rules). Data annotations such as <c>RequiredAttribute</c> and
95+
/// <c>MaxLengthAttribute</c> on parameter types influence the generated JSON schema exposed
96+
/// to clients, but they are not enforced at runtime by the SDK. Validation should be performed explicitly within the tool method.
97+
/// </para>
98+
/// <para>
99+
/// To signal an error (including validation failures) back to the client, either throw an <see cref="McpException"/>
100+
/// or return a <see cref="CallToolResult"/> with <see cref="CallToolResult.IsError"/> set to <see langword="true"/>.
101+
/// When a tool throws an <see cref="McpException"/>, its <see cref="Exception.Message"/> is included in the error result
102+
/// sent to the client. Throwing any other exception type also results in an error <see cref="CallToolResult"/>, but with
103+
/// a generic error message (to avoid leaking sensitive information). Alternatively, a tool can declare a return type of
104+
/// <see cref="CallToolResult"/> to have full control over both success and error responses.
105+
/// </para>
106+
/// <para>
107+
/// It is important to provide clear <see cref="System.ComponentModel.DescriptionAttribute"/> values on tool methods and their parameters.
108+
/// These descriptions are surfaced to AI models and help them determine when and how to use the tool, what values to pass for each parameter,
109+
/// and what constraints the parameters have. Well-written descriptions reduce incorrect tool invocations and improve the quality of
110+
/// model interactions.
111+
/// </para>
112+
/// <para>
93113
/// Return values from a method are used to create the <see cref="CallToolResult"/> that is sent back to the client:
94114
/// </para>
95115
/// <list type="table">

src/ModelContextProtocol.Core/Server/McpServerToolAttribute.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ namespace ModelContextProtocol.Server;
8282
/// to provide data to the method.
8383
/// </para>
8484
/// <para>
85+
/// The tool method is responsible for validating its own input arguments (e.g., checking required fields, value ranges, string lengths, or
86+
/// any other business rules). Data annotations such as <c>RequiredAttribute</c> and
87+
/// <c>MaxLengthAttribute</c> on parameter types influence the generated JSON schema exposed
88+
/// to clients, but they are not enforced at runtime by the SDK. Validation should be performed explicitly within the tool method.
89+
/// </para>
90+
/// <para>
91+
/// To signal an error (including validation failures) back to the client, either throw an <see cref="McpException"/>
92+
/// or return a <see cref="CallToolResult"/> with <see cref="CallToolResult.IsError"/> set to <see langword="true"/>.
93+
/// When a tool throws an <see cref="McpException"/>, its <see cref="Exception.Message"/> is included in the error result
94+
/// sent to the client. Throwing any other exception type also results in an error <see cref="CallToolResult"/>, but with
95+
/// a generic error message (to avoid leaking sensitive information). Alternatively, a tool can declare a return type of
96+
/// <see cref="CallToolResult"/> to have full control over both success and error responses.
97+
/// </para>
98+
/// <para>
99+
/// It is important to provide clear <see cref="System.ComponentModel.DescriptionAttribute"/> values on tool methods and their parameters.
100+
/// These descriptions are surfaced to AI models and help them determine when and how to use the tool, what values to pass for each parameter,
101+
/// and what constraints the parameters have. Well-written descriptions reduce incorrect tool invocations and improve the quality of
102+
/// model interactions.
103+
/// </para>
104+
/// <para>
85105
/// Return values from a method are used to create the <see cref="CallToolResult"/> that is sent back to the client:
86106
/// </para>
87107
/// <list type="table">

0 commit comments

Comments
 (0)