send_message
Send messages in real-time within MCP Chat rooms. Use this tool to initiate conversations by providing room ID, message, and client ID, then call wait_for_message to receive responses and maintain interactive communication flow.
Instructions
Send a message to your chat partner.
IMPORTANT: After sending a message, you should immediately call wait_for_message to receive the response. This enables real-time conversation flow.
Typical usage:
Call send_message to send your message
Call wait_for_message to wait for the response
Repeat
Args: room_id: The ID of the chat room message: The message to send client_id: Your client identifier (from enter_queue or join_room)
Returns: Success status or error information
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| client_id | Yes | ||
| message | Yes | ||
| room_id | Yes |
Implementation Reference
- mcp_chat/server.py:149-257 (handler)The primary handler for the 'send_message' MCP tool. Decorated with @mcp.tool() which registers it with the FastMCP server. Implements the logic to send a message to the chat partner in the specified room, using message queues for delivery and notifications.@mcp.tool() async def send_message(room_id: str, message: str, client_id: str) -> Dict[str, Any]: """Send a message to your chat partner. IMPORTANT: After sending a message, you should immediately call wait_for_message to receive the response. This enables real-time conversation flow. Typical usage: 1. Call send_message to send your message 2. Call wait_for_message to wait for the response 3. Repeat Args: room_id: The ID of the chat room message: The message to send client_id: Your client identifier (from enter_queue or join_room) Returns: Success status or error information """ # Use the provided client_id connection_id = client_id # Get user user = connections.get(connection_id) if not user: logger.error(f"User not found for client_id: {client_id}") logger.debug(f"Active connections: {list(connections.keys())}") return { "success": False, "error": f"User not found. Invalid client_id: {client_id}", } # Get room room = await room_manager.get_room(room_id) if not room: return {"success": False, "error": "Room not found"} if not room.active: return {"success": False, "error": "Chat has ended"} # Verify user is in the room if not room.has_user(user.user_id): return {"success": False, "error": "You are not in this room"} # Get partner partner = room.get_partner(user.user_id) if not partner: return {"success": False, "error": "Partner not found"} # Create message msg = Message(room_id=room_id, sender_id=user.user_id, content=message) # Log message logger.info(f"Message from {user.name} to {partner.name}: {message[:50]}...") # Create message data message_data = { "content": message, "sender_name": user.name, "sender_id": user.user_id, "timestamp": msg.timestamp.isoformat(), "message_id": msg.message_id, } # Deliver to waiting recipients via message queues if room_id in message_queues: # Create a copy of items to avoid modification during iteration recipients = list(message_queues[room_id].items()) for recipient_id, queue in recipients: if recipient_id != user.user_id: # Don't send to self try: # Put message in queue (non-blocking) queue.put_nowait(message_data) logger.info( f"Delivered message to waiting queue for {recipient_id}" ) except asyncio.QueueFull: logger.warning(f"Queue full for recipient {recipient_id}") except Exception as e: # Handle case where queue was closed/cancelled logger.warning(f"Failed to deliver to {recipient_id}: {e}") # Clean up the dead queue if ( room_id in message_queues and recipient_id in message_queues[room_id] ): del message_queues[room_id][recipient_id] if not message_queues[room_id]: del message_queues[room_id] # Still send notification for future notification support await send_notification( partner.connection_id, "message.received", { "room_id": room_id, "message": message, "sender": {"user_id": user.user_id, "display_name": user.name}, "timestamp": msg.timestamp.isoformat(), }, ) return { "success": True, "message_id": msg.message_id, "timestamp": msg.timestamp.isoformat(), }
- mcp_chat/server.py:149-149 (registration)The @mcp.tool() decorator registers the send_message function as an MCP tool.@mcp.tool()
- mcp_chat/server.py:150-168 (schema)Input schema defined by function parameters (room_id: str, message: str, client_id: str) and detailed in docstring. Output is Dict[str, Any] with success status.async def send_message(room_id: str, message: str, client_id: str) -> Dict[str, Any]: """Send a message to your chat partner. IMPORTANT: After sending a message, you should immediately call wait_for_message to receive the response. This enables real-time conversation flow. Typical usage: 1. Call send_message to send your message 2. Call wait_for_message to wait for the response 3. Repeat Args: room_id: The ID of the chat room message: The message to send client_id: Your client identifier (from enter_queue or join_room) Returns: Success status or error information """