Skip to content

Refactor InvokeFunction code #1044

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 0 additions & 11 deletions BotSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.Realtime", "s
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.PizzaBot.MCPServer", "tests\BotSharp.PizzaBot.MCPServer\BotSharp.PizzaBot.MCPServer.csproj", "{8D2AD45F-836A-516F-DE6A-71443CEBB18A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.MCP", "src\Infrastructure\BotSharp.Core.MCP\BotSharp.Core.MCP.csproj", "{534D1DD5-9D62-115C-F230-47080D76CE52}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Test.ComputerUse", "tests\BotSharp.Test.ComputerUse\BotSharp.Test.ComputerUse.csproj", "{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.OsDriver", "src\Plugins\BotSharp.Plugin.OsDriver\BotSharp.Plugin.OsDriver.csproj", "{B268E2F0-060F-8466-7D81-ABA4D735CA59}"
Expand Down Expand Up @@ -553,14 +551,6 @@ Global
{8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|Any CPU.Build.0 = Release|Any CPU
{8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|x64.ActiveCfg = Release|Any CPU
{8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|x64.Build.0 = Release|Any CPU
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|x64.ActiveCfg = Debug|Any CPU
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|x64.Build.0 = Debug|Any CPU
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|Any CPU.Build.0 = Release|Any CPU
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|x64.ActiveCfg = Release|Any CPU
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|x64.Build.0 = Release|Any CPU
{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -672,7 +662,6 @@ Global
{AF329442-B48E-4B48-A18A-1C869D1BA6F5} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
{781F1465-365C-0F22-1775-25025DAFA4C7} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
{8D2AD45F-836A-516F-DE6A-71443CEBB18A} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}
{534D1DD5-9D62-115C-F230-47080D76CE52} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}
{B268E2F0-060F-8466-7D81-ABA4D735CA59} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
{970BE341-9AC8-99A5-6572-E703C1E02FCB} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
Expand Down
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Shouldly" Version="4.3.0" />
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.8" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.1.0-preview.8" />
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.11" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.1.0-preview.11" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="BotSharp.Core" Version="$(BotSharpVersion)" />
Expand Down
15 changes: 15 additions & 0 deletions IFunctionExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BotSharp.Core.Routing
{
public interface IFunctionExecutor
{
public Task<bool> Execute(RoleDialogModel message);

public Task<string> GetIndication(RoleDialogModel message);
}
}
18 changes: 0 additions & 18 deletions src/Infrastructure/BotSharp.Core.MCP/BotSharp.Core.MCP.csproj

This file was deleted.

64 changes: 0 additions & 64 deletions src/Infrastructure/BotSharp.Core.MCP/BotSharpMCPExtensions.cs

This file was deleted.

18 changes: 0 additions & 18 deletions src/Infrastructure/BotSharp.Core.MCP/Using.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Nanoid" />
<PackageReference Include="Rougamo.Fody" />
<PackageReference Include="ModelContextProtocol" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 35 additions & 0 deletions src/Infrastructure/BotSharp.Core/MCP/BotSharpMCPExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using BotSharp.Core.MCP.Hooks;
using BotSharp.Core.MCP.Managers;
using BotSharp.Core.MCP.Services;
using BotSharp.Core.MCP.Settings;
using Microsoft.Extensions.Configuration;

namespace BotSharp.Core.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("MCP").Get<McpSettings>();
services.AddScoped(provider => settings);

if (settings != null && settings.Enabled && !settings.McpServerConfigs.IsNullOrEmpty())
{
services.AddScoped<IMcpService, McpService>();

var clientManager = new McpClientManager(settings);
services.AddScoped(provider => clientManager);

// Register hooks
services.AddScoped<IAgentHook, McpToolAgentHook>();
}
return services;
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
using BotSharp.Core.MCP.Managers;
using BotSharp.Core.MCP.Settings;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Client;

namespace BotSharp.Core.MCP.Services;

public class McpService : IMcpService
{
private readonly IServiceProvider _services;
private readonly ILogger<McpService> _logger;
private readonly McpClientManager _mcpClientManager;

public McpService(
IServiceProvider services,
ILogger<McpService> logger)
ILogger<McpService> logger,
McpClientManager mcpClient)
{
_services = services;
_logger = logger;
_mcpClientManager = mcpClient;
}

public IEnumerable<McpServerOptionModel> GetServerConfigs()
Expand All @@ -24,9 +29,9 @@ public IEnumerable<McpServerOptionModel> GetServerConfigs()

foreach (var config in configs)
{
var tools = _services.GetServices<IFunctionCallback>()
.Where(x => x.Provider == config.Name)
.Select(x => x.Name);
var tools = _mcpClientManager.GetMcpClientAsync(config.Id)
.Result.ListToolsAsync()
.Result.Select(x=> x.Name);

options.Add(new McpServerOptionModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using BotSharp.Abstraction.Templating;

namespace BotSharp.Core.Routing.Executor;

public class DummyFunctionExecutor: IFunctionExecutor
{
private FunctionDef functionDef;
private readonly IServiceProvider _services;

public DummyFunctionExecutor(FunctionDef function, IServiceProvider services)
{
functionDef = function;
_services = services;
}


public async Task<bool> ExecuteAsync(RoleDialogModel message)
{
var render = _services.GetRequiredService<ITemplateRender>();
var state = _services.GetRequiredService<IConversationStateService>();

var dict = new Dictionary<string, object>();
foreach (var item in state.GetStates())
{
dict[item.Key] = item.Value;
}

var text = render.Render(functionDef.Output, dict);
message.Content = text;
return true;
}

public async Task<string> GetIndicatorAsync(RoleDialogModel message)
{
return "Running";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using BotSharp.Abstraction.Functions;

namespace BotSharp.Core.Routing.Executor;

public class FunctionCallbackExecutor : IFunctionExecutor
{
IFunctionCallback functionCallback;

public FunctionCallbackExecutor(IFunctionCallback functionCallback)
{
this.functionCallback = functionCallback;
}

public async Task<bool> ExecuteAsync(RoleDialogModel message)
{
return await functionCallback.Execute(message);
}

public async Task<string> GetIndicatorAsync(RoleDialogModel message)
{
return await functionCallback.GetIndication(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using BotSharp.Abstraction.Functions;

namespace BotSharp.Core.Routing.Executor;

internal class FunctionExecutorFactory
{
public static IFunctionExecutor Create(string functionName, Agent agent, IFunctionCallback functioncall, IServiceProvider serviceProvider)
Copy link
Collaborator

@iceljc iceljc May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not necessary to pass "functionCallback" from outside. Just function name will be enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

functionCallback is botsharp functionexecutor:FunctionCallbackExecutor,Determine whether it is a FunctionCalling defined within the BotSharp framework by checking if FunctionCallback is null

{
if(functioncall != null)
{
return new FunctionCallbackExecutor(functioncall);
}

var funDef = agent?.Functions?.FirstOrDefault(x => x.Name == functionName);
if (funDef != null)
{
if (!string.IsNullOrWhiteSpace(funDef?.Output))
{
return new DummyFunctionExecutor(funDef,serviceProvider);
}
}
else
{
funDef = agent?.SecondaryFunctions?.FirstOrDefault(x => x.Name == functionName);
if (funDef != null)
{
if (!string.IsNullOrWhiteSpace(funDef?.Output))
{
return new DummyFunctionExecutor(funDef, serviceProvider);
}
else
{
var mcpServerId = agent?.McpTools?.Where(x => x.Functions.Any(y => y.Name == funDef.Name))
.FirstOrDefault().ServerId;
return new MCPToolExecutor(mcpServerId, functionName, serviceProvider);
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BotSharp.Core.Routing.Executor;

public interface IFunctionExecutor
{
public Task<bool> ExecuteAsync(RoleDialogModel message);

public Task<string> GetIndicatorAsync(RoleDialogModel message);
}
Loading
Loading