|
| 1 | +This topic uses [Microsoft Agent Framework](https://learn.microsoft.com/en-us/agent-framework/overview/?pivots=programming-language-csharp) with an Azure OpenAI-backed `IChatClient`. |
| 2 | + |
| 3 | +#include btn-open-github with { |
| 4 | + href: "https://github.com/DevExpress-Examples/devextreme-chat-ai-integration-ms-agent-framework" |
| 5 | +} |
| 6 | + |
| 7 | +Compared to the base `IChatClient` setup, this implementation adds: |
| 8 | + |
| 9 | +- Multiple specialized agents (`VisionAgent`, `SupportAgent`, `Editor`). |
| 10 | +- A sequential workflow (`VirtualAssistant`) that orchestrates agent execution. |
| 11 | +- [MCP-based documentation tools](/Documentation/Guide/AI_Features/DevExpress_MCP_Server_Configuration/) for the support agent. |
| 12 | +- Multipart request handling for text and file attachments. |
| 13 | + |
| 14 | +The application uses one shared `IChatClient` instance, then composes agents and workflow on top of it: |
| 15 | + |
| 16 | + <!-- tab: Program.cs --> |
| 17 | + using Azure.AI.OpenAI; |
| 18 | + using Microsoft.Agents.AI; |
| 19 | + using Microsoft.Agents.AI.Hosting; |
| 20 | + using Microsoft.Agents.AI.Workflows; |
| 21 | + using Microsoft.Extensions.AI; |
| 22 | + using Microsoft.Extensions.Options; |
| 23 | + using System.ClientModel; |
| 24 | + |
| 25 | + builder.Services.AddSingleton<IChatClient>(sp => { |
| 26 | + var opts = sp.GetRequiredService<IOptions<AzureOpenAIOptions>>().Value; |
| 27 | + var client = new AzureOpenAIClient( |
| 28 | + new Uri(opts.Endpoint), |
| 29 | + new ApiKeyCredential(opts.ApiKey)); |
| 30 | + return client.GetChatClient(opts.ModelName).AsIChatClient(); |
| 31 | + }); |
| 32 | + |
| 33 | + builder.AddAIAgent("VisionAgent", (sp, key) => { |
| 34 | + var chatClient = sp.GetRequiredService<IChatClient>(); |
| 35 | + return new ChatClientAgent(chatClient, name: key, instructions: "Vision analysis instructions"); |
| 36 | + }); |
| 37 | + |
| 38 | + builder.AddAIAgent("SupportAgent", (sp, key) => { |
| 39 | + var chatClient = sp.GetRequiredService<IChatClient>(); |
| 40 | + return new ChatClientAgent(chatClient, name: key, instructions: "Support instructions", tools: [.. mcpTools.Cast<AITool>()]); |
| 41 | + }); |
| 42 | + |
| 43 | + builder.AddAIAgent("Editor", (sp, key) => { |
| 44 | + var chatClient = sp.GetRequiredService<IChatClient>(); |
| 45 | + return new ChatClientAgent(chatClient, name: key, instructions: "Editing instructions"); |
| 46 | + }); |
| 47 | + |
| 48 | + builder.AddWorkflow("VirtualAssistant", (sp, key) => { |
| 49 | + var visionAgent = sp.GetRequiredKeyedService<AIAgent>("VisionAgent"); |
| 50 | + var supportAgent = sp.GetRequiredKeyedService<AIAgent>("SupportAgent"); |
| 51 | + var editorAgent = sp.GetRequiredKeyedService<AIAgent>("Editor"); |
| 52 | + |
| 53 | + return AgentWorkflowBuilder.BuildSequential( |
| 54 | + workflowName: key, |
| 55 | + agents: [visionAgent, supportAgent, editorAgent] |
| 56 | + ); |
| 57 | + }).AddAsAIAgent(); |
| 58 | + |
| 59 | +The support agent can call DevExpress documentation tools through MCP: |
| 60 | + |
| 61 | + <!-- tab: Program.cs --> |
| 62 | + using ModelContextProtocol.Client; |
| 63 | + |
| 64 | + await using var mcpClient = await McpClient.CreateAsync( |
| 65 | + new HttpClientTransport(new HttpClientTransportOptions |
| 66 | + { |
| 67 | + Endpoint = new Uri("https://api.devexpress.com/mcp/docs") |
| 68 | + }) |
| 69 | + ); |
| 70 | + var mcpTools = await mcpClient.ListToolsAsync().ConfigureAwait(false); |
| 71 | + |
| 72 | +Assign `mcpTools` to the support agent in the `tools` parameter. |
| 73 | + |
| 74 | +This server accepts `multipart/form-data` for `POST /api/Chat/GetAIResponse`: |
| 75 | + |
| 76 | +- `text`: user message text |
| 77 | +- `id`: client message identifier |
| 78 | +- `timestamp`: message timestamp in ISO 8601 format |
| 79 | +- `attachments`: optional attachment metadata |
| 80 | +- `files`: optional files for image analysis |
| 81 | + |
| 82 | +Use this format when your Chat UI sends files with user messages. |
| 83 | + |
| 84 | +For baseline `IChatClient` registration and Azure options, see [IChatClient](/Documentation/Guide/UI_Components/Chat/Integrate_with_AI_Service/#Azure_OpenAI/ASPNET_Web_API_Backend/IChatClient). |
0 commit comments