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
2 changes: 1 addition & 1 deletion dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<PackageVersion Include="Microsoft.SemanticKernel.Planners.OpenAI" Version="1.47.0-preview" />
<PackageVersion Include="Microsoft.VisualStudio.Threading" Version="17.12.19" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.10" />
<PackageVersion Include="ModelContextProtocol" Version="0.3.0-preview.2" />
<PackageVersion Include="ModelContextProtocol" Version="0.4.0-preview.3" />
<PackageVersion Include="MSTest.TestFramework" Version="3.8.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
<PackageVersion Include="Npgsql" Version="8.0.7" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static async Task RunAsync()
Console.WriteLine($"Running the {nameof(AgentAvailableAsMCPToolSample)} sample.");

// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
McpClient mcpClient = await CreateMcpClientAsync();

// Retrieve and display the list provided by the MCP server
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static async Task RunAsync()
Console.WriteLine($"Running the {nameof(AzureAIAgentWithMCPToolsSample)} sample.");

// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
McpClient mcpClient = await CreateMcpClientAsync();

// Retrieve and display the list provided by the MCP server
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,24 @@ internal abstract class BaseSample
/// <param name="kernel">Optional kernel instance to use for the MCP client.</param>
/// <param name="samplingRequestHandler">Optional handler for MCP sampling requests.</param>
/// <returns>An instance of <see cref="IMcpClient"/>.</returns>
protected static Task<IMcpClient> CreateMcpClientAsync(
protected static Task<McpClient> CreateMcpClientAsync(
Kernel? kernel = null,
Func<Kernel, CreateMessageRequestParams?, IProgress<ProgressNotificationValue>, CancellationToken, Task<CreateMessageResult>>? samplingRequestHandler = null)
{
KernelFunction? skSamplingHandler = null;

// Create and return the MCP client
return McpClientFactory.CreateAsync(
return McpClient.CreateAsync(
clientTransport: new StdioClientTransport(new StdioClientTransportOptions
{
Name = "MCPServer",
Command = GetMCPServerPath(), // Path to the MCPServer executable
}),
clientOptions: samplingRequestHandler != null ? new McpClientOptions()
{
Capabilities = new ClientCapabilities
Handlers = new()
{
Sampling = new SamplingCapability
{
SamplingHandler = InvokeHandlerAsync
},
SamplingHandler = InvokeHandlerAsync,
},
} : null
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static async Task RunAsync()
Console.WriteLine($"Running the {nameof(ChatCompletionAgentWithMCPToolsSample)} sample.");

// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
McpClient mcpClient = await CreateMcpClientAsync();

// Retrieve and display the list provided by the MCP server
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static async Task RunAsync()
Console.WriteLine($"Running the {nameof(MCPPromptSample)} sample.");

// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
McpClient mcpClient = await CreateMcpClientAsync();

// Retrieve and display the list of prompts provided by the MCP server
IList<McpClientPrompt> prompts = await mcpClient.ListPromptsAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static async Task RunAsync()
Console.WriteLine($"Running the {nameof(MCPResourceTemplatesSample)} sample.");

// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
McpClient mcpClient = await CreateMcpClientAsync();

// Retrieve list of resource templates provided by the MCP server and display them
IList<McpClientResourceTemplate> resourceTemplates = await mcpClient.ListResourceTemplatesAsync();
Expand All @@ -48,7 +48,7 @@ public static async Task RunAsync()
string prompt = "What is the Semantic Kernel?";

// Retrieve relevant to the prompt records via MCP resource template
ReadResourceResult resource = await mcpClient.ReadResourceAsync($"vectorStore://records/{prompt}");
ReadResourceResult resource = await mcpClient.ReadResourceAsync(new Uri($"vectorStore://records/{prompt}"));

// Add the resource content/records to the chat history and prompt the AI model to explain what SK is
ChatHistory chatHistory = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static async Task RunAsync()
Console.WriteLine($"Running the {nameof(MCPResourcesSample)} sample.");

// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
McpClient mcpClient = await CreateMcpClientAsync();

// Retrieve list of resources provided by the MCP server and display them
IList<McpClientResource> resources = await mcpClient.ListResourcesAsync();
Expand All @@ -46,7 +46,7 @@ public static async Task RunAsync()
};

// Retrieve the `image://cat.jpg` resource from the MCP server
ReadResourceResult resource = await mcpClient.ReadResourceAsync("image://cat.jpg");
ReadResourceResult resource = await mcpClient.ReadResourceAsync(new Uri("image://cat.jpg"));

// Add the resource to the chat history and prompt the AI model to describe the content of the image
ChatHistory chatHistory = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static async Task RunAsync()
kernel.FunctionInvocationFilters.Add(new HumanInTheLoopFilter());

// Create an MCP client with a custom sampling request handler
await using IMcpClient mcpClient = await CreateMcpClientAsync(kernel, SamplingRequestHandlerAsync);
McpClient mcpClient = await CreateMcpClientAsync(kernel, SamplingRequestHandlerAsync);

// Import MCP tools as Kernel functions so AI model can call them
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static async Task RunAsync()
Console.WriteLine($"Running the {nameof(MCPToolsSample)} sample.");

// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
McpClient mcpClient = await CreateMcpClientAsync();

// Retrieve and display the list provided by the MCP server
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ public static ResourceDefinition CreateBlobResource(string uri, string name, byt
/// <returns>The result of the invocation.</returns>
public async ValueTask<ReadResourceResult> InvokeHandlerAsync(RequestContext<ReadResourceRequestParams> context, CancellationToken cancellationToken)
{
if (this._kernelFunction == null)
{
this._kernelFunction = KernelFunctionFactory.CreateFromMethod(this.Handler);
}
this._kernelFunction ??= KernelFunctionFactory.CreateFromMethod(this.Handler);

this.Kernel
??= context.Server.Services?.GetRequiredService<Kernel>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal sealed class MailboxUtils
/// mechanism for summarization.
/// </summary>
[KernelFunction]
public static async Task<string> SummarizeUnreadEmailsAsync([FromKernelServices] IMcpServer server)
public static async Task<string> SummarizeUnreadEmailsAsync([FromKernelServices] McpServer server)
{
if (server.ClientCapabilities?.Sampling is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}

// Create an MCPClient for the GitHub server
await using var mcpClient = await McpClientFactory.CreateAsync(new StdioClientTransport(new()
var mcpClient = await McpClient.CreateAsync(new StdioClientTransport(new()
{
Name = "MCPServer",
Command = "npx",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@
builder.AddConsole();
});

// Create SSE client transport for the MCP server
// Create streamable HTTP client transport for the MCP server
var serverUrl = "http://localhost:7071/";
var transport = new SseClientTransport(new()
var transport = new HttpClientTransport(new()
{
Endpoint = new Uri(serverUrl),
Name = "Secure Weather Client",
OAuth = new()
{
ClientName = "ProtectedMcpClient",
ClientId = "ProtectedMcpClient",
RedirectUri = new Uri("http://localhost:1179/callback"),
AuthorizationRedirectDelegate = HandleAuthorizationUrlAsync,
}
}, httpClient, consoleLoggerFactory);

// Create an MCPClient for the protected MCP server
await using var mcpClient = await McpClientFactory.CreateAsync(transport, loggerFactory: consoleLoggerFactory);
await using var mcpClient = await McpClient.CreateAsync(transport, loggerFactory: consoleLoggerFactory);

// Retrieve the list of tools available on the GitHub server
var tools = await mcpClient.ListToolsAsync().ConfigureAwait(false);
Expand Down
Loading