Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/ModelContextProtocol/McpJsonUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ModelContextProtocol.Protocol;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;

Expand Down Expand Up @@ -75,6 +76,25 @@ internal static bool IsValidMcpToolSchema(JsonElement element)
return false; // No type keyword found.
}

private static readonly string[] s_rootSchemaKeywordsToRemove = ["title", "description"];
internal static AIJsonSchemaCreateOptions DefaultSchemaCreateOptions { get; } = new()
{
TransformOptions = new()
{
TransformSchemaNode = static (ctx, node) =>
{
if (ctx.Path is [] && node is JsonObject obj)
{
foreach (string keywordToRemove in s_rootSchemaKeywordsToRemove)
{
obj.Remove(keywordToRemove);
}
}
return node;
},
}
};

// Keep in sync with CreateDefaultOptions above.
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
return null;
}
},
JsonSchemaCreateOptions = options?.SchemaCreateOptions,
JsonSchemaCreateOptions = options?.SchemaCreateOptions ?? McpJsonUtilities.DefaultSchemaCreateOptions,
};

/// <summary>Creates an <see cref="McpServerPrompt"/> that wraps the specified <see cref="AIFunction"/>.</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
return null;
}
},
JsonSchemaCreateOptions = options?.SchemaCreateOptions,
JsonSchemaCreateOptions = options?.SchemaCreateOptions ?? McpJsonUtilities.DefaultSchemaCreateOptions,
};

/// <summary>Creates an <see cref="McpServerTool"/> that wraps the specified <see cref="AIFunction"/>.</summary>
Expand Down
18 changes: 18 additions & 0 deletions tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using Moq;
using System.ComponentModel;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -381,6 +382,23 @@ public async Task SupportsSchemaCreateOptions()
);
}

[Fact]
public void TrimsDescriptionAndTitleKeywordsFromRootSchema()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a similar test for prompts?

{
McpServerTool tool = McpServerTool.Create(Add);

Assert.Equal("My awesome adding tool", tool.ProtocolTool.Description);
Assert.False(tool.ProtocolTool.InputSchema.TryGetProperty("description", out _));
Assert.False(tool.ProtocolTool.InputSchema.TryGetProperty("title", out _));

// Preserves any nested description keywords
Assert.Equal("The first argument", tool.ProtocolTool.InputSchema.GetProperty("properties").GetProperty("a").GetProperty("description").GetString());
Assert.Equal("The second argument", tool.ProtocolTool.InputSchema.GetProperty("properties").GetProperty("b").GetProperty("description").GetString());

[Description("My awesome adding tool")]
static int Add([Description("The first argument")] int a , [Description("The second argument")] int b) => a + b;
}

private sealed class MyService;

private class DisposableToolType : IDisposable
Expand Down