Skip to main content
Glama

send_message_stream

Send messages to A2A agents and receive streaming responses for real-time interaction and multi-turn conversations.

Instructions

Send a message to an A2A agent and stream the response.

Args: agent_url: URL of the A2A agent message: Message to send session_id: Optional session ID for multi-turn conversations

Returns: Stream of agent's responses

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
agent_urlYes
messageYes
session_idNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The core handler function for the 'send_message_stream' tool, decorated with @mcp.tool(). It sends a streaming message to a registered A2A agent using A2AClient.send_task_streaming(), processes the event stream (TaskStatusUpdateEvent and TaskArtifactUpdateEvent), accumulates responses, messages, and artifacts, and returns a complete response dictionary.
    @mcp.tool()
    async def send_message_stream(
        agent_url: str,
        message: str,
        session_id: Optional[str] = None,
        ctx: Context = None,
    ) -> Dict[str, Any]:
        """
        Send a message to an A2A agent and stream the response.
        
        Args:
            agent_url: URL of the A2A agent
            message: Message to send
            session_id: Optional session ID for multi-turn conversations
            
        Returns:
            Stream of agent's responses
        """
        if agent_url not in registered_agents:
            return {
                "status": "error",
                "message": f"Agent not registered: {agent_url}",
            }
        
        # Create a client for the agent
        client = A2AClient(url=agent_url)
        
        try:
            # Generate a task ID
            task_id = str(uuid.uuid4())
            
            # Store the mapping of task_id to agent_url for later reference
            task_agent_mapping[task_id] = agent_url
            
            # Save the task mapping to disk
            save_to_json(task_agent_mapping, TASK_AGENT_MAPPING_FILE)
            
            # Create the message
            a2a_message = Message(
                role="user",
                parts=[TextPart(text=message)],
            )
            
            if ctx:
                await ctx.info(f"Sending message to agent (streaming): {message}")
                
            # Start progress indication
            if ctx:
                await ctx.info("Processing...")
            
            # Dictionary to accumulate streaming responses
            complete_response = {
                "status": "success",
                "task_id": task_id,
                "session_id": session_id,
                "state": "working",
                "messages": [],
                "artifacts": [],
            }
            
            # Create payload as a single dictionary
            payload = {
                "id": task_id,
                "message": a2a_message,
            }
            if session_id:
                payload["sessionId"] = session_id
            
            # Send the task and subscribe to updates
            stream = client.send_task_streaming(payload)
            
            # Process and report stream events
            try:
                all_events = []
                
                async for event in stream:
                    # Save all events for debugging
                    all_events.append({
                        "type": str(type(event)),
                        "dir": str(dir(event)),
                    })
                    
                    if hasattr(event, "result"):
                        if hasattr(event.result, "status"):
                            # It's a TaskStatusUpdateEvent
                            status_event = event.result
                            
                            # Update the state
                            if hasattr(status_event, "status") and hasattr(status_event.status, "state"):
                                complete_response["state"] = status_event.status.state
                            
                            # Extract any message
                            if hasattr(status_event, "status") and hasattr(status_event.status, "message") and status_event.status.message:
                                message_text = ""
                                for part in status_event.status.message.parts:
                                    if part.type == "text":
                                        message_text += part.text
                                
                                if message_text:
                                    complete_response["messages"].append(message_text)
                                    if ctx:
                                        await ctx.info(f"Agent: {message_text}")
                            
                            # If this is the final event, set session ID
                            if hasattr(status_event, "final") and status_event.final:
                                complete_response["session_id"] = getattr(status_event, "sessionId", session_id)
                            
                        elif hasattr(event.result, "artifact"):
                            # It's a TaskArtifactUpdateEvent
                            artifact_event = event.result
                            
                            # Extract artifact content
                            artifact_data = {
                                "name": artifact_event.artifact.name if hasattr(artifact_event.artifact, "name") else "unnamed",
                                "contents": [],
                            }
                            
                            for part in artifact_event.artifact.parts:
                                if part.type == "text":
                                    artifact_data["contents"].append({
                                        "type": "text",
                                        "text": part.text,
                                    })
                                elif part.type == "data":
                                    artifact_data["contents"].append({
                                        "type": "data",
                                        "data": part.data,
                                    })
                            
                            complete_response["artifacts"].append(artifact_data)
                            
                            if ctx:
                                await ctx.info(f"Received artifact: {artifact_data['name']}")
                    else:
                        # Unknown event type, try to extract what we can
                        complete_response["unknown_events"] = complete_response.get("unknown_events", []) + [
                            {
                                "type": str(type(event)),
                                "dir": str(dir(event))
                            }
                        ]
                
                # Include debug info            
                complete_response["_debug_info"] = {
                    "all_events": all_events
                }
                
                return complete_response
            except Exception as e:
                return {
                    "status": "error",
                    "message": f"Error processing stream events: {str(e)}",
                    "_debug_info": {
                        "all_events": all_events
                    }
                }
                
        except Exception as e:
            return {
                "status": "error",
                "message": f"Error sending message (stream): {str(e)}",
            }
  • The @mcp.tool() decorator registers the send_message_stream function as an MCP tool.
    @mcp.tool()
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden. It discloses the streaming behavior and optional session management for conversations, which are valuable behavioral traits. However, it doesn't mention authentication requirements, rate limits, error conditions, or what happens when streaming fails—significant gaps for a network operation tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is perfectly structured with a clear purpose statement followed by organized Args and Returns sections. Every sentence earns its place by providing essential information without redundancy. The front-loaded purpose statement immediately communicates the tool's core function.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (network communication with streaming), no annotations, but with an output schema (implied by 'Returns: Stream'), the description does reasonably well. It covers the basic operation, parameters, and return type. However, it lacks details about error handling, streaming mechanics, or performance characteristics that would be helpful for complete understanding.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate. It provides clear semantic meaning for all three parameters: 'agent_url' as the target URL, 'message' as the content to send, and 'session_id' for conversation continuity. This adds substantial value beyond the bare schema types, though it doesn't specify URL format or message constraints.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Send a message to an A2A agent and stream the response'), identifies the resource (A2A agent), and distinguishes it from sibling 'send_message' by specifying the streaming behavior. This provides immediate differentiation from non-streaming alternatives.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context through the mention of 'multi-turn conversations' and streaming, suggesting this is for interactive scenarios where real-time responses are needed. However, it doesn't explicitly state when to choose this over the non-streaming 'send_message' or other alternatives like 'get_task_result'.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/GongRzhe/A2A-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server