Synthesize return description into tool description when UseStructuredContent is false (#1049)

This commit is contained in:
Copilot 2025-12-05 10:24:49 -08:00 • committed by GitHub
commit 4808ca0324
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -118,7 +118,7 @@ internal sealed partial class AIFunctionMcpServerTool : McpServerTool
Tool tool = new()
{
Name = options?.Name ?? function.Name,
Description = options?.Description ?? function.Description,
Description = GetToolDescription(function, options),
InputSchema = function.JsonSchema,
OutputSchema = CreateOutputSchema(function, options, out bool structuredOutputRequiresWrapping),
Icons = options?.Icons,
@ -406,6 +406,57 @@ internal sealed partial class AIFunctionMcpServerTool : McpServerTool
}
}
/// <summary>
/// Gets the tool description, synthesizing from both the function description and return description when appropriate.
/// </summary>
/// <remarks>
/// When UseStructuredContent is true, the return description is included in the output schema.
/// When UseStructuredContent is false (default), if there's a return description in the ReturnJsonSchema,
/// it will be appended to the tool description so the information is still available to consumers.
/// </remarks>
private static string? GetToolDescription(AIFunction function, McpServerToolCreateOptions? options)
{
string? description = options?.Description ?? function.Description;
// If structured content is enabled, the return description will be in the output schema
if (options?.UseStructuredContent is true)
{
return description;
}
// When structured content is disabled, try to extract the return description from ReturnJsonSchema
// and append it to the tool description so the information is available to consumers
string? returnDescription = GetReturnDescription(function.ReturnJsonSchema);
if (string.IsNullOrWhiteSpace(returnDescription))
{
return description;
}
// Synthesize a combined description
if (string.IsNullOrWhiteSpace(description))
{
return $"Returns: {returnDescription}";
}
return $"{description}\nReturns: {returnDescription}";
}
/// <summary>
/// Extracts the description property from a ReturnJsonSchema if present.
/// </summary>
private static string? GetReturnDescription(JsonElement? returnJsonSchema)
{
if (returnJsonSchema is not JsonElement schema ||
schema.ValueKind is not JsonValueKind.Object ||
!schema.TryGetProperty("description", out JsonElement descriptionElement) ||
descriptionElement.ValueKind is not JsonValueKind.String)
{
return null;
}
return descriptionElement.GetString();
}
private static JsonElement? CreateOutputSchema(AIFunction function, McpServerToolCreateOptions? toolCreateOptions, out bool structuredOutputRequiresWrapping)
{
structuredOutputRequiresWrapping = false;