MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / serve_stream

Function serve_stream

tests/test_open_telemetry.py:654–668  ·  view source on GitHub ↗

Serves the `stream_events` to each request on a local port, and gives the base URL.

()

Source from the content-addressed store, hash-verified

652
653@asynccontextmanager
654async def serve_stream() -> AsyncIterator[str]:
655 """Serves the `stream_events` to each request on a local port, and gives the base URL."""
656
657 async def respond(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
658 await reader.readuntil(b"\r\n\r\n")
659 body = sse(stream_events())
660 writer.write(b"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\ntransfer-encoding: chunked\r\n\r\n")
661 writer.write(b"%x\r\n%b\r\n0\r\n\r\n" % (len(body), body))
662 await writer.drain()
663 await reader.read()
664 writer.close()
665
666 server = await asyncio.start_server(respond, "127.0.0.1", 0)
667 async with server:
668 yield f"http://127.0.0.1:{server.sockets[0].getsockname()[1]}"
669
670
671FIRST_DELTAS = [

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected