-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdebug_mcp_connection.py
79 lines (68 loc) · 2.28 KB
/
debug_mcp_connection.py
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
import asyncio
import sys
import json
import subprocess
import textwrap
async def test_server(server_path):
print(f"Testing MCP server at: {server_path}")
# Start the server process
process = subprocess.Popen(
[sys.executable, "-u", server_path], # -u for unbuffered output
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1, # Line buffered
env={
"PYTHONIOENCODING": "utf-8",
"PYTHONUNBUFFERED": "1"
}
)
# Send an initialize message
init_message = {
"jsonrpc": "2.0",
"id": 0,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {
"name": "test-client",
"version": "1.0.0"
}
}
}
# Send the message to the server
init_json = json.dumps(init_message) + "\n"
print(f"Sending: {init_json.strip()}")
process.stdin.write(init_json)
process.stdin.flush()
# Read the response
response_line = process.stdout.readline()
print(f"Raw response: {repr(response_line)}")
# Check for invalid characters
if response_line.strip():
try:
parsed = json.loads(response_line)
print("Successfully parsed JSON response:")
print(json.dumps(parsed, indent=2))
except json.JSONDecodeError as e:
print(f"JSON parse error: {e}")
print("First 10 characters:", repr(response_line[:10]))
# Examine the response in more detail
for i, char in enumerate(response_line[:20]):
print(f"Character {i}: {repr(char)} (ASCII: {ord(char)})")
# Wait briefly and terminate the process
await asyncio.sleep(1)
process.terminate()
process.wait()
# Show stderr for debugging
stderr_output = process.stderr.read()
if stderr_output:
print("\nServer stderr output:")
print(textwrap.indent(stderr_output, " "))
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python debug_mcp_connection.py path/to/server.py")
sys.exit(1)
asyncio.run(test_server(sys.argv[1]))