from __future__ import annotations from typing import Any, Awaitable, Callable from ai_chess.sides import Side from ai_chess.state import AppState Tool = Callable[[dict[str, Any]], Awaitable[dict[str, Any]]] def build_tools_for_side(side: Side, state: AppState) -> dict[str, Tool]: async def _publish(event_type: str, **payload: Any) -> None: await state.events.publish({"type": event_type, **payload}) async def get_board(_args: dict[str, Any]) -> dict[str, Any]: g = state.game if g is None: return {"error": "no_active_game"} st = g.get_state() return { "fen": st["fen"], "ascii": st["ascii"], "side_to_move": st["side_to_move"], "status": st["status"], } async def get_history(_args: dict[str, Any]) -> dict[str, Any]: g = state.game if g is None: return {"error": "no_active_game"} return {"history": [h.to_dict() for h in g.history]} async def status(_args: dict[str, Any]) -> dict[str, Any]: g = state.game if g is None: return {"error": "no_active_game"} return g.get_state()["status"] async def move(args: dict[str, Any]) -> dict[str, Any]: g = state.game if g is None: return {"ok": False, "reason": "no_active_game"} mv = str(args.get("move", "")).strip() if not mv: return {"ok": False, "reason": "missing_move"} comment = args.get("comment") if comment is not None: comment = str(comment) result = g.try_move(side, mv, comment=comment, allow_illegal=state.settings.allow_illegal_moves) payload = result.to_dict() if result.ok: await _publish( "move", side=side.value, san=result.san, uci=result.uci, comment=comment, fen=result.fen, ascii=result.ascii, illegal=result.illegal, status=result.status, history=result.history, ) if g.is_game_over(): await _publish( "game_over", result=g.result, termination=g.termination, ) return payload async def resign(args: dict[str, Any]) -> dict[str, Any]: if not state.settings.allow_draw_resign: return {"ok": False, "reason": "resign_disabled"} g = state.game if g is None: return {"ok": False, "reason": "no_active_game"} out = g.resign(side, comment=args.get("comment")) if out.get("ok"): await _publish("resign", side=side.value, comment=args.get("comment")) await _publish("game_over", result=g.result, termination=g.termination) return out async def offer_draw(args: dict[str, Any]) -> dict[str, Any]: if not state.settings.allow_draw_resign: return {"ok": False, "reason": "draw_disabled"} g = state.game if g is None: return {"ok": False, "reason": "no_active_game"} out = g.offer_draw(side, comment=args.get("comment")) if out.get("ok"): if g.is_game_over(): await _publish("game_over", result=g.result, termination=g.termination) else: await _publish("draw_offered", side=side.value, comment=args.get("comment")) return out async def accept_draw(args: dict[str, Any]) -> dict[str, Any]: if not state.settings.allow_draw_resign: return {"ok": False, "reason": "draw_disabled"} g = state.game if g is None: return {"ok": False, "reason": "no_active_game"} out = g.accept_draw(side) if out.get("ok"): await _publish("game_over", result=g.result, termination=g.termination) return out async def decline_draw(_args: dict[str, Any]) -> dict[str, Any]: if not state.settings.allow_draw_resign: return {"ok": False, "reason": "draw_disabled"} g = state.game if g is None: return {"ok": False, "reason": "no_active_game"} out = g.decline_draw(side) if out.get("ok"): await state.events.publish({"type": "draw_declined", "side": side.value}) return out return { "get_board": get_board, "get_history": get_history, "status": status, "move": move, "resign": resign, "offer_draw": offer_draw, "accept_draw": accept_draw, "decline_draw": decline_draw, } # JSON schema fragments for tool advertising in MCP TOOL_SCHEMAS: dict[str, dict[str, Any]] = { "get_board": { "description": "Получить текущую позицию (ASCII доска, FEN, чей ход, статус).", "inputSchema": {"type": "object", "properties": {}}, }, "get_history": { "description": "Получить список ходов с комментариями обеих сторон.", "inputSchema": {"type": "object", "properties": {}}, }, "status": { "description": "Статус партии (результат, шахи, счётчики).", "inputSchema": {"type": "object", "properties": {}}, }, "move": { "description": "Сделать ход. Поле comment передаётся сопернику вместе с уведомлением.", "inputSchema": { "type": "object", "properties": { "move": {"type": "string", "description": "UCI (e2e4) или SAN (e4, Nf3, O-O)."}, "comment": {"type": "string", "description": "Реплика сопернику (1–3 предложения)."}, }, "required": ["move"], }, }, "resign": { "description": "Сдаться (если разрешено настройками).", "inputSchema": { "type": "object", "properties": {"comment": {"type": "string"}}, }, }, "offer_draw": { "description": "Предложить ничью (если разрешено настройками).", "inputSchema": { "type": "object", "properties": {"comment": {"type": "string"}}, }, }, "accept_draw": { "description": "Принять оффер ничьи противника.", "inputSchema": {"type": "object", "properties": {}}, }, "decline_draw": { "description": "Отклонить оффер ничьи противника.", "inputSchema": {"type": "object", "properties": {}}, }, }