Back to Devexpress

AIChatResource.AIContentProvider Delegate

blazor-devexpress-dot-aiintegration-dot-blazor-dot-chat-dot-aichatresource.md

latest2.8 KB
Original Source

AIChatResource.AIContentProvider Delegate

Defines a delegate that returns AI resource content for a chat.

Namespace : DevExpress.AIIntegration.Blazor.Chat

Assembly : DevExpress.AIIntegration.Blazor.Chat.v25.2.dll

NuGet Package : DevExpress.AIIntegration.Blazor.Chat

Declaration

csharp
public delegate Task<IList<AIContent>> AIContentProvider(
    AIChatResource resource,
    CancellationToken ct
);

Parameters

NameTypeDescription
resourceAIChatResource

AI Chat resource.

| | ct | CancellationToken |

A cancellation token that cancels the content retrieval operation.

|

Returns

TypeDescription
Task<IList<Microsoft.Extensions.AI.AIContent>>

A task that returns a list of AIContent items supplied to the chat.

|

Remarks

Use the AIContentProvider delegate to supply AIContent items to a chat. The delegate runs when the chat needs input generated from the associated AIChatResource (for example, text fragments or files).

The following code snippet assigns a custom content provider to supply assistant input:

razor
@using ModelContextProtocol
@using ModelContextProtocol.Client
@inject NavigationManager NavigationManager

<DxAIChat Resources="Resources" />

@code{
    IEnumerable<AIChatResource> Resources { get; set; } = [];
    IMcpClient McpClient { get; set; }

    protected override async Task OnInitializedAsync() {
        await base.OnInitializedAsync();
        await LoadMcpResources();
    }

    async Task LoadMcpResources() {
        var transport = new SseClientTransport(new() { Endpoint = new(NavigationManager.BaseUri + "mcp") });
        McpClient = await McpClientFactory.CreateAsync(transport);
        var mcpResources = await McpClient.ListResourcesAsync();
        Resources = mcpResources.Select(x =>
          new AIChatResource(x.Uri, x.Name, LoadResourceData, x.MimeType, x.Description)
          );
    }

    async Task<IList<AIContent>> LoadResourceData(AIChatResource resource, CancellationToken ct) {
        var readResource = await McpClient.ReadResourceAsync(resource.Uri, ct);
        return readResource.Contents.ToAIContents();
    }
}

See Also

DevExpress.AIIntegration.Blazor.Chat Namespace