Skip to content

Commit 870f6ba

Browse files
update ModelContextProtocol version to 0.4.0-preview.3
1 parent 236c739 commit 870f6ba

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@
9999
<PackageVersion Include="MSTest.TestAdapter" Version="4.0.2" />
100100
<PackageVersion Include="MSTest.TestFramework" Version="4.0.2" />
101101
<PackageVersion Include="Shouldly" Version="4.3.0" />
102-
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.11" />
103-
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.1.0-preview.11" />
102+
<PackageVersion Include="ModelContextProtocol" Version="0.4.0-preview.3" />
103+
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.4.0-preview.3" />
104104
</ItemGroup>
105105
<ItemGroup>
106106
<PackageVersion Include="BotSharp.Core" Version="$(BotSharpVersion)" />

src/Infrastructure/BotSharp.Core/MCP/Managers/McpClientManager.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using BotSharp.Core.MCP.Settings;
22
using ModelContextProtocol.Client;
3-
using ModelContextProtocol.Protocol.Transport;
43

54
namespace BotSharp.Core.MCP.Managers;
65

@@ -17,7 +16,7 @@ public McpClientManager(
1716
_logger = logger;
1817
}
1918

20-
public async Task<IMcpClient?> GetMcpClientAsync(string serverId)
19+
public async Task<McpClient?> GetMcpClientAsync(string serverId)
2120
{
2221
try
2322
{
@@ -31,7 +30,7 @@ public McpClientManager(
3130
IClientTransport? transport = null;
3231
if (config.SseConfig != null)
3332
{
34-
transport = new SseClientTransport(new SseClientTransportOptions
33+
transport = new HttpClientTransport(new HttpClientTransportOptions
3534
{
3635
Name = config.Name,
3736
Endpoint = new Uri(config.SseConfig.EndPoint),
@@ -56,7 +55,7 @@ public McpClientManager(
5655
return null;
5756
}
5857

59-
return await McpClientFactory.CreateAsync(transport, settings.McpClientOptions);
58+
return await McpClient.CreateAsync(transport, settings.McpClientOptions);
6059
}
6160
catch (Exception ex)
6261
{

src/Infrastructure/BotSharp.Core/Routing/Executor/MCPToolExecutor.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
using BotSharp.Abstraction.Routing.Executor;
22
using BotSharp.Core.MCP.Managers;
3-
using ModelContextProtocol.Client;
3+
using ModelContextProtocol.Protocol;
44

55
namespace BotSharp.Core.Routing.Executor;
66

7-
public class McpToolExecutor: IFunctionExecutor
7+
public class McpToolExecutor : IFunctionExecutor
88
{
99
private readonly IServiceProvider _services;
1010
private readonly string _mcpServerId;
1111
private readonly string _functionName;
1212

1313
public McpToolExecutor(IServiceProvider services, string mcpServerId, string functionName)
14-
{
14+
{
1515
_services = services;
1616
_mcpServerId = mcpServerId;
1717
_functionName = functionName;
@@ -22,16 +22,22 @@ public async Task<bool> ExecuteAsync(RoleDialogModel message)
2222
try
2323
{
2424
// Convert arguments to dictionary format expected by mcpdotnet
25-
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
25+
Dictionary<string, object?> argDict = JsonToDictionary(message.FunctionArgs);
2626

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

30+
if (client == null)
31+
{
32+
message.Content = $"MCP client for server {_mcpServerId} not found.";
33+
return false;
34+
}
35+
3036
// Call the tool through mcpdotnet
3137
var result = await client.CallToolAsync(_functionName, !argDict.IsNullOrEmpty() ? argDict : []);
3238

3339
// Extract the text content from the result
34-
var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text));
40+
var json = string.Join("\n", result.Content.Where(c => c is TextContentBlock).Select(c => ((TextContentBlock)c).Text));
3541

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

5258

53-
private static Dictionary<string, object> JsonToDictionary(string? json)
59+
private static Dictionary<string, object?> JsonToDictionary(string? json)
5460
{
5561
if (string.IsNullOrEmpty(json))
5662
{
@@ -62,9 +68,9 @@ private static Dictionary<string, object> JsonToDictionary(string? json)
6268
return JsonElementToDictionary(root);
6369
}
6470

65-
private static Dictionary<string, object> JsonElementToDictionary(JsonElement element)
71+
private static Dictionary<string, object?> JsonElementToDictionary(JsonElement element)
6672
{
67-
Dictionary<string, object> dictionary = [];
73+
Dictionary<string, object?> dictionary = [];
6874

6975
if (element.ValueKind == JsonValueKind.Object)
7076
{

tests/BotSharp.PizzaBot.MCPServer/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var builder = WebApplication.CreateBuilder(args);
22
builder.Services.AddMcpServer()
3+
.WithHttpTransport()
34
.WithToolsFromAssembly();
45
var app = builder.Build();
56

0 commit comments

Comments
 (0)