Skip to content

@mfui/anthropic ​

@mfui/anthropic adapts Anthropic Messages SSE streams into MFUI semantic SSE streams.

Use this package when the upstream stream emits Anthropic events such as content_block_delta, message_start, and message_delta.

createMFUIResponse() ​

Creates an MFUI semantic SSE Response from an upstream Anthropic stream.

Import ​

ts
import { createMFUIResponse } from '@mfui/anthropic';

Signature ​

ts
function createMFUIResponse(
  source: AnthropicStreamSource,
  mfui: MFUIManifest,
  options?: AnthropicMFUIResponseOptions,
): Response

Parameters ​

NameTypeRequiredDefaultDescription
sourceAnthropicStreamSourceYesn/aProvider Response, response body stream, or null.
mfuiMFUIManifestYesn/aComponents available to this request.
optionsAnthropicMFUIResponseOptionsNo{}Response, parser, writer, and lifecycle options.

Returns ​

TypeDescription
Responsetext/event-stream response that emits MFUI semantic SSE events.

Example ​

ts
import { createMFUIPrompt } from '@mfui/server';
import { createMFUIResponse } from '@mfui/anthropic';

const upstream = await fetch('https://api.anthropic.com/v1/messages', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    model: 'claude-sonnet-4-5',
    system: [
      'You are a helpful assistant.',
      createMFUIPrompt(mfui),
    ].join('\n\n'),
    messages,
    stream: true,
  }),
});

return createMFUIResponse(upstream, mfui);

Notes ​

When provider processing fails, the returned MFUI stream emits an error event and then closes. onError is called when provided.

AnthropicStreamSource ​

Source accepted by createMFUIResponse(), readStream(), and pipeMFUIStream().

Shape ​

ts
type AnthropicStreamSource =
  | Response
  | ReadableStream<Uint8Array>
  | null

AnthropicMFUIResponseOptions ​

Options for createMFUIResponse().

PropertyTypeRequiredDefaultDescription
closebooleanNotrueWhether to close the MFUI parser and writer when the provider stream finishes.
parserMFUIBlockParserOptionsNo{}Options passed to createMFUIBlockParser().
writerMFUIStreamWriterOptionsNo{}Options passed to createMFUIStreamWriter().
responseInitResponseInitNo{}Init object passed to the returned Response.
onMessageMFUIMessageHandlerNon/aCalled with the final projected message after the MFUI response finishes.
onErrorMFUIErrorHandlerNon/aCalled when provider stream processing or MFUI block parsing fails.

readStream() ​

Reads Anthropic SSE events and yields normalized JSON events.

Import ​

ts
import { readStream } from '@mfui/anthropic';

Signature ​

ts
function readStream(
  source: AnthropicStreamSource,
): AsyncIterable<AnthropicStreamEvent>

Parameters ​

NameTypeRequiredDefaultDescription
sourceAnthropicStreamSourceYesn/aProvider SSE source.

Returns ​

TypeDescription
AsyncIterable<AnthropicStreamEvent>Parsed provider events. The type field is taken from the SSE event name when present, otherwise from data.type.

AnthropicStreamEvent ​

Provider event shape consumed by writeMFUIStream().

PropertyTypeRequiredDefaultDescription
typestringNon/aProvider event type. MFUI reads message_start, content_block_delta, and message_delta.
messageRecord<string, unknown>Non/aMessage object. message.usage.input_tokens maps to inputTokens.
deltaRecord<string, unknown>Non/aContent block delta. Text is read when delta.type is text_delta and delta.text is a string.
usageRecord<string, unknown>Non/aMessage delta usage. usage.output_tokens maps to outputTokens.
[key]unknownNon/aAdditional provider fields are ignored by MFUI.

writeMFUIStream() ​

Writes parsed Anthropic events into an MFUI block parser.

Import ​

ts
import { writeMFUIStream } from '@mfui/anthropic';

Signature ​

ts
function writeMFUIStream(
  stream: AsyncIterable<AnthropicStreamEvent>,
  parser: MFUIBlockParser,
  options?: AnthropicMFUIStreamOptions,
): Promise<void>

Parameters ​

NameTypeRequiredDefaultDescription
streamAsyncIterable<AnthropicStreamEvent>Yesn/aParsed provider events.
parserMFUIBlockParserYesn/aMFUI block parser receiving provider text deltas.
optionsAnthropicMFUIStreamOptionsNo{}Stream writing options.

Returns ​

TypeDescription
Promise<void>Resolves after the provider stream is fully consumed and the parser is flushed or closed.

AnthropicMFUIStreamOptions ​

Options for writeMFUIStream() and pipeMFUIStream().

PropertyTypeRequiredDefaultDescription
closebooleanNotrueWhether to call parser.close() when the provider stream ends. When false, parser.flush() is called instead.

pipeMFUIStream() ​

Reads a provider SSE source and writes it into an MFUI block parser.

Import ​

ts
import { pipeMFUIStream } from '@mfui/anthropic';

Signature ​

ts
function pipeMFUIStream(
  source: AnthropicStreamSource,
  parser: MFUIBlockParser,
  options?: AnthropicMFUIStreamOptions,
): Promise<void>

Parameters ​

NameTypeRequiredDefaultDescription
sourceAnthropicStreamSourceYesn/aProvider SSE source.
parserMFUIBlockParserYesn/aMFUI block parser receiving provider text deltas.
optionsAnthropicMFUIStreamOptionsNo{}Stream writing options.

Returns ​

TypeDescription
Promise<void>Resolves after the provider stream is consumed.