Skip to main content

Endpoints

An AG-UI endpoint is a normal ASP.NET Core POST endpoint. It receives a RunAgentInput, calls an IChatClient, converts the streamed response to AG-UI events, and returns Server-Sent Events.

Register AG-UI services

Call AddAGUI() during startup. It configures ASP.NET Core JSON options with the AG-UI source-generated serializer context and registers interrupt content types used by the IChatClient pipeline.

Endpoint pattern

Every AG-UI endpoint follows the same five steps.
1

Receive RunAgentInput

Map a POST endpoint that receives [FromBody] RunAgentInput.
2

Create ChatRequestContext

Call input.ToChatRequestContext(jsonSerializerOptions, streamOptions?). The returned context exposes ctx.Messages and ctx.ChatOptions.
3

Call the IChatClient

Pass ctx.Messages and ctx.ChatOptions to chatClient.GetStreamingResponseAsync(...).
4

Convert to AG-UI events

Pipe the ChatResponseUpdate stream through .AsAGUIEventStreamAsync(ctx, cancellationToken).
5

Return Server-Sent Events

Return TypedResults.ServerSentEvents(WrapAsSseItems(events, ct)).

Minimal endpoint

This is the canonical endpoint shape used by the .NET samples.

Key APIs

ToChatRequestContext

ToChatRequestContext adapts the AG-UI request to the Microsoft.Extensions.AI request shape.
ChatRequestContext contains:
  • ctx.Input - the original RunAgentInput
  • ctx.Messages - converted List<ChatMessage>
  • ctx.ChatOptions - configured ChatOptions
The original input is also available to downstream chat clients and tools at ctx.ChatOptions.AdditionalProperties[AGUIConstants.RunAgentInputKey].
Client-tool and approval-flow routing happens automatically. Client-declared tools from RunAgentInput.Tools are installed on ctx.ChatOptions.Tools and routed through the approval pipeline.

AsAGUIEventStreamAsync

AsAGUIEventStreamAsync converts streamed ChatResponseUpdate values into AG-UI BaseEvent values.
It emits run lifecycle events, maps text content to TEXT_MESSAGE_* events, maps FunctionCallContent and FunctionResultContent to tool events, maps TextReasoningContent to reasoning events, and passes through any BaseEvent stored in ChatResponseUpdate.RawRepresentation.

WrapAsSseItems

ASP.NET Core’s TypedResults.ServerSentEvents writes SseItem<T> values. The small helper wraps each AG-UI event in SseItem<BaseEvent>.

Adding stream options

Pass AGUIStreamOptions when the endpoint needs custom stream conversion.
See Extensibility for the available mapping hooks.