ProvenPin Install

Anthropic SDK message streaming with text deltas, tested with a mocked SSE fetch: working versions (tested 2026-10-15)

Stream Claude responses with the Anthropic TypeScript SDK (messages.stream, text events, finalMessage), tested offline with a fake fetch returning Anthropic SSE events.

passed2 passed · 0 failed2026-10-15 13:27 UTC
Status
passed
Last passed
2026-10-15 13:27 UTC
Tests
2 / 2
Language
TypeScript
Runtime
node v22.23.3
pinned versions3
@anthropic-ai/sdk@0.134.0tsx@4.23.15@types/node@24.19.2
run it
npm install --no-audit --no-fund --ignore-scripts=false
npm test
test environment
node v22.23.3
linux 6.17.0-1022-azure
x64
runner: github-actions 38742087302

The code

Exactly the files that passed. Your agent can pull them with get_recipe("anthropic-streaming-mock") (install), or see them on GitHub.

package.json
{
  "name": "anthropic-streaming-mock",
  "private": true,
  "type": "module",
  "scripts": {
    "test": "node --import tsx --test test/*.test.ts"
  },
  "dependencies": {
    "@anthropic-ai/sdk": "0.134.0"
  },
  "devDependencies": {
    "tsx": "4.23.15",
    "@types/node": "24.19.2"
  }
}
src/stream.ts
import Anthropic from "@anthropic-ai/sdk";

export async function streamReply(client: Anthropic, prompt: string, onText: (t: string) => void) {
  const stream = client.messages.stream({ model: "claude-sonnet-4-5", max_tokens: 256, messages: [{ role: "user", content: prompt }] });
  stream.on("text", onText);
  const final = await stream.finalMessage(); // resolves once message_stop arrives
  return { text: final.content.filter((b) => b.type === "text").map((b) => b.text).join(""), stop: final.stop_reason, outputTokens: final.usage.output_tokens };
}
test/stream.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import Anthropic from "@anthropic-ai/sdk";
import { streamReply } from "../src/stream.ts";

const ev = (type: string, data: object) => `event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`;
function client(events: string[]) {
  const fetch = async () => new Response(new ReadableStream({ start(c) { for (const e of events) c.enqueue(new TextEncoder().encode(e)); c.close(); } }), { status: 200, headers: { "content-type": "text/event-stream" } });
  return new Anthropic({ apiKey: "test", baseURL: "http://mock.local", fetch: fetch as any, maxRetries: 0 });
}

test("streams text deltas and builds the final message", async () => {
  const parts: string[] = [];
  const r = await streamReply(client([
    ev("message_start", { message: { id: "m1", type: "message", role: "assistant", model: "claude-sonnet-4-5", content: [], stop_reason: null, stop_sequence: null, usage: { input_tokens: 5, output_tokens: 1 } } }),
    ev("content_block_start", { index: 0, content_block: { type: "text", text: "" } }),
    ev("content_block_delta", { index: 0, delta: { type: "text_delta", text: "Hi " } }),
    ev("content_block_delta", { index: 0, delta: { type: "text_delta", text: "there" } }),
    ev("content_block_stop", { index: 0 }),
    ev("message_delta", { delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } }),
    ev("message_stop", {}),
  ]), "hello", (t) => parts.push(t));
  assert.deepEqual(parts, ["Hi ", "there"]);
  assert.deepEqual(r, { text: "Hi there", stop: "end_turn", outputTokens: 3 });
});

test("stream error events reject", async () => {
  await assert.rejects(streamReply(client([ev("error", { error: { type: "overloaded_error", message: "Overloaded" } })]), "x", () => {}));
});

Connect to ProvenPin

Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.

terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp

Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.