| 1 | using System.Text.Json; |
| 2 | using ModelContextProtocol.Protocol; |
| 3 | using Tool = ModelContextProtocol.Protocol.Tool; |
| 4 | |
| 5 | namespace TelegramRelay.Mcp; |
| 6 | |
| 7 | internal static class ToolCatalog |
| 8 | { |
| 9 | private static JsonElement Schema(object schema) => JsonSerializer.SerializeToElement(schema); |
| 10 | |
| 11 | internal static List<Tool> Build() => |
| 12 | [ |
| 13 | new Tool |
| 14 | { |
| 15 | Name = "telegram_get_messages", |
| 16 | Description = "Get messages from a Telegram chat. Returns JSON array (Id, Date, FromUserId, Text, ReplyToMsgId, TopicId). For forum groups use topic_id to filter by topic.", |
| 17 | InputSchema = Schema(new |
| 18 | { |
| 19 | type = "object", |
| 20 | properties = new |
| 21 | { |
| 22 | chat_id = new { type = "integer", description = "Chat ID (e.g. -1001234567890 for supergroup)." }, |
| 23 | topic_id = new { type = "integer", description = "Optional: topic (thread) ID to filter messages in a forum group." } |
| 24 | }, |
| 25 | required = new[] { "chat_id" } |
| 26 | }) |
| 27 | }, |
| 28 | new Tool |
| 29 | { |
| 30 | Name = "telegram_send_message", |
| 31 | Description = "Send a message to a Telegram chat. Optional topic_id for forum groups.", |
| 32 | InputSchema = Schema(new |
| 33 | { |
| 34 | type = "object", |
| 35 | properties = new |
| 36 | { |
| 37 | chat_id = new { type = "integer", description = "Chat ID." }, |
| 38 | message = new { type = "string", description = "Message text to send." }, |
| 39 | topic_id = new { type = "integer", description = "Optional: topic ID for forum groups." } |
| 40 | }, |
| 41 | required = new[] { "chat_id", "message" } |
| 42 | }) |
| 43 | }, |
| 44 | new Tool |
| 45 | { |
| 46 | Name = "telegram_create_topic", |
| 47 | Description = "Create a forum topic in a supergroup (forum must be enabled; requires manage_topics). Returns the new topic ID.", |
| 48 | InputSchema = Schema(new |
| 49 | { |
| 50 | type = "object", |
| 51 | properties = new |
| 52 | { |
| 53 | chat_id = new { type = "integer", description = "Supergroup chat ID (e.g. -1001234567890)." }, |
| 54 | title = new { type = "string", description = "Topic title." } |
| 55 | }, |
| 56 | required = new[] { "chat_id", "title" } |
| 57 | }) |
| 58 | }, |
| 59 | new Tool |
| 60 | { |
| 61 | Name = "telegram_invite_user", |
| 62 | Description = "Invite a user by username to a chat (group/supergroup).", |
| 63 | InputSchema = Schema(new |
| 64 | { |
| 65 | type = "object", |
| 66 | properties = new |
| 67 | { |
| 68 | chat_id = new { type = "integer", description = "Chat ID." }, |
| 69 | username = new { type = "string", description = "Telegram username (without @)." } |
| 70 | }, |
| 71 | required = new[] { "chat_id", "username" } |
| 72 | }) |
| 73 | }, |
| 74 | new Tool |
| 75 | { |
| 76 | Name = "telegram_remove_user", |
| 77 | Description = "Remove a user by username from a chat (group/supergroup).", |
| 78 | InputSchema = Schema(new |
| 79 | { |
| 80 | type = "object", |
| 81 | properties = new |
| 82 | { |
| 83 | chat_id = new { type = "integer", description = "Chat ID." }, |
| 84 | username = new { type = "string", description = "Telegram username (without @)." } |
| 85 | }, |
| 86 | required = new[] { "chat_id", "username" } |
| 87 | }) |
| 88 | }, |
| 89 | new Tool |
| 90 | { |
| 91 | Name = "telegram_send_message_to_user", |
| 92 | Description = "Send a direct message to a user by their Telegram username.", |
| 93 | InputSchema = Schema(new |
| 94 | { |
| 95 | type = "object", |
| 96 | properties = new |
| 97 | { |
| 98 | username = new { type = "string", description = "Telegram username (without @)." }, |
| 99 | message = new { type = "string", description = "Message text." } |
| 100 | }, |
| 101 | required = new[] { "username", "message" } |
| 102 | }) |
| 103 | }, |
| 104 | new Tool |
| 105 | { |
| 106 | Name = "telegram_list_chats", |
| 107 | Description = "List all chats (groups, supergroups) the account is in. Returns JSON array of { chat_id, title }. Use to find chat_id by group name (e.g. AI Guiders).", |
| 108 | InputSchema = Schema(new |
| 109 | { |
| 110 | type = "object", |
| 111 | properties = new { }, |
| 112 | required = Array.Empty<string>() |
| 113 | }) |
| 114 | }, |
| 115 | new Tool |
| 116 | { |
| 117 | Name = "telegram_edit_message", |
| 118 | Description = "Edit an existing message in a Telegram chat. Only messages sent by this account can be edited. message_id is the Id from get_messages.", |
| 119 | InputSchema = Schema(new |
| 120 | { |
| 121 | type = "object", |
| 122 | properties = new |
| 123 | { |
| 124 | chat_id = new { type = "integer", description = "Chat ID." }, |
| 125 | message_id = new { type = "integer", description = "ID of the message to edit (from telegram_get_messages)." }, |
| 126 | message = new { type = "string", description = "New message text." } |
| 127 | }, |
| 128 | required = new[] { "chat_id", "message_id", "message" } |
| 129 | }) |
| 130 | }, |
| 131 | new Tool |
| 132 | { |
| 133 | Name = "telegram_delete_message", |
| 134 | Description = "Delete a message in a Telegram chat. Only messages sent by this account (or with admin rights in channels). message_id from get_messages (Id field).", |
| 135 | InputSchema = Schema(new |
| 136 | { |
| 137 | type = "object", |
| 138 | properties = new |
| 139 | { |
| 140 | chat_id = new { type = "integer", description = "Chat ID." }, |
| 141 | message_id = new { type = "integer", description = "ID of the message to delete (from telegram_get_messages)." } |
| 142 | }, |
| 143 | required = new[] { "chat_id", "message_id" } |
| 144 | }) |
| 145 | }, |
| 146 | new Tool |
| 147 | { |
| 148 | Name = "telegram_update_old_messages", |
| 149 | Description = "Re-format old messages in a chat: fetches the last page of history, edits each message sent by this account with the same text so it gets MD→HTML formatting (bold, code, etc.). Optional topic_id for forum groups.", |
| 150 | InputSchema = Schema(new |
| 151 | { |
| 152 | type = "object", |
| 153 | properties = new |
| 154 | { |
| 155 | chat_id = new { type = "integer", description = "Chat ID." }, |
| 156 | topic_id = new { type = "integer", description = "Optional: topic ID to limit to one thread in a forum group." } |
| 157 | }, |
| 158 | required = new[] { "chat_id" } |
| 159 | }) |
| 160 | } |
| 161 | ]; |
| 162 | } |
| 163 | |