Skip to main content
Glama

ban_user

Ban a user from a chat by user ID and chat ID, with optional duration and message revocation.

Instructions

Ban a user from a chat.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chat_idYes
user_idYes
ban_duration_hoursNo
revoke_messagesNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
okYes
errorNo
user_idNo
permanentNo
untilNo

Implementation Reference

  • The ban_user tool handler — an async function decorated with @mcp.tool that bans a user from a chat via bot.ban_chat_member(), supporting optional ban_duration_hours and revoke_messages. Wraps result in BanUserResult and includes audit logging.
    async def ban_user(
        chat_id: int,
        user_id: int,
        ban_duration_hours: int | None = None,
        revoke_messages: bool = False,
    ) -> BanUserResult:
        """Ban a user from a chat."""
        if not ctx.is_chat_allowed(chat_id):
            result = BanUserResult(ok=False, error=f"Chat {chat_id} is not allowed.")
            if ctx.audit_logger:
                ctx.audit_logger.log(
                    "ban_user",
                    {"chat_id": chat_id, "user_id": user_id},
                    result.ok,
                    result.error,
                )
            return result
    
        until_date = None
        if ban_duration_hours:
            until_date = datetime.now(timezone.utc) + timedelta(hours=ban_duration_hours)
    
        try:
            if ctx.rate_limiter:
                await ctx.rate_limiter.acquire()
            await ctx.bot.ban_chat_member(
                chat_id=chat_id,
                user_id=user_id,
                until_date=until_date,
                revoke_messages=revoke_messages,
            )
            result = BanUserResult(
                ok=True,
                user_id=user_id,
                permanent=until_date is None,
                until=until_date.isoformat() if until_date else None,
            )
        except (TelegramBadRequest, TelegramForbiddenError) as exc:
            result = BanUserResult(ok=False, error=str(exc))
    
        if ctx.audit_logger:
            ctx.audit_logger.log(
                "ban_user",
                {"chat_id": chat_id, "user_id": user_id},
                result.ok,
                result.error,
            )
        return result
  • BanUserResult model — a ToolResponse subclass with fields: user_id (int), permanent (bool), until (str), used as the return type of the ban_user handler.
    class BanUserResult(ToolResponse):
        user_id: int | None = None
        permanent: bool | None = None
        until: str | None = None
  • Registration gate: the @mcp.tool decorator is applied to ban_user() only if allowed_tools is None or contains 'ban_user'.
    if allowed_tools is None or "ban_user" in allowed_tools:
    
        @mcp.tool
  • Call to register_chat_tools() in the MCP server's _register_tools method, which triggers the registration of ban_user among other chat tools.
    register_chat_tools(self._mcp, self._ctx, allowed_tools=at)
  • Permission mapping: 'ban_user' is assigned PermissionLevel.MODERATION in the TOOL_PERMISSIONS dict.
    "ban_user": PermissionLevel.MODERATION,
Behavior2/5

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

With no annotations, the description must convey behavioral traits. It only states 'Ban,' missing critical details like whether the ban is reversible, what happens to existing messages, or permissions required. The description does not disclose effects beyond the basic action.

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

Conciseness4/5

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

The description is a single front-loaded sentence with no fluff. However, it is too brief and omits important information, which slightly reduces its effectiveness despite its conciseness.

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

Completeness1/5

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

Despite having an output schema and 4 parameters, the description provides no context about return values, error conditions, permissions, or behavioral consequences. For a destructive action like banning, this completeness is severely lacking.

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

Parameters1/5

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

Schema description coverage is 0%, and the description adds no explanation for any of the four parameters (chat_id, user_id, ban_duration_hours, revoke_messages). Parameter names are somewhat self-explanatory but insufficient for an agent to understand nuances like 'revoke_messages' behavior.

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

Purpose4/5

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

The description 'Ban a user from a chat' clearly states the action (ban) and the resource (user from chat). It effectively distinguishes from the sibling tool 'unban_user' and other chat management tools, though it lacks details about the scope or consequences of banning.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives like 'unban_user' or other moderation tools. Usage is implied but not explicitly stated, and no context setting is provided.

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/Py2755/aiogram-mcp'

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