Host a Discord bot 24/7 from your AI agent, over MCP
Does your bot work on your computer? Connected to while over MCP, your agent keeps it running 24/7, and the bot restarts on its own if it stops.
By Aloïs Oddon, co-founder of Nextmap. Facts checked on November 5, 2026.
Prerequisites
- A Discord bot, in Node.js or Python.
- An AI agent that works in a terminal: Claude Code, Codex, Cursor or Gemini CLI.
Step 1: Give your agent these instructions
Open your agent in your bot’s folder, and paste these instructions into it:
Connect to while with my account (while.sh/agents), then deploy my app.
Your agent adds while’s MCP server itself. If it asks you to, restart it. On the first call to while, a page opens in your browser. In Claude Code, if it doesn’t open, type /mcp, choose while, then Authenticate. On that page, choose “A new app”, then click Authorize.
Step 2: Check that your bot is connected
Your agent reads the bot’s logs in while, and tells you when it’s connected to Discord.
What your agent does
It writes while.yaml next to your bot:
apiVersion: while/v1
project: mon-bot
services:
- name: bot
type: worker
command: [node, bot.js]
network:
egress: all
Your bot runs continuously, with no public address. The last two lines open the internet to it, so it can reach Discord.
Keep the bot’s token secret
You, to your agent: Connect the bot with its Discord token.
You enter the token in while: your bot receives it, and your agent never sees it.
Add a command to the bot
You, to your agent: Add the /planning command to the bot.
Your agent changes the code and deploys the bot again in a few seconds, without ever running it twice.
Post at a set time
You, to your agent: Post the schedule every Monday at 9 am.
Your agent declares a scheduled job, with the same code and the same token.
Add a database
You, to your agent: Add a PostgreSQL database.
Your agent declares the database in while.yaml, and its address reaches your code in DATABASE_URL. It’s backed up every day, and can go back to a precise moment if needed.
Go back to the previous version
You, to your agent: Go back to the previous version of the bot.
The previous version of the bot takes over in a few seconds, without ever running twice. Then your agent reads what broke in while’s logs, fixes it and deploys again.
In the docs: PostgreSQL databases, Scheduled jobs, Worker, Versions and rollback and Variables and secrets.
Next steps
Sources
- Discord, messages (posting a message to a channel through the HTTP API), accessed November 2, 2026
- Discord, getting started (the bot token), accessed November 2, 2026