Streaming API
How Cosmo streams real-time AI responses and sync events.
Cosmo uses Server-Sent Events (SSE) for real-time streaming — not WebSocket. SSE is a standard HTTP mechanism where the server keeps a connection open and pushes newline-delimited JSON events.
For the full event schema and rendering guidance, see SSE Events.
Chat Streaming
Chat responses are streamed from POST /api/v1/chat. The server responds with:
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-aliveEach event is a JSON object on a data: line, terminated by a blank line:
data: {"sessionId":"...","type":"thinking"}\n\n
data: {"sessionId":"...","type":"text","text":"Hello"}\n\n
data: {"sessionId":"...","type":"done","contextUsage":{...}}\n\nConsuming the stream in JavaScript
const response = await fetch('https://api.cosmo.dev/api/v1/chat', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: 'Summarize this week' }),
})
const reader = response.body!.getReader()
const decoder = new TextDecoder()
while (true) {
const { done, value } = await reader.read()
if (done) break
const lines = decoder.decode(value).split('\n')
for (const line of lines) {
if (!line.startsWith('data: ')) continue
const event = JSON.parse(line.slice(6))
handleEvent(event)
}
}Consuming the stream in Node.js
import { createParser } from 'eventsource-parser'
const res = await fetch('https://api.cosmo.dev/api/v1/chat', { ... })
const parser = createParser((event) => {
if (event.type === 'event') {
const data = JSON.parse(event.data)
handleEvent(data)
}
})
for await (const chunk of res.body) {
parser.feed(Buffer.from(chunk).toString())
}MCP Transport
The Cosmo engine connects to MCP servers over three transports, all provided by the official @modelcontextprotocol/sdk:
| Transport | Use case |
|---|---|
stdio | Local MCP servers running as child processes |
streamable-http | Remote MCP servers with HTTP streaming (supports OAuth 2.1) |
sse | Remote MCP servers using legacy SSE transport |
The transport is selected automatically based on the connector's command or url configuration. You do not need to configure this manually.
Desktop IPC
In the desktop app, chat events flow over Electron IPC (not SSE). The main process runs the LLM engine and emits events to the renderer via ipcMain.emit. The event shape is identical to the SSE schema — the same ChatEvent interface is used in both paths. See IPC API for details.