Merge branch 'master' into dev_fix_pizza

This commit is contained in:
Gil Zhang 2025-03-28 10:35:24 +08:00
commit 0df79b0371
10 changed files with 136 additions and 114 deletions

View file

@ -47,7 +47,8 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\BotSharp.MCP\BotSharp.MCP.csproj" />
</ItemGroup>
<ItemGroup>

View file

@ -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<IEnumerable<Tool>> 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<IEnumerable<string>> GetAgentLabels()
{

View file

@ -79,7 +79,6 @@
<ItemGroup>
<ProjectReference Include="..\..\tests\BotSharp.Plugin.PizzaBot\BotSharp.Plugin.PizzaBot.csproj" />
<ProjectReference Include="..\BotSharp.ServiceDefaults\BotSharp.ServiceDefaults.csproj" />
<ProjectReference Include="..\Infrastructure\BotSharp.MCP\BotSharp.MCP.csproj" />
</ItemGroup>
<ItemGroup Condition="$(SolutionName)==BotSharp">
@ -122,6 +121,7 @@
<ProjectReference Include="..\Plugins\BotSharp.Plugin.MetaGLM\BotSharp.Plugin.MetaGLM.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.Planner\BotSharp.Plugin.Planner.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.TencentCos\BotSharp.Plugin.TencentCos.csproj" />
<ProjectReference Include="..\Infrastructure\BotSharp.MCP\BotSharp.MCP.csproj" />
</ItemGroup>
<ItemGroup>

View file

@ -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<bool> 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<bool> 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;
}
}

View file

@ -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<bool> 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<bool> 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;
}
}

View file

@ -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<bool> Execute(RoleDialogModel message)
// {
// message.Content = "The order number is P123-01";
// var state = _service.GetRequiredService<IConversationStateService>();
// state.SetState("order_number", "P123-01");
public async Task<bool> Execute(RoleDialogModel message)
{
message.Content = "The order number is P123-01";
var state = _service.GetRequiredService<IConversationStateService>();
state.SetState("order_number", "P123-01");
// return true;
// }
//}
return true;
}
}

View file

@ -32,16 +32,20 @@ public class PizzaBotConversationHook : ConversationHookBase
return base.OnTaskCompleted(message);
}
#if USE_BOTSHARP
public override async Task OnResponseGenerated(RoleDialogModel message)
{
var agentService = _services.GetRequiredService<IAgentService>();
var state = _services.GetRequiredService<IConversationStateService>();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
if (agent.McpTools.Any(item => item.Functions.Any(x => x.Name == message.FunctionName)))
{
var data = JsonDocument.Parse(JsonSerializer.Serialize(message.Data));
state.SaveStateByArgs(data);
}
await base.OnResponseGenerated(message);
}
#endif
}

View file

@ -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" ]
// }
//}
{
"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" ]
}
}

View file

@ -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" ]
// }
//}
{
"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" ]
}
}

View file

@ -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" ]
// }
//}
{
"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" ]
}
}