discord_api
Execute raw Discord API commands using REST methods or slash command syntax to manage bots, configure servers, and handle message operations directly.
Instructions
Execute raw Discord API commands. Supports both REST API calls and application commands.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| method | Yes | HTTP method (GET, POST, PUT, PATCH, DELETE) | |
| endpoint | Yes | Discord API endpoint (e.g., 'guilds/{guild.id}/roles' or command like '/role create') | |
| payload | No | Optional request payload/body |
Implementation Reference
- src/discord_raw_mcp/server.py:43-78 (handler)Core handler function that performs the raw HTTP request to Discord API using aiohttp, handles responses and errors.async def execute_discord_api(method: str, endpoint: str, payload: dict = None) -> dict: """Execute a raw Discord API request.""" if not discord_client: raise RuntimeError("Discord client not ready") async with aiohttp.ClientSession() as session: headers = { "Authorization": f"Bot {DISCORD_TOKEN}", "Content-Type": "application/json", } url = f"{DISCORD_API_BASE}/{endpoint.lstrip('/')}" async with session.request( method=method.upper(), url=url, headers=headers, json=payload if payload else None ) as response: if response.status == 204: # No content return {"success": True} response_data = await response.json() if not response.ok: error_msg = f"Discord API error: {response.status} - {response_data.get('message', 'Unknown error')}" logger.error(error_msg) return { "success": False, "error": error_msg, "status": response.status, "details": response_data } return response_data
- src/discord_raw_mcp/server.py:86-104 (schema)Input schema defining the parameters for the discord_api tool: method (enum), endpoint (string), optional payload (object).inputSchema={ "type": "object", "properties": { "method": { "type": "string", "description": "HTTP method (GET, POST, PUT, PATCH, DELETE)", "enum": ["GET", "POST", "PUT", "PATCH", "DELETE"] }, "endpoint": { "type": "string", "description": "Discord API endpoint (e.g., 'guilds/{guild.id}/roles' or command like '/role create')" }, "payload": { "type": "object", "description": "Optional request payload/body" } }, "required": ["method", "endpoint"] }
- src/discord_raw_mcp/server.py:83-106 (registration)Registration of the 'discord_api' tool via @app.list_tools(), including name, description, and input schema.Tool( name="discord_api", description="Execute raw Discord API commands. Supports both REST API calls and application commands.", inputSchema={ "type": "object", "properties": { "method": { "type": "string", "description": "HTTP method (GET, POST, PUT, PATCH, DELETE)", "enum": ["GET", "POST", "PUT", "PATCH", "DELETE"] }, "endpoint": { "type": "string", "description": "Discord API endpoint (e.g., 'guilds/{guild.id}/roles' or command like '/role create')" }, "payload": { "type": "object", "description": "Optional request payload/body" } }, "required": ["method", "endpoint"] } ) ]
- src/discord_raw_mcp/server.py:34-78 (helper)Constants for Discord API version and base URL used in the tool implementation.DISCORD_API_VERSION = "10" DISCORD_API_BASE = f"https://discord.com/api/v{DISCORD_API_VERSION}" @bot.event async def on_ready(): global discord_client discord_client = bot logger.info(f"Logged in as {bot.user.name}") async def execute_discord_api(method: str, endpoint: str, payload: dict = None) -> dict: """Execute a raw Discord API request.""" if not discord_client: raise RuntimeError("Discord client not ready") async with aiohttp.ClientSession() as session: headers = { "Authorization": f"Bot {DISCORD_TOKEN}", "Content-Type": "application/json", } url = f"{DISCORD_API_BASE}/{endpoint.lstrip('/')}" async with session.request( method=method.upper(), url=url, headers=headers, json=payload if payload else None ) as response: if response.status == 204: # No content return {"success": True} response_data = await response.json() if not response.ok: error_msg = f"Discord API error: {response.status} - {response_data.get('message', 'Unknown error')}" logger.error(error_msg) return { "success": False, "error": error_msg, "status": response.status, "details": response_data } return response_data
- Helper logic in call_tool for handling slash command syntax like '/role create', parsing args and converting to API calls.# Handle slash command syntax if endpoint.startswith('/'): # Convert slash command to API call command_parts = endpoint[1:].split() # Remove leading / and split if len(command_parts) < 2: return [TextContent( type="text", text=f"Invalid slash command format. Must include command and subcommand." )] command, subcommand, *args = command_parts # Example: Convert /role create to appropriate API call if command == "role": if subcommand == "create": # Parse arguments from the command string arg_dict = {} for arg in args: if ":" in arg: key, value = arg.split(":", 1) arg_dict[key] = value # Modify method and endpoint for role creation method = "POST" guild_id = arg_dict.get("guild_id", payload.get("guild_id") if payload else None) if not guild_id: return [TextContent( type="text", text="guild_id is required for role creation" )] endpoint = f"guilds/{guild_id}/roles" payload = { "name": arg_dict.get("name", "New Role"), "permissions": arg_dict.get("permissions", "0"), "color": int(arg_dict.get("color", "0"), 16) if "color" in arg_dict else 0, "hoist": arg_dict.get("hoist", "false").lower() == "true", "mentionable": arg_dict.get("mentionable", "false").lower() == "true" } try: