52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
"""Minimaler MCP-Server über stdio, nur mit der Standardbibliothek.
|
|
|
|
Ein JSON-Objekt je Zeile. Ausgabe als Bytes, damit Windows kein \r einfügt.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
from datetime import datetime
|
|
|
|
TOOL = {
|
|
"name": "zeit",
|
|
"description": "Liefert Datum und Uhrzeit dieses Rechners.",
|
|
"inputSchema": {"type": "object", "properties": {}},
|
|
}
|
|
|
|
|
|
def answer(message):
|
|
method = message.get("method")
|
|
if method == "initialize":
|
|
return {"protocolVersion": message["params"]["protocolVersion"],
|
|
"capabilities": {"tools": {}}, "serverInfo": {"name": "zeit", "version": "1.0.0"}}
|
|
if method == "tools/list":
|
|
return {"tools": [TOOL]}
|
|
if method == "tools/call":
|
|
hint = os.environ.get("ZEIT_ZONE_HINWEIS", "")
|
|
text = f"{datetime.now().astimezone():%d.%m.%Y %H:%M:%S %Z} UHR-4711" + (f" ({hint})" if hint else "")
|
|
return {"content": [{"type": "text", "text": text}]}
|
|
if method == "ping":
|
|
return {}
|
|
return None
|
|
|
|
|
|
def main():
|
|
for line in sys.stdin:
|
|
if not line.strip():
|
|
continue
|
|
message = json.loads(line)
|
|
if "id" not in message:
|
|
continue # Benachrichtigungen brauchen keine Antwort
|
|
result = answer(message)
|
|
if result is None:
|
|
reply = {"jsonrpc": "2.0", "id": message["id"],
|
|
"error": {"code": -32601, "message": f"unbekannt: {message.get('method')}"}}
|
|
else:
|
|
reply = {"jsonrpc": "2.0", "id": message["id"], "result": result}
|
|
sys.stdout.buffer.write(json.dumps(reply).encode("utf-8") + b"\n")
|
|
sys.stdout.buffer.flush()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|