import sys; sys.stdin.reconfigure(encoding="utf-8")
import sys, json, pathlib
D = pathlib.Path(__file__).parent / "notes"
TOOLS = [{"name": "list_notes", "description": "메모 폴더의 파일 이름 목록(읽기 전용)", "inputSchema": {"type": "object", "properties": {}}},
         {"name": "read_note", "description": "메모 파일 하나 읽기(읽기 전용)", "inputSchema": {"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}}]
def send(o): sys.stdout.write(json.dumps(o) + "\n"); sys.stdout.flush()
for line in sys.stdin:
    if not line.strip(): continue
    m = json.loads(line); i = m.get("id"); me = m.get("method")
    if me == "initialize":
        send({"jsonrpc": "2.0", "id": i, "result": {"protocolVersion": m["params"]["protocolVersion"], "capabilities": {"tools": {}}, "serverInfo": {"name": "notes", "version": "1"}}})
    elif me == "tools/list":
        send({"jsonrpc": "2.0", "id": i, "result": {"tools": TOOLS}})
    elif me == "tools/call":
        p = m["params"]; n = p["name"]
        if n == "list_notes": txt = "\n".join(f.name for f in D.glob("*.txt"))
        else:
            f = D / pathlib.Path(p["arguments"]["name"]).name
            txt = f.read_text(encoding="utf-8") if f.exists() else "없음"
        send({"jsonrpc": "2.0", "id": i, "result": {"content": [{"type": "text", "text": txt}]}})
    elif i is not None:
        send({"jsonrpc": "2.0", "id": i, "result": {}})
