Skip to main content
Glama

subscribe_events

Subscribe to real-time Telegram events filtered by chat ID and event type. Read event data from the queue resource.

Instructions

Subscribe to real-time Telegram events.

Receive notifications when new events match your filters. Read the telegram://events/queue resource to get event data.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chat_idsNoOnly events from these chats (None = all allowed chats).
event_typesNoFilter by type: "message", "command" (None = all).

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
okYes
errorNo
subscription_idNo
chat_idsNo
event_typesNo
noteNo

Implementation Reference

  • The subscribe_events tool handler: subscribes to real-time Telegram events with optional chat_id and event_type filters. Validates EventManager is configured, checks chat permissions, then calls ctx.event_manager.subscribe() and returns a subscription ID.
    async def subscribe_events(
        chat_ids: list[int] | None = None,
        event_types: list[str] | None = None,
    ) -> SubscribeEventsResult:
        """Subscribe to real-time Telegram events.
    
        Receive notifications when new events match your filters.
        Read the telegram://events/queue resource to get event data.
    
        Args:
            chat_ids: Only events from these chats (None = all allowed chats).
            event_types: Filter by type: "message", "command" (None = all).
        """
        audit_args = {"chat_ids": chat_ids, "event_types": event_types}
    
        if ctx.event_manager is None:
            result = SubscribeEventsResult(
                ok=False,
                error="EventManager is not configured. Pass event_manager to AiogramMCP.",
            )
            if ctx.audit_logger:
                ctx.audit_logger.log("subscribe_events", audit_args, result.ok, result.error)
            return result
    
        if chat_ids is not None:
            for cid in chat_ids:
                if not ctx.is_chat_allowed(cid):
                    result = SubscribeEventsResult(
                        ok=False,
                        error=f"Chat {cid} is not in allowed_chat_ids.",
                    )
                    if ctx.audit_logger:
                        ctx.audit_logger.log("subscribe_events", audit_args, result.ok, result.error)
                    return result
    
        sub_id = ctx.event_manager.subscribe(
            chat_ids=chat_ids,
            event_types=event_types,
        )
        result = SubscribeEventsResult(
            ok=True,
            subscription_id=sub_id,
            chat_ids=chat_ids,
            event_types=event_types,
            note="Read telegram://events/queue to get events.",
        )
        if ctx.audit_logger:
            ctx.audit_logger.log("subscribe_events", audit_args, result.ok, result.error)
        return result
  • Output schema for subscribe_events: extends ToolResponse with subscription_id, chat_ids, event_types, and note fields.
    class SubscribeEventsResult(ToolResponse):
        subscription_id: str | None = None
        chat_ids: list[int] | None = None
        event_types: list[str] | None = None
        note: str | None = None
  • Registration function: register_event_tools is called from AiogramMCP._register_tools() (server.py:100). It conditionally registers subscribe_events and unsubscribe_events as FastMCP tools via @mcp.tool decorator if allowed_tools permits.
    def register_event_tools(
        mcp: FastMCP, ctx: BotContext, allowed_tools: set[str] | None = None
    ) -> None:
        if allowed_tools is None or "subscribe_events" in allowed_tools:
    
            @mcp.tool
            async def subscribe_events(
                chat_ids: list[int] | None = None,
                event_types: list[str] | None = None,
            ) -> SubscribeEventsResult:
                """Subscribe to real-time Telegram events.
    
                Receive notifications when new events match your filters.
                Read the telegram://events/queue resource to get event data.
    
                Args:
                    chat_ids: Only events from these chats (None = all allowed chats).
                    event_types: Filter by type: "message", "command" (None = all).
                """
                audit_args = {"chat_ids": chat_ids, "event_types": event_types}
    
                if ctx.event_manager is None:
                    result = SubscribeEventsResult(
                        ok=False,
                        error="EventManager is not configured. Pass event_manager to AiogramMCP.",
                    )
                    if ctx.audit_logger:
                        ctx.audit_logger.log("subscribe_events", audit_args, result.ok, result.error)
                    return result
    
                if chat_ids is not None:
                    for cid in chat_ids:
                        if not ctx.is_chat_allowed(cid):
                            result = SubscribeEventsResult(
                                ok=False,
                                error=f"Chat {cid} is not in allowed_chat_ids.",
                            )
                            if ctx.audit_logger:
                                ctx.audit_logger.log("subscribe_events", audit_args, result.ok, result.error)
                            return result
    
                sub_id = ctx.event_manager.subscribe(
                    chat_ids=chat_ids,
                    event_types=event_types,
                )
                result = SubscribeEventsResult(
                    ok=True,
                    subscription_id=sub_id,
                    chat_ids=chat_ids,
                    event_types=event_types,
                    note="Read telegram://events/queue to get events.",
                )
                if ctx.audit_logger:
                    ctx.audit_logger.log("subscribe_events", audit_args, result.ok, result.error)
                return result
    
        if allowed_tools is None or "unsubscribe_events" in allowed_tools:
    
            @mcp.tool
            async def unsubscribe_events(subscription_id: str) -> UnsubscribeEventsResult:
                """Unsubscribe from real-time Telegram events.
    
                Args:
                    subscription_id: The subscription ID returned by subscribe_events.
                """
                audit_args = {"subscription_id": subscription_id}
    
                if ctx.event_manager is None:
                    result = UnsubscribeEventsResult(
                        ok=False,
                        error="EventManager is not configured.",
                    )
                    if ctx.audit_logger:
                        ctx.audit_logger.log("unsubscribe_events", audit_args, result.ok, result.error)
                    return result
    
                removed = ctx.event_manager.unsubscribe(subscription_id)
                if removed:
                    result = UnsubscribeEventsResult(ok=True, subscription_id=subscription_id)
                else:
                    result = UnsubscribeEventsResult(
                        ok=False,
                        error=f"Subscription '{subscription_id}' not found.",
                    )
                if ctx.audit_logger:
                    ctx.audit_logger.log("unsubscribe_events", audit_args, result.ok, result.error)
                return result
  • EventManager.subscribe() is called by the subscribe_events handler. Creates a Subscription with a UUID-based ID and stores it in the subscriptions dict.
    def subscribe(
        self,
        *,
        chat_ids: list[int] | None = None,
        event_types: list[str] | None = None,
        session: Any = None,
    ) -> str:
        """Create a subscription and return its ID."""
        sub_id = uuid.uuid4().hex[:12]
        self._subscriptions[sub_id] = Subscription(
            id=sub_id,
            chat_ids=chat_ids,
            event_types=event_types,
            session=session,
        )
        logger.info("Subscription %s created (chats=%s, types=%s)", sub_id, chat_ids, event_types)
        return sub_id
Behavior3/5

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

With no annotations, the description explains the subscription and queue retrieval mechanism but lacks details on lifecycle, side effects, or required cleanup.

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 concise with two front-loaded sentences, no fluff, and clearly communicates the core action and data retrieval method.

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 output schema and full parameter descriptions, the description adequately covers subscription and data reading, though it could mention the need for unsubscribe.

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

Parameters3/5

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

Schema coverage is 100% and the description adds no extra meaning beyond the provided schema descriptions for chat_ids and event_types.

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 tool subscribes to real-time Telegram events and mentions filtering, distinguishing it from siblings like unsubscribe_events and other send/get tools.

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

Usage Guidelines3/5

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

The description implies usage for receiving events but does not explicitly contrast with alternatives or mention when not to use it.

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