From 870f6ba1924c42b7b54d6e106d878e6244b35f09 Mon Sep 17 00:00:00 2001 From: gil zhang Date: Tue, 25 Nov 2025 16:00:30 +0800 Subject: [PATCH] update ModelContextProtocol version to 0.4.0-preview.3 --- Directory.Packages.props | 4 ++-- .../MCP/Managers/McpClientManager.cs | 7 +++--- .../Routing/Executor/MCPToolExecutor.cs | 22 ++++++++++++------- tests/BotSharp.PizzaBot.MCPServer/Program.cs | 1 + 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 88401e719..aae7921bf 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -99,8 +99,8 @@ - - + + diff --git a/src/Infrastructure/BotSharp.Core/MCP/Managers/McpClientManager.cs b/src/Infrastructure/BotSharp.Core/MCP/Managers/McpClientManager.cs index 50b798eb4..9a06a85b8 100644 --- a/src/Infrastructure/BotSharp.Core/MCP/Managers/McpClientManager.cs +++ b/src/Infrastructure/BotSharp.Core/MCP/Managers/McpClientManager.cs @@ -1,6 +1,5 @@ using BotSharp.Core.MCP.Settings; using ModelContextProtocol.Client; -using ModelContextProtocol.Protocol.Transport; namespace BotSharp.Core.MCP.Managers; @@ -17,7 +16,7 @@ public McpClientManager( _logger = logger; } - public async Task GetMcpClientAsync(string serverId) + public async Task GetMcpClientAsync(string serverId) { try { @@ -31,7 +30,7 @@ public McpClientManager( IClientTransport? transport = null; if (config.SseConfig != null) { - transport = new SseClientTransport(new SseClientTransportOptions + transport = new HttpClientTransport(new HttpClientTransportOptions { Name = config.Name, Endpoint = new Uri(config.SseConfig.EndPoint), @@ -56,7 +55,7 @@ public McpClientManager( return null; } - return await McpClientFactory.CreateAsync(transport, settings.McpClientOptions); + return await McpClient.CreateAsync(transport, settings.McpClientOptions); } catch (Exception ex) { diff --git a/src/Infrastructure/BotSharp.Core/Routing/Executor/MCPToolExecutor.cs b/src/Infrastructure/BotSharp.Core/Routing/Executor/MCPToolExecutor.cs index c452e8066..8cf7d18e5 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Executor/MCPToolExecutor.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Executor/MCPToolExecutor.cs @@ -1,17 +1,17 @@ using BotSharp.Abstraction.Routing.Executor; using BotSharp.Core.MCP.Managers; -using ModelContextProtocol.Client; +using ModelContextProtocol.Protocol; namespace BotSharp.Core.Routing.Executor; -public class McpToolExecutor: IFunctionExecutor +public class McpToolExecutor : IFunctionExecutor { private readonly IServiceProvider _services; private readonly string _mcpServerId; private readonly string _functionName; public McpToolExecutor(IServiceProvider services, string mcpServerId, string functionName) - { + { _services = services; _mcpServerId = mcpServerId; _functionName = functionName; @@ -22,16 +22,22 @@ public async Task ExecuteAsync(RoleDialogModel message) try { // Convert arguments to dictionary format expected by mcpdotnet - Dictionary argDict = JsonToDictionary(message.FunctionArgs); + Dictionary argDict = JsonToDictionary(message.FunctionArgs); var clientManager = _services.GetRequiredService(); var client = await clientManager.GetMcpClientAsync(_mcpServerId); + if (client == null) + { + message.Content = $"MCP client for server {_mcpServerId} not found."; + return false; + } + // Call the tool through mcpdotnet var result = await client.CallToolAsync(_functionName, !argDict.IsNullOrEmpty() ? argDict : []); // Extract the text content from the result - var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text)); + var json = string.Join("\n", result.Content.Where(c => c is TextContentBlock).Select(c => ((TextContentBlock)c).Text)); message.Content = json; message.Data = json.JsonContent(); @@ -50,7 +56,7 @@ public async Task GetIndicatorAsync(RoleDialogModel message) } - private static Dictionary JsonToDictionary(string? json) + private static Dictionary JsonToDictionary(string? json) { if (string.IsNullOrEmpty(json)) { @@ -62,9 +68,9 @@ private static Dictionary JsonToDictionary(string? json) return JsonElementToDictionary(root); } - private static Dictionary JsonElementToDictionary(JsonElement element) + private static Dictionary JsonElementToDictionary(JsonElement element) { - Dictionary dictionary = []; + Dictionary dictionary = []; if (element.ValueKind == JsonValueKind.Object) { diff --git a/tests/BotSharp.PizzaBot.MCPServer/Program.cs b/tests/BotSharp.PizzaBot.MCPServer/Program.cs index 2a77ff619..a34c0ee31 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/Program.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/Program.cs @@ -1,5 +1,6 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddMcpServer() + .WithHttpTransport() .WithToolsFromAssembly(); var app = builder.Build();