From 7a9d7ffc75baea2e5bba3b0560c5f989aebbd5c9 Mon Sep 17 00:00:00 2001 From: geffzhang Date: Fri, 21 Mar 2025 13:14:19 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Aswitch=20official=20sdk=20ModelCon?= =?UTF-8?q?textProtocol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mcpdotnet now is mcp offical sdk --- BotSharp.sln | 22 +++++++++---------- .../Agents/Enums/AgentField.cs | 2 +- .../Services/AgentService.UpdateAgent.cs | 2 +- .../FileRepository/FileRepository.Agent.cs | 2 +- .../BotSharp.MCP/AIFunctionUtilities.cs | 13 +++++------ .../BotSharp.MCP/BotSharp.MCP.csproj | 2 +- .../BotSharp.MCP/Functions/McpToolFunction.cs | 6 ++--- .../BotSharp.MCP/Hooks/MCPResponseHook.cs | 11 ++-------- .../BotSharp.MCP/Hooks/MCPToolAgentHook.cs | 2 +- .../BotSharp.MCP/MCPClientManager.cs | 2 +- src/Infrastructure/BotSharp.MCP/McpPlugin.cs | 7 +++--- .../BotSharp.MCP/Settings/MCPSettings.cs | 4 ++-- .../Repository/MongoRepository.Agent.cs | 2 +- .../BotSharp.PizzaBot.MCPServer.csproj | 2 +- .../McpEndpointRouteBuilderExtensions.cs | 8 +++---- tests/BotSharp.PizzaBot.MCPServer/Program.cs | 2 +- .../SseServerStreamTransport.cs | 8 +++---- .../Tools/MakePayment.cs | 2 +- .../Tools/PizzaPrices.cs | 4 ++-- .../Tools/PlaceOrder.cs | 2 +- 20 files changed, 47 insertions(+), 58 deletions(-) diff --git a/BotSharp.sln b/BotSharp.sln index 17352903..919e630d 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -131,10 +131,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.MCP", "src\Infrast EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.Realtime", "src\Infrastructure\BotSharp.Core.Realtime\BotSharp.Core.Realtime.csproj", "{781F1465-365C-0F22-1775-25025DAFA4C7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mcpdotnet", "..\mcpdotnet\src\mcpdotnet\mcpdotnet.csproj", "{32D455C9-C7CF-928B-F878-8EAF0198328E}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.PizzaBot.MCPServer", "tests\BotSharp.PizzaBot.MCPServer\BotSharp.PizzaBot.MCPServer.csproj", "{8D2AD45F-836A-516F-DE6A-71443CEBB18A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelContextProtocol", "..\Ai4c\mcpcsharp-sdk\src\ModelContextProtocol\ModelContextProtocol.csproj", "{13E4F06A-D4F2-49FA-E979-DE3937137579}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -543,14 +543,6 @@ Global {781F1465-365C-0F22-1775-25025DAFA4C7}.Release|Any CPU.Build.0 = Release|Any CPU {781F1465-365C-0F22-1775-25025DAFA4C7}.Release|x64.ActiveCfg = Release|Any CPU {781F1465-365C-0F22-1775-25025DAFA4C7}.Release|x64.Build.0 = Release|Any CPU - {32D455C9-C7CF-928B-F878-8EAF0198328E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {32D455C9-C7CF-928B-F878-8EAF0198328E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {32D455C9-C7CF-928B-F878-8EAF0198328E}.Debug|x64.ActiveCfg = Debug|Any CPU - {32D455C9-C7CF-928B-F878-8EAF0198328E}.Debug|x64.Build.0 = Debug|Any CPU - {32D455C9-C7CF-928B-F878-8EAF0198328E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {32D455C9-C7CF-928B-F878-8EAF0198328E}.Release|Any CPU.Build.0 = Release|Any CPU - {32D455C9-C7CF-928B-F878-8EAF0198328E}.Release|x64.ActiveCfg = Release|Any CPU - {32D455C9-C7CF-928B-F878-8EAF0198328E}.Release|x64.Build.0 = Release|Any CPU {8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Debug|Any CPU.Build.0 = Debug|Any CPU {8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -559,6 +551,14 @@ Global {8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|Any CPU.Build.0 = Release|Any CPU {8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|x64.ActiveCfg = Release|Any CPU {8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|x64.Build.0 = Release|Any CPU + {13E4F06A-D4F2-49FA-E979-DE3937137579}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {13E4F06A-D4F2-49FA-E979-DE3937137579}.Debug|Any CPU.Build.0 = Debug|Any CPU + {13E4F06A-D4F2-49FA-E979-DE3937137579}.Debug|x64.ActiveCfg = Debug|Any CPU + {13E4F06A-D4F2-49FA-E979-DE3937137579}.Debug|x64.Build.0 = Debug|Any CPU + {13E4F06A-D4F2-49FA-E979-DE3937137579}.Release|Any CPU.ActiveCfg = Release|Any CPU + {13E4F06A-D4F2-49FA-E979-DE3937137579}.Release|Any CPU.Build.0 = Release|Any CPU + {13E4F06A-D4F2-49FA-E979-DE3937137579}.Release|x64.ActiveCfg = Release|Any CPU + {13E4F06A-D4F2-49FA-E979-DE3937137579}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -622,8 +622,8 @@ Global {AF329442-B48E-4B48-A18A-1C869D1BA6F5} = {D5293208-2BEF-42FC-A64C-5954F61720BA} {684781D5-3DD4-6A0B-B53F-0A362CD6BB0C} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749} {781F1465-365C-0F22-1775-25025DAFA4C7} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749} - {32D455C9-C7CF-928B-F878-8EAF0198328E} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC} {8D2AD45F-836A-516F-DE6A-71443CEBB18A} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC} + {13E4F06A-D4F2-49FA-E979-DE3937137579} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19} diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs index 413f9eb8..2ecddf0c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs @@ -19,7 +19,7 @@ public enum AgentField Sample, LlmConfig, Utility, - Acp, + McpTool, KnowledgeBase, Rule, MaxMessageCount diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 7e9da19c..dd8743d4 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -197,7 +197,7 @@ public partial class AgentService var samples = GetSamplesFromFile(dir); return agent.SetInstruction(defaultInstruction) .SetChannelInstructions(channelInstructions) - .SetTemplates(templates) + .SetTemplates(templates) .SetFunctions(functions) .SetResponses(responses) .SetSamples(samples); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs index ae401508..4da61828 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs @@ -60,7 +60,7 @@ namespace BotSharp.Core.Repository case AgentField.Utility: UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities); break; - case AgentField.Acp: + case AgentField.McpTool: UpdateAgentMcpTools(agent.Id, agent.McpTools); break; case AgentField.KnowledgeBase: diff --git a/src/Infrastructure/BotSharp.MCP/AIFunctionUtilities.cs b/src/Infrastructure/BotSharp.MCP/AIFunctionUtilities.cs index e86cbf92..8a409f67 100644 --- a/src/Infrastructure/BotSharp.MCP/AIFunctionUtilities.cs +++ b/src/Infrastructure/BotSharp.MCP/AIFunctionUtilities.cs @@ -1,9 +1,8 @@ using BotSharp.Abstraction.Functions.Models; -using McpDotNet.Protocol.Types; +using ModelContextProtocol.Protocol.Types; using System; using System.Collections.Generic; using System.Text.Json; -using System.Text.Json.Serialization; namespace BotSharp.Core.MCP; @@ -16,19 +15,19 @@ internal static class AIFunctionUtilities throw new ArgumentNullException(nameof(tool)); } - var properties = tool.InputSchema?.Properties; - var required = tool.InputSchema?.Required ?? new List(); + var properties = tool.InputSchema.GetProperty("properties"); + var required = tool.InputSchema.GetProperty("required"); FunctionDef funDef = new FunctionDef { Name = tool.Name, - Description = tool.Description?? string.Empty, + Description = tool.Description ?? string.Empty, Type = "function", Parameters = new FunctionParametersDef { Type = "object", - Properties = properties != null ? JsonSerializer.SerializeToDocument(properties) : JsonDocument.Parse("{}"), - Required = required + Properties = JsonDocument.Parse(properties.GetRawText()), + Required = JsonSerializer.Deserialize>(required.GetRawText()) } }; diff --git a/src/Infrastructure/BotSharp.MCP/BotSharp.MCP.csproj b/src/Infrastructure/BotSharp.MCP/BotSharp.MCP.csproj index 2980894f..dff1c73b 100644 --- a/src/Infrastructure/BotSharp.MCP/BotSharp.MCP.csproj +++ b/src/Infrastructure/BotSharp.MCP/BotSharp.MCP.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/Infrastructure/BotSharp.MCP/Functions/McpToolFunction.cs b/src/Infrastructure/BotSharp.MCP/Functions/McpToolFunction.cs index 07397db4..1f6f2c60 100644 --- a/src/Infrastructure/BotSharp.MCP/Functions/McpToolFunction.cs +++ b/src/Infrastructure/BotSharp.MCP/Functions/McpToolFunction.cs @@ -1,12 +1,10 @@ using BotSharp.Abstraction.Agents; -using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Functions; using BotSharp.Abstraction.Utilities; -using McpDotNet.Client; -using McpDotNet.Protocol.Types; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; +using ModelContextProtocol.Client; +using ModelContextProtocol.Protocol.Types; using System; using System.Collections.Generic; using System.Linq; diff --git a/src/Infrastructure/BotSharp.MCP/Hooks/MCPResponseHook.cs b/src/Infrastructure/BotSharp.MCP/Hooks/MCPResponseHook.cs index 9658eae6..c5470582 100644 --- a/src/Infrastructure/BotSharp.MCP/Hooks/MCPResponseHook.cs +++ b/src/Infrastructure/BotSharp.MCP/Hooks/MCPResponseHook.cs @@ -1,25 +1,18 @@ -using BotSharp.Abstraction.Agents.Enums; -using BotSharp.Abstraction.Agents.Settings; using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Conversations.Models; -using BotSharp.Abstraction.Infrastructures.Enums; -using BotSharp.Abstraction.Translation; using Microsoft.Extensions.DependencyInjection; using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Text.Json; +using System.Threading.Tasks; namespace BotSharp.MCP.Hooks; public class MCPResponseHook : ConversationHookBase { private readonly IServiceProvider _services; - private readonly IConversationStateService _states; - private const string AIAssistant = BuiltInAgentId.AIAssistant; + private readonly IConversationStateService _states; public MCPResponseHook(IServiceProvider services, IConversationStateService states) diff --git a/src/Infrastructure/BotSharp.MCP/Hooks/MCPToolAgentHook.cs b/src/Infrastructure/BotSharp.MCP/Hooks/MCPToolAgentHook.cs index e4cc3e58..e56ab68e 100644 --- a/src/Infrastructure/BotSharp.MCP/Hooks/MCPToolAgentHook.cs +++ b/src/Infrastructure/BotSharp.MCP/Hooks/MCPToolAgentHook.cs @@ -6,8 +6,8 @@ using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Functions.Models; using BotSharp.Core.Mcp; using BotSharp.Core.MCP; -using McpDotNet.Client; using Microsoft.Extensions.DependencyInjection; +using ModelContextProtocol.Client; using System; using System.Collections.Generic; using System.Linq; diff --git a/src/Infrastructure/BotSharp.MCP/MCPClientManager.cs b/src/Infrastructure/BotSharp.MCP/MCPClientManager.cs index 7e833503..78d94a1a 100644 --- a/src/Infrastructure/BotSharp.MCP/MCPClientManager.cs +++ b/src/Infrastructure/BotSharp.MCP/MCPClientManager.cs @@ -1,6 +1,6 @@ using BotSharp.Core.Mcp.Settings; -using McpDotNet.Client; using Microsoft.Extensions.Logging; +using ModelContextProtocol.Client; using System; using System.Linq; using System.Threading.Tasks; diff --git a/src/Infrastructure/BotSharp.MCP/McpPlugin.cs b/src/Infrastructure/BotSharp.MCP/McpPlugin.cs index d161984a..1780ff56 100644 --- a/src/Infrastructure/BotSharp.MCP/McpPlugin.cs +++ b/src/Infrastructure/BotSharp.MCP/McpPlugin.cs @@ -5,12 +5,11 @@ using BotSharp.Abstraction.Plugins; using BotSharp.Core.Mcp.Functions; using BotSharp.Core.Mcp.Settings; using BotSharp.MCP.Hooks; -using McpDotNet.Client; -using McpDotNet.Protocol.Types; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using ModelContextProtocol.Client; +using ModelContextProtocol.Configuration; using System.Linq; using System.Threading.Tasks; @@ -44,7 +43,7 @@ public class McpPlugin : IBotSharpPlugin services.AddScoped(); } - private async Task RegisterFunctionCall(IServiceCollection services, McpDotNet.Configuration.McpServerConfig server) + private async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server) { var client = await clientManager.GetMcpClientAsync(server.Id); var tools = await client.ListToolsAsync().ToListAsync(); diff --git a/src/Infrastructure/BotSharp.MCP/Settings/MCPSettings.cs b/src/Infrastructure/BotSharp.MCP/Settings/MCPSettings.cs index 34e408e3..1279a08d 100644 --- a/src/Infrastructure/BotSharp.MCP/Settings/MCPSettings.cs +++ b/src/Infrastructure/BotSharp.MCP/Settings/MCPSettings.cs @@ -1,5 +1,5 @@ -using McpDotNet.Client; -using McpDotNet.Configuration; +using ModelContextProtocol.Client; +using ModelContextProtocol.Configuration; using System.Collections.Generic; namespace BotSharp.Core.Mcp.Settings; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 069845ef..de13a2d3 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -61,7 +61,7 @@ public partial class MongoRepository case AgentField.Utility: UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities); break; - case AgentField.Acp: + case AgentField.McpTool: UpdateAgentMcps(agent.Id, agent.McpTools); break; case AgentField.KnowledgeBase: diff --git a/tests/BotSharp.PizzaBot.MCPServer/BotSharp.PizzaBot.MCPServer.csproj b/tests/BotSharp.PizzaBot.MCPServer/BotSharp.PizzaBot.MCPServer.csproj index 880099e5..41800784 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/BotSharp.PizzaBot.MCPServer.csproj +++ b/tests/BotSharp.PizzaBot.MCPServer/BotSharp.PizzaBot.MCPServer.csproj @@ -14,6 +14,6 @@ - + diff --git a/tests/BotSharp.PizzaBot.MCPServer/McpEndpointRouteBuilderExtensions.cs b/tests/BotSharp.PizzaBot.MCPServer/McpEndpointRouteBuilderExtensions.cs index 25553962..f2138444 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/McpEndpointRouteBuilderExtensions.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/McpEndpointRouteBuilderExtensions.cs @@ -1,7 +1,7 @@ -using McpDotNet.Protocol.Messages; -using McpDotNet.Server; -using McpDotNet.Utils.Json; using Microsoft.Extensions.Options; +using ModelContextProtocol.Protocol.Messages; +using ModelContextProtocol.Server; +using ModelContextProtocol.Utils.Json; namespace BotSharp.PizzaBot.MCPServer; @@ -45,7 +45,7 @@ public static class McpEndpointRouteBuilderExtensions return; } - var message = await context.Request.ReadFromJsonAsync(JsonSerializerOptionsExtensions.DefaultOptions, context.RequestAborted); + var message = await context.Request.ReadFromJsonAsync(McpJsonUtilities.DefaultOptions, context.RequestAborted); if (message is null) { await Results.BadRequest("No message in request body.").ExecuteAsync(context); diff --git a/tests/BotSharp.PizzaBot.MCPServer/Program.cs b/tests/BotSharp.PizzaBot.MCPServer/Program.cs index c82745a6..fb39e6cd 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/Program.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/Program.cs @@ -1,5 +1,5 @@ using BotSharp.PizzaBot.MCPServer; -using McpDotNet; +using ModelContextProtocol; var builder = WebApplication.CreateBuilder(args); builder.Services.AddMcpServer().WithTools(); diff --git a/tests/BotSharp.PizzaBot.MCPServer/SseServerStreamTransport.cs b/tests/BotSharp.PizzaBot.MCPServer/SseServerStreamTransport.cs index 47e0d09a..7a7ce51a 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/SseServerStreamTransport.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/SseServerStreamTransport.cs @@ -1,6 +1,6 @@ -using McpDotNet.Protocol.Messages; -using McpDotNet.Protocol.Transport; -using McpDotNet.Utils.Json; +using ModelContextProtocol.Protocol.Messages; +using ModelContextProtocol.Protocol.Transport; +using ModelContextProtocol.Utils.Json; using System.Buffers; using System.Net.ServerSentEvents; using System.Text.Json; @@ -28,7 +28,7 @@ public class SseServerStreamTransport(Stream sseResponseStream) : ITransport return; } - JsonSerializer.Serialize(GetUtf8JsonWriter(writer), item.Data, JsonSerializerOptionsExtensions.DefaultOptions); + JsonSerializer.Serialize(GetUtf8JsonWriter(writer), item.Data, McpJsonUtilities.DefaultOptions); } // The very first SSE event isn't really an IJsonRpcMessage, but there's no API to write a single item of a different type, diff --git a/tests/BotSharp.PizzaBot.MCPServer/Tools/MakePayment.cs b/tests/BotSharp.PizzaBot.MCPServer/Tools/MakePayment.cs index c6870aa8..a236d874 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/Tools/MakePayment.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/Tools/MakePayment.cs @@ -1,4 +1,4 @@ -using McpDotNet.Server; +using ModelContextProtocol.Server; using System.ComponentModel; using System.ComponentModel.DataAnnotations; diff --git a/tests/BotSharp.PizzaBot.MCPServer/Tools/PizzaPrices.cs b/tests/BotSharp.PizzaBot.MCPServer/Tools/PizzaPrices.cs index db0892d2..7ca766f3 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/Tools/PizzaPrices.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/Tools/PizzaPrices.cs @@ -1,6 +1,6 @@ -using McpDotNet.Server; -using System.ComponentModel.DataAnnotations; +using ModelContextProtocol.Server; using System.ComponentModel; +using System.ComponentModel.DataAnnotations; using System.Dynamic; using System.Text.Json; diff --git a/tests/BotSharp.PizzaBot.MCPServer/Tools/PlaceOrder.cs b/tests/BotSharp.PizzaBot.MCPServer/Tools/PlaceOrder.cs index 7b38327a..a6237b6d 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/Tools/PlaceOrder.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/Tools/PlaceOrder.cs @@ -1,4 +1,4 @@ -using McpDotNet.Server; +using ModelContextProtocol.Server; using System.ComponentModel; using System.ComponentModel.DataAnnotations;