Skip to content

Commit 729edc1

Browse files
committed
Allow override of name and description of McpClientTool
1 parent 5d3fb65 commit 729edc1

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/ModelContextProtocol/Client/McpClientTool.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,50 @@ namespace ModelContextProtocol.Client;
99
public sealed class McpClientTool : AIFunction
1010
{
1111
private readonly IMcpClient _client;
12+
private readonly string? _nameOverride;
13+
private readonly string? _descriptionOverride;
1214

13-
internal McpClientTool(IMcpClient client, Tool tool)
15+
internal McpClientTool(IMcpClient client, Tool tool, string? nameOverride = null, string? descriptionOverride = null)
1416
{
1517
_client = client;
1618
ProtocolTool = tool;
19+
_nameOverride = nameOverride;
20+
_descriptionOverride = descriptionOverride;
21+
}
22+
23+
/// <summary>
24+
/// Creates a new instance of the tool with the specified name.
25+
/// This is useful for optimizing the tool name for specific models or for prefixing the tool name with a (usually server-derived) namespace to avoid conflicts.
26+
/// The server will still be called with the original tool name, so no mapping is required.
27+
/// </summary>
28+
/// <param name="name">The model-facing name to give the tool</param>
29+
/// <returns>Equivalent McpClientTool, but with the provided name</returns>
30+
public McpClientTool WithName(string name)
31+
{
32+
return new McpClientTool(_client, ProtocolTool, name, _descriptionOverride);
33+
}
34+
35+
/// <summary>
36+
/// Creates a new instance of the tool with the specified description.
37+
/// This can be used to provide modified or additional (e.g. examples) context to the model about the tool.
38+
/// This will in general require a hard-coded mapping in the client.
39+
/// It is not recommended to use this without running evaluations to ensure the model actually benefits from the custom description.
40+
/// </summary>
41+
/// <param name="description"></param>
42+
/// <returns></returns>
43+
public McpClientTool WithDescription(string description)
44+
{
45+
return new McpClientTool(_client, ProtocolTool, _nameOverride, description);
1746
}
1847

1948
/// <summary>Gets the protocol <see cref="Tool"/> type for this instance.</summary>
2049
public Tool ProtocolTool { get; }
2150

2251
/// <inheritdoc/>
23-
public override string Name => ProtocolTool.Name;
52+
public override string Name => _nameOverride ?? ProtocolTool.Name;
2453

2554
/// <inheritdoc/>
26-
public override string Description => ProtocolTool.Description ?? string.Empty;
55+
public override string Description => _descriptionOverride ?? ProtocolTool.Description ?? string.Empty;
2756

2857
/// <inheritdoc/>
2958
public override JsonElement JsonSchema => ProtocolTool.InputSchema;

0 commit comments

Comments
 (0)