---
title: "Host a Discord bot 24/7 over MCP with while.sh"
url: https://while.sh/en/guides/host-discord-bot/
author: "Aloïs Oddon, co-founder of Nextmap"
updated: 2026-11-05
---

# 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.

## 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:

```text titre="Instructions"
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:

```yaml titre="while.yaml"
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.

## Next steps

- [Run a scheduled job](/en/guides/run-cron-job/)
- [Deploy a Docker container](/en/guides/deploy-docker-container/)
- [Compare the hosts that have an MCP server](/en/comparisons/mcp-hosting-claude-code/)

## Sources

1. [Discord, messages (posting a message to a channel through the HTTP API)](https://docs.discord.com/developers/resources/message), 2026-11-02
2. [Discord, getting started (the bot token)](https://docs.discord.com/developers/quick-start/getting-started), 2026-11-02

## Questions

### Does my bot answer twice during an update?

No: a new version replaces the old one without ever running twice.

### Node.js or Python?

Both, or any other language with a Dockerfile.

### How much does a bot cost?

A bot uses little, and you only pay for the CPU and memory it uses.
