#!/usr/bin/env python3
"""
šÆ LM Studio SSE Fix - Final Solution
Masalah: LM Studio mendapat error "Invalid content type, expected 'text/event-stream'"
Solusi: Endpoint /events dengan format yang kompatibel dengan LM Studio
Status Error Sebelumnya:
- LM studio Log: 2025-11-03 01:17:50
- SSE content-type error meskipun sudah diimplementasi
Fix yang Diterapkan:
1. ā
Endpoint /events ditambahkan ke mcp_server_runner.py
2. ā
Content-type: text/event-stream dengan headers CORS
3. ā
Format event yang kompatibel dengan LM Studio:
- event: connect (connection established)
- event: tools (tools list)
- event: heartbeat (keep-alive)
Cara Test:
1. Start server: python run.py
2. Test endpoint: python test_events_simple.py
3. Konfigurasi LM Studio dengan endpoint: http://localhost:8000/events
Konfigurasi LM Studio:
- URL: http://localhost:8000
- SSE Endpoint: http://localhost:8000/events
- Protocol: HTTP dengan SSE
- Content-Type akan otomatis: text/event-stream
Endpoints yang tersedia untuk LM Studio:
- GET /events - SSE streaming (LM Studio compatible)
- GET /stream - Alternative streaming
- GET /sse/tools - Original SSE tools
- POST /jsonrpc - JSON-RPC 2.0
- GET /health - Health check
"""
import requests
import time
def test_all_sse_endpoints():
"""Test semua SSE endpoints untuk memastikan kompatibilitas"""
endpoints = [
("/events", "LM Studio compatible SSE"),
("/stream", "Alternative streaming"),
("/sse/tools", "Original SSE tools"),
("/sse/status", "SSE status")
]
print("šÆ LM Studio SSE Compatibility Test")
print("=" * 60)
base_url = "http://localhost:8000"
for endpoint, description in endpoints:
print(f"\nš Testing {endpoint} ({description})...")
try:
headers = {
'Accept': 'text/event-stream',
'User-Agent': 'LMStudio/1.0',
'Connection': 'keep-alive'
}
response = requests.get(f"{base_url}{endpoint}", headers=headers, timeout=3, stream=True)
print(f" ā
Status: {response.status_code}")
content_type = response.headers.get('content-type', 'N/A')
print(f" š Content-Type: {content_type}")
if 'text/event-stream' in content_type:
print(f" šÆ COMPATIBLE: Correct SSE content-type!")
# Try to read one chunk
try:
first_chunk = next(response.iter_content(chunk_size=256, decode_unicode=True))
print(f" š Sample: {repr(first_chunk[:100])}")
except:
print(f" š Stream available but couldn't read sample")
else:
print(f" ā INCOMPATIBLE: Wrong content-type")
except requests.exceptions.Timeout:
print(f" ā° Timeout (normal for infinite SSE streams)")
except requests.exceptions.ConnectionError:
print(f" ā Connection refused - server not running?")
except Exception as e:
print(f" ā Error: {e}")
print("\n" + "=" * 60)
print("š FINAL STATUS:")
print("šÆ If /events shows 'COMPATIBLE', LM Studio should work!")
print("š” Configure LM Studio with: http://localhost:8000/events")
print("š Full server info: http://localhost:8000")
if __name__ == "__main__":
print("ā³ Waiting 2 seconds for server to start...")
time.sleep(2)
test_all_sse_endpoints()