Skip to main content
MCPAgent provides two streaming APIs. Use stream() for tool-step progress and a final string. Use streamEvents() for provider-neutral token, tool, usage, error, and completion events.

Stream tool steps

Read the generator directly when you need both yielded steps and its final return value.
For each tool call, stream() can yield an initial step with an empty observation, followed by another step containing the tool result. A for await loop receives the steps but does not expose the generator’s final return value.

Stream provider-neutral events

streamEvents() uses the same LlmStreamEvent variants for every supported provider.
Accumulate text-delta values when your UI needs the complete final answer. streamEvents() itself returns no result and does not write the streamed turn to conversation memory.

Handle every event variant

Tool execution failures are emitted as tool-result with isError: true. Other streaming failures can appear as error. Decide whether your UI should show a partial answer, retry, or fail the request.

Cancel a stream

Pass an AbortSignal through the run options:

Next steps