Skip to content

Commit f4495d9

Browse files
.Net: Update MCP nuget package (#11288)
### Motivation, Context and Description Update the MCP samples to use the latest MCP nuget package. **Contributes to**: #11199
1 parent 53a503c commit f4495d9

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

dotnet/Directory.Packages.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<PackageVersion Include="EntityFramework" Version="6.5.1" />
3030
<PackageVersion Include="FastBertTokenizer" Version="1.0.28" />
3131
<PackageVersion Include="Google.Apis.Auth" Version="1.69.0" />
32-
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.1.25171.12" />
32+
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.4" />
3333
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.13" />
3434
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.14" />
3535
<PackageVersion Include="Microsoft.ML.Tokenizers.Data.Cl100kBase" Version="1.0.1" />

dotnet/samples/Demos/ModelContextProtocolClientServer/MCPClient/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
using Microsoft.Extensions.Logging;
1010
using Microsoft.SemanticKernel;
1111
using Microsoft.SemanticKernel.Connectors.OpenAI;
12+
using ModelContextProtocol;
1213
using ModelContextProtocol.Client;
13-
using ModelContextProtocol.Configuration;
1414
using ModelContextProtocol.Protocol.Transport;
1515

1616
namespace MCPClient;
@@ -52,7 +52,7 @@ public static async Task Main(string[] args)
5252

5353
// Retrieve and display the list of tools available on the MCP server
5454
Console.WriteLine("Available MCP tools:");
55-
var tools = await mcpClient.GetAIFunctionsAsync().ConfigureAwait(false);
55+
var tools = await mcpClient.ListToolsAsync().ConfigureAwait(false);
5656
foreach (var tool in tools)
5757
{
5858
Console.WriteLine($"{tool.Name}: {tool.Description}");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.SemanticKernel;
4+
using ModelContextProtocol.Server;
5+
6+
namespace MCPServer;
7+
8+
/// <summary>
9+
/// Extension methods for <see cref="IMcpServerBuilder"/>.
10+
/// </summary>
11+
public static class McpServerBuilderExtensions
12+
{
13+
/// <summary>
14+
/// Adds all functions of the kernel plugins as tools to the server.
15+
/// </summary>
16+
/// <param name="builder">The builder instance.</param>
17+
/// <param name="plugins">The kernel plugins to add as tools to the server.</param>
18+
/// <returns>The builder instance.</returns>
19+
public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, KernelPluginCollection plugins)
20+
{
21+
foreach (var plugin in plugins)
22+
{
23+
foreach (var function in plugin)
24+
{
25+
builder.Services.AddSingleton(services => McpServerTool.Create(function.AsAIFunction()));
26+
}
27+
}
28+
29+
return builder;
30+
}
31+
}

dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using MCPServer;
34
using MCPServer.Tools;
45
using Microsoft.SemanticKernel;
5-
using ModelContextProtocol;
66

77
// Create a kernel builder and add plugins
88
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
@@ -16,6 +16,6 @@
1616
builder.Services
1717
.AddMcpServer()
1818
.WithStdioServerTransport()
19-
// Add kernel functions to the MCP server as MCP tools
20-
.WithTools(kernel.Plugins.SelectMany(p => p.Select(f => f.AsAIFunction())));
19+
// Add all functions from the kernel plugins to the MCP server as tools
20+
.WithTools(kernel.Plugins);
2121
await builder.Build().RunAsync();

dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
new() { ClientInfo = new() { Name = "GitHub", Version = "1.0.0" } }).ConfigureAwait(false);
3535

3636
// Retrieve the list of tools available on the GitHub server
37-
var tools = await mcpClient.GetAIFunctionsAsync().ConfigureAwait(false);
37+
var tools = await mcpClient.ListToolsAsync().ConfigureAwait(false);
3838
foreach (var tool in tools)
3939
{
4040
Console.WriteLine($"{tool.Name}: {tool.Description}");

0 commit comments

Comments
 (0)