feat:refactor mcp with extensions
This commit is contained in:
parent
4b411e464a
commit
1ddbc33b16
68
src/Infrastructure/BotSharp.MCP/BotSharpMCPExtensions.cs
Normal file
68
src/Infrastructure/BotSharp.MCP/BotSharpMCPExtensions.cs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Functions;
|
||||
using BotSharp.Core.Mcp.Functions;
|
||||
using BotSharp.Core.Mcp.Settings;
|
||||
using BotSharp.Core.Mcp;
|
||||
using BotSharp.MCP.Hooks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using ModelContextProtocol.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ModelContextProtocol.Client;
|
||||
|
||||
namespace BotSharp.MCP;
|
||||
|
||||
public static class BotSharpMCPExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Add mcp
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddBotSharpMCP(this IServiceCollection services,
|
||||
IConfiguration config)
|
||||
{
|
||||
var settings = config.GetSection("MCPSettings").Get<MCPSettings>();
|
||||
services.AddScoped<MCPSettings>(provider => { return settings; });
|
||||
if (settings != null)
|
||||
{
|
||||
|
||||
var clientManager = new MCPClientManager(settings);
|
||||
services.AddSingleton(clientManager);
|
||||
|
||||
foreach (var server in settings.McpServerConfigs)
|
||||
{
|
||||
RegisterFunctionCall(services, server, clientManager)
|
||||
.ConfigureAwait(false)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}
|
||||
// Register hooks
|
||||
services.AddScoped<IAgentHook, MCPToolAgentHook>();
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
private static async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server, MCPClientManager clientManager)
|
||||
{
|
||||
var client = await clientManager.GetMcpClientAsync(server.Id);
|
||||
var tools = await client.ListToolsAsync();
|
||||
|
||||
foreach (var tool in tools)
|
||||
{
|
||||
services.AddScoped(provider => { return tool; });
|
||||
|
||||
services.AddScoped<IFunctionCallback>(provider =>
|
||||
{
|
||||
var funcTool = new McpToolAdapter(provider, tool, clientManager);
|
||||
return funcTool;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Conversations;
|
||||
using BotSharp.Abstraction.Functions;
|
||||
using BotSharp.Abstraction.Plugins;
|
||||
using BotSharp.Core.Mcp.Functions;
|
||||
using BotSharp.Core.Mcp.Settings;
|
||||
using BotSharp.MCP.Hooks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using ModelContextProtocol.Client;
|
||||
using ModelContextProtocol.Configuration;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Core.Mcp;
|
||||
|
||||
public class McpPlugin : IBotSharpPlugin
|
||||
{
|
||||
public string Id => "5d779611-0012-46cb-a754-4ca4770e88ac";
|
||||
public string Name => "MCP Plugin";
|
||||
public string Description => "Integrated MCP tools";
|
||||
|
||||
private MCPClientManager clientManager;
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
var settings = config.GetSection("MCPSettings").Get<MCPSettings>();
|
||||
services.AddScoped<MCPSettings>(provider => { return settings; });
|
||||
|
||||
clientManager = new MCPClientManager(settings);
|
||||
services.AddSingleton(clientManager);
|
||||
|
||||
foreach (var server in settings.McpServerConfigs)
|
||||
{
|
||||
RegisterFunctionCall(services, server)
|
||||
.ConfigureAwait(false)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}
|
||||
// Register hooks
|
||||
services.AddScoped<IAgentHook, MCPToolAgentHook>();
|
||||
}
|
||||
|
||||
private async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server)
|
||||
{
|
||||
var client = await clientManager.GetMcpClientAsync(server.Id);
|
||||
var tools = await client.ListToolsAsync();
|
||||
|
||||
foreach (var tool in tools)
|
||||
{
|
||||
services.AddScoped(provider => { return tool; });
|
||||
|
||||
services.AddScoped<IFunctionCallback>(provider =>
|
||||
{
|
||||
var funcTool = new McpToolAdapter(provider, tool, clientManager);
|
||||
return funcTool;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,6 @@ public class MCPSettings
|
|||
{
|
||||
public McpClientOptions McpClientOptions { get; set; }
|
||||
|
||||
public List<McpServerConfig> McpServerConfigs { get; set; }
|
||||
public List<McpServerConfig> McpServerConfigs { get; set; } = new();
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ using BotSharp.Plugin.ChatHub;
|
|||
using Serilog;
|
||||
using BotSharp.Abstraction.Messaging.JsonConverters;
|
||||
using StackExchange.Redis;
|
||||
using BotSharp.MCP;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
|
|
@ -23,6 +24,7 @@ string[] allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get
|
|||
options.JsonSerializerOptions.Converters.Add(new RichContentJsonConverter());
|
||||
options.JsonSerializerOptions.Converters.Add(new TemplateMessageJsonConverter());
|
||||
}).AddBotSharpOpenAPI(builder.Configuration, allowedOrigins, builder.Environment, true)
|
||||
.AddBotSharpMCP(builder.Configuration)
|
||||
.AddBotSharpLogger(builder.Configuration);
|
||||
|
||||
// Add service defaults & Aspire components.
|
||||
|
|
|
|||
|
|
@ -171,23 +171,23 @@
|
|||
"Model": "gpt-4o-mini"
|
||||
}
|
||||
},
|
||||
"MCPSettings": {
|
||||
"McpClientOptions": {
|
||||
"ClientInfo": {
|
||||
"Name": "SimpleToolsBotsharp",
|
||||
"Version": "1.0.0"
|
||||
}
|
||||
},
|
||||
"McpServerConfigs": [
|
||||
{
|
||||
"Id": "PizzaServer",
|
||||
"Name": "PizzaServer",
|
||||
"TransportType": "sse",
|
||||
"TransportOptions": [],
|
||||
"Location": "http://localhost:58905/sse"
|
||||
}
|
||||
]
|
||||
},
|
||||
//"MCPSettings": {
|
||||
// "McpClientOptions": {
|
||||
// "ClientInfo": {
|
||||
// "Name": "SimpleToolsBotsharp",
|
||||
// "Version": "1.0.0"
|
||||
// }
|
||||
// },
|
||||
// "McpServerConfigs": [
|
||||
// {
|
||||
// "Id": "PizzaServer",
|
||||
// "Name": "PizzaServer",
|
||||
// "TransportType": "sse",
|
||||
// "TransportOptions": [],
|
||||
// "Location": "http://localhost:58905/sse"
|
||||
// }
|
||||
// ]
|
||||
//},
|
||||
"Conversation": {
|
||||
"DataDir": "conversations",
|
||||
"ShowVerboseLog": false,
|
||||
|
|
@ -413,7 +413,6 @@
|
|||
"BotSharp.Core.Crontab",
|
||||
"BotSharp.Core.Realtime",
|
||||
"BotSharp.Logger",
|
||||
"BotSharp.MCP",
|
||||
"BotSharp.Plugin.MongoStorage",
|
||||
"BotSharp.Plugin.Dashboard",
|
||||
"BotSharp.Plugin.OpenAI",
|
||||
|
|
|
|||
Loading…
Reference in a new issue