Skip to content

Commit 76ccbec

Browse files
stephentoubCopilot
andcommitted
Restore UseStructuredContent on McpServerToolCreateOptions
Keep UseStructuredContent as a non-breaking public property alongside the new OutputSchema property. UseStructuredContent on options (or the attribute) triggers output schema inference from the return type; OutputSchema allows providing an explicit schema directly. Also fix JsonNode-to-JsonElement conversion in CallToolResult<T>.ToCallToolResult after StructuredContent type change on main. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0815ca9 commit 76ccbec

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/ModelContextProtocol.Core/Protocol/CallToolResultOfT.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ CallToolResult ICallToolResultTyped.ToCallToolResult(JsonSerializerOptions seria
5757
return new()
5858
{
5959
Content = [new TextContentBlock { Text = structuredContent?.ToString() ?? "null" }],
60-
StructuredContent = structuredContent,
60+
StructuredContent = structuredContent is not null
61+
? JsonSerializer.Deserialize<JsonElement>(structuredContent, McpJsonUtilities.JsonContext.Default.JsonElement)
62+
: null,
6163
IsError = IsError,
6264
Meta = Meta,
6365
};

src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ private static McpServerToolCreateOptions DeriveOptions(MethodInfo method, McpSe
174174
{
175175
McpServerToolCreateOptions newOptions = options?.Clone() ?? new();
176176

177-
bool useStructuredContent = false;
178177
if (method.GetCustomAttribute<McpServerToolAttribute>() is { } toolAttr)
179178
{
180179
newOptions.Name ??= toolAttr.Name;
@@ -211,7 +210,10 @@ private static McpServerToolCreateOptions DeriveOptions(MethodInfo method, McpSe
211210
newOptions.Execution.TaskSupport ??= taskSupport;
212211
}
213212

214-
useStructuredContent = toolAttr.UseStructuredContent;
213+
if (toolAttr.UseStructuredContent)
214+
{
215+
newOptions.UseStructuredContent = true;
216+
}
215217
}
216218

217219
if (method.GetCustomAttribute<DescriptionAttribute>() is { } descAttr)
@@ -223,10 +225,10 @@ private static McpServerToolCreateOptions DeriveOptions(MethodInfo method, McpSe
223225
newOptions.Metadata ??= CreateMetadata(method);
224226

225227
// Generate the output schema from the return type if needed.
226-
// UseStructuredContent on the attribute uses T from CallToolResult<T> or the return type directly.
228+
// UseStructuredContent on the attribute or options uses T from CallToolResult<T> or the return type directly.
227229
// CallToolResult<T> return types automatically infer the schema from T even without UseStructuredContent.
228230
Type? outputSchemaType = GetCallToolResultContentType(method.ReturnType);
229-
if (outputSchemaType is null && useStructuredContent)
231+
if (outputSchemaType is null && newOptions.UseStructuredContent)
230232
{
231233
outputSchemaType = method.ReturnType;
232234
}

src/ModelContextProtocol.Core/Server/McpServerToolCreateOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public sealed class McpServerToolCreateOptions
117117
/// </remarks>
118118
public bool? ReadOnly { get; set; }
119119

120+
/// <summary>
121+
/// Gets or sets a value that indicates whether the tool should report an output schema for structured content.
122+
/// </summary>
123+
/// <value>
124+
/// The default is <see langword="false"/>.
125+
/// </value>
126+
/// <remarks>
127+
/// When enabled, the tool will attempt to populate the <see cref="Tool.OutputSchema"/>
128+
/// and provide structured content in the <see cref="CallToolResult.StructuredContent"/> property.
129+
/// </remarks>
130+
public bool UseStructuredContent { get; set; }
131+
120132
/// <summary>
121133
/// Gets or sets a JSON Schema object to use as the tool's output schema.
122134
/// </summary>
@@ -218,6 +230,7 @@ internal McpServerToolCreateOptions Clone() =>
218230
Idempotent = Idempotent,
219231
OpenWorld = OpenWorld,
220232
ReadOnly = ReadOnly,
233+
UseStructuredContent = UseStructuredContent,
221234
OutputSchema = OutputSchema,
222235
SerializerOptions = SerializerOptions,
223236
SchemaCreateOptions = SchemaCreateOptions,

0 commit comments

Comments
 (0)