Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// There are still some work left here:
/// 1- The factory structure doesn't follow the factory pattern.
Expand Down Expand Up @@ -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)
Expand All @@ -80,4 +89,39 @@ private IKernelBuilder AddModel(IKernelBuilder kernelBuilder)
new DefaultAzureCredential()
);
}

private async Task<IMcpClient> 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);
}
}
}