simple_telegram_bot_mcp.py•1.4 kB
"""
Simple Telegram Bot MCP Server using FastMCP
This is a minimal MCP server that exposes the send_message function as a tool.
"""
import os
import requests
from fastmcp import FastMCP
from dotenv import load_dotenv
load_dotenv()
token = os.getenv('TELEGRAM_BOT_TOKEN')
chat_id = os.getenv('TELEGRAM_CHAT_ID')
from mcp.server.fastmcp import FastMCP, Context
from smithery.decorators import smithery
from pydantic import BaseModel
class ConfigSchema(BaseModel):
telegramBotToken: str
telegramChatId: str
@smithery.server(config_schema=ConfigSchema)
def create_server(config: ConfigSchema):
server = FastMCP("Simple Telegram Bot MCP")
@server.tool()
def send_telegram_message(text: str, ctx: Context) -> str:
"""Send a message to a Telegram chat using the simple send_message function"""
ctx = create_server.get_context()
config = ctx.session_config
chat_id = config.telegramChatId
token = config.telegramBotToken
url = f"https://api.telegram.org/bot{token}/sendMessage"
try:
rj = requests.get(url, params={'chat_id': chat_id, 'text': text}).json()
if rj['ok']:
return 'success'
else:
print(rj)
return f"Error sending message: {rj['description']}"
except Exception as e:
return f"Error sending message: {str(e)}"