Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@
<PackageVersion Include="MSTest.TestAdapter" Version="4.0.2" />
<PackageVersion Include="MSTest.TestFramework" Version="4.0.2" />
<PackageVersion Include="Shouldly" Version="4.3.0" />
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.11" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.1.0-preview.11" />
<PackageVersion Include="ModelContextProtocol" Version="0.4.0-preview.3" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.4.0-preview.3" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="BotSharp.Core" Version="$(BotSharpVersion)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using BotSharp.Core.MCP.Settings;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;

namespace BotSharp.Core.MCP.Managers;

Expand All @@ -17,7 +16,7 @@ public McpClientManager(
_logger = logger;
}

public async Task<IMcpClient?> GetMcpClientAsync(string serverId)
public async Task<McpClient?> GetMcpClientAsync(string serverId)
{
try
{
Expand All @@ -31,7 +30,7 @@ public McpClientManager(
IClientTransport? transport = null;
if (config.SseConfig != null)
{
transport = new SseClientTransport(new SseClientTransportOptions
transport = new HttpClientTransport(new HttpClientTransportOptions
{
Name = config.Name,
Endpoint = new Uri(config.SseConfig.EndPoint),
Expand All @@ -56,7 +55,7 @@ public McpClientManager(
return null;
}

return await McpClientFactory.CreateAsync(transport, settings.McpClientOptions);
return await McpClient.CreateAsync(transport, settings.McpClientOptions);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using BotSharp.Abstraction.Routing.Executor;
using BotSharp.Core.MCP.Managers;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;

namespace BotSharp.Core.Routing.Executor;

public class McpToolExecutor: IFunctionExecutor
public class McpToolExecutor : IFunctionExecutor
{
private readonly IServiceProvider _services;
private readonly string _mcpServerId;
private readonly string _functionName;

public McpToolExecutor(IServiceProvider services, string mcpServerId, string functionName)
{
{
_services = services;
_mcpServerId = mcpServerId;
_functionName = functionName;
Expand All @@ -22,16 +22,22 @@ public async Task<bool> ExecuteAsync(RoleDialogModel message)
try
{
// Convert arguments to dictionary format expected by mcpdotnet
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
Dictionary<string, object?> argDict = JsonToDictionary(message.FunctionArgs);

var clientManager = _services.GetRequiredService<McpClientManager>();
var client = await clientManager.GetMcpClientAsync(_mcpServerId);

if (client == null)
{
message.Content = $"MCP client for server {_mcpServerId} not found.";
return false;
}

// Call the tool through mcpdotnet
var result = await client.CallToolAsync(_functionName, !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));
var json = string.Join("\n", result.Content.Where(c => c is TextContentBlock).Select(c => ((TextContentBlock)c).Text));

message.Content = json;
message.Data = json.JsonContent();
Expand All @@ -50,7 +56,7 @@ public async Task<string> GetIndicatorAsync(RoleDialogModel message)
}


private static Dictionary<string, object> JsonToDictionary(string? json)
private static Dictionary<string, object?> JsonToDictionary(string? json)
{
if (string.IsNullOrEmpty(json))
{
Expand All @@ -62,9 +68,9 @@ private static Dictionary<string, object> JsonToDictionary(string? json)
return JsonElementToDictionary(root);
}

private static Dictionary<string, object> JsonElementToDictionary(JsonElement element)
private static Dictionary<string, object?> JsonElementToDictionary(JsonElement element)
{
Dictionary<string, object> dictionary = [];
Dictionary<string, object?> dictionary = [];

if (element.ValueKind == JsonValueKind.Object)
{
Expand Down
1 change: 1 addition & 0 deletions tests/BotSharp.PizzaBot.MCPServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly();
var app = builder.Build();

Expand Down
Loading