How to integrate Discordbot MCP with LlamaIndex

Trusted by
AWS
Glean
Zoom
Airtable

30 min · no commitment · see it on your stack

Discordbot logo
LlamaIndex logo
divider

Introduction

This guide walks you through connecting Discordbot to LlamaIndex using the Composio tool router. By the end, you'll have a working Discordbot agent that can add reaction emoji to welcome message, bulk delete spam messages in general channel, assign moderator role to user instantly through natural language commands.

This guide will help you understand how to give your LlamaIndex agent real control over a Discordbot account through Composio's Discordbot MCP server.

Before we dive in, let's take a quick look at the key ideas and tools involved.

Also integrate Discordbot with

TL;DR

Here's what you'll learn:
  • Set your OpenAI and Composio API keys
  • Install LlamaIndex and Composio packages
  • Create a Composio Tool Router session for Discordbot
  • Connect LlamaIndex to the Discordbot MCP server
  • Build a Discordbot-powered agent using LlamaIndex
  • Interact with Discordbot through natural language

What is LlamaIndex?

LlamaIndex is a data framework for building LLM applications. It provides tools for connecting LLMs to external data sources and services through agents and tools.

Key features include:

  • ReAct Agent: Reasoning and acting pattern for tool-using agents
  • MCP Tools: Native support for Model Context Protocol
  • Context Management: Maintain conversation context across interactions
  • Async Support: Built for async/await patterns

What is the Discordbot MCP server, and what's possible with it?

The Discordbot MCP server is an implementation of the Model Context Protocol that connects your AI agent and assistants like Claude, Cursor, etc directly to your Discordbot account. It provides structured and secure access to your Discord servers, so your agent can perform actions like moderating users, managing roles, handling group messages, triggering auto-moderation, and automating channel maintenance on your behalf.

  • User moderation and banning: Instruct your agent to ban or bulk ban users, delete their recent messages, and keep your community safe from unwanted behavior.
  • Role and membership management: Automatically assign roles, add users to servers or threads, and update member information to streamline onboarding and permissions.
  • Automated message handling: Let your agent bulk delete messages in a channel or add emoji reactions to keep conversations tidy and interactive.
  • Custom command creation: Enable your agent to create new global application commands, so your Discord server can respond to custom triggers and workflows.
  • Auto moderation rule setup: Have your agent set up or update auto moderation rules for your guild, ensuring safe and compliant community interactions around the clock.

Supported Tools & Triggers

Tools
Add recipient to group channelAdds a user to a Discord group DM channel.
Add guild memberAdds a user to a Discord guild using their OAuth2 access token (which must have guilds.
Assign role to guild memberAssigns a role to a guild member.
Add Reaction To MessageAdds an emoji reaction from the bot to a message.
Add thread memberAdds a user to a specific, unarchived thread.
Ban user from guildPermanently bans a user from a Discord guild, optionally deleting their recent messages.
Bulk ban users from guildBans up to 200 users from a Discord guild, optionally deleting their recent messages.
Bulk Delete MessagesBulk deletes messages in a Discord channel.
Create global application commandCreates a new global Discord application command.
Create auto moderation ruleCreates a new auto moderation rule for a Discord guild.
Create channel inviteCreates a new invite link for a Discord channel.
Initiate user channel with recipientCreates a new direct message (DM) channel or retrieves an existing one, using recipient_id for a 1-on-1 DM or access_tokens for a group DM; this action only establishes or fetches the channel and does not send messages.
Create new guildCreates a new Discord guild (server).
Create guild application commandCreates a new guild-specific application command.
Create guild channelCreates a new Discord channel (text, voice, category, etc.
Create guild emojiCreates a new custom emoji in a specified Discord guild, requiring CREATE_EXPRESSIONS permission and adherence to guild emoji limits.
Create guild from templateCreates a new Discord guild by applying channels, roles, and settings from a specified guild template code.
Create role with guild idCreates a new role in a Discord guild with customizable name, permissions, color, hoist, mentionability, and icon.
Create guild scheduled eventCreates a new scheduled event in a Discord guild.
Create guild stickerUploads a PNG, APNG, GIF, or Lottie JSON file (max 512KB) as a new custom sticker to the specified Discord guild; requires Manage Expressions permission.
Create guild templateCreates a template of an existing Discord guild's structure (settings, roles, channels) but not its content (messages, members).
Post interaction callbackSends a response to a Discord interaction (e.
Send Message To ChannelSends a message to a Discord channel.
Create stage instanceCreates a new Stage instance in a Stage channel for hosting live audio events.
Create new thread in channelCreates a new thread in a text, announcement, forum, or media channel.
Create thread from messageCreates a new thread from a specific message in a Discord channel, requiring CREATE_PUBLIC_THREADS permission.
Create channel webhookCreates a new webhook in a specified Discord channel, requiring MANAGE_WEBHOOKS permission.
Crosspost MessageCrossposts a message from an announcement channel to all channels following it.
Delete All ReactionsRemoves all reactions from a message.
Delete All Reactions By EmojiRemoves all reactions for a specific emoji from a message.
Delete global application commandPermanently deletes a global application command.
Delete auto moderation rulePermanently deletes a specific auto moderation rule from a Discord guild.
Delete channel by idPermanently deletes a Discord channel by its ID.
Delete channel permission overrideRemoves channel-specific permission overwrites for a user or role, reverting them to default permissions inherited from the server or category.
Remove user from group DMRemoves a recipient from a Discord group DM channel.
Delete guild by idPermanently deletes a specified Discord guild (server).
Delete guild application commandPermanently deletes a guild-specific application command.
Delete guild emoji by idPermanently deletes a specified custom emoji from a guild, requiring Manage Expressions permissions; cannot delete default emojis and is irreversible.
Delete guild integrationPermanently deletes a specific integration from a Discord guild, removing any associated webhooks and kicking the bot if present.
Delete guild member by idRemoves (kicks) a member from a Discord guild.
Delete guild member roleRemoves a specified role from a member of a Discord guild.
Delete role from guildPermanently deletes a specified role from a Discord guild, revoking it from all members.
Delete guild scheduled eventPermanently deletes a specific scheduled event from a Discord guild.
Delete guild stickerPermanently deletes a custom sticker from a Discord guild; the specified guild and sticker must exist, and this action is irreversible.
Delete guild template by codeDeletes an existing guild template by its unique code from a specified guild, returning the deleted template's details.
Delete MessagePermanently deletes a message from a Discord channel.
Remove My ReactionRemoves the bot's own emoji reaction from a message.
Delete original webhook messagePermanently deletes the original (@original) message posted by a webhook or interaction response.
Delete stage instancePermanently deletes the Stage instance for a given stage channel, ending the live audio event.
Remove thread memberRemoves a user from a specified, unarchived thread.
Remove User ReactionRemoves a specific user's emoji reaction from a message.
Delete webhook by idPermanently deletes a Discord webhook by its unique ID.
Delete webhook by tokenPermanently deletes a Discord webhook using its ID and token, without bot authentication.
Delete webhook messageDeletes a message previously sent by a Discord webhook.
Execute GitHub-compatible webhookForwards GitHub event notifications to a Discord channel via a GitHub-compatible webhook endpoint.
Execute Slack-compatible webhookSends messages to Discord via its Slack-compatible webhook endpoint, supporting Slack attachment formatting.
Execute webhookExecutes a Discord webhook to send messages, embeds, or interactive components to a channel or thread.
Add follower to channel via webhookFollows an Announcement Channel to relay its messages to a target channel via webhook.
List active threads in guildRetrieves all active (non-archived) threads in a Discord guild that the bot can access.
Get application detailsRetrieves the full details of a Discord application by its application_id.
Get global application commandRetrieves a specific global application command by its application ID and command ID.
Get application role connections metadataRetrieves all role connection metadata records for a Discord application.
Get user role connectionFetches the role connection object for the current user for a specified Discord application.
Get auto moderation ruleRetrieves the complete configuration of a specific auto moderation rule within a Discord guild.
Get bot gatewayRetrieves the WSS URL, recommended shard count, and session start limits for connecting a bot to the Discord Gateway.
Retrieve channel detailsRetrieves detailed metadata for a specific Discord channel by its channel_id.
Get gateway URLRetrieves the WebSocket URL to connect to Discord's Gateway for receiving real-time events.
Retrieve guild detailsRetrieves detailed information for a specified Discord guild (server) by its guild_id, optionally including approximate member and presence counts if with_counts is true.
Get guild application commandRetrieves detailed information for a specific application command within a Discord guild.
Get guild command permissionsRetrieves the permissions for a specific application command within a guild.
Get guild banFetches the ban details for a specific user in a Discord guild, if that user is currently banned.
Fetch emoji by guild and idRetrieves details for a specific custom emoji within a specified Discord guild, requiring valid and accessible guild and emoji IDs.
Retrieve guild member by user idRetrieves detailed information for a specific member of a Discord guild, provided the bot belongs to the guild and has necessary permissions.
Preview guild by idFetches a public preview of a Discord guild by its ID, if the guild has the preview feature enabled.
Get guild scheduled eventRetrieves a specific scheduled event from a Discord guild by its ID, optionally including the count of subscribed users.
Get guild onboardingRetrieves the onboarding settings for a specified Discord guild, including prompts, options, default channels, and enabled status.
Retrieve sticker from guildRetrieves a specific sticker from a Discord guild using the guild and sticker IDs; requires the sticker to exist in the guild.
Retrieve guild template with codeRetrieves the complete structure and details of a Discord guild template using its unique code.
Retrieve guild vanity urlRetrieves the vanity URL invite code and usage count for a Discord guild.
Retrieve guild webhooksRetrieves all webhooks for a specified Discord guild, requiring MANAGE_WEBHOOKS permission.
Retrieve guild welcome screenRetrieves the welcome screen configuration for a Discord guild with the Community feature enabled.
Retrieve guild widget jsonRetrieves the public JSON widget data for a Discord guild, if the widget is enabled for that guild.
Get Guild Widget PNGTool to retrieve a PNG image widget for a Discord guild.
Retrieve guild widget settingsRetrieves the widget settings for a specified Discord guild, indicating if the widget is enabled and its configured channel ID.
Get MessageRetrieves a specific message from a Discord channel by channel and message ID.
Get my applicationRetrieves detailed information about the current authenticated Discord application via /applications/@me.
Get my OAuth2 applicationRetrieves detailed information about the OAuth2 application associated with the current authentication.
Retrieve original webhook messageRetrieves the original (@original) message from a Discord webhook or interaction response.
Get OAuth2 public keysRetrieves Discord's OAuth2 public keys in JWK format for verifying access tokens.
Get stage instanceRetrieves the active Stage instance for a specified stage channel.
Get stickerRetrieves a specific Discord sticker by its unique ID.
Retrieve thread member by idRetrieves a member from a specified thread using their user ID, optionally including guild member details.
Get userFetches public information for a Discord user by their user ID.
Retrieve webhook by idRetrieves detailed information for a Discord webhook by its unique ID.
Retrieve webhook by tokenRetrieves a Discord webhook's configuration using its ID and token, without requiring bot authentication.
Retrieve webhook messageRetrieves a specific message previously sent by a Discord webhook.
Resolve invite by codeResolves a Discord invite code to get its details, optionally including member counts and expiration.
Revoke invite by codeRevokes a Discord invite using its code, permanently preventing new joins via this link.
Join threadJoins the authenticated user to a thread specified by channel_id.
Leave GuildEnables the bot to leave a specified Discord guild (server).
Leave threadRemoves the currently authenticated user from a specified thread.
List global application commandsFetches all global application commands for the specified Discord application.
List auto moderation rulesRetrieves all auto moderation rules for a specified Discord guild.
List channel invitesFetches all active invites for a given Discord channel.
List Channel WebhooksRetrieves all webhooks configured in a given Discord channel.
List guild command permissionsRetrieves all guild-level permission settings for all commands of a specific application within a guild.
List guild application commandsFetches all application commands registered for a specific guild.
Get guild audit logsRetrieves audit log entries for a specified Discord guild, requiring VIEW_AUDIT_LOG permission.
List guild bansFetches a list of users banned from a specified Discord guild.
Retrieve guild channelsFetches all channels (text, voice, category, etc.
Retrieve guild emojisFetches all custom emoji objects for a specified Discord guild if the bot has access; returns only custom guild emojis, not standard Unicode or Nitro emojis.
List guild integrationsLists all integration objects for a specified Discord guild.
List guild invitesRetrieves all currently active invite codes for a specified Discord guild.
Get guild membersRetrieves a list of members for a Discord guild.
List guild rolesFetches all roles in a Discord guild, providing details for each role including permissions, color, position, and other attributes.
List guild scheduled eventsRetrieves a list of scheduled events for a specified Discord guild, optionally including subscribed user counts.
List scheduled event usersFetches users who have expressed interest in a specific scheduled event within a Discord guild.
Retrieve guild stickersRetrieves all custom sticker objects for a Discord guild; does not include standard/Nitro stickers.
Get guild templates by guild idRetrieves all guild templates for an existing Discord guild, specified by its ID.
List guild voice regionsFetches a list of available voice regions for a specified Discord guild.
List Reactions By EmojiLists users who reacted to a message with a specific emoji.
Fetch Messages From ChannelRetrieves messages from a Discord channel, ordered newest first.
List my private archived threadsRetrieves private archived threads from a channel that the current user is a member of.
List Pinned MessagesRetrieves all pinned messages from a Discord channel.
List private archived threadsLists private archived threads in a Discord channel, sorted by most recent archival.
List public archived threadsLists public archived threads in a Discord channel, sorted by most recent archival.
List sticker packsFetches all available Nitro sticker packs from Discord, excluding custom or guild-specific sticker packs.
List thread membersRetrieves members of a specified Discord thread, with an option to include full guild member objects.
List voice regionsLists all available Discord voice regions that can be used when setting a voice or stage channel's region.
Pin MessagePins a message in a Discord channel.
Preview guild prunePreviews the number of members that would be pruned from a Discord guild based on inactivity days and optional roles; does not remove members.
Prune inactive guild membersRemoves inactive members from a Discord guild.
Update guild onboarding configurationConfigures or updates a Discord guild's new member onboarding flow, including prompts, options, default channels, and enabled status.
Search guild members by username or nicknameSearches for members in a specific Discord guild by matching a query string against usernames and nicknames.
Modify channel permissionsUpdates or creates a permission overwrite for a role (type 0) or member (type 1) within a Discord channel using allow and deny bitwise values.
Sync guild templateSynchronizes a guild template with its source guild, updating it to match the source's current configuration; does not affect guilds already created from this template.
Test bot token authenticationTool to validate the configured Discord bot token by fetching the current authenticated bot user.
Trigger typing indicatorShows the bot is typing in a Discord channel.
Unban user from guildRevokes a ban for a user from a Discord guild, allowing them to rejoin.
Unpin MessageUnpins a message from a Discord channel.
Update applicationUpdates a Discord application's settings using its application_id.
Update global application commandUpdates properties of a global application command.
Update user application role connectionUpdates the current user's application role connection metadata for Discord's Linked Roles feature.
Update auto moderation ruleUpdates an existing auto moderation rule in a Discord guild.
Update channel settingsUpdates a Discord channel's settings (name, topic, permissions, etc.
Update guild settingsUpdates settings for a Discord guild (server).
Update guild application commandUpdates properties of a guild-specific application command.
Update guild emojiUpdates a custom emoji's name and/or role restrictions in a Discord guild; cannot create or delete emojis, and role updates for managed emojis may be restricted by their integration.
Modify guild member detailsUpdates a guild member's attributes including nickname, roles, voice state, timeout status, and flags.
Modify guild roleUpdates a Discord guild role's attributes (name, permissions, color, etc.
Update guild scheduled eventUpdates an existing scheduled event in a Discord guild.
Update guild sticker infoModifies a guild sticker's name, description, or tags.
Update guild templateUpdates a Discord guild template's name and/or description; omitted fields retain current values, and an empty string for description clears it.
Update guild welcome screenUpdates a guild's welcome screen configuration, including description, enabled status, and up to 5 welcome channels.
Update guild widget settingsUpdates a Discord guild's widget settings, such as its enabled state or invite channel.
Edit MessageEdits a message previously sent by the bot.
Update my applicationUpdates settings for the current authenticated Discord application via /applications/@me.
Update bot's nickname in guildModifies the current bot's member profile (nickname) in a Discord guild.
Update current bot user profileUpdates the current bot user's Discord username and/or avatar.
Update original webhook messageUpdates the original (@original) message previously sent by a webhook or interaction response.
Update own voice stateUpdates the bot's own voice state in a guild Stage channel, such as toggling suppress or requesting to speak.
Update user voice stateUpdates another user's voice state in a Discord stage channel.
Update webhook detailsUpdates properties of an existing Discord webhook such as name, avatar, or channel.
Update webhook by tokenUpdates a Discord webhook's name and/or avatar using its ID and token, without bot authentication.
Update webhook messageUpdates a message previously sent by a webhook, allowing modification of content, embeds, attachments, or components.

What is the Composio tool router, and how does it fit here?

What is Composio SDK?

Composio's Composio SDK helps agents find the right tools for a task at runtime. You can plug in multiple toolkits (like Gmail, HubSpot, and GitHub), and the agent will identify the relevant app and action to complete multi-step workflows. This can reduce token usage and improve the reliability of tool calls. Read more here: Getting started with Composio SDK

The tool router generates a secure MCP URL that your agents can access to perform actions.

How the Composio SDK works

The Composio SDK follows a three-phase workflow:

  1. Discovery: Searches for tools matching your task and returns relevant toolkits with their details.
  2. Authentication: Checks for active connections. If missing, creates an auth config and returns a connection URL via Auth Link.
  3. Execution: Executes the action using the authenticated connection.

Step-by-step Guide

Prerequisites

Before you begin, make sure you have:
  • Python 3.8/Node 16 or higher installed
  • A Composio account with the API key
  • An OpenAI API key
  • A Discordbot account and project
  • Basic familiarity with async Python/Typescript

Getting API Keys for OpenAI, Composio, and Discordbot

OpenAI API key (OPENAI_API_KEY)
  • Go to the OpenAI dashboard
  • Create an API key if you don't have one
  • Assign it to OPENAI_API_KEY in .env
Composio API key and user ID
  • Log into the Composio dashboard
  • Copy your API key from Settings
    • Use this as COMPOSIO_API_KEY
  • Pick a stable user identifier (email or ID)
    • Use this as COMPOSIO_USER_ID

Installing dependencies

pip install composio-llamaindex llama-index llama-index-llms-openai llama-index-tools-mcp python-dotenv

Create a new Python project and install the necessary dependencies:

  • composio-llamaindex: Composio's LlamaIndex integration
  • llama-index: Core LlamaIndex framework
  • llama-index-llms-openai: OpenAI LLM integration
  • llama-index-tools-mcp: MCP client for LlamaIndex
  • python-dotenv: Environment variable management

Set environment variables

bash
OPENAI_API_KEY=your-openai-api-key
COMPOSIO_API_KEY=your-composio-api-key
COMPOSIO_USER_ID=your-user-id

Create a .env file in your project root:

These credentials will be used to:

  • Authenticate with OpenAI's GPT-5 model
  • Connect to Composio's Tool Router
  • Identify your Composio user session for Discordbot access

Import modules

import asyncio
import os
import dotenv

from composio import Composio
from composio_llamaindex import LlamaIndexProvider
from llama_index.core.agent.workflow import ReActAgent
from llama_index.core.workflow import Context
from llama_index.llms.openai import OpenAI
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec

dotenv.load_dotenv()

Create a new file called discordbot_llamaindex_agent.py and import the required modules:

Key imports:

  • asyncio: For async/await support
  • Composio: Main client for Composio services
  • LlamaIndexProvider: Adapts Composio tools for LlamaIndex
  • ReActAgent: LlamaIndex's reasoning and action agent
  • BasicMCPClient: Connects to MCP endpoints
  • McpToolSpec: Converts MCP tools to LlamaIndex format

Load environment variables and initialize Composio

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
COMPOSIO_API_KEY = os.getenv("COMPOSIO_API_KEY")
COMPOSIO_USER_ID = os.getenv("COMPOSIO_USER_ID")

if not OPENAI_API_KEY:
    raise ValueError("OPENAI_API_KEY is not set in the environment")
if not COMPOSIO_API_KEY:
    raise ValueError("COMPOSIO_API_KEY is not set in the environment")
if not COMPOSIO_USER_ID:
    raise ValueError("COMPOSIO_USER_ID is not set in the environment")

What's happening:

This ensures missing credentials cause early, clear errors before the agent attempts to initialise.

Create a Tool Router session and build the agent function

async def build_agent() -> ReActAgent:
    composio_client = Composio(
        api_key=COMPOSIO_API_KEY,
        provider=LlamaIndexProvider(),
    )

    session = composio_client.create(
        user_id=COMPOSIO_USER_ID,
        toolkits=["discordbot"],
    )

    mcp_url = session.mcp.url
    print(f"Composio MCP URL: {mcp_url}")

    mcp_client = BasicMCPClient(mcp_url, headers={"x-api-key": COMPOSIO_API_KEY})
    mcp_tool_spec = McpToolSpec(client=mcp_client)
    tools = await mcp_tool_spec.to_tool_list_async()

    llm = OpenAI(model="gpt-5")

    description = "An agent that uses Composio Tool Router MCP tools to perform Discordbot actions."
    system_prompt = """
    You are a helpful assistant connected to Composio Tool Router.
    Use the available tools to answer user queries and perform Discordbot actions.
    """
    return ReActAgent(tools=tools, llm=llm, description=description, system_prompt=system_prompt, verbose=True)

What's happening here:

  • We create a Composio client using your API key and configure it with the LlamaIndex provider
  • We then create a tool router MCP session for your user, specifying the toolkits we want to use (in this case, discordbot)
  • The session returns an MCP HTTP endpoint URL that acts as a gateway to all your configured tools
  • LlamaIndex will connect to this endpoint to dynamically discover and use the available Discordbot tools.
  • The MCP tools are mapped to LlamaIndex-compatible tools and plug them into the Agent.

Create an interactive chat loop

async def chat_loop(agent: ReActAgent) -> None:
    ctx = Context(agent)
    print("Type 'quit', 'exit', or Ctrl+C to stop.")

    while True:
        try:
            user_input = input("\nYou: ").strip()
        except (KeyboardInterrupt, EOFError):
            print("\nBye!")
            break

        if not user_input or user_input.lower() in {"quit", "exit"}:
            print("Bye!")
            break

        try:
            print("Agent: ", end="", flush=True)
            handler = agent.run(user_input, ctx=ctx)

            async for event in handler.stream_events():
                # Stream token-by-token from LLM responses
                if hasattr(event, "delta") and event.delta:
                    print(event.delta, end="", flush=True)
                # Show tool calls as they happen
                elif hasattr(event, "tool_name"):
                    print(f"\n[Using tool: {event.tool_name}]", flush=True)

            # Get final response
            response = await handler
            print()  # Newline after streaming
        except KeyboardInterrupt:
            print("\n[Interrupted]")
            continue
        except Exception as e:
            print(f"\nError: {e}")

What's happening here:

  • We're creating a direct terminal interface to chat with your Discordbot database
  • The LLM's responses are streamed to the CLI for faster interaction.
  • The agent uses context to maintain conversation history
  • You can type 'quit' or 'exit' to stop the chat loop gracefully
  • Agent responses and any errors are displayed in a clear, readable format

Define the main entry point

async def main() -> None:
    agent = await build_agent()
    await chat_loop(agent)

if __name__ == "__main__":
    # Handle Ctrl+C gracefully
    signal.signal(signal.SIGINT, lambda s, f: (print("\nBye!"), exit(0)))
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\nBye!")

What's happening here:

  • We're orchestrating the entire application flow
  • The agent gets built with proper error handling
  • Then we kick off the interactive chat loop so you can start talking to Discordbot

Run the agent

npx ts-node llamaindex-agent.ts

When prompted, authenticate and authorise your agent with Discordbot, then start asking questions.

Complete Code

Here's the complete code to get you started with Discordbot and LlamaIndex:

import asyncio
import os
import signal
import dotenv

from composio import Composio
from composio_llamaindex import LlamaIndexProvider
from llama_index.core.agent.workflow import ReActAgent
from llama_index.core.workflow import Context
from llama_index.llms.openai import OpenAI
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec

dotenv.load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
COMPOSIO_API_KEY = os.getenv("COMPOSIO_API_KEY")
COMPOSIO_USER_ID = os.getenv("COMPOSIO_USER_ID")

if not OPENAI_API_KEY:
    raise ValueError("OPENAI_API_KEY is not set")
if not COMPOSIO_API_KEY:
    raise ValueError("COMPOSIO_API_KEY is not set")
if not COMPOSIO_USER_ID:
    raise ValueError("COMPOSIO_USER_ID is not set")

async def build_agent() -> ReActAgent:
    composio_client = Composio(
        api_key=COMPOSIO_API_KEY,
        provider=LlamaIndexProvider(),
    )

    session = composio_client.create(
        user_id=COMPOSIO_USER_ID,
        toolkits=["discordbot"],
    )

    mcp_url = session.mcp.url
    print(f"Composio MCP URL: {mcp_url}")

    mcp_client = BasicMCPClient(mcp_url, headers={"x-api-key": COMPOSIO_API_KEY})
    mcp_tool_spec = McpToolSpec(client=mcp_client)
    tools = await mcp_tool_spec.to_tool_list_async()

    llm = OpenAI(model="gpt-5")
    description = "An agent that uses Composio Tool Router MCP tools to perform Discordbot actions."
    system_prompt = """
    You are a helpful assistant connected to Composio Tool Router.
    Use the available tools to answer user queries and perform Discordbot actions.
    """
    return ReActAgent(
        tools=tools,
        llm=llm,
        description=description,
        system_prompt=system_prompt,
        verbose=True,
    );

async def chat_loop(agent: ReActAgent) -> None:
    ctx = Context(agent)
    print("Type 'quit', 'exit', or Ctrl+C to stop.")

    while True:
        try:
            user_input = input("\nYou: ").strip()
        except (KeyboardInterrupt, EOFError):
            print("\nBye!")
            break

        if not user_input or user_input.lower() in {"quit", "exit"}:
            print("Bye!")
            break

        try:
            print("Agent: ", end="", flush=True)
            handler = agent.run(user_input, ctx=ctx)

            async for event in handler.stream_events():
                # Stream token-by-token from LLM responses
                if hasattr(event, "delta") and event.delta:
                    print(event.delta, end="", flush=True)
                # Show tool calls as they happen
                elif hasattr(event, "tool_name"):
                    print(f"\n[Using tool: {event.tool_name}]", flush=True)

            # Get final response
            response = await handler
            print()  # Newline after streaming
        except KeyboardInterrupt:
            print("\n[Interrupted]")
            continue
        except Exception as e:
            print(f"\nError: {e}")

async def main() -> None:
    agent = await build_agent()
    await chat_loop(agent)

if __name__ == "__main__":
    # Handle Ctrl+C gracefully
    signal.signal(signal.SIGINT, lambda s, f: (print("\nBye!"), exit(0)))
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\nBye!")

Conclusion

You've successfully connected Discordbot to LlamaIndex through Composio's Tool Router MCP layer. Key takeaways:
  • Tool Router dynamically exposes Discordbot tools through an MCP endpoint
  • LlamaIndex's ReActAgent handles reasoning and orchestration; Composio handles integrations
  • The agent becomes more capable without increasing prompt size
  • Async Python provides clean, efficient execution of agent workflows
You can easily extend this to other toolkits like Gmail, Notion, Stripe, GitHub, and more by adding them to the toolkits parameter.

How to build Discordbot MCP Agent with another framework

FAQ

What are the differences in Tool Router MCP and Discordbot MCP?

With a standalone Discordbot MCP server, the agents and LLMs can only access a fixed set of Discordbot tools tied to that server. However, with the Composio Tool Router, agents can dynamically load tools from Discordbot and many other apps based on the task at hand, all through a single MCP endpoint.

Can I use Tool Router MCP with LlamaIndex?

Yes, you can. LlamaIndex fully supports MCP integration. You get structured tool calling, message history handling, and model orchestration while Tool Router takes care of discovering and serving the right Discordbot tools.

Can I manage the permissions and scopes for Discordbot while using Tool Router?

Yes, absolutely. You can configure which Discordbot scopes and actions are allowed when connecting your account to Composio. You can also bring your own OAuth credentials or API configuration so you keep full control over what the agent can do.

How safe is my data with Composio Tool Router?

All sensitive data such as tokens, keys, and configuration is fully encrypted at rest and in transit. Composio is SOC 2 Type 2 compliant and follows strict security practices so your Discordbot data and credentials are handled as safely as possible.

Used by agents from

Context
Letta
glean
HubSpot
Agent.ai
Altera
DataStax
Entelligence
Rolai
Context
Letta
glean
HubSpot
Agent.ai
Altera
DataStax
Entelligence
Rolai
Context
Letta
glean
HubSpot
Agent.ai
Altera
DataStax
Entelligence
Rolai

Never worry about agent reliability

We handle tool reliability, observability, and security so you never have to second-guess an agent action.