Add OutputSchemaType and OutputSchema for tools returning CallToolResult#1272
Add OutputSchemaType and OutputSchema for tools returning CallToolResult#1272
Conversation
…pServerToolCreateOptions - Added Type? OutputSchemaType property to McpServerToolAttribute - Added JsonElement? OutputSchema property to McpServerToolCreateOptions - Updated DeriveOptions to generate schema from OutputSchemaType via AIJsonUtilities.CreateJsonSchema - Updated CreateOutputSchema to use explicit OutputSchema when provided (takes precedence) - OutputSchema forces structured content behavior even if UseStructuredContent is false - Updated Clone() to preserve OutputSchema - Added 8 new tests covering various scenarios - Updated XML documentation Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…namicallyAccessedMembers - In DeriveOptions, force UseStructuredContent=true when OutputSchemaType is set - Simplify GetToolDescription to only check UseStructuredContent (no longer needs OutputSchema check) - Remove unnecessary [DynamicallyAccessedMembers] from OutputSchemaType property Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…tests, add using for JsonDocument - Add Debug.Assert in CreateOutputSchema to verify UseStructuredContent is true when OutputSchema is set - Force UseStructuredContent=true in DeriveOptions when OutputSchema is set via options - Update docs to say "to a non-null value" for UseStructuredContent auto-enable - Add using statements for all JsonDocument.Parse calls in tests - Rewrite attribute-based tests to use lambdas instead of reflection - Add test for OutputSchemaType with UseStructuredContent=false to verify it's overridden - Remove OutputSchemaTypeTools helper class (no longer needed) Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
@copilot Please give a short explanation of the feature added in this PR from the perspective of a user of the SDK. |
When a tool method returns This PR lets you specify the output schema type independently of the return type: Via attribute: [McpServerTool(OutputSchemaType = typeof(MyResponse))]
public static CallToolResult MyTool(string input)
{
var response = new MyResponse { Name = "test", Score = 42 };
return new CallToolResult
{
Content = [new TextContentBlock { Text = "done" }],
StructuredContent = JsonSerializer.SerializeToNode(response),
};
}Via create options: McpServerTool.Create(myDelegate, new McpServerToolCreateOptions
{
OutputSchema = myJsonSchemaElement, // a JsonElement with the schema
});In both cases, the tool's |
Type? OutputSchemaTypeproperty toMcpServerToolAttributeJsonElement? OutputSchemaproperty toMcpServerToolCreateOptionsDeriveOptionsinAIFunctionMcpServerToolto generate schema fromOutputSchemaTypeviaAIJsonUtilities.CreateJsonSchemaCreateOutputSchemainAIFunctionMcpServerToolto use the explicitOutputSchemafrom options when providedUseStructuredContent = truewhenOutputSchemaTypeis set inDeriveOptionsUseStructuredContent = truewhenOutputSchemais set via optionsDebug.AssertinCreateOutputSchemaverifyingUseStructuredContentis trueClone()method to includeOutputSchemaOutputSchemaTypeon the attribute using lambda syntaxOutputSchemaon create options withusingforJsonDocumentOutputSchemaTypewithUseStructuredContent=falseto verify it's overridden??=pattern per review feedback[DynamicallyAccessedMembers]fromOutputSchemaTypeOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.