-
Notifications
You must be signed in to change notification settings - Fork 627
Add CallToolResult<T>, CallToolAsync<T>, and OutputSchema for tools returning CallToolResult #1272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
15
commits into
main
Choose a base branch
from
copilot/support-output-schema-independently
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+602
−33
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
301550d
Initial plan
Copilot b111284
Add OutputSchemaType to McpServerToolAttribute and OutputSchema to Mc…
Copilot 5c82239
Refactor OutputSchemaType check to use ??= pattern
Copilot 444dcfa
Set UseStructuredContent=true when OutputSchemaType is set, remove Dy…
Copilot a6b7ef5
Address review feedback: assert UseStructuredContent, use lambdas in …
Copilot 1d9f40c
Merge branch 'main' into copilot/support-output-schema-independently
stephentoub dcfff8e
Add CallToolResult<T> and CallToolAsync<T>, remove OutputSchemaType f…
Copilot b109728
Add tests for CallToolResult<T>, CallToolAsync<T>, and update existin…
Copilot dd3e73b
Use last content block instead of first in CallToolAsync<T>
Copilot 8930269
Remove ICallToolResultTyped, move conversion logic to AIFunctionMcpSe…
Copilot 6db5287
Remove UseStructuredContent from McpServerToolCreateOptions
Copilot 84dcb46
Revert to ICallToolResultTyped with ToCallToolResult conversion method
Copilot 17c3e9f
Address review feedback: serialize once, fix schema inference order, …
Copilot f3d00f0
Return CallToolResult<T> from CallToolAsync<T>, use ToString and Firs…
Copilot 24bbac3
CallToolResult<T> derives from Result, CallToolAsync<T> returns T and…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/ModelContextProtocol.Core/Protocol/CallToolResultOfT.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
|
|
||
| namespace ModelContextProtocol.Protocol; | ||
|
|
||
| /// <summary> | ||
| /// Represents a strongly-typed result of a <see cref="RequestMethods.ToolsCall"/> request. | ||
| /// </summary> | ||
| /// <typeparam name="T"> | ||
| /// The type of the structured content returned by the tool. This type is used to infer the | ||
| /// <see cref="Tool.OutputSchema"/> advertised by the tool. | ||
| /// </typeparam> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// <see cref="CallToolResult{T}"/> provides a way to return strongly-typed structured content from a tool | ||
| /// while still providing access to <see cref="Result.Meta"/> and <see cref="IsError"/>. When a tool method | ||
| /// returns <see cref="CallToolResult{T}"/>, the SDK uses <typeparamref name="T"/> to infer the output schema | ||
| /// and serializes <see cref="Content"/> as both the text content and structured content of the response. | ||
| /// </para> | ||
| /// <para> | ||
| /// This type is a peer of <see cref="CallToolResult"/>, not a subclass. Use <see cref="CallToolResult"/> when | ||
| /// you need full control over individual content blocks, and <see cref="CallToolResult{T}"/> when you want | ||
| /// the SDK to handle serialization of a strongly-typed result. | ||
| /// </para> | ||
| /// </remarks> | ||
| public sealed class CallToolResult<T> : Result, ICallToolResultTyped | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the typed content returned by the tool. | ||
| /// </summary> | ||
| public T? Content { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value that indicates whether the tool call was unsuccessful. | ||
| /// </summary> | ||
| /// <value> | ||
| /// <see langword="true"/> to signify that the tool execution failed; <see langword="false"/> if it was successful. | ||
| /// </value> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Tool execution errors (including input validation errors, API failures, and business logic errors) | ||
| /// are reported with this property set to <see langword="true"/> and details in the <see cref="Content"/> | ||
| /// property, rather than as protocol-level errors. | ||
| /// </para> | ||
| /// <para> | ||
| /// This design allows language models to receive detailed error feedback and potentially self-correct | ||
| /// in subsequent requests. | ||
| /// </para> | ||
| /// </remarks> | ||
| public bool? IsError { get; set; } | ||
|
|
||
| /// <inheritdoc/> | ||
| CallToolResult ICallToolResultTyped.ToCallToolResult(JsonSerializerOptions serializerOptions) | ||
| { | ||
| JsonNode? structuredContent = JsonSerializer.SerializeToNode(Content, serializerOptions.GetTypeInfo(typeof(T))); | ||
|
|
||
| return new() | ||
| { | ||
| Content = [new TextContentBlock { Text = structuredContent?.ToString() ?? "null" }], | ||
| StructuredContent = structuredContent, | ||
| IsError = IsError, | ||
| Meta = Meta, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Internal interface for converting strongly-typed tool results to <see cref="CallToolResult"/>. | ||
| /// </summary> | ||
| internal interface ICallToolResultTyped | ||
| { | ||
| /// <summary> | ||
| /// Converts the strongly-typed result to a <see cref="CallToolResult"/>. | ||
| /// </summary> | ||
| CallToolResult ToCallToolResult(JsonSerializerOptions serializerOptions); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot why can't all of this schema handling for CallToolResult be done where the rest of the schema detection is done? There's already code unwrapping tasks, creating return schema for the return type, etc. I'm missing why this needs to be done separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consolidated in 24bbac3 — unified into a single block after all attribute handling:
GetCallToolResultContentTypealways runs first (handlesCallToolResult<T>with or without the attribute), thenUseStructuredContentfills in the return type only whenCallToolResult<T>wasn't detected.