microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/A365Mcp/Program.cs
39lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.ClientModel; |
| 5 | using A365Mcp; |
| 6 | using Azure.AI.OpenAI; |
| 7 | using Microsoft.Extensions.AI; |
| 8 | using Microsoft.Teams.Apps; |
| 9 | |
| 10 | // Wires up the Teams bot application and delegates AI execution to Agent. |
| 11 | // Handler registration lives in TeamsBotAppHandlers.cs. |
| 12 | |
| 13 | WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args); |
| 14 | builder.Services.AddTeamsBotApplication(); |
| 15 | |
| 16 | builder.Services.AddChatClient(sp => |
| 17 | { |
| 18 | IConfiguration config = sp.GetRequiredService<IConfiguration>(); |
| 19 | string endpoint = config["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("AzureOpenAI:Endpoint is required."); |
| 20 | string apiKey = config["AzureOpenAI:ApiKey"] ?? throw new InvalidOperationException("AzureOpenAI:ApiKey is required."); |
| 21 | string modelId = config["AzureOpenAI:ModelId"] ?? throw new InvalidOperationException("AzureOpenAI:ModelId is required."); |
| 22 | |
| 23 | return new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey)) |
| 24 | .GetChatClient(modelId) |
| 25 | .AsIChatClient(); |
| 26 | }) |
| 27 | .UseFunctionInvocation(); |
| 28 | |
| 29 | builder.Services.AddSingleton<IMcpClientFactory, McpClientFactory>(); |
| 30 | builder.Services.AddSingleton<Agent>(); |
| 31 | |
| 32 | WebApplication webApp = builder.Build(); |
| 33 | |
| 34 | Agent agent = webApp.Services.GetRequiredService<Agent>(); |
| 35 | ILogger handlerLogger = webApp.Services.GetRequiredService<ILoggerFactory>().CreateLogger("A365Mcp.TeamsBotAppHandlers"); |
| 36 | |
| 37 | webApp.UseTeamsBotApplication().RegisterHandlers(agent, handlerLogger); |
| 38 | |
| 39 | webApp.Run(); |
| 40 | |