-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_postgresql.py
More file actions
102 lines (87 loc) · 2.57 KB
/
Copy pathtest_postgresql.py
File metadata and controls
102 lines (87 loc) · 2.57 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
import json
import subprocess
import sys
import time
# Database credentials (replace with your test DB)
PG_DB_CONFIG = {
"db_type": "postgresql",
"host": "localhost",
"port": 5432,
"user": "testuser",
"password": "testpassword",
"database": "testdb",
"connection_name": "pg_test_conn"
}
_process = None
def start_server():
global _process
_process = subprocess.Popen(
[sys.executable, "mcp_server.py"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=0
)
# Wait for server to be ready
time.sleep(1)
return _process
def send_request(request):
request_json = json.dumps(request) + "\n"
_process.stdin.write(request_json)
_process.stdin.flush()
response_line = _process.stdout.readline()
if response_line.strip():
return json.loads(response_line.strip())
return None
def send_notification(notification):
notification_json = json.dumps(notification) + "\n"
_process.stdin.write(notification_json)
_process.stdin.flush()
def cleanup():
if _process:
_process.terminate()
_process.wait(timeout=5)
def test_postgresql_connection():
print("🚀 Testing PostgreSQL Connection...")
# 1. Initialize
send_request({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}})
send_notification({"jsonrpc": "2.0", "method": "notifications/initialized"})
# 2. Connect to PostgreSQL
connect_req = {
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "connect_database",
"arguments": PG_DB_CONFIG
}
}
response = send_request(connect_req)
content = json.loads(response['result']['content'][0]['text'])
if not content.get("success"):
print(f"❌ Connection failed: {content.get('error')}")
return False
print("✅ Connection successful!")
# 3. List tables
list_tables_req = {
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "list_tables",
"arguments": {"connection_name": PG_DB_CONFIG["connection_name"]}
}
}
response = send_request(list_tables_req)
content = json.loads(response['result']['content'][0]['text'])
print(f"✅ List tables result: {content}")
return True
if __name__ == "__main__":
start_server()
try:
if not test_postgresql_connection():
sys.exit(1)
finally:
cleanup()
print("✅ PostgreSQL tests finished.")