Merge pull request #982 from iceljc/master

add mcp service
This commit is contained in:
geffzhang 2025-04-02 07:07:22 +08:00 committed by GitHub
commit db4dcb5571
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 107 additions and 3 deletions

View file

@ -0,0 +1,38 @@
namespace BotSharp.Abstraction.MCP.Models;
public class McpServerConfigModel
{
/// <summary>
/// Unique identifier for this server configuration.
/// </summary>
public string Id { get; set; } = null!;
/// <summary>
/// Display name for the server.
/// </summary>
public string Name { get; set; } = null!;
/// <summary>
/// The type of transport to use.
/// </summary>
[JsonPropertyName("transport_type")]
public string TransportType { get; set; } = null!;
/// <summary>
/// For stdio transport: path to the executable
/// For HTTP transport: base URL of the server
/// </summary>
public string? Location { get; set; }
/// <summary>
/// Arguments (if any) to pass to the executable.
/// </summary>
public string[]? Arguments { get; set; }
/// <summary>
/// Additional transport-specific configuration.
/// </summary>
[JsonPropertyName("transport_options")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, string>? TransportOptions { get; set; }
}

View file

@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.MCP.Services;
public interface IMcpService
{
IEnumerable<McpServerConfigModel> GetServerConfigs() => [];
}

View file

@ -18,4 +18,5 @@ global using BotSharp.Abstraction.Messaging.Enums;
global using BotSharp.Abstraction.Files.Models;
global using BotSharp.Abstraction.Files.Enums;
global using BotSharp.Abstraction.Knowledges.Models;
global using BotSharp.Abstraction.Crontab.Models;
global using BotSharp.Abstraction.Crontab.Models;
global using BotSharp.Abstraction.MCP.Models;

View file

@ -20,6 +20,7 @@ public static class BotSharpMcpExtensions
{
var settings = config.GetSection("MCP").Get<McpSettings>();
services.AddScoped(provider => { return settings; });
if (settings != null && settings.Enabled && !settings.McpServerConfigs.IsNullOrEmpty())
{
var clientManager = new McpClientManager(settings);

View file

@ -1,8 +1,9 @@
using Microsoft.Extensions.Configuration;
using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Plugins;
using BotSharp.Abstraction.Settings;
using BotSharp.Core.MCP.Settings;
using Microsoft.Extensions.Configuration;
using BotSharp.Core.MCP.Services;
namespace BotSharp.Core.MCP;
@ -20,6 +21,7 @@ public class McpPlugin : IBotSharpPlugin
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<IMcpService, McpService>();
}
public bool AttachMenu(List<PluginMenuDef> menu)

View file

@ -0,0 +1,33 @@
using BotSharp.Core.MCP.Settings;
using Microsoft.Extensions.Logging;
namespace BotSharp.Core.MCP.Services;
public class McpService : IMcpService
{
private readonly IServiceProvider _services;
private readonly ILogger<McpService> _logger;
public McpService(
IServiceProvider services,
ILogger<McpService> logger)
{
_services = services;
_logger = logger;
}
public IEnumerable<McpServerConfigModel> GetServerConfigs()
{
var settings = _services.GetRequiredService<McpSettings>();
var configs = settings?.McpServerConfigs ?? [];
return configs.Select(x => new McpServerConfigModel
{
Id = x.Id,
Name = x.Name,
TransportType = x.TransportType,
TransportOptions = x.TransportOptions,
Arguments = x.Arguments,
Location = x.Location
});
}
}

View file

@ -13,4 +13,6 @@ global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Functions;
global using BotSharp.Abstraction.Functions.Models;
global using BotSharp.Abstraction.Utilities;
global using BotSharp.Abstraction.MCP.Models;
global using BotSharp.Abstraction.MCP.Services;

View file

@ -0,0 +1,21 @@
using BotSharp.Abstraction.MCP.Models;
using BotSharp.Abstraction.MCP.Services;
namespace BotSharp.OpenAPI.Controllers;
public class McpController : ControllerBase
{
private readonly IServiceProvider _services;
public McpController(IServiceProvider services)
{
_services = services;
}
[HttpGet("/mcp/server-configs")]
public IEnumerable<McpServerConfigModel> GetMcpServerConfigs()
{
var mcp = _services.GetRequiredService<IMcpService>();
return mcp.GetServerConfigs();
}
}

View file

@ -1,6 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;
using System.Text.Json.Serialization;
namespace BotSharp.OpenAPI.ViewModels.Agents;
@ -42,6 +41,7 @@ public class AgentUpdateModel
/// <summary>
/// McpTools
/// </summary>
[JsonPropertyName("mcp_tools")]
public List<McpTool>? McpTools { get; set; }
/// <summary>