Skip to content

Commit ab0399a

Browse files
author
Jicheng Lu
committed
add mcp service
1 parent a3cb654 commit ab0399a

File tree

9 files changed

+107
-3
lines changed

9 files changed

+107
-3
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace BotSharp.Abstraction.MCP.Models;
2+
3+
public class McpServerConfigModel
4+
{
5+
/// <summary>
6+
/// Unique identifier for this server configuration.
7+
/// </summary>
8+
public string Id { get; set; } = null!;
9+
10+
/// <summary>
11+
/// Display name for the server.
12+
/// </summary>
13+
public string Name { get; set; } = null!;
14+
15+
/// <summary>
16+
/// The type of transport to use.
17+
/// </summary>
18+
[JsonPropertyName("transport_type")]
19+
public string TransportType { get; set; } = null!;
20+
21+
/// <summary>
22+
/// For stdio transport: path to the executable
23+
/// For HTTP transport: base URL of the server
24+
/// </summary>
25+
public string? Location { get; set; }
26+
27+
/// <summary>
28+
/// Arguments (if any) to pass to the executable.
29+
/// </summary>
30+
public string[]? Arguments { get; set; }
31+
32+
/// <summary>
33+
/// Additional transport-specific configuration.
34+
/// </summary>
35+
[JsonPropertyName("transport_options")]
36+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
37+
public Dictionary<string, string>? TransportOptions { get; set; }
38+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Abstraction.MCP.Services;
2+
3+
public interface IMcpService
4+
{
5+
IEnumerable<McpServerConfigModel> GetServerConfigs() => [];
6+
}

src/Infrastructure/BotSharp.Abstraction/Using.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
global using BotSharp.Abstraction.Files.Models;
1919
global using BotSharp.Abstraction.Files.Enums;
2020
global using BotSharp.Abstraction.Knowledges.Models;
21-
global using BotSharp.Abstraction.Crontab.Models;
21+
global using BotSharp.Abstraction.Crontab.Models;
22+
global using BotSharp.Abstraction.MCP.Models;

src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static IServiceCollection AddBotSharpMCP(this IServiceCollection services
2020
{
2121
var settings = config.GetSection("MCP").Get<McpSettings>();
2222
services.AddScoped(provider => { return settings; });
23+
2324
if (settings != null && settings.Enabled && !settings.McpServerConfigs.IsNullOrEmpty())
2425
{
2526
var clientManager = new McpClientManager(settings);

src/Infrastructure/BotSharp.Core.MCP/McpPlugin.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
using Microsoft.Extensions.Configuration;
12
using BotSharp.Abstraction.Plugins.Models;
23
using BotSharp.Abstraction.Plugins;
34
using BotSharp.Abstraction.Settings;
45
using BotSharp.Core.MCP.Settings;
5-
using Microsoft.Extensions.Configuration;
6+
using BotSharp.Core.MCP.Services;
67

78
namespace BotSharp.Core.MCP;
89

@@ -20,6 +21,7 @@ public object GetNewSettingsInstance() =>
2021

2122
public void RegisterDI(IServiceCollection services, IConfiguration config)
2223
{
24+
services.AddScoped<IMcpService, McpService>();
2325
}
2426

2527
public bool AttachMenu(List<PluginMenuDef> menu)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using BotSharp.Core.MCP.Settings;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace BotSharp.Core.MCP.Services;
5+
6+
public class McpService : IMcpService
7+
{
8+
private readonly IServiceProvider _services;
9+
private readonly ILogger<McpService> _logger;
10+
11+
public McpService(
12+
IServiceProvider services,
13+
ILogger<McpService> logger)
14+
{
15+
_services = services;
16+
_logger = logger;
17+
}
18+
19+
public IEnumerable<McpServerConfigModel> GetServerConfigs()
20+
{
21+
var settings = _services.GetRequiredService<McpSettings>();
22+
var configs = settings?.McpServerConfigs ?? [];
23+
return configs.Select(x => new McpServerConfigModel
24+
{
25+
Id = x.Id,
26+
Name = x.Name,
27+
TransportType = x.TransportType,
28+
TransportOptions = x.TransportOptions,
29+
Arguments = x.Arguments,
30+
Location = x.Location
31+
});
32+
}
33+
}

src/Infrastructure/BotSharp.Core.MCP/Using.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
global using BotSharp.Abstraction.Functions;
1414
global using BotSharp.Abstraction.Functions.Models;
1515
global using BotSharp.Abstraction.Utilities;
16+
global using BotSharp.Abstraction.MCP.Models;
17+
global using BotSharp.Abstraction.MCP.Services;
1618

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using BotSharp.Abstraction.MCP.Models;
2+
using BotSharp.Abstraction.MCP.Services;
3+
4+
namespace BotSharp.OpenAPI.Controllers;
5+
6+
public class McpController : ControllerBase
7+
{
8+
private readonly IServiceProvider _services;
9+
10+
public McpController(IServiceProvider services)
11+
{
12+
_services = services;
13+
}
14+
15+
[HttpGet("/mcp/server-configs")]
16+
public IEnumerable<McpServerConfigModel> GetMcpServerConfigs()
17+
{
18+
var mcp = _services.GetRequiredService<IMcpService>();
19+
return mcp.GetServerConfigs();
20+
}
21+
}

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/Request/AgentUpdateModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using BotSharp.Abstraction.Agents.Models;
22
using BotSharp.Abstraction.Functions.Models;
3-
using BotSharp.Abstraction.Routing.Models;
43
using System.Text.Json.Serialization;
54

65
namespace BotSharp.OpenAPI.ViewModels.Agents;
@@ -42,6 +41,7 @@ public class AgentUpdateModel
4241
/// <summary>
4342
/// McpTools
4443
/// </summary>
44+
[JsonPropertyName("mcp_tools")]
4545
public List<McpTool>? McpTools { get; set; }
4646

4747
/// <summary>

0 commit comments

Comments
 (0)