import TelegramBot from 'node-telegram-bot-api';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import dotenv from 'dotenv';
import { setupWalletHandlers, setupBalanceHandlers, setupNetworkHandlers, setupTransactionQueryHandlers } from './handlers.js';
import { setupTransferHandlers, setupDeployHandlers, setupTokenHandlers, setupNFTHandlers } from './transaction-handlers.js';
dotenv.config();
// Configuration
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN!;
const SMITHERY_API_KEY = process.env.SMITHERY_API_KEY;
const SMITHERY_PROFILE = process.env.SMITHERY_PROFILE;
// User session management
interface UserSession {
mcpClient: Client;
transport: StreamableHTTPClientTransport;
availableTools: any[];
}
const userSessions = new Map<number, UserSession>();
// Create Telegram bot
const bot = new TelegramBot(TELEGRAM_BOT_TOKEN, { polling: true });
// Initialize MCP client for a user
async function initializeMCPClient(userId: number): Promise<UserSession> {
// Check if session already exists
if (userSessions.has(userId)) {
return userSessions.get(userId)!;
}
// Construct server URL with authentication
const url = new URL('https://server.smithery.ai/@cuongpo/hyperion-mcp-server/mcp');
url.searchParams.set('api_key', SMITHERY_API_KEY);
url.searchParams.set('profile', SMITHERY_PROFILE);
const transport = new StreamableHTTPClientTransport(url);
// Create MCP client
const mcpClient = new Client({
name: 'Hyperion Telegram Bot',
version: '1.0.0',
});
await mcpClient.connect(transport);
// List available tools
const toolsResponse = await mcpClient.listTools();
const availableTools = toolsResponse.tools;
const session: UserSession = {
mcpClient,
transport,
availableTools,
};
userSessions.set(userId, session);
return session;
}
// Command: /start
bot.onText(/\/start/, async (msg) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
try {
await initializeMCPClient(userId);
const welcomeMessage = `
🚀 *Welcome to Hyperion MCP Bot!*
I'm your blockchain assistant powered by Hyperion MCP Server.
*Available Commands:*
/tools - List all available blockchain tools
/help - Show detailed help
*Wallet:* /wallet create | import | list | set | current | address
*Balance:* /balance [address]
*Network:* /gas | /network | /tx | /block | /estimate
*Deploy:* /deploy erc20 | erc721
*Transfer:* /transfer <to> <amount>
*Tokens:* /token info | mint
*NFTs:* /nft info | mint
Type any command to get started!
`;
bot.sendMessage(chatId, welcomeMessage, { parse_mode: 'Markdown' });
} catch (error) {
bot.sendMessage(chatId, `❌ Error initializing bot: ${error}`);
}
});
// Command: /tools
bot.onText(/\/tools/, async (msg) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
try {
const session = await initializeMCPClient(userId);
const toolsList = session.availableTools
.map((tool, index) => `${index + 1}. *${tool.name}*\n ${tool.description}`)
.join('\n\n');
const message = `🔧 *Available Tools:*\n\n${toolsList}`;
bot.sendMessage(chatId, message, { parse_mode: 'Markdown' });
} catch (error) {
bot.sendMessage(chatId, `❌ Error fetching tools: ${error}`);
}
});
// Command: /help
bot.onText(/\/help/, (msg) => {
const chatId = msg.chat.id;
const helpMessage = `
📖 *Hyperion MCP Bot Help*
*Wallet Commands:*
/wallet create - Create a new wallet
/wallet import <mnemonic|privatekey> - Import existing wallet
/wallet list - List all your wallets
/wallet set <address> - Set current active wallet
/wallet current - Get current wallet info
/wallet address - Get your wallet address
*Balance Commands:*
/balance - Check your wallet balance
/balance <address> - Check balance of any address
*Network Commands:*
/gas - Check current gas prices
/network - Full network information
/tx <hash> - Get transaction details
/block <number|hash> - Get block information
/estimate <to> <amount> - Estimate gas cost
*Transaction Commands:*
/transfer <to> <amount> - Transfer tokens
/deploy erc20 <name> <symbol> - Deploy ERC20 token
/deploy erc721 <name> <symbol> - Deploy ERC721 NFT
*Token Commands:*
/token info <address> - Get ERC20 token information
/token mint <address> <amount> - Mint ERC20 tokens (owner only)
*NFT Commands:*
/nft info <address> [tokenId] - Get NFT information
/nft mint <address> <to> <tokenURI> - Mint NFT (owner only)
*Info Commands:*
/tools - List all available tools
/help - Show this help message
For detailed usage, use the format:
\`/command parameter1 parameter2\`
`;
bot.sendMessage(chatId, helpMessage, { parse_mode: 'Markdown' });
});
// Setup all command handlers
setupWalletHandlers(bot, userSessions);
setupBalanceHandlers(bot, userSessions);
setupTransferHandlers(bot, userSessions);
setupDeployHandlers(bot, userSessions);
setupNetworkHandlers(bot, userSessions);
setupTransactionQueryHandlers(bot, userSessions);
setupTokenHandlers(bot, userSessions);
setupNFTHandlers(bot, userSessions);
// Handle errors
bot.on('polling_error', (error) => {
console.error('Polling error:', error);
});
console.log('🤖 Hyperion Telegram Bot is running...');