leave_chat
Exit a specific chat room in MCP Chat by providing the room ID and client identifier. Confirms the success of leaving the room.
Instructions
Leave the current chat room.
Args: room_id: The ID of the chat room to leave client_id: Your client identifier (from enter_queue)
Returns: Success status
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| client_id | Yes | ||
| room_id | Yes |
Implementation Reference
- mcp_chat/server.py:259-322 (handler)The 'leave_chat' tool handler function. It validates the user and room, closes the room using RoomManager, logs the event, and notifies the partner via message queue and notification if applicable. The @mcp.tool() decorator registers this function as an MCP tool named 'leave_chat'.@mcp.tool() async def leave_chat(room_id: str, client_id: str) -> Dict[str, Any]: """Leave the current chat room. Args: room_id: The ID of the chat room to leave client_id: Your client identifier (from enter_queue) Returns: Success status """ # Use the provided client_id connection_id = client_id # Get user user = connections.get(connection_id) if not user: return {"success": False, "error": "User not found"} # Get room room = await room_manager.get_room(room_id) if not room: return {"success": False, "error": "Room not found"} # 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 before closing room partner = room.get_partner(user.user_id) # Close the room await room_manager.close_room(room_id) # Log logger.info(f"User {user.name} left room {room_id}") # Notify partner if they exist if partner: # Send disconnection message to waiting queue if room_id in message_queues and partner.user_id in message_queues[room_id]: disconnect_msg = { "content": "[System] Your chat partner has left the conversation.", "sender_name": "System", "sender_id": "system", "timestamp": datetime.now().isoformat(), "message_id": str(uuid.uuid4()), "system": True, "disconnect": True, } try: message_queues[room_id][partner.user_id].put_nowait(disconnect_msg) except (asyncio.QueueFull, RuntimeError) as e: # Queue might be full or closed logger.debug(f"Could not send disconnect notification: {e}") # Also send regular notification await send_notification( partner.connection_id, "partner.disconnected", {"room_id": room_id, "reason": "left"}, ) return {"success": True, "message": "Successfully left the chat"}