Fix streaming mode for CLI, MCP, TCP protocols and add SSE reconnection

Merged
H3
2mo ago
+507-105

Summary

call_tool_streaming failed for several protocols instead of emitting the full result as a single chunk the way the HTTP protocol does. This PR fixes those and implements the SSE reconnection that the call template already advertised.

Streaming fixes

  • CLI: raised NotImplementedError on every streaming call. Now runs the command and yields the result once.
  • MCP: yielded the un-awaited coroutine instead of the result.
  • TCP: was a plain coroutine returning a generator, so the client's async for failed with a TypeError. UDP gets the same shape and return annotation.

SSE

  • reconnect / retry_timeout were accepted but never acted on. When an established stream drops, the client now waits retry_timeout ms (overridden by any retry: field from the server), reconnects with the Last-Event-ID header, and gives up after MAX_RECONNECT_ATTEMPTS (5) per call so a tool call can never hang forever. A clean end of stream completes the call and never reconnects. Connection or HTTP errors on the initial request still fail immediately, so a wrong URL does not trigger retries. The redirect refusal from the security hardening applies to every attempt.
  • The parser handles CRLF line endings and a trailing event without a terminating blank line.

Already correct and untouched: file, text, gql, http, streamable_http, websocket.

Test plan

  • New tests: single-chunk streaming for CLI, MCP, TCP, UDP
  • New tests: SSE reconnect resumes with Last-Event-ID; reconnect=False raises after partial events; attempt cap is honored
  • pytest over the cli, socket, mcp and http plugin test suites on top of current dev: 288 passed, 9 skipped

Companion PR: universal-tool-calling-protocol/typescript-utcp#41 brings the TypeScript implementation to parity.

🤖 Generated with Claude Code


Summary by cubic

Fixes streaming tool calls for CLI, MCP, TCP, and UDP, which previously failed or yielded the wrong object instead of emitting the full result as one chunk. Also implements and hardens SSE reconnection, which the call template advertised but never performed, and pins the MCP plugin to mcp 1.x.

Bug Fixes

  • CLI now runs the command and yields the result once instead of raising NotImplementedError.
  • MCP now awaits the tool call before yielding it.
  • TCP streaming is now an async generator; UDP gets the same shape and AsyncGenerator return annotation.
  • Pins mcp to <2; version 2.x removed FastMCP and McpError, which broke test collection in CI.

New Features

  • When an established SSE stream drops, the client waits retry_timeout ms (or the server's retry: value), reconnects with Last-Event-ID, and stops after 5 attempts.
  • A clean end of stream completes the call without reconnecting; initial connection and HTTP errors still fail immediately, and redirects remain refused.
  • Reconnect handshake failures are retried (only the initial handshake fails fast), and the reconnect delay is capped at 60 s.
  • The handshake is bounded by a 30-second timeout so a silent server cannot hang the call.
  • The parser handles CRLF splits across chunks, multi-byte UTF-8, and trailing unterminated events; events over 16 MiB without a delimiter raise SseProtocolError, which is never retried.