Skip to main content
Glama

subscribe

Monitor blockchain events in real-time by subscribing to new blocks, transaction logs, or mined transactions using the Alchemy MCP Plugin.

Instructions

Subscribe to blockchain events

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
typeYesThe type of subscription
addressNoThe address to filter by (for logs)
topicsNoThe topics to filter by (for logs)

Implementation Reference

  • Handler function for the 'subscribe' tool. Validates parameters, creates an Alchemy WebSocket subscription based on the type ('newHeads', 'logs', 'pendingTransactions', 'mined'), stores the subscription with a generated ID, and returns the subscriptionId.
    private async handleSubscribe(args: Record<string, unknown>) {
      const params = this.validateAndCastParams<SubscribeParams>(
        args,
        isValidSubscribeParams,
        "Invalid subscribe parameters"
      );
    
      const subscriptionId = Math.random().toString(36).substring(7);
      let subscription;
    
      switch (params.type) {
        case "newHeads":
          subscription = this.alchemy.ws.on("block", (blockNumber) => {
            console.log("[WebSocket] New block:", blockNumber);
          });
          break;
        case "logs":
          subscription = this.alchemy.ws.on(
            {
              address: params.address,
              topics: params.topics,
            },
            (log) => {
              console.log("[WebSocket] New log:", log);
            }
          );
          break;
        case "pendingTransactions":
          subscription = this.alchemy.ws.on("pending", (tx) => {
            console.log("[WebSocket] Pending transaction:", tx);
          });
          break;
        case "mined":
          subscription = this.alchemy.ws.on("mined", (tx) => {
            console.log("[WebSocket] Mined transaction:", tx);
          });
          break;
        default:
          throw new McpError(
            ErrorCode.InvalidParams,
            `Unknown subscription type: ${params.type}`
          );
      }
    
      this.activeSubscriptions.set(subscriptionId, subscription);
      return { subscriptionId };
    }
  • index.ts:936-961 (registration)
    Registration of the 'subscribe' tool in the ListTools response, including name, description, and inputSchema.
    {
      name: "subscribe",
      description: "Subscribe to blockchain events",
      inputSchema: {
        type: "object",
        properties: {
          type: {
            type: "string",
            enum: ["newHeads", "logs", "pendingTransactions", "mined"],
            description: "The type of subscription",
          },
          address: {
            type: "string",
            description: "The address to filter by (for logs)",
          },
          topics: {
            type: "array",
            items: {
              type: "string",
            },
            description: "The topics to filter by (for logs)",
          },
        },
        required: ["type"],
      },
    },
  • Type definition for SubscribeParams used in the handler for input validation.
    type SubscribeParams = {
      type: string;
      address?: string;
      topics?: string[];
    };
  • Validation helper function isValidSubscribeParams used to validate subscribe parameters before casting.
    isValidSubscribeParams = (args: any): args is SubscribeParams => {
      return (
        typeof args === "object" &&
        args !== null &&
        typeof args.type === "string" &&
        ["newHeads", "logs", "pendingTransactions", "mined"].includes(
          args.type
        ) &&
        (args.address === undefined || typeof args.address === "string") &&
        (args.topics === undefined || Array.isArray(args.topics))
      );
    };
  • index.ts:999-1001 (registration)
    Dispatcher switch case that routes 'subscribe' tool calls to the handleSubscribe handler.
    case "subscribe":
      result = await this.handleSubscribe(request.params.arguments);
      break;
Behavior2/5

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

With no annotations provided, the description carries full burden but only states the action without behavioral details. It doesn't disclose whether this is a long-running operation, how events are delivered (e.g., via WebSocket, callback), error handling, or any rate limits. This leaves significant gaps for an agent to understand how to use it effectively.

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 a single, efficient sentence with zero waste. It's front-loaded and directly conveys the core action without unnecessary elaboration, making it easy to parse quickly.

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

Completeness2/5

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

Given the complexity of a subscription tool (likely involving real-time events and state management), no annotations, and no output schema, the description is insufficient. It doesn't explain what happens after subscription, how to handle responses, or integration with 'unsubscribe,' leaving critical context gaps for proper usage.

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 description coverage is 100%, so the schema already documents all parameters well. The description adds no additional meaning about parameters beyond implying subscription to 'blockchain events,' which aligns with the schema's enum values. This meets the baseline for high schema coverage.

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 clearly states the verb ('subscribe') and resource ('blockchain events'), making the purpose immediately understandable. However, it doesn't differentiate this tool from its sibling 'unsubscribe' or explain what subscription entails beyond the basic action.

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?

No guidance is provided about when to use this tool versus alternatives like polling with other sibling tools (e.g., get_block_number, get_transaction). The description lacks context about subscription scenarios, prerequisites, or comparisons to other methods for monitoring blockchain events.

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/itsanishjain/alchemy-sdk-mcp'

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