Skip to content

Commit 2a54c03

Browse files
committed
Only ServerName should be optional
1 parent eb8df02 commit 2a54c03

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/McpServerToolCallContent.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ public sealed class McpServerToolCallContent : AIContent
2222
/// Initializes a new instance of the <see cref="McpServerToolCallContent"/> class.
2323
/// </summary>
2424
/// <param name="callId">The tool call ID.</param>
25-
/// <exception cref="ArgumentNullException"><paramref name="callId"/> is <see langword="null"/>.</exception>
26-
/// <exception cref="ArgumentException"><paramref name="callId"/> is empty or composed entirely of whitespace.</exception>
27-
public McpServerToolCallContent(string callId)
25+
/// <param name="toolName">The tool name.</param>
26+
/// <exception cref="ArgumentNullException"><paramref name="callId"/> or <paramref name="toolName"/> is <see langword="null"/>.</exception>
27+
/// <exception cref="ArgumentException"><paramref name="callId"/> or <paramref name="toolName"/> is empty or composed entirely of whitespace.</exception>
28+
public McpServerToolCallContent(string callId, string toolName)
2829
{
2930
CallId = Throw.IfNullOrWhitespace(callId);
31+
ToolName = Throw.IfNullOrWhitespace(toolName);
3032
}
3133

3234
/// <summary>
@@ -35,9 +37,9 @@ public McpServerToolCallContent(string callId)
3537
public string CallId { get; }
3638

3739
/// <summary>
38-
/// Gets or sets the name of the tool called.
40+
/// Gets the name of the tool called.
3941
/// </summary>
40-
public string? ToolName { get; set; }
42+
public string ToolName { get; }
4143

4244
/// <summary>
4345
/// Gets or sets the name of the MCP server.

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,8 @@ internal static IEnumerable<ChatMessage> ToChatMessages(IEnumerable<ResponseItem
181181
break;
182182

183183
case McpToolCallApprovalRequestItem mtcari:
184-
message.Contents.Add(new McpServerToolApprovalRequestContent(mtcari.Id, new(mtcari.Id)
184+
message.Contents.Add(new McpServerToolApprovalRequestContent(mtcari.Id, new(mtcari.Id, mtcari.ToolName)
185185
{
186-
ToolName = mtcari.ToolName,
187186
ServerName = mtcari.ServerLabel,
188187
Arguments = JsonSerializer.Deserialize(mtcari.ToolArguments.ToMemory().Span, OpenAIJsonContext.Default.IReadOnlyDictionaryStringObject)!,
189188
RawRepresentation = mtcari,
@@ -309,9 +308,8 @@ ChatResponseUpdate CreateUpdate(AIContent? content = null) =>
309308
break;
310309

311310
case StreamingResponseOutputItemDoneUpdate outputItemDoneUpdate when outputItemDoneUpdate.Item is McpToolCallApprovalRequestItem mtcari:
312-
yield return CreateUpdate(new McpServerToolApprovalRequestContent(mtcari.Id, new(mtcari.Id)
311+
yield return CreateUpdate(new McpServerToolApprovalRequestContent(mtcari.Id, new(mtcari.Id, mtcari.ToolName)
313312
{
314-
ToolName = mtcari.ToolName,
315313
ServerName = mtcari.ServerLabel,
316314
Arguments = JsonSerializer.Deserialize(mtcari.ToolArguments.ToMemory().Span, OpenAIJsonContext.Default.IReadOnlyDictionaryStringObject)!,
317315
RawRepresentation = mtcari,
@@ -879,9 +877,8 @@ private static void PopulateAnnotations(ResponseContentPart source, AIContent de
879877
/// <summary>Adds new <see cref="AIContent"/> for the specified <paramref name="mtci"/> into <paramref name="contents"/>.</summary>
880878
private static void AddMcpToolCallContent(McpToolCallItem mtci, IList<AIContent> contents)
881879
{
882-
contents.Add(new McpServerToolCallContent(mtci.Id)
880+
contents.Add(new McpServerToolCallContent(mtci.Id, mtci.ToolName)
883881
{
884-
ToolName = mtci.ToolName,
885882
ServerName = mtci.ServerLabel,
886883
Arguments = JsonSerializer.Deserialize(mtci.ToolArguments.ToMemory().Span, OpenAIJsonContext.Default.IReadOnlyDictionaryStringObject)!,
887884

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/McpServerToolCallContentTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ public class McpServerToolCallContentTests
1212
[Fact]
1313
public void Constructor_PropsDefault()
1414
{
15-
McpServerToolCallContent c = new("callId1");
15+
McpServerToolCallContent c = new("callId1", "toolName");
1616

1717
Assert.Null(c.RawRepresentation);
1818
Assert.Null(c.AdditionalProperties);
1919

2020
Assert.Equal("callId1", c.CallId);
21-
Assert.Null(c.ToolName);
22-
Assert.Null(c.ServerName);
21+
Assert.Equal("toolName", c.ToolName);
2322

23+
Assert.Null(c.ServerName);
2424
Assert.Null(c.Arguments);
2525
}
2626

2727
[Fact]
2828
public void Constructor_PropsRoundtrip()
2929
{
30-
McpServerToolCallContent c = new("callId1");
30+
McpServerToolCallContent c = new("callId1", "toolName");
3131

3232
Assert.Null(c.RawRepresentation);
3333
object raw = new();
@@ -44,21 +44,21 @@ public void Constructor_PropsRoundtrip()
4444
c.Arguments = args;
4545
Assert.Same(args, c.Arguments);
4646

47-
Assert.Equal("callId1", c.CallId);
47+
Assert.Null(c.ServerName);
48+
c.ServerName = "testServer";
49+
Assert.Equal("testServer", c.ServerName);
4850

49-
Assert.Null(c.ToolName);
50-
c.ToolName = "toolName";
51+
Assert.Equal("callId1", c.CallId);
5152
Assert.Equal("toolName", c.ToolName);
52-
53-
Assert.Null(c.ServerName);
54-
c.ServerName = "serverName";
55-
Assert.Equal("serverName", c.ServerName);
5653
}
5754

5855
[Fact]
5956
public void Constructor_Throws()
6057
{
61-
Assert.Throws<ArgumentException>("callId", () => new McpServerToolCallContent(string.Empty));
62-
Assert.Throws<ArgumentNullException>("callId", () => new McpServerToolCallContent(null!));
58+
Assert.Throws<ArgumentException>("callId", () => new McpServerToolCallContent(string.Empty, "name"));
59+
Assert.Throws<ArgumentException>("toolName", () => new McpServerToolCallContent("callId1", string.Empty));
60+
61+
Assert.Throws<ArgumentNullException>("callId", () => new McpServerToolCallContent(null!, "name"));
62+
Assert.Throws<ArgumentNullException>("toolName", () => new McpServerToolCallContent("callId1", null!));
6363
}
6464
}

0 commit comments

Comments
 (0)