Skip to content

refine init mcp #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs
Original file line number Diff line number Diff line change
@@ -43,18 +43,22 @@ public static IServiceCollection AddBotSharpMCP(this IServiceCollection services

private static async Task RegisterFunctionCall(IServiceCollection services, McpServerConfigModel server, McpClientManager clientManager)
{
var client = await clientManager.GetMcpClientAsync(server.Id);
var tools = await client.ListToolsAsync();

foreach (var tool in tools)
try
{
services.AddScoped(provider => tool);
var client = await clientManager.GetMcpClientAsync(server.Id);
var tools = await client.ListToolsAsync();

services.AddScoped<IFunctionCallback>(provider =>
foreach (var tool in tools)
{
var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager);
return funcTool;
});
services.AddScoped(provider => tool);

services.AddScoped<IFunctionCallback>(provider =>
{
var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager);
return funcTool;
});
}
}
catch { }
}
}
38 changes: 24 additions & 14 deletions src/Infrastructure/BotSharp.Core.MCP/Functions/McpToolAdapter.cs
Original file line number Diff line number Diff line change
@@ -28,30 +28,40 @@ public McpToolAdapter(

public async Task<bool> Execute(RoleDialogModel message)
{
// Convert arguments to dictionary format expected by mcpdotnet
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
var currentAgentId = message.CurrentAgentId;
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(currentAgentId);
var serverId = agent.McpTools.Where(t => t.Functions.Any(f => f.Name == Name)).FirstOrDefault().ServerId;
try
{
// Convert arguments to dictionary format expected by mcpdotnet
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
var currentAgentId = message.CurrentAgentId;
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(currentAgentId);
var serverId = agent.McpTools.Where(t => t.Functions.Any(f => f.Name == Name)).FirstOrDefault().ServerId;

var client = await _clientManager.GetMcpClientAsync(serverId);
var client = await _clientManager.GetMcpClientAsync(serverId);

// Call the tool through mcpdotnet
var result = await client.CallToolAsync(_tool.Name, argDict.IsNullOrEmpty() ? new() : argDict);
// Call the tool through mcpdotnet
var result = await client.CallToolAsync(_tool.Name, !argDict.IsNullOrEmpty() ? argDict : []);

// Extract the text content from the result
var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text));
// Extract the text content from the result
var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text));

message.Content = json;
message.Data = json.JsonContent();
return true;
message.Content = json;
message.Data = json.JsonContent();
return true;
}
catch (Exception ex)
{
message.Content = $"Error when calling tool {Name} of MCP server {Provider}. {ex.Message}";
return false;
}
}

private static Dictionary<string, object> JsonToDictionary(string? json)
{
if (string.IsNullOrEmpty(json))
{
return [];
}

using JsonDocument doc = JsonDocument.Parse(json);
JsonElement root = doc.RootElement;

Unchanged files with check annotations Beta

public class UserActivationModel
{
public string UserName { get; set; }

Check warning on line 5 in src/Infrastructure/BotSharp.Abstraction/Users/Models/UserActivationModel.cs

GitHub Actions / build (macos-latest)

Non-nullable property 'UserName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 5 in src/Infrastructure/BotSharp.Abstraction/Users/Models/UserActivationModel.cs

GitHub Actions / build (macos-latest)

Non-nullable property 'UserName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 5 in src/Infrastructure/BotSharp.Abstraction/Users/Models/UserActivationModel.cs

GitHub Actions / build (windows-latest)

Non-nullable property 'UserName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string VerificationCode { get; set; }
public string RegionCode { get; set; } = "CN";
}
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
User? GetUserByUserName(string userName) => throw new NotImplementedException();
void UpdateUserName(string userId, string userName) => throw new NotImplementedException();
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.
void CreateUser(User user) => throw new NotImplementedException();
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
void UpdateUserVerified(string userId) => throw new NotImplementedException();
Task DoAction(MessageInfo message, ElementActionArgs action, BrowserActionResult result);
Task PressKey(MessageInfo message, string key);
Task<BrowserActionResult> InputUserText(BrowserActionParams actionParams);

Check warning on line 17 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 17 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 17 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 17 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 17 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 17 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'
Task<BrowserActionResult> InputUserPassword(BrowserActionParams actionParams);

Check warning on line 18 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 18 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 18 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 18 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 18 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 18 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'
Task<BrowserActionResult> ClickButton(BrowserActionParams actionParams);

Check warning on line 19 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 19 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 19 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 19 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 19 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 19 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'
Task<BrowserActionResult> ClickElement(BrowserActionParams actionParams);

Check warning on line 20 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 20 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 20 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 20 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 20 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 20 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'
Task<BrowserActionResult> ChangeListValue(BrowserActionParams actionParams);

Check warning on line 21 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 21 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 21 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 21 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 21 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 21 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'
Task<BrowserActionResult> CheckRadioButton(BrowserActionParams actionParams);

Check warning on line 22 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 22 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 22 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 22 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 22 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 22 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'
Task<BrowserActionResult> ChangeCheckbox(BrowserActionParams actionParams);

Check warning on line 23 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 23 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 23 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 23 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 23 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 23 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'
Task<BrowserActionResult> GoToPage(MessageInfo message, PageActionArgs args);
Task<string> ExtractData(BrowserActionParams actionParams);

Check warning on line 25 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 25 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (macos-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 25 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 25 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (ubuntu-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 25 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'

Check warning on line 25 in src/Infrastructure/BotSharp.Abstraction/Browsing/IWebBrowser.cs

GitHub Actions / build (windows-latest)

'BrowserActionParams' is obsolete: 'This class is deprecated, use BrowserActionArgs instead.'
Task<T> EvaluateScript<T>(string contextId, string script);
Task CloseBrowser(string contextId);
Task<bool> IsBrowserClosed(string contextId);
public class VectorUpdateModel : VectorCreateModel
{
public string Id { get; set; }

Check warning on line 5 in src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorUpdateModel.cs

GitHub Actions / build (ubuntu-latest)

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 5 in src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorUpdateModel.cs

GitHub Actions / build (ubuntu-latest)

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
public class RoutingProfileRecord : RecordBase
{
public string Name { get; set; }

Check warning on line 7 in src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingProfileRecord.cs

GitHub Actions / build (windows-latest)

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public List<string> AgentIds { get; set; }
}