From 451e7802ebb1f3da246ceda10f2b86e6a378d724 Mon Sep 17 00:00:00 2001 From: geffzhang Date: Fri, 28 Mar 2025 09:46:01 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix=EF=BC=9A=20pizza=20demo=20remove=20mcp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BotSharp.OpenAPI/BotSharp.OpenAPI.csproj | 3 +- .../Controllers/AgentController.cs | 19 +++++++++- src/WebStarter/WebStarter.csproj | 2 +- .../Functions/GetPizzaPricesFn.cs | 36 +++++++++--------- .../Functions/MakePaymentFn.cs | 32 ++++++++-------- .../Functions/PlaceOrderFn.cs | 38 +++++++++---------- .../Hooks/PizzaBotConversationHook.cs | 2 + 7 files changed, 76 insertions(+), 56 deletions(-) diff --git a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj index 9162cd16..121afd35 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj +++ b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj @@ -47,7 +47,8 @@ - + + diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index 00c82ebe..6e49658d 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -1,4 +1,7 @@ using BotSharp.Abstraction.Agents.Models; +using BotSharp.Core.Mcp; +using ModelContextProtocol.Client; +using ModelContextProtocol.Protocol.Types; namespace BotSharp.OpenAPI.Controllers; @@ -9,15 +12,19 @@ public class AgentController : ControllerBase private readonly IAgentService _agentService; private readonly IUserIdentity _user; private readonly IServiceProvider _services; + private readonly MCPClientManager _clientManager; public AgentController( IAgentService agentService, IUserIdentity user, - IServiceProvider services) + IServiceProvider services, + MCPClientManager mCPClientManager + ) { _agentService = agentService; _user = user; _services = services; + _clientManager = mCPClientManager; } [HttpGet("/agent/settings")] @@ -167,6 +174,16 @@ public class AgentController : ControllerBase return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList(); } + [HttpGet("/agent/mcp/tools")] + public async Task> GetMCPTools(string serverId) + { + var client = await _clientManager.GetMcpClientAsync(serverId); + var tools = await client.ListToolsAsync().ToListAsync(); + + return tools.Where(x => !string.IsNullOrWhiteSpace(x.Name)) + .OrderBy(x => x.Name).ToList(); + } + [HttpGet("/agent/labels")] public async Task> GetAgentLabels() { diff --git a/src/WebStarter/WebStarter.csproj b/src/WebStarter/WebStarter.csproj index c2f3f0ed..b151edf3 100644 --- a/src/WebStarter/WebStarter.csproj +++ b/src/WebStarter/WebStarter.csproj @@ -30,7 +30,6 @@ - @@ -73,6 +72,7 @@ + diff --git a/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaPricesFn.cs b/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaPricesFn.cs index 9433c6a8..4fa39c7c 100644 --- a/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaPricesFn.cs +++ b/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaPricesFn.cs @@ -1,21 +1,21 @@ -//using BotSharp.Abstraction.Conversations.Models; -//using System.Text.Json; +using BotSharp.Abstraction.Conversations.Models; +using System.Text.Json; -//namespace BotSharp.Plugin.PizzaBot.Functions; +namespace BotSharp.Plugin.PizzaBot.Functions; -//public class GetPizzaPricesFn : IFunctionCallback -//{ -// public string Name => "get_pizza_price"; +public class GetPizzaPricesFn : IFunctionCallback +{ + public string Name => "get_pizza_price"; -// public async Task Execute(RoleDialogModel message) -// { -// message.Data = new -// { -// pepperoni_unit_price = 3.2, -// cheese_unit_price = 3.5, -// margherita_unit_price = 3.8, -// }; -// message.Content = JsonSerializer.Serialize(message.Data); -// return true; -// } -//} + public async Task Execute(RoleDialogModel message) + { + message.Data = new + { + pepperoni_unit_price = 3.2, + cheese_unit_price = 3.5, + margherita_unit_price = 3.8, + }; + message.Content = JsonSerializer.Serialize(message.Data); + return true; + } +} diff --git a/tests/BotSharp.Plugin.PizzaBot/Functions/MakePaymentFn.cs b/tests/BotSharp.Plugin.PizzaBot/Functions/MakePaymentFn.cs index 5a34a6ce..144eb05a 100644 --- a/tests/BotSharp.Plugin.PizzaBot/Functions/MakePaymentFn.cs +++ b/tests/BotSharp.Plugin.PizzaBot/Functions/MakePaymentFn.cs @@ -1,19 +1,19 @@ -//using BotSharp.Abstraction.Conversations.Models; +using BotSharp.Abstraction.Conversations.Models; -//namespace BotSharp.Plugin.PizzaBot.Functions; +namespace BotSharp.Plugin.PizzaBot.Functions; -//public class MakePaymentFn : IFunctionCallback -//{ -// public string Name => "make_payment"; +public class MakePaymentFn : IFunctionCallback +{ + public string Name => "make_payment"; -// public async Task Execute(RoleDialogModel message) -// { -// message.Content = "Payment proceed successfully. Thank you for your business. Have a great day!"; -// message.Data = new -// { -// Transaction = Guid.NewGuid().ToString(), -// Status = "Success" -// }; -// return true; -// } -//} + public async Task Execute(RoleDialogModel message) + { + message.Content = "Payment proceed successfully. Thank you for your business. Have a great day!"; + message.Data = new + { + Transaction = Guid.NewGuid().ToString(), + Status = "Success" + }; + return true; + } +} diff --git a/tests/BotSharp.Plugin.PizzaBot/Functions/PlaceOrderFn.cs b/tests/BotSharp.Plugin.PizzaBot/Functions/PlaceOrderFn.cs index 47d27363..87466956 100644 --- a/tests/BotSharp.Plugin.PizzaBot/Functions/PlaceOrderFn.cs +++ b/tests/BotSharp.Plugin.PizzaBot/Functions/PlaceOrderFn.cs @@ -1,24 +1,24 @@ -//using BotSharp.Abstraction.Conversations; -//using BotSharp.Abstraction.Conversations.Models; +using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Conversations.Models; -//namespace BotSharp.Plugin.PizzaBot.Functions; +namespace BotSharp.Plugin.PizzaBot.Functions; -//public class PlaceOrderFn : IFunctionCallback -//{ -// public string Name => "place_an_order"; +public class PlaceOrderFn : IFunctionCallback +{ + public string Name => "place_an_order"; -// private readonly IServiceProvider _service; -// public PlaceOrderFn(IServiceProvider service) -// { -// _service = service; -// } + private readonly IServiceProvider _service; + public PlaceOrderFn(IServiceProvider service) + { + _service = service; + } -// public async Task Execute(RoleDialogModel message) -// { -// message.Content = "The order number is P123-01"; -// var state = _service.GetRequiredService(); -// state.SetState("order_number", "P123-01"); + public async Task Execute(RoleDialogModel message) + { + message.Content = "The order number is P123-01"; + var state = _service.GetRequiredService(); + state.SetState("order_number", "P123-01"); -// return true; -// } -//} + return true; + } +} diff --git a/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotConversationHook.cs b/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotConversationHook.cs index 92083ee7..9b31371f 100644 --- a/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotConversationHook.cs +++ b/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotConversationHook.cs @@ -37,11 +37,13 @@ public class PizzaBotConversationHook : ConversationHookBase var agentService = _services.GetRequiredService(); var state = _services.GetRequiredService(); var agent = await agentService.LoadAgent(message.CurrentAgentId); +#if USE_BOTSHARP if (agent.McpTools.Any(item => item.Functions.Any(x => x.Name == message.FunctionName))) { var data = JsonDocument.Parse(JsonSerializer.Serialize(message.Data)); state.SaveStateByArgs(data); } +#endif await base.OnResponseGenerated(message); } } From 71336f9ecf555b697f12ae10798721856db4f2c8 Mon Sep 17 00:00:00 2001 From: geffzhang Date: Fri, 28 Mar 2025 09:49:54 +0800 Subject: [PATCH 2/4] Update PizzaBotConversationHook.cs --- .../Hooks/PizzaBotConversationHook.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotConversationHook.cs b/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotConversationHook.cs index 9b31371f..3ebd4df3 100644 --- a/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotConversationHook.cs +++ b/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotConversationHook.cs @@ -32,18 +32,20 @@ public class PizzaBotConversationHook : ConversationHookBase return base.OnTaskCompleted(message); } + #if USE_BOTSHARP public override async Task OnResponseGenerated(RoleDialogModel message) { var agentService = _services.GetRequiredService(); var state = _services.GetRequiredService(); var agent = await agentService.LoadAgent(message.CurrentAgentId); -#if USE_BOTSHARP + if (agent.McpTools.Any(item => item.Functions.Any(x => x.Name == message.FunctionName))) { var data = JsonDocument.Parse(JsonSerializer.Serialize(message.Data)); state.SaveStateByArgs(data); } -#endif + await base.OnResponseGenerated(message); } + #endif } From b80cba3cfc640b5c48da3b74a891f7ab6b4fe1cc Mon Sep 17 00:00:00 2001 From: geffzhang Date: Fri, 28 Mar 2025 09:55:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix=EF=BC=9A=20pizza=20demo=20remove=20mcp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../functions/get_pizza_price.json | 36 +++++++-------- .../functions/place_an_order.json | 44 +++++++++---------- .../functions/make_payment.json | 36 +++++++-------- 3 files changed, 58 insertions(+), 58 deletions(-) diff --git a/tests/BotSharp.Plugin.PizzaBot/data/agents/c2b57a74-ae4e-4c81-b3ad-9ac5bff982bd/functions/get_pizza_price.json b/tests/BotSharp.Plugin.PizzaBot/data/agents/c2b57a74-ae4e-4c81-b3ad-9ac5bff982bd/functions/get_pizza_price.json index 1ecdbb65..e2eaac31 100644 --- a/tests/BotSharp.Plugin.PizzaBot/data/agents/c2b57a74-ae4e-4c81-b3ad-9ac5bff982bd/functions/get_pizza_price.json +++ b/tests/BotSharp.Plugin.PizzaBot/data/agents/c2b57a74-ae4e-4c81-b3ad-9ac5bff982bd/functions/get_pizza_price.json @@ -1,18 +1,18 @@ -//{ -// "name": "get_pizza_price", -// "description": "call this function to get the pizza price", -// "parameters": { -// "type": "object", -// "properties": { -// "pizza_type": { -// "type": "string", -// "description": "The pizza type." -// }, -// "quantity": { -// "type": "string", -// "description": "quantity of pizza." -// } -// }, -// "required": [ "pizza_type", "quantity" ] -// } -//} \ No newline at end of file +{ + "name": "get_pizza_price", + "description": "call this function to get the pizza price", + "parameters": { + "type": "object", + "properties": { + "pizza_type": { + "type": "string", + "description": "The pizza type." + }, + "quantity": { + "type": "string", + "description": "quantity of pizza." + } + }, + "required": [ "pizza_type", "quantity" ] + } +} \ No newline at end of file diff --git a/tests/BotSharp.Plugin.PizzaBot/data/agents/c2b57a74-ae4e-4c81-b3ad-9ac5bff982bd/functions/place_an_order.json b/tests/BotSharp.Plugin.PizzaBot/data/agents/c2b57a74-ae4e-4c81-b3ad-9ac5bff982bd/functions/place_an_order.json index a01f0005..d5514fba 100644 --- a/tests/BotSharp.Plugin.PizzaBot/data/agents/c2b57a74-ae4e-4c81-b3ad-9ac5bff982bd/functions/place_an_order.json +++ b/tests/BotSharp.Plugin.PizzaBot/data/agents/c2b57a74-ae4e-4c81-b3ad-9ac5bff982bd/functions/place_an_order.json @@ -1,22 +1,22 @@ -//{ -// "name": "place_an_order", -// "description": "Place an order when user has confirmed the pizza type and quantity.", -// "parameters": { -// "type": "object", -// "properties": { -// "pizza_type": { -// "type": "string", -// "description": "The pizza type." -// }, -// "quantity": { -// "type": "number", -// "description": "quantity of pizza." -// }, -// "unit_price": { -// "type": "number", -// "description": "unit price" -// } -// }, -// "required": [ "pizza_type", "quantity", "unit_price" ] -// } -//} \ No newline at end of file +{ + "name": "place_an_order", + "description": "Place an order when user has confirmed the pizza type and quantity.", + "parameters": { + "type": "object", + "properties": { + "pizza_type": { + "type": "string", + "description": "The pizza type." + }, + "quantity": { + "type": "number", + "description": "quantity of pizza." + }, + "unit_price": { + "type": "number", + "description": "unit price" + } + }, + "required": [ "pizza_type", "quantity", "unit_price" ] + } +} \ No newline at end of file diff --git a/tests/BotSharp.Plugin.PizzaBot/data/agents/fe8c60aa-b114-4ef3-93cb-a8efeac80f75/functions/make_payment.json b/tests/BotSharp.Plugin.PizzaBot/data/agents/fe8c60aa-b114-4ef3-93cb-a8efeac80f75/functions/make_payment.json index 914f215e..82782c94 100644 --- a/tests/BotSharp.Plugin.PizzaBot/data/agents/fe8c60aa-b114-4ef3-93cb-a8efeac80f75/functions/make_payment.json +++ b/tests/BotSharp.Plugin.PizzaBot/data/agents/fe8c60aa-b114-4ef3-93cb-a8efeac80f75/functions/make_payment.json @@ -1,18 +1,18 @@ -//{ -// "name": "make_payment", -// "description": "call this function to make payment", -// "parameters": { -// "type": "object", -// "properties": { -// "order_number": { -// "type": "string", -// "description": "order number." -// }, -// "total_amount": { -// "type": "string", -// "description": "total amount." -// } -// }, -// "required": [ "order_number", "total_amount" ] -// } -//} \ No newline at end of file +{ + "name": "make_payment", + "description": "call this function to make payment", + "parameters": { + "type": "object", + "properties": { + "order_number": { + "type": "string", + "description": "order number." + }, + "total_amount": { + "type": "string", + "description": "total amount." + } + }, + "required": [ "order_number", "total_amount" ] + } +} \ No newline at end of file From d439d38bb9a68bc3ebff7260bc7f7ae4b153afba Mon Sep 17 00:00:00 2001 From: geffzhang Date: Fri, 28 Mar 2025 18:13:08 +0800 Subject: [PATCH 4/4] =?UTF-8?q?feat=EF=BC=9A=20upgrade=20ModelContextProto?= =?UTF-8?q?col=200.1.0-preview.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Packages.props | 3 ++- tests/BotSharp.PizzaBot.MCPServer/Program.cs | 3 ++- tests/BotSharp.PizzaBot.MCPServer/Tools/MakePayment.cs | 4 ++-- tests/BotSharp.PizzaBot.MCPServer/Tools/PizzaPrices.cs | 4 ++-- tests/BotSharp.PizzaBot.MCPServer/Tools/PlaceOrder.cs | 4 ++-- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 3354b992..91fe2944 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -110,7 +110,8 @@ - + + diff --git a/tests/BotSharp.PizzaBot.MCPServer/Program.cs b/tests/BotSharp.PizzaBot.MCPServer/Program.cs index fb39e6cd..d2cf912e 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/Program.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/Program.cs @@ -2,7 +2,8 @@ using BotSharp.PizzaBot.MCPServer; using ModelContextProtocol; var builder = WebApplication.CreateBuilder(args); -builder.Services.AddMcpServer().WithTools(); +builder.Services.AddMcpServer() + .WithToolsFromAssembly(); var app = builder.Build(); app.MapGet("/", () => "This is a test server with only stub functionality!"); diff --git a/tests/BotSharp.PizzaBot.MCPServer/Tools/MakePayment.cs b/tests/BotSharp.PizzaBot.MCPServer/Tools/MakePayment.cs index a236d874..da198c47 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/Tools/MakePayment.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/Tools/MakePayment.cs @@ -4,10 +4,10 @@ using System.ComponentModel.DataAnnotations; namespace BotSharp.PizzaBot.MCPServer.Tools; -[McpToolType] +[McpServerToolType] public static class MakePayment { - [McpTool(name: "make_payment"), Description("call this function to make payment.")] + [McpServerTool(name: "make_payment"), Description("call this function to make payment.")] public static string Make_Payment( [Description("order number"),Required] string order_number, [Description("total amount"),Required] int total_amount) diff --git a/tests/BotSharp.PizzaBot.MCPServer/Tools/PizzaPrices.cs b/tests/BotSharp.PizzaBot.MCPServer/Tools/PizzaPrices.cs index 7ca766f3..762d67db 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/Tools/PizzaPrices.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/Tools/PizzaPrices.cs @@ -6,10 +6,10 @@ using System.Text.Json; namespace BotSharp.PizzaBot.MCPServer.Tools; -[McpToolType] +[McpServerToolType] public static class PizzaPrices { - [McpTool(name: "get_pizza_prices"), Description("call this function to get pizza unit price.")] + [McpServerTool(name: "get_pizza_prices"), Description("call this function to get pizza unit price.")] public static string GetPizzaPrices( [Description("The pizza type."), Required] string pizza_type, [Description("quantity of pizza"), Required] int quantity) diff --git a/tests/BotSharp.PizzaBot.MCPServer/Tools/PlaceOrder.cs b/tests/BotSharp.PizzaBot.MCPServer/Tools/PlaceOrder.cs index a6237b6d..73245751 100644 --- a/tests/BotSharp.PizzaBot.MCPServer/Tools/PlaceOrder.cs +++ b/tests/BotSharp.PizzaBot.MCPServer/Tools/PlaceOrder.cs @@ -4,10 +4,10 @@ using System.ComponentModel.DataAnnotations; namespace BotSharp.PizzaBot.MCPServer.Tools; -[McpToolType] +[McpServerToolType] public static class PlaceOrder { - [McpTool(name: "place_an_order"), Description("Place an order when user has confirmed the pizza type and quantity.")] + [McpServerTool(name: "place_an_order"), Description("Place an order when user has confirmed the pizza type and quantity.")] public static string PlaceAnOrder( [Description("The pizza type."), Required] string pizza_type, [Description("quantity of pizza"), Required] int quantity,