|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test MCP prompts via HTTP protocol""" |
| 3 | + |
| 4 | +import requests |
| 5 | +import json |
| 6 | +import sys |
| 7 | +import os |
| 8 | + |
| 9 | +# Server URL - use localhost when running on the server |
| 10 | +SERVER_URL = os.getenv("MCP_SERVER_URL", "http://localhost:8080/mcp") |
| 11 | + |
| 12 | +def test_list_prompts(): |
| 13 | + """Test listing prompts via MCP protocol""" |
| 14 | + print("Testing MCP prompts/list endpoint...\n") |
| 15 | + |
| 16 | + # MCP request to list prompts |
| 17 | + request = { |
| 18 | + "jsonrpc": "2.0", |
| 19 | + "id": 1, |
| 20 | + "method": "prompts/list", |
| 21 | + "params": {} |
| 22 | + } |
| 23 | + |
| 24 | + try: |
| 25 | + response = requests.post(SERVER_URL, json=request, timeout=10) |
| 26 | + response.raise_for_status() |
| 27 | + |
| 28 | + data = response.json() |
| 29 | + |
| 30 | + if "error" in data: |
| 31 | + print(f"❌ Error: {data['error']}") |
| 32 | + return False |
| 33 | + |
| 34 | + if "result" not in data: |
| 35 | + print(f"❌ Unexpected response: {data}") |
| 36 | + return False |
| 37 | + |
| 38 | + prompts = data["result"].get("prompts", []) |
| 39 | + |
| 40 | + print(f"✅ Found {len(prompts)} prompts:\n") |
| 41 | + print("=" * 80) |
| 42 | + |
| 43 | + for prompt in prompts: |
| 44 | + name = prompt.get("name", "unknown") |
| 45 | + description = prompt.get("description", "") |
| 46 | + args = prompt.get("arguments", []) |
| 47 | + |
| 48 | + print(f"\n📝 {name}") |
| 49 | + print(f" Description: {description}") |
| 50 | + if args: |
| 51 | + arg_list = [f"{a['name']}{'*' if a.get('required') else ''}" for a in args] |
| 52 | + print(f" Arguments: {', '.join(arg_list)}") |
| 53 | + |
| 54 | + print("\n" + "=" * 80) |
| 55 | + print(f"\n✅ MCP prompts/list endpoint working correctly!") |
| 56 | + return True |
| 57 | + |
| 58 | + except requests.exceptions.RequestException as e: |
| 59 | + print(f"❌ Request failed: {e}") |
| 60 | + return False |
| 61 | + except json.JSONDecodeError as e: |
| 62 | + print(f"❌ Invalid JSON response: {e}") |
| 63 | + return False |
| 64 | + |
| 65 | +def test_get_prompt(): |
| 66 | + """Test getting a specific prompt""" |
| 67 | + print("\n\nTesting MCP prompts/get endpoint...\n") |
| 68 | + |
| 69 | + # Test getting the health_check prompt |
| 70 | + request = { |
| 71 | + "jsonrpc": "2.0", |
| 72 | + "id": 2, |
| 73 | + "method": "prompts/get", |
| 74 | + "params": { |
| 75 | + "name": "health_check", |
| 76 | + "arguments": {} |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + try: |
| 81 | + response = requests.post(SERVER_URL, json=request, timeout=10) |
| 82 | + response.raise_for_status() |
| 83 | + |
| 84 | + data = response.json() |
| 85 | + |
| 86 | + if "error" in data: |
| 87 | + print(f"❌ Error: {data['error']}") |
| 88 | + return False |
| 89 | + |
| 90 | + if "result" not in data: |
| 91 | + print(f"❌ Unexpected response: {data}") |
| 92 | + return False |
| 93 | + |
| 94 | + messages = data["result"].get("messages", []) |
| 95 | + |
| 96 | + print(f"✅ Retrieved health_check prompt") |
| 97 | + print(f" Messages: {len(messages)}") |
| 98 | + |
| 99 | + if messages: |
| 100 | + print(f"\n📄 Prompt content preview:") |
| 101 | + print("─" * 80) |
| 102 | + content = messages[0].get("content", {}) |
| 103 | + text = content.get("text", "") if isinstance(content, dict) else str(content) |
| 104 | + # Show first 300 characters |
| 105 | + preview = text[:300] + "..." if len(text) > 300 else text |
| 106 | + print(preview) |
| 107 | + print("─" * 80) |
| 108 | + |
| 109 | + print(f"\n✅ MCP prompts/get endpoint working correctly!") |
| 110 | + return True |
| 111 | + |
| 112 | + except requests.exceptions.RequestException as e: |
| 113 | + print(f"❌ Request failed: {e}") |
| 114 | + return False |
| 115 | + except json.JSONDecodeError as e: |
| 116 | + print(f"❌ Invalid JSON response: {e}") |
| 117 | + return False |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + success = test_list_prompts() |
| 121 | + if success: |
| 122 | + success = test_get_prompt() |
| 123 | + |
| 124 | + sys.exit(0 if success else 1) |
0 commit comments