MCPcopy Create free account
hub / github.com/IBM/mcp-cli / _process_chunk

Method _process_chunk

src/mcp_cli/chat/streaming_handler.py:470–506  ·  view source on GitHub ↗

Process a single streaming chunk. Args: raw_chunk: Raw chunk from LLM provider

(self, raw_chunk: dict[str, Any])

Source from the content-addressed store, hash-verified

468 self._interrupted = True
469
470 async def _process_chunk(self, raw_chunk: dict[str, Any]) -> None:
471 """Process a single streaming chunk.
472
473 Args:
474 raw_chunk: Raw chunk from LLM provider
475 """
476 # Use display to process chunk (normalizes format)
477 await self.display.add_chunk(raw_chunk)
478
479 # Broadcast token to dashboard if connected
480 if self._dashboard_bridge is not None:
481 token: str | None = None
482 if "response" in raw_chunk:
483 token = raw_chunk["response"]
484 elif "content" in raw_chunk:
485 token = raw_chunk["content"]
486 elif "text" in raw_chunk:
487 token = raw_chunk["text"]
488 elif "delta" in raw_chunk and isinstance(raw_chunk["delta"], dict):
489 token = raw_chunk["delta"].get("content")
490 elif "choices" in raw_chunk and raw_chunk["choices"]:
491 delta = raw_chunk["choices"][0].get("delta", {})
492 if isinstance(delta, dict):
493 token = delta.get("content")
494 if token:
495 try:
496 await self._dashboard_bridge.on_token(token)
497 except Exception as _e:
498 logger.debug("Dashboard on_token error: %s", _e)
499
500 # Extract tool calls if present
501 if "tool_calls" in raw_chunk and raw_chunk["tool_calls"]:
502 self.tool_accumulator.process_chunk_tool_calls(raw_chunk["tool_calls"])
503
504 # Capture usage data (providers send it with the final chunk)
505 if "usage" in raw_chunk and raw_chunk["usage"]:
506 self._usage = raw_chunk["usage"]
507
508 async def _handle_non_streaming(
509 self,

Calls 4

on_tokenMethod · 0.80
add_chunkMethod · 0.45
getMethod · 0.45