Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
simplified dependencies of ToMessage
  • Loading branch information
KrzysztofCwalina committed Jun 27, 2025
commit 422fb5daa91d7c6906402be32bde61500564bb6d
26 changes: 12 additions & 14 deletions docs/guides/mcp/chat_with_mcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#:package ModelContextProtocol.Core@*-*
#:property PublishAot=false

using System.Text.Json;
using ModelContextProtocol;
using ModelContextProtocol.Client;
using OpenAI.Chat;

Expand Down Expand Up @@ -45,7 +47,7 @@
Console.WriteLine("Tool call detected, calling MCP server...");
ModelContextProtocol.Protocol.CallToolResult result = await mcpClient.CallToolAsync(call.FunctionName, mcpArguments!);
Console.WriteLine($"tool call result {result.Content[0]}");
ChatMessage message = result.ToMessage(call);
ChatMessage message = call.ToMessage(result.Content.ToAIContents());
conversation.Add(message);
}
goto start;
Expand All @@ -60,27 +62,23 @@
// this is temporary. all these APIs will endup being in one of the packages used here.
public static class TemporaryExtensions
{
public static ChatMessage ToMessage(this ModelContextProtocol.Protocol.CallToolResult mcpCallResult, ChatToolCall openaiCall)
// this needs to be in the adapter package
public static ChatMessage ToMessage(this ChatToolCall openaiCall, IEnumerable<Microsoft.Extensions.AI.AIContent> contents)
{
List<ChatMessageContentPart> parts = new();
var sc = mcpCallResult.StructuredContent;

foreach (ModelContextProtocol.Protocol.ContentBlock block in mcpCallResult.Content)
foreach (Microsoft.Extensions.AI.AIContent content in contents)
{
if (block is ModelContextProtocol.Protocol.TextContentBlock textContent)
{
parts.Add(ChatMessageContentPart.CreateTextPart(textContent.Text));
}
else
{
throw new NotImplementedException();
}
string serialized = JsonSerializer.Serialize(content.RawRepresentation);
using JsonDocument json = JsonDocument.Parse(serialized);
JsonElement text = json.RootElement.GetProperty("text");
string textValue = text.GetString() ?? string.Empty;
parts.Add(ChatMessageContentPart.CreateTextPart(textValue));
}
ToolChatMessage message = ChatMessage.CreateToolMessage(openaiCall.Id, parts);
return message;
}

// this is in the adapter package; waiting for package to be dropped.
// this is in the adapter package
public static ChatTool AsOpenAIChatTool(this Microsoft.Extensions.AI.AIFunction mcpTool)
{
return ChatTool.CreateFunctionTool(
Expand Down