fix: pizza demo remove mcp

This commit is contained in:
geffzhang 2025-03-28 09:46:01 +08:00
parent f0bf0b443c
commit 451e7802eb
7 changed files with 76 additions and 56 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

@ -30,7 +30,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">
@ -73,6 +72,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

@ -37,11 +37,13 @@ public class PizzaBotConversationHook : ConversationHookBase
var agentService = _services.GetRequiredService<IAgentService>();
var state = _services.GetRequiredService<IConversationStateService>();
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);
}
}