| 1 | import { |
| 2 | ContextItem, |
| 3 | ContextProviderDescription, |
| 4 | ContextProviderExtras, |
| 5 | ContextSubmenuItem, |
| 6 | FetchFunction, |
| 7 | LoadSubmenuItemsArgs, |
| 8 | } from "../../index.js"; |
| 9 | import { BaseContextProvider } from "../index.js"; |
| 10 | |
| 11 | //Define helper interfaces for Discord responses |
| 12 | export interface DiscordChannel { |
| 13 | id: string; |
| 14 | name?: string; |
| 15 | icon?: string; |
| 16 | topic?: string; |
| 17 | type?: number; |
| 18 | guild_id?: string; |
| 19 | } |
| 20 | |
| 21 | interface DiscordMessage { |
| 22 | id: string; |
| 23 | content: string; |
| 24 | author: { |
| 25 | id: string; |
| 26 | username: string; |
| 27 | }; |
| 28 | timestamp: string; |
| 29 | } |
| 30 | |
| 31 | class DiscordContextProvider extends BaseContextProvider { |
| 32 | static description: ContextProviderDescription = { |
| 33 | title: "discord", |
| 34 | displayTitle: "Discord", |
| 35 | description: "Select a channel", |
| 36 | type: "submenu", |
| 37 | }; |
| 38 | |
| 39 | private baseUrl = "https://discord.com/api/v10"; |
| 40 | |
| 41 | // Helper function to get the full fetch URL |
| 42 | private getUrl(path: string): string { |
| 43 | return `${this.baseUrl}${path}`; |
| 44 | } |
| 45 | |
| 46 | get deprecationMessage() { |
| 47 | return "The Discord context provider is now deprecated and may be removed in a later version. Please consider using the Discord MCP (hub.docker.com/r/mcp/mcp-discord) instead."; |
| 48 | } |
| 49 | |
| 50 | async fetchMessages( |
| 51 | channelId: string, |
| 52 | fetch: FetchFunction, |
| 53 | ): Promise<Array<DiscordMessage>> { |
| 54 | const url = this.getUrl(`/channels/${channelId}/messages`); |
| 55 | // Fetch messages from the specified channel using the provided fetch function |
| 56 | const response = await fetch(url, { |
| 57 | headers: { |
| 58 | Authorization: `Bot ${this.options.discordKey}`, |
| 59 | "Content-Type": "application/json", |
| 60 | }, |
| 61 | }); |
| 62 | |
| 63 | if (!response.ok) { |
| 64 | throw new Error(`Failed to fetch messages: ${response.statusText}`); |
| 65 | } |
| 66 | |
| 67 | return response.json(); |
| 68 | } |
| 69 | |
| 70 | async fetchChannels(fetch: FetchFunction): Promise<Array<DiscordChannel>> { |
| 71 | const url = this.getUrl(`/guilds/${this.options.guildId}/channels`); |
| 72 | const response = await fetch(url, { |
| 73 | headers: { |
| 74 | Authorization: `Bot ${this.options.discordKey}`, |
| 75 | "Content-Type": "application/json", |
| 76 | }, |
| 77 | }); |
| 78 | |
| 79 | if (!response.ok) { |
| 80 | throw new Error(`Failed to fetch channels: ${response.statusText}`); |
| 81 | } |
| 82 | |
| 83 | const channels = await response.json(); |
| 84 | // Filter channels to only include text channels (type 0) |
| 85 | return channels.filter((channel: DiscordChannel) => channel.type === 0); |
| 86 | } |
| 87 | |
| 88 | async getContextItems( |
| 89 | query: string, |
| 90 | extras: ContextProviderExtras, |
| 91 | ): Promise<ContextItem[]> { |
| 92 | const channels = await this.fetchChannels(extras.fetch); |
| 93 | |
| 94 | let channelId: string; |
| 95 | // Find the channel by ID or name in the query string. If not found, use the first channel |
| 96 | const selectedChannel = channels.find( |
| 97 | (channel) => channel.id === query || channel.name === query, |
| 98 | ); |
| 99 | |
| 100 | channelId = selectedChannel ? selectedChannel.id : channels[0].id; |
| 101 | |
| 102 | const parts = ["# Discord Channel Messages", `Channel ID: ${channelId}`]; |
| 103 | const messages = await this.fetchMessages(channelId, extras.fetch); |
| 104 | |
| 105 | if (messages.length > 0) { |
| 106 | parts.push("## Messages"); |
| 107 | // Format each message into a markdown string |
| 108 | messages.forEach((message) => { |
| 109 | parts.push( |
| 110 | `### ${message.author?.username ?? "Unknown User"} on ${ |
| 111 | message.timestamp |
| 112 | }\n\n${message.content}`, |
| 113 | ); |
| 114 | }); |
| 115 | } else { |
| 116 | parts.push("No messages found."); |
| 117 | } |
| 118 | |
| 119 | const content = parts.join("\n\n"); |
| 120 | |
| 121 | return [ |
| 122 | { |
| 123 | name: `Messages from Channel ${channelId}`, |
| 124 | content, |
| 125 | description: "Latest messages from the channel", |
| 126 | }, |
| 127 | ]; |
| 128 | } |
| 129 | |
| 130 | async loadSubmenuItems( |
| 131 | args: LoadSubmenuItemsArgs, |
| 132 | ): Promise<ContextSubmenuItem[]> { |
| 133 | const channels = await this.fetchChannels(args.fetch); |
| 134 | |
| 135 | return channels.map((channel) => ({ |
| 136 | id: channel.id, |
| 137 | title: channel.name || `Channel ${channel.id}`, |
| 138 | description: channel.topic ?? "", |
| 139 | })); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | export default DiscordContextProvider; |