test_server.pyโข3.79 kB
#!/usr/bin/env python3
"""
Test script for the authenticated Discord MCP Server
This script demonstrates how to generate tokens and make authenticated requests
"""
import requests
import json
from app import generate_access_token
# Configuration
SERVER_URL = "http://localhost:8000"
YOUR_DISCORD_TOKEN = "YOUR_DISCORD_BOT_TOKEN_HERE" # Replace with your actual Discord bot token
TEST_CHANNEL_ID = "1234567890123456789" # Replace with a test channel ID
def test_server():
"""Test the authenticated Discord MCP server"""
print("๐งช Testing Authenticated Discord MCP Server")
print("=" * 50)
# Step 1: Generate access token
print("๐ Step 1: Generating access token...")
try:
access_token = generate_access_token(YOUR_DISCORD_TOKEN, "test-user")
print(f"โ
Token generated successfully!")
print(f"๐ Token: {access_token[:50]}...")
except Exception as e:
print(f"โ Failed to generate token: {e}")
return
# Step 2: Test server health
print("\n๐ Step 2: Testing server health...")
try:
response = requests.get(f"{SERVER_URL}/health")
if response.status_code == 200:
print("โ
Server is running!")
else:
print(f"โ ๏ธ Server response: {response.status_code}")
except Exception as e:
print(f"โ Server not reachable: {e}")
print("๐ก Make sure to start the server first with: python app.py")
return
# Step 3: Test authenticated tool call
print("\n๐ Step 3: Testing authenticated tool call...")
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
# Example: Get channel info
payload = {
"method": "get_channel_info",
"params": {
"channel_id": TEST_CHANNEL_ID
}
}
try:
response = requests.post(f"{SERVER_URL}/mcp/v1/tools/invoke",
headers=headers,
json=payload)
if response.status_code == 200:
result = response.json()
print("โ
Tool call successful!")
print(f"๐ Response: {json.dumps(result, indent=2)}")
else:
print(f"โ Tool call failed: {response.status_code}")
print(f"๐ Response: {response.text}")
except Exception as e:
print(f"โ Tool call error: {e}")
print("\n๐ Test completed!")
def show_usage():
"""Show how to use the server"""
print("\n๐ How to use this authenticated MCP server:")
print("=" * 50)
print("1. Start the server:")
print(" python app.py")
print("\n2. Generate a token (replace with your Discord bot token):")
print(f" python -c \"from app import generate_access_token; print(generate_access_token('YOUR_DISCORD_TOKEN'))\"")
print("\n3. Make authenticated requests:")
print(" - Include the token in the Authorization header: 'Bearer <token>'")
print(" - Call tools via POST to /mcp/v1/tools/invoke")
print(" - Tools available: send_message, get_messages, get_channel_info, search_messages, moderate_content")
print("\n๐ง Example curl command:")
print("""
curl -X POST http://localhost:8000/mcp/v1/tools/invoke \\
-H "Authorization: Bearer YOUR_JWT_TOKEN" \\
-H "Content-Type: application/json" \\
-d '{
"method": "get_channel_info",
"params": {
"channel_id": "1234567890123456789"
}
}'
""")
if __name__ == "__main__":
if YOUR_DISCORD_TOKEN == "YOUR_DISCORD_BOT_TOKEN_HERE":
print("โ ๏ธ Please update YOUR_DISCORD_TOKEN in this script first!")
show_usage()
else:
test_server()
show_usage()