Skip to main content
Glama
hbd

MCP Chat

by hbd

leave_chat

Exit a chat room in the MCP Chat server by providing room and client identifiers to disconnect from group conversations.

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
NameRequiredDescriptionDefault
room_idYes
client_idYes

Implementation Reference

  • Implementation of the 'leave_chat' tool handler. This async function handles the logic for a user leaving a chat room: validates user and room, closes the room using RoomManager, logs the action, and notifies the partner via message queue and notification if applicable. Registered via @mcp.tool() decorator.
    @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"}

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/hbd/mcp-chat'

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