diff --git a/samples/csharp/FoundryA365/src/hello_world_a365_agent/AgentLogic/SemanticKernel/SemanticKernelAgentLogicServiceFactory.cs b/samples/csharp/FoundryA365/src/hello_world_a365_agent/AgentLogic/SemanticKernel/SemanticKernelAgentLogicServiceFactory.cs index 2dd2eb48..9020e298 100644 --- a/samples/csharp/FoundryA365/src/hello_world_a365_agent/AgentLogic/SemanticKernel/SemanticKernelAgentLogicServiceFactory.cs +++ b/samples/csharp/FoundryA365/src/hello_world_a365_agent/AgentLogic/SemanticKernel/SemanticKernelAgentLogicServiceFactory.cs @@ -7,10 +7,12 @@ namespace HelloWorldA365.AgentLogic.SemanticKernel; using HelloWorldA365.Models; // added for PresenceState using HelloWorldA365.Services; using Microsoft.Agents.A365.Tooling.Extensions.SemanticKernel.Services; +using Microsoft.Agents.A365.Tooling.Handlers; using Microsoft.Agents.Builder; using Microsoft.Agents.Builder.App.UserAuth; using Microsoft.SemanticKernel; - +using ModelContextProtocol.Client; + /// /// There are still some work left here: /// 1- The factory structure doesn't follow the factory pattern. @@ -64,6 +66,13 @@ private async Task ConfigureKernelPlugins(AgentMetadata agent, Kernel kernel, IT string authHandlerName = string.Empty; await mcpToolRegistrationService.AddToolServersToAgentAsync(kernel, userAuthorization, authHandlerName, turnContext, accessToken.Token); + + var mcpClient = await CreateMcpClientWithAuthHandlers(new Uri("https://zava-api.icysand-d49b587e.northcentralusstage.azurecontainerapps.io/mcp"), accessToken.Token); + var tools = await mcpClient.ListToolsAsync(); + + logger.LogInformation($"Successfully retrieved {tools.Count} tools from mcp_Zava"); + + kernel.Plugins.AddFromFunctions("mcp_Zava", tools.Select(x => x.AsKernelFunction())); } private IKernelBuilder AddModel(IKernelBuilder kernelBuilder) @@ -80,4 +89,39 @@ private IKernelBuilder AddModel(IKernelBuilder kernelBuilder) new DefaultAzureCredential() ); } + + private async Task CreateMcpClientWithAuthHandlers(Uri endpoint, string authToken) + { + // Create HTTP client handler chain for MCP service authentication + var httpClientHandler = new HttpClientHandler(); + // Create a simple authentication handler that adds the bearer token + var authHandler = new BearerTokenHandler(authToken) + { + InnerHandler = httpClientHandler + }; + logger.LogInformation($"Configured authentication handler for MCP endpoint {endpoint}"); + // Create logging handler (optional - for debugging HTTP requests) + var loggingHandler = new HttpLoggingHandler(logger) + { + InnerHandler = authHandler + }; + // Setup SSE client transport options without manual token management + var options = new SseClientTransportOptions + { + Endpoint = endpoint, + TransportMode = HttpTransportMode.AutoDetect, + }; + // Create HTTP client with the authentication handler chain + + var httpClient = new HttpClient(loggingHandler); + var clientTransport = new SseClientTransport(options, httpClient); + try + { + return await McpClientFactory.CreateAsync(clientTransport); + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed to create MCP client for endpoint '{endpoint}': {ex.Message}", ex); + } + } } \ No newline at end of file