111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Oeffentlicher MCP-Smoke-Test — NIGHTLY.
|
|
|
|
Prueft die oeffentliche Route (DNS, nginx-vhost, TLS/Cert-SAN, Bearer-Auth)
|
|
fuer alle oeffentlichen MCP-Server: https://<name>.mcp.slohmaier.de/mcp.
|
|
|
|
Nutzt ein dediziertes, nicht-ablaufendes Service-Token (Label `smoketest`,
|
|
User mcptest) aus /opt/mcp-servers/service_tokens.json. Dadurch entstehen
|
|
keine /token-Aufrufe und das nginx-Rate-Limit `mcp_auth` (10/min) wird nicht
|
|
belastet. Read-only: initialize + tools/list, kein Datenzugriff.
|
|
|
|
Lauf: /opt/mcp-servers/venv/bin/python -m pytest test_public_smoke.py -q
|
|
"""
|
|
|
|
import json
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
PUBLIC_HOSTS = {
|
|
"notes": "notes.mcp.slohmaier.de",
|
|
"calendar": "calendar.mcp.slohmaier.de",
|
|
"mail": "mail.mcp.slohmaier.de",
|
|
"contacts": "contacts.mcp.slohmaier.de",
|
|
"files": "files.mcp.slohmaier.de",
|
|
"openwearables": "openwearables.mcp.slohmaier.de",
|
|
"owamail": "owamail.mcp.slohmaier.de",
|
|
}
|
|
|
|
SERVICE_TOKENS = "/opt/mcp-servers/service_tokens.json"
|
|
INIT = {
|
|
"jsonrpc": "2.0",
|
|
"id": 1,
|
|
"method": "initialize",
|
|
"params": {
|
|
"protocolVersion": "2025-06-18",
|
|
"capabilities": {},
|
|
"clientInfo": {"name": "public-smoke", "version": "1"},
|
|
},
|
|
}
|
|
|
|
pytestmark = pytest.mark.smoke
|
|
|
|
|
|
def _token():
|
|
with open(SERVICE_TOKENS) as f:
|
|
data = json.load(f)
|
|
for tok, info in data.items():
|
|
if isinstance(info, dict) and info.get("label") == "smoketest":
|
|
return tok
|
|
pytest.skip("kein Service-Token mit Label 'smoketest' in service_tokens.json")
|
|
|
|
|
|
def _headers(token=None, session=None):
|
|
h = {
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json, text/event-stream",
|
|
}
|
|
if token:
|
|
h["Authorization"] = f"Bearer {token}"
|
|
if session:
|
|
h["mcp-session-id"] = session
|
|
return h
|
|
|
|
|
|
def _jsonrpc(resp):
|
|
for line in resp.text.split("\n"):
|
|
if line.startswith("data: "):
|
|
return json.loads(line[6:])
|
|
try:
|
|
return resp.json()
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
@pytest.mark.parametrize("svc,host", list(PUBLIC_HOSTS.items()))
|
|
def test_public_requires_auth(svc, host):
|
|
"""Ohne Bearer muss die oeffentliche Route 401 liefern (Route/TLS erreichbar)."""
|
|
r = httpx.post(f"https://{host}/mcp", json=INIT, headers=_headers(), timeout=30)
|
|
assert r.status_code == 401, f"{svc}: erwartet 401 ohne Token, bekam {r.status_code}"
|
|
|
|
|
|
@pytest.mark.parametrize("svc,host", list(PUBLIC_HOSTS.items()))
|
|
def test_public_initialize_and_tools(svc, host):
|
|
"""initialize + tools/list ueber die oeffentliche URL mit Service-Token."""
|
|
token = _token()
|
|
r = httpx.post(f"https://{host}/mcp", json=INIT, headers=_headers(token), timeout=30)
|
|
assert r.status_code == 200, f"{svc}: initialize HTTP {r.status_code}: {r.text[:200]}"
|
|
res = _jsonrpc(r)
|
|
assert res and res.get("result", {}).get("serverInfo"), f"{svc}: kein serverInfo"
|
|
session = r.headers.get("mcp-session-id")
|
|
|
|
# initialized-Notification (Server, die sie verlangen); Fehler ignorieren.
|
|
if session:
|
|
httpx.post(
|
|
f"https://{host}/mcp",
|
|
json={"jsonrpc": "2.0", "method": "notifications/initialized"},
|
|
headers=_headers(token, session),
|
|
timeout=30,
|
|
)
|
|
|
|
r2 = httpx.post(
|
|
f"https://{host}/mcp",
|
|
json={"jsonrpc": "2.0", "id": 2, "method": "tools/list"},
|
|
headers=_headers(token, session),
|
|
timeout=30,
|
|
)
|
|
assert r2.status_code == 200, f"{svc}: tools/list HTTP {r2.status_code}: {r2.text[:200]}"
|
|
res2 = _jsonrpc(r2)
|
|
assert res2 and res2.get("result", {}).get("tools"), f"{svc}: tools/list leer"
|