-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_http_tools.py
More file actions
179 lines (149 loc) · 5.95 KB
/
test_http_tools.py
File metadata and controls
179 lines (149 loc) · 5.95 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
"""
使用 HTTP SSE 流式测试 MCP 服务器的工具列表
"""
import json
import requests
def parse_sse_response(text):
"""解析 SSE 格式的响应"""
# SSE 格式: event: message\ndata: {...}\n\n
lines = text.strip().split("\n")
for line in lines:
if line.startswith("data: "):
data = line[6:] # 去掉 "data: " 前缀
try:
return json.loads(data)
except (json.JSONDecodeError, ValueError):
pass
return None
def test_mcp_http():
"""通过 HTTP SSE 流式请求测试 MCP 服务器"""
base_url = "http://127.0.0.1:8080/mcp"
print("\n" + "=" * 60)
print(f"Testing MCP Server (HTTP SSE): {base_url}")
print("=" * 60)
# 第1步: 初始化会话
print("\n📡 Step 1: Initialize session...")
init_payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {"name": "test-client", "version": "1.0.0"},
},
}
try:
response = requests.post(
base_url,
json=init_payload,
headers={
"Content-Type": "application/json",
"Accept": "application/json, text/event-stream",
},
)
print(f" Status: {response.status_code}")
print(f" Content-Type: {response.headers.get('Content-Type')}")
if response.status_code == 200:
# 解析 SSE 响应
result = parse_sse_response(response.text)
if result and "result" in result:
server_info = result["result"].get("serverInfo", {})
print(f" ✅ Server: {server_info.get('name')}")
print(f" Version: {server_info.get('version')}")
else:
print(f" ❌ Error: {response.text}")
return
# 获取 session ID(如果有)
session_id = None
# 检查多种可能的 header 名称
session_headers = [
"x-mcp-session-id",
"mcp-session-id",
"x-session-id",
"session-id",
"x-mcp-session",
]
for header in session_headers:
if header in response.headers:
session_id = response.headers[header]
print(f" Session ID ({header}): {session_id}")
break
if not session_id:
print(" ⚠️ No session ID in response headers")
print(f" Available headers: {list(response.headers.keys())}")
# 第2步: 请求工具列表
print("\n📋 Step 2: List tools...")
tools_payload = {
"jsonrpc": "2.0",
"id": 2,
"method": "tools/list",
"params": {},
}
headers = {
"Content-Type": "application/json",
"Accept": "application/json, text/event-stream",
}
if session_id:
# 尝试所有可能的 session header 名称
headers["x-mcp-session-id"] = session_id
headers["mcp-session-id"] = session_id
headers["x-session-id"] = session_id
tools_response = requests.post(base_url, json=tools_payload, headers=headers)
print(f" Status: {tools_response.status_code}")
if tools_response.status_code == 200:
# 解析 SSE 响应
tools_result = parse_sse_response(tools_response.text)
if (
tools_result
and "result" in tools_result
and "tools" in tools_result["result"]
):
tools = tools_result["result"]["tools"]
print(f"\n✅ Discovered {len(tools)} tools:\n")
# 列出所有工具
for i, tool in enumerate(tools, 1):
marker = (
"🆕" if tool.get("name") == "get_candles_snapshot" else " "
)
print(f"{marker} {i:2d}. {tool.get('name')}")
# 检查 get_candles_snapshot
print("\n" + "=" * 60)
tool_names = [t.get("name") for t in tools]
if "get_candles_snapshot" in tool_names:
print("✅✅✅ get_candles_snapshot IS AVAILABLE via HTTP!")
# 显示详细信息
snapshot_tool = next(
t for t in tools if t.get("name") == "get_candles_snapshot"
)
print("\n📝 Tool Details:")
print(f" Name: {snapshot_tool.get('name')}")
if "description" in snapshot_tool:
desc = snapshot_tool["description"]
# 只显示前150个字符
print(f" Description: {desc[:150]}...")
if "inputSchema" in snapshot_tool:
schema = snapshot_tool["inputSchema"]
if "required" in schema:
print(f" Required: {schema['required']}")
if "properties" in schema:
print(f" Parameters: {list(schema['properties'].keys())}")
else:
print("❌ get_candles_snapshot NOT FOUND in HTTP response")
print(f"\nFirst 5 tools: {', '.join(tool_names[:5])}")
print("=" * 60 + "\n")
else:
print("\n⚠️ Unexpected response format:")
print(json.dumps(tools_result, indent=2)[:500])
else:
print(f" ❌ Error: {tools_response.text}")
except Exception as e:
print(f"\n❌ Error: {e}")
print(f" Type: {type(e).__name__}")
import traceback
traceback.print_exc()
print("\n⚠️ Make sure server is running:")
print(" uv run start")
if __name__ == "__main__":
test_mcp_http()