Skip to content

Commit 530e01c

Browse files
author
Jicheng Lu
committed
refine mcp settings
1 parent 832472f commit 530e01c

File tree

6 files changed

+31
-21
lines changed

6 files changed

+31
-21
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public static class BotSharpMcpExtensions
1919
public static IServiceCollection AddBotSharpMCP(this IServiceCollection services, IConfiguration config)
2020
{
2121
var settings = config.GetSection("MCP").Get<McpSettings>();
22-
services.AddScoped<McpSettings>(provider => { return settings; });
23-
if (settings != null && !settings.McpServerConfigs.IsNullOrEmpty())
22+
services.AddScoped(provider => { return settings; });
23+
if (settings != null && settings.Enabled && !settings.McpServerConfigs.IsNullOrEmpty())
2424
{
2525
var clientManager = new McpClientManager(settings);
2626
services.AddSingleton(clientManager);

src/Infrastructure/BotSharp.Core.MCP/Hooks/MCPToolAgentHook.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Core.MCP.Helpers;
22
using BotSharp.Core.MCP.Managers;
3+
using BotSharp.Core.MCP.Settings;
34
using ModelContextProtocol.Client;
45

56
namespace BotSharp.Core.MCP.Hooks;
@@ -39,6 +40,13 @@ public override void OnAgentMcpToolLoaded(Agent agent)
3940
private async Task<IEnumerable<FunctionDef>> GetMcpContent(Agent agent)
4041
{
4142
var functionDefs = new List<FunctionDef>();
43+
44+
var settings = _services.GetRequiredService<McpSettings>();
45+
if (settings?.Enabled != true)
46+
{
47+
return functionDefs;
48+
}
49+
4250
var mcpClientManager = _services.GetRequiredService<McpClientManager>();
4351
var mcps = agent.McpTools.Where(x => !x.Disabled);
4452
foreach (var item in mcps)

src/Infrastructure/BotSharp.Core.MCP/Settings/MCPSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace BotSharp.Core.MCP.Settings;
55

66
public class McpSettings
77
{
8+
public bool Enabled { get; set; } = true;
89
public McpClientOptions McpClientOptions { get; set; }
910
public List<McpServerConfig> McpServerConfigs { get; set; } = new();
1011

src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,4 @@ await hook.OnResponseGenerated(new InstructResponseModel
227227

228228
return message;
229229
}
230-
231-
#region Private methods
232-
private async Task<byte[]> DownloadFile(InstructFileModel file)
233-
{
234-
var bytes = new byte[0];
235-
if (!string.IsNullOrEmpty(file.FileUrl))
236-
{
237-
var http = _services.GetRequiredService<IHttpClientFactory>();
238-
using var client = http.CreateClient();
239-
bytes = await client.GetByteArrayAsync(file.FileUrl);
240-
}
241-
else if (!string.IsNullOrEmpty(file.FileData))
242-
{
243-
(_, bytes) = FileUtility.GetFileInfoFromData(file.FileData);
244-
}
245-
246-
return bytes;
247-
}
248-
#endregion
249230
}

src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public FileInstructService(
1919
_services = services;
2020
}
2121

22+
#region Private methods
2223
private void DeleteIfExistDirectory(string? dir, bool createNew = false)
2324
{
2425
if (_fileStorage.ExistDirectory(dir))
@@ -31,6 +32,23 @@ private void DeleteIfExistDirectory(string? dir, bool createNew = false)
3132
}
3233
}
3334

35+
private async Task<byte[]> DownloadFile(InstructFileModel file)
36+
{
37+
var bytes = new byte[0];
38+
if (!string.IsNullOrEmpty(file.FileUrl))
39+
{
40+
var http = _services.GetRequiredService<IHttpClientFactory>();
41+
using var client = http.CreateClient();
42+
bytes = await client.GetByteArrayAsync(file.FileUrl);
43+
}
44+
else if (!string.IsNullOrEmpty(file.FileData))
45+
{
46+
(_, bytes) = FileUtility.GetFileInfoFromData(file.FileData);
47+
}
48+
49+
return bytes;
50+
}
51+
3452
private async Task<string?> GetAgentTemplate(string agentId, string? templateName)
3553
{
3654
if (string.IsNullOrWhiteSpace(agentId) || string.IsNullOrWhiteSpace(templateName))
@@ -48,4 +66,5 @@ private void DeleteIfExistDirectory(string? dir, bool createNew = false)
4866
var instruction = agentService.RenderedTemplate(agent, templateName);
4967
return instruction;
5068
}
69+
#endregion
5170
}

src/WebStarter/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
},
174174

175175
"MCP": {
176+
"Enabled": true,
176177
"McpClientOptions": {
177178
"ClientInfo": {
178179
"Name": "SimpleToolsBotsharp",

0 commit comments

Comments
 (0)