-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest_complete_server.py
More file actions
130 lines (100 loc) · 4.11 KB
/
test_complete_server.py
File metadata and controls
130 lines (100 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
"""Test script for complete MCP server with all capabilities."""
from mcp2py import load, DefaultSamplingHandler
import os
def test_basic_features():
"""Test basic MCP features."""
print("=" * 60)
print("Testing Complete MCP Server")
print("=" * 60)
# Load server
print("\n1️⃣ Loading test server...")
server = load("python tests/test_server.py")
print(f"\n📊 Server Capabilities:")
print(f" Tools: {list(server._tools.keys())}")
print(f" Resources: {list(server._resources.keys())}")
print(f" Prompts: {list(server._prompts.keys())}")
# Test basic tools
print(f"\n2️⃣ Testing Basic Tools:")
result = server.echo(message="Hello from mcp2py!")
print(f" ✓ echo: {result}")
result = server.add(a=15, b=27)
print(f" ✓ add: {result}")
# Test resources
print(f"\n3️⃣ Testing Resources:")
docs = server.API_Documentation
print(f" ✓ API Documentation: {len(docs)} chars")
print(f" Preview: {docs[:100]}...")
import json
version = json.loads(server.Version_Info)
print(f" ✓ Version Info: v{version['version']}")
print(f" Protocol: {version['protocol_version']}")
stats = json.loads(server.Server_Statistics)
print(f" ✓ Server Statistics: {stats}")
# Test prompts
print(f"\n4️⃣ Testing Prompts:")
messages = server.review_code(
code="def factorial(n):\n return n * factorial(n-1)",
focus="bugs"
)
print(f" ✓ review_code: Generated {len(messages)} messages")
print(f" Role: {messages[0]['role']}")
print(f" Preview: {str(messages[0]['content'])[:80]}...")
messages = server.generate_readme(
project_name="awesome-lib",
description="An awesome library",
features="Fast, Simple, Powerful"
)
print(f" ✓ generate_readme: Generated {len(messages)} messages")
messages = server.explain_mcp()
print(f" ✓ explain_mcp: Generated {len(messages)} messages")
print(f"\n5️⃣ Sampling & Elicitation Tools (not tested - require interaction):")
print(f" ✓ analyze_sentiment: Available (triggers sampling)")
print(f" ✓ confirm_action: Available (triggers elicitation)")
server.close()
print(f"\n✅ All basic features working!")
print("=" * 60)
def test_sampling():
"""Test sampling feature (requires API key)."""
print("\n" + "=" * 60)
print("Testing Sampling (Optional - requires API key)")
print("=" * 60)
# Check for API key
if not os.getenv("OPENAI_API_KEY") and not os.getenv("ANTHROPIC_API_KEY"):
print("\n⚠️ No API keys found. Skipping sampling test.")
print(" Set OPENAI_API_KEY or ANTHROPIC_API_KEY to test sampling.")
return
print("\n🤖 Testing LLM Sampling:")
server = load("python tests/test_server.py")
try:
result = server.analyze_sentiment(text="I absolutely love Python programming!")
print(f" ✓ Sentiment analysis: {result}")
except Exception as e:
print(f" ✗ Sampling failed: {type(e).__name__}: {e}")
server.close()
def test_elicitation():
"""Test elicitation feature (interactive)."""
print("\n" + "=" * 60)
print("Testing Elicitation (Interactive)")
print("=" * 60)
print("\n💬 Testing User Input Prompts:")
server = load("python tests/test_server.py")
try:
print("\nThis will prompt you for confirmation...")
result = server.confirm_action(action="delete all files")
print(f" ✓ User response: {result}")
except KeyboardInterrupt:
print("\n ✗ Test cancelled by user")
except Exception as e:
print(f" ✗ Elicitation failed: {type(e).__name__}: {e}")
server.close()
if __name__ == "__main__":
# Always run basic tests
test_basic_features()
# Optional: test sampling if requested
if "--sampling" in __import__("sys").argv:
test_sampling()
# Optional: test elicitation if requested
if "--elicitation" in __import__("sys").argv:
test_elicitation()
print("\n💡 Tip: Run with --sampling or --elicitation to test those features")