Skip to main content
Glama

ygopro-mcp

把 YGOPro 客户端接到 MCP(Model Context Protocol):LLM / agent 通过 20 个 MCP 工具, 用「语义动作」打牌——不用拼 CTOS/STOC 二进制帧,也不需要用像素点屏幕。

   LLM / agent(MCP 宿主)
        │  MCP over stdio (JSON-RPC)
        ▼
   bridge/mcp-server.mjs        薄桥:20 个工具 → HTTP
        │  HTTP 127.0.0.1:7788(房主)/ 7789(客机)
        ▼
   带控制层的 YGOPro 客户端      进程内 HTTP server + 动作编码 + UI 注入
        │  CTOS_RESPONSE / STOC_*
        ▼
   ygopro 内核(ocgcore)+ 真实客户端窗口
  • 控制层负责:协议编解码、线程安全、把「召唤/盖放/发动/攻击/选卡」编码成内核能接受的响应, 并尽量走真实 UI 注入(点不中时自动回退为直连响应,保证绝不死锁)。

  • bridge 只做两件事:把 MCP 工具映射成 HTTP 调用;提供一个参考策略(policy-semantic.mjs) 和运维工具(doctor / supervisor / netfault / net-probe)。


1. 前置条件

需要

说明

带控制层的 YGOPro 客户端

本仓库不包含客户端二进制。控制层是客户端源码里的扩展(gframe/control*.cpp,HTTP 端点 /state /prompt /wait_prompt /actions /act_id /net /ui 等)。已测试构建:YGOPro.exe 2026-11-27 17:22:16 / 6,338,048 BSHA256:99FC53A3B2A7121A7EFBCF81A312F9499E603E8122ED6C37CC326C9B023C0708核对:node doctor.mjs --client <exe>(默认比 size;加 --strict-build 同时比 sha256 —— 用于区分"同 size 不同 build")。构建:用带控制层补丁的 YGOPro 源码(MyCard 客户端 fork)自行编译,产物放到例如 mycard-client/bin/release/x64/YGOPro.exe。

Node.js ≥ 18

只用内置模块(fetch/http/child_process),零 npm 依赖。

(可选)卡组 .ydk

客户端自己的 deck/ 目录;控制层用 POST /deck?name=Blackwing 预载。本仓库不含卡图/卡表。

控制端口:房主默认 7788,客机默认 7789(客户端用 YGO_CONTROL_PORT 指定)。


Related MCP server: lemonade-bench

2. 快速开始(3 步)

# ① 先启动带控制层的客户端(房主 / 客机各一个进程)
#    房主示例(控制端口 7788,监听 7911)
$env:YGO_CONTROL_PORT='7788'
.\mycard-client\bin\release\x64\YGOPro.exe -c -h 127.0.0.1 -p 7911 -n HostA -d Blackwing -k

# ② 启动 MCP server(stdio;本身不占网络端口)
cd bridge
npm start                 # 等价于 node mcp-server.mjs
#   客机位(7789)时:
#   $env:CONTROL_URL='http://127.0.0.1:7789'; npm start

# ③ 在你的 MCP 宿主里配置(示例见 examples/)
#   command = node
#   args    = ["<绝对路径>/bridge/mcp-server.mjs"]
#   env     = { CONTROL_URL: "http://127.0.0.1:7788" }

宿主配置片段:examples/claude_desktop_config.json(Claude Desktop)、examples/cursor.mcp.json (Cursor)、examples/dsh.mcp.json(DeepSeek Harness)。路径换成你自己的即可。

最小调用循环(agent 侧):

control_wait_prompt({since: lastSeq})    // 长轮询,等一个需要你回答的 prompt
control_actions()                        // 本 prompt 的候选:{id, kind, label}
control_act_id({id})                     // 提交你选中的候选
lastSeq = prompt.seq

三条铁律:① 动作必须匹配当前 seq(旧 seq 会被拒:stale seq);② /state.started ≠ /health.started(前者是"决斗已开始",后者是"控制服务在监听");③ 看到 UI fallback 是正常兜底,不是故障。


3. 自检

cd bridge
npm run doctor              # = node doctor.mjs(默认探测 127.0.0.1:7788)
node doctor.mjs --port 7789 # 客机位
node doctor.mjs --client "C:\path\to\YGOPro.exe"   # 顺带核对 exe 指纹(size)
node doctor.mjs --client "C:\path\to\YGOPro.exe" --strict-build   # 发布自检:size + sha256 都比

输出 [GREEN]/[YELLOW]/[RED]/[INFO] 每项检查 + HINTS + JSON;退出码 0 = 全绿,1 = 有问题。 没有客户端时会输出 RED 并提示「先启动带控制层的 YGOPro 客户端(房主 7788 / 客机 7789)」。

--client 默认只比 size(跨机/跨时区/复制场景都可移植);加 --strict-build 时才比 sha256。 这两个必须区分:真实出现过"两个不同 build 的 exe 大小完全一样(都是 6,295,040 B)"的情况,只看 size 分辨不出。


4. 目录说明

路径

类别

作用

mcp-server.mjs

runtime

MCP stdio server,20 个工具 → HTTP

policy-semantic.mjs

runtime

参考策略(语义决策循环;可当 LLM 的替身)

supervisor.mjs

runtime

常驻监护:读 /net 判掉线;--profile lan 只记账,--profile ranked 重启客户端并重连+验证

doctor.mjs

ops

自检(本文件)

netfault.mjs

ops

故障注入 TCP 代理(freeze/drop/reject),用于测掉线检测

net-probe.mjs

ops

轮询 /net 出时间线 CSV + EVENT DEAD/ALIVE

lan-run.ps1 / lan-host.cmd / lan-guest.cmd

ops

本地 LAN 一键起房/客机跑局

reconnect-lab-run.ps1

ops

掉线检测实验(host + 客机走 netfault + 注入故障 + 汇总)

netfault.md / net-probe.md

doc

对应工具的用法

docs/

doc

权威接口规范与设计文档(见下)

examples/

doc

MCP 宿主配置片段

dev/

dev-only

维护者的开发/排障脚本(一次性探针、旧实验脚本),不属于运行时分发内容,分发时可整目录删掉

维护者的开发/排障脚本(一次性探针、旧实验脚本)不属于运行时分发内容。


5. 已知限制

  • LAN 不支持续局:本地 LAN 服务端不能"掉线后回到原对局";supervisor --profile lan 只检测+记账。 真正的续局验证只能在线上(--profile ranked,需要客户端 journal//net 支持)。

  • ranked(线上排位) 需要较新的客户端构建(/net、isWin/isLoss/isDraw、deckError、prompt 快照一致性修复); 旧构建会缺字段(doctor 会提示,policy 会记 isWin=unknown 而不是猜)。

  • 自动化有账号风险:排位自动对战可能违反服务条款/被判异常,请自行评估;本仓库不含任何账号凭据。

  • 不含卡图/卡表/卡组:只做控制层桥接,卡牌数据由客户端自己管理。

  • UI 注入不是 100%:部分语义词(select/sum/sort/counter/announce/auto)仍走直连响应(刻意保留的兜底), 详见 docs/direct-actions.md。

  • 动作被内核拒绝时看 /state.retryCount/retryStreak;prompt 卡住时看 /state.pipeline 与 pipeline.recentMsgs(需要新构建)。

  • Bo3 新局(Game2/3)会重置结果状态:客户端在 STOC_DUEL_START 时把 winPlayer/winPlayerLocal/winReason 重置为 -1、awaiting=false、tpPending/sidePending=false(日志 DUEL_START reset result state (match game N))。 旧客户端可能让新局沿用上一局的 winPlayer,导致 policy 一开局就误判"对局已结束"并退出——遇到请升级客户端。

更新记录(近期)

  • 2026-11-27 客户端 17:22:16 / 6,338,048 B:position 掩码 + ignore keep-own 定稿版——MSG_SELECT_POSITION 捕获合法掩码 → /prompt.selectablePositions / selectablePositionsHex;/actions.position 按掩码动态生成(表攻/里攻/表守/里守),旧版写死 defense=0x02(=里侧攻击,非法响应 seq 连发)已修;编码校验掩码,单 bit 自动应答。当前构建 /actions 项只有 id/kind/label(无 index 字段)——按 label 选、act_id 自动发正确 POS 值(日志 i=1/2/4/8);index 字段自下个 build(>17:22:16)起提供。ignore 不再吞我方诱发:activatable_cards 有我方候选时保留窗口(YGO_CHAIN_IGNORE_KEEP_OWN 默认 1,0=旧行为;日志 ownCand=.. ignoreDecline=.. keepOwn=..)。验证 position-mask-ignore-validation.md:mask 0x5 选 defense 发 i=4 推进/无 RETRY、mask 0x9 里守 i=8、ignore 下我方诱发正常弹出、env=0 对照旧行为、四态回归 PASS。

  • 2026-11-27 客户端 17:09:18 / 6,336,000 B(历史):side_ok label 三态 + changed 版——/actions(sidePending)side_ok label 三态(no changes / confirm side deck (changed) / counts mismatch),顶格 changed:true|false;counts 相等 ≠ 没换牌。现已被 17:22:16 版取代。

  • 2026-11-27 客户端 15:03:45 / 6,335,488 B(历史):SelfSeat 修复版(lastChainEvent 阵营反)——SelfSeat() 在对局/换备期间以 dInfo.isFirst 为准(isFirst==true ⟺ 服务端座位 0);修 MC 不发 STOC_TYPE_CHANGE、launcher -d/-k 使 -c/-j fallback 失效时 guest 被错判 seat 0;SEAT resolved seat=.. isFirst=.. 可核对;验证 lastchain-side-validation.md(guest「我方发动 [黑洞]」/ host「对手发动 [黑洞]」)。现已被 17:09:18 版取代。

  • 2026-11-27 客户端 11:04:58 / 6,334,976 B(历史):时点四态 normal 定稿版——0=all(全部时点) / 1=avail(全部可用时点) / 2=ignore(无视所有时点) / 3=normal(常用时点),进入对局默认 normal;YGO_CHAIN_MODE 4 值默认 normal;行为语义见 MCP-INTERFACE.md/PLAYBOOK-FULL.md(all=任何时候都给窗口;avail=有合法动作就问;ignore=自动放弃但强制/必发仍处理;normal=在 avail 基础上过滤非常用询问)。现已被 15:03:45 版取代。

  • 2026-11-26 客户端 10:26:12 / 6,335,488 B(历史):座位坐标/isMine 统一修复版——ToServerSeat(localCtrl,mySeat) 全路径统一(ctrl=服务端座位、isMine==(ctrl==mySeat)、name 回填)、places 含 ctrlRaw、chain label 修正;验证 seat-isMine-validation.md。现已被 11:04:58 版取代。

  • 2026-11-23 客户端 14:53:57 / 6,334,464 B(历史):MCP 实战反馈修复版——wait_prompt 阻塞语义修正(seq>since && awaiting)、候选卡 ctrl/isMine 修正 + selectableCards[].resp(响应用 resp/seqNo 不是下标 i)、BUG-1 归类外部断线。现已被 10:26:12 版取代。

  • 2026-11-20 客户端 15:34:59 / 6,329,856 B(历史):stall 诊断版(判死逻辑不变)——/net 新增 stallMs/oppTimeLeft/oppClockFresh、NET STALL(YGO_STALL_WARN_MS 默认 120000/30s);stallMs>120s && oppClockFresh=false = 对方卡住 → control_leave 离桌重排。现已被 14:53:57 版取代。

  • 2026-11-19 客户端 17:33:31 / 6,330,368 B(历史):看门狗回归修复版(修 g_timeLeft 不清零导致 clockRunning 永久 true)——g_timeLeftTick + clockFresh(<3s) + clockRunning=ourTurn&&clockFresh;/net 新增 ourTurn/oppTurn/timeLeft/clockFresh;heartbeat 空闲时 15s;验证 net-watchdog-validation.md 4/4 PASS;不再建议 600000(默认 60000)。现已被 15:34:59 版取代。

  • 2026-11-17 客户端 15:18:21 / 6,329,344 B(历史):逐卡选择修复版(修"有对象的攻击不结算")——/actions 对 MSG_SELECT_CARD/TRIBUTE/UNSELECT_CARD(单目标)每个候选一条 kind:"select_card"(label 带 atk/def/loc/seq),select_auto 只作多选兜底;/prompt.selectableCards[] 新增 position、战斗目标窗 isAttackTarget:true;单目标 id=0 仍等价旧行为;验证 attack-target-choice-validation.md A–E PASS。现已被 17:33:31 版取代。

  • 2026-11-16 客户端 11:31:00 / 6,328,320 B(历史):probe-fails 误判修复版——/tip 探活超时不再单独判死(旧版 2 次失败即 confirmed_dead+断线,对手长展开 7.5s 静默会被误踢);probe 失败只置 probe_failed 并打 NET probe failed fails=.. (defer to passive silence guard);新增 YGO_NET_PROBE_FAIL_MAX(默认 5,仅降级/日志);判死统一由被动静默 YGO_NET_DEAD_SILENCE_MS(默认 60s)+ 我方回合/倒计时/awaiting/busy 守卫决定;现已被 15:18:21 版取代。

  • 2026-11-16 客户端 10:25:14 / 6,327,808 B(历史):EOF guard 版(防"LLM 静默 → 服务端 AFK ~90s eof")——YGO_PROMPT_AUTO_DEFAULT_MS(默认 60000,0=关)60s 自动发保守默认、YGO_PROMPT_IDLE_WARN_MS(45000)告警、/prompt.awaitingSinceMs;现已被 11:31:00 版取代。

  • 2026-11-15 客户端 20:11:00 / 6,323,712 B(历史):SORT 已实机验证(realMsg=25 len=3 hex=000102,裸位序无前缀、直连字节 UI skip sort -> direct bytes)+ announce 140–143 修复(kPromptMsgs/announceNumberValues/announce_number/race/attrib/card;number=数值、race/attrib=掩码、card=卡号)+ 攻击目标/TP 自动/判死 60s;现已被 10:25:14 版取代。

  • 2026-11-15 客户端 19:05:42 / 6,321,152 B(历史):真实 LLM 使用反馈修复版——① 战斗攻击目标(curMsg 15↔10 循环、攻击不结算)修复(selectMin==0→[0]、select 直连字节、stale seq/snapshot 防护),LAN 实机 PASS(send15=2 retry15=0、hex=0100、LP 8000→3900→1850、loops=0);② TP 默认 2s 自动答(YGO_TP_AUTO/YGO_TP_AUTO_RES,默认 res=2=后攻,可配 YGO_TP_AUTO_RES=1 先攻;实测 TP auto-answered res=2);③ 判死误杀修复(默认阈值 60s + 我方回合/倒计时在走不判死,NET ALIVE reason=our-turn-idle;实测 40s 思考无 NET DEAD;旧 exe 仍需 YGO_NET_DEAD_SILENCE_MS=600000);④ MSG_SORT_CARD 裸位序字节无前缀(hex=0001)已实现,待 LAN 实机触发验证;现已被 20:11:00 版取代。

  • 2026-11-13 客户端 14:51:46 / 6,319,104 B(历史):LAN 全流程验收(VERDICT: over=True won=2 games=2)——/state.match 整场结果(game/over/games[]/gamesWonLocal/gamesWonOpp/lastWinnerLocal/lastReasonName)、/start latch 修复、YGO_TP/YGO_SIDE_AUTO、控制驱动 STOC_REPLAY 直接跳过(replay: dialog skipped,LAN 实测 双方各 2 次、replay-save=0、stoc=23 stall=0);MCP 侧新增 control_match 一句话报结果;现已被 19:05:42 版取代。

  • 2026-11-13 客户端 01:11:32 / 6,316,032 B(历史):控制驱动下 STOC_REPLAY 跳过 + 3/3 Bo3 实机验收;现已被 14:51:46 版取代。

  • 2026-11-10 客户端 00:07:02 / 6,295,040 B:Bo3 Game2/3 结果状态重置(DUEL_START reset result state); bridge 侧同步 fingerprint + sha256,doctor 新增 --strict-build(size+sha256)。

  • 2026-11-09 客户端:Bo3 换备牌 side_ok(STOC_CHANGE_SIDE)+ 回放保存框自动跳过(限流 1s)。

  • 2026-11-08 客户端:先/后攻交给 LLM(/actions kind:"tp");policy 支持 TP_PREF。

  • 2026-11-07 客户端:排位全链路(拿房→选卡组→准备→对局→自然结算)、isWin/isLoss/isDraw、deckError、/net 探活。


6. 权威文档

文档

内容

docs/PLAYBOOK-FULL.md

全流程权威操作手册(给 AI/LLM,零歧义):系统架构、20 个工具逐个说明(参数/返回/适用/失败)、排队→进房→卡组→准备→开始→TP→决策→局末→换备→下一局→整场结束的每一步判定、状态字段字典、计时节奏、完整实战脚本 + 真实时间线、排错表、已知限制

docs/MATCH-PLAYBOOK.md

精简版操作手册(给 AI):一页速查;详细版见 PLAYBOOK-FULL.md

docs/MCP-INTERFACE.md

接口规范(权威):20 个工具、所有 HTTP 端点、/state//prompt//ui 字段、/actions kind 全表、连锁语义、UI 注入设计、运行开关、排障

docs/RECONNECT-DESIGN.md

掉线/重连设计:/net 状态机、supervisor 职责、journal(resume-context-<port>.json)、探活参数

docs/supervisor.md

supervisor 用法、状态机、JSONL 字段

docs/direct-actions.md

哪些动作走 UI、哪些走直连、对 LLM 的影响、改进路线

net-probe.md / netfault.md

掉线检测量化工具 / 故障注入代理用法

环境变量清单见 .env.example(与代码里 process.env.* 一一对应)。


7. FAQ / 排障

Q: doctor.mjs 报 RED "连不上 127.0.0.1:7788"? A: 客户端没起、或没用带控制层的构建、或端口不是 7788。检查客户端启动日志是否有 control: started port=7788;用 netstat -ano | findstr :7788 看端口。

Q: 两个客户端同时跑,端口冲突? A: 每个客户端要不同的 YGO_CONTROL_PORT:房主 7788、客机 7789(lan-run.ps1 就是这么分的)。 对应 MCP server 的 CONTROL_URL 也要指向你要控制的那一端。

Q: 客机想走代理/故障注入,但流量没过去? A: 客户端的 -j 参数会用 system.conf 的 lasthost/lastport 自动加入并忽略 -p; 要经代理必须不带 -j、起来后用控制层 GET /join?ip=127.0.0.1&port=7912&pass= 显式进房, 并断言代理 totalConnections >= 1(reconnect-lab-run.ps1 已内置该断言)。

Q: 动作被拒 (ok:false / stale seq)? A: prompt 已经前进了。丢弃这次决策,重新 control_wait_prompt + control_actions; /state.retryStreak 持续上涨说明响应编码有问题(看 /state.lastRetryAfterMsg)。

Q: 卡组非法 / 开局不动? A: 新构建在 /state.deckError 给出原因;用合法的 .ydk(例如 Blackwing.ydk)。 旧构建会弹"卡组非法"模态框阻塞,需要新构建(会自动 suppress)。

Q: 怎么确认"我们赢了还是输了"? A: 用 /state.isWin(本地视角)。不要用 winPlayer(那是 raw 座位号,本机是 raw seat 1, winPlayer=0 表示对手赢);旧构建没有 isWin 时用 winPlayerLocal===0,再没有就记 unknown,不要猜。

Available Tools

14 tools
control_actB

Submit a response for the prompt with the given seq. Use either i (int32 encoded response, e.g. idle (index<<16)|cmd, -1 for chain decline) or hex (raw response bytes, e.g. card selection "0100" = select 1 card at index 0).

Inputs: seq (required), i, hex.

control_act_idB

Execute one of the actions returned by control_actions, by id.

Inputs: id (required).

control_actionsA

List the semantic actions available for the current prompt (id + kind + human label, e.g. summon/special summon/attack/chain/zone). The caller only picks an id; the client encodes the kernel response itself.

Inputs: no named parameters.

control_chatB

Recent server chat / system messages (useful to see why a room was rejected or a duel was kicked).

Inputs: no named parameters.

control_deckA

Preload a deck by name (file under deck/*.ydk). Call before control_ready so the deck update is valid.

Inputs: name.

control_healthC

Health check of the control layer.

Inputs: no named parameters.

control_joinB

Connect to a game server (LAN or a matchmade room).

Inputs: ip, pass, port.

control_lobbyA

Room/prep-screen state: our seat (0=host,1=guest), whether we are host, readiness of both seats, deck loaded, raw HS status bytes. Use this before ready/start.

Inputs: no named parameters.

control_msgA

Last raw server message (id, length, hex) - debug only.

Inputs: no named parameters.

control_readyA

Send CTOS_HS_READY (click the Ready button) for the current room.

Inputs: no named parameters.

control_startA

Click "Start" (CTOS_HS_START). Only valid when we are the host (seat 0) and both players are ready.

Inputs: no named parameters.

control_stateB

Get the full duel state: turn, phase, LP, hand/field/grave/deck counts with card details, pending prompt counters.

Inputs: no named parameters.

control_surrenderB

Surrender the current duel (CTOS_SURRENDER).

Inputs: no named parameters.

control_wait_promptB

Long-poll until it is this client's turn to respond. Returns the prompt: seq (pass it back to control_act), curMsg (what kind of decision), and candidate counters. This is the main agent loop primitive.

Inputs: since, timeout_ms.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.6/5.0

    Scored across 14 tools

    Disambiguation4/5

    Most tools map to distinct duel-control lifecycle stages (lobby, deck, ready, start, prompt, act, surrender), so selection is usually clear. The main overlap is the prompt-response group: control_act and control_act_id both submit a decision and differ mainly by encoding, though the raw-vs-semantic descriptions help.

    Naming Consistency4/5

    All names share a consistent control_ namespace and snake_case formatting, making the set easy to scan. However, suffixes mix nouns (state, lobby, chat) and verbs (act, start, join) rather than following a pure verb_noun pattern.

    Tool Count5/5

    Fourteen tools are well within a reasonable range for controlling a duel client, and each covers a distinct concern: session setup, prompt loop, action submission, state inspection, surrender, chat, and health. The debug/health/msg tools are minor extras but do not feel like bloat.

    Completeness4/5

    The surface covers join, deck preload, ready, start, prompt handling, action execution, state inspection, surrender, and chat, which is most of the duel session lifecycle. Minor gaps remain around explicit disconnect/leave and room creation or listing, with some room state visible only through chat or raw messages.

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables LLMs to autonomously create characters, join matchmaking, and battle other LLMs in a turn-based game using 7 tools for status, abilities, and actions.
      ISC
    • A
      license
      A
      quality
      D
      maintenance
      Enables tool-using language models to play Lemonade Tycoon Deluxe as a text-only business benchmark, exposing game UI state and actions as semantic MCP tools without vision or shell access.
      23
      MIT
    • F
      license
      Not graded
      quality
      C
      maintenance
      Enables LLMs to control a Minecraft bot and interact with the game through MCP tools, including observing state, moving, mining, crafting, building, farming, managing inventory, enchanting, and chatting in-game.
      1
      -
    • A
      license
      Not graded
      quality
      D
      maintenance
      Provides Yu-Gi-Oh! card data, banlist, deck management, OCG rules engine, duel simulation, replay analysis, and YGOPro2 bridge for AI agents via MCP.
      7
      BSD Zero Clause