|
139 | 139 | def check_server(): |
140 | 140 | """Check if HTTP server is running.""" |
141 | 141 | try: |
142 | | - response = requests.get(f"{BASE_URL}/health", timeout=2) |
| 142 | + # Health endpoint requires Accept: text/event-stream header |
| 143 | + response = requests.get( |
| 144 | + f"{BASE_URL}/health", |
| 145 | + headers={"Accept": "text/event-stream"}, |
| 146 | + timeout=2 |
| 147 | + ) |
143 | 148 | if response.status_code == 200: |
144 | 149 | print(f"✓ Server is running at {BASE_URL}") |
145 | 150 | return True |
| 151 | + else: |
| 152 | + print(f"\n⚠️ Server responded with status {response.status_code}") |
| 153 | + print(f"Response: {response.text}") |
| 154 | + return False |
146 | 155 | except requests.exceptions.RequestException as e: |
147 | 156 | print(f"\n❌ Server not running at {BASE_URL}") |
148 | 157 | print(f"Error: {e}") |
149 | 158 | print("\nStart server with:") |
150 | | - print(f" ./start_http_server.sh") |
| 159 | + print(f" codegraph start http --port {HTTP_PORT}") |
151 | 160 | print(f" OR") |
152 | | - print(f" ./target/release/codegraph start http --port {HTTP_PORT}") |
| 161 | + print(f" ./start_http_server.sh") |
153 | 162 | return False |
154 | 163 |
|
155 | 164 | def send_mcp_request(payload, timeout=60): |
156 | | - """Send MCP request via HTTP POST and wait for response.""" |
| 165 | + """Send MCP request via HTTP POST and wait for SSE response.""" |
157 | 166 | try: |
158 | 167 | start_time = time.time() |
159 | 168 |
|
| 169 | + # HTTP server returns SSE stream - need to accept text/event-stream |
160 | 170 | response = requests.post( |
161 | 171 | f"{BASE_URL}/mcp", |
162 | | - headers={"Content-Type": "application/json"}, |
| 172 | + headers={ |
| 173 | + "Content-Type": "application/json", |
| 174 | + "Accept": "text/event-stream" |
| 175 | + }, |
163 | 176 | json=payload, |
164 | | - timeout=timeout |
| 177 | + timeout=timeout, |
| 178 | + stream=True |
165 | 179 | ) |
166 | 180 |
|
167 | 181 | duration = time.time() - start_time |
168 | 182 |
|
169 | 183 | if response.status_code == 200: |
170 | | - return response.json(), duration |
| 184 | + # Parse SSE stream for JSON-RPC responses |
| 185 | + result_data = None |
| 186 | + for line in response.iter_lines(decode_unicode=True): |
| 187 | + if not line or line.startswith(':'): |
| 188 | + continue |
| 189 | + if line.startswith('data: '): |
| 190 | + data = line[6:] # Remove 'data: ' prefix |
| 191 | + try: |
| 192 | + event = json.loads(data) |
| 193 | + # Look for the final result |
| 194 | + if "result" in event: |
| 195 | + result_data = event |
| 196 | + break |
| 197 | + except json.JSONDecodeError: |
| 198 | + continue |
| 199 | + |
| 200 | + if result_data: |
| 201 | + return result_data, time.time() - start_time |
| 202 | + else: |
| 203 | + print(f"⚠️ No result found in SSE stream") |
| 204 | + return None, time.time() - start_time |
171 | 205 | else: |
172 | 206 | print(f"❌ HTTP {response.status_code}: {response.text[:200]}") |
173 | 207 | return None, duration |
|
0 commit comments