diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index aabec92049a..53b400b30e9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -471,3 +471,12 @@ Here is an example table that could be generated | Modules/Common : copyTable : 351 : ~ : ~ : 2 | ------------------------------------------------------------------------------------------------------------------------------------- ``` + +## MCP server (AI assistant integration) + +Path of Building can run a [Model Context Protocol](https://modelcontextprotocol.io) server inside the +application (the **MCP** button in the bottom-left bar). AI assistants such as Claude Code then read and +change the build that is open in the window: items, gems, passives, configuration and what-if modifiers. +The server is a sub script (`src/MCPServer.lua`) relaying JSON-RPC to `src/Modules/MCPStatus.lua`; the +tools live in `src/Modules/MCPTools.lua`. See [docs/mcp.md](docs/mcp.md) for setup, the protocol details +and the list of tools. diff --git a/docs/mcp.md b/docs/mcp.md new file mode 100644 index 00000000000..67f0b998292 --- /dev/null +++ b/docs/mcp.md @@ -0,0 +1,71 @@ +# MCP server (AI assistants driving Path of Building) + +Path of Building can run a [Model Context Protocol](https://modelcontextprotocol.io) server inside +the application. An AI assistant that supports MCP (Claude Code, Claude Desktop, Cursor, ...) then +works on the build that is **open in the window**: when it equips an item, allocates a passive or +flips a configuration option, the change appears in the window immediately, marks the build unsaved +and can be undone like any other edit. Nothing runs headless and no second copy of PoB is started. + +## Turning it on + +1. Click **MCP** in the bottom-left bar (next to *Options* and *About*). +2. Tick **MCP server**. The panel shows the URL the server listens on, by default + `http://127.0.0.1:7390/mcp`. The switch and the port are saved with your settings, so the server + comes back the next time PoB starts. +3. Connect a client: + * **Claude Code** – copy the command from the panel, or run + `claude mcp add --transport http pob http://127.0.0.1:7390/mcp` + * **Other clients** – add a remote / Streamable HTTP MCP server with the URL from the panel. + +The bar button shows the number of clients that were active in the last ten minutes. The panel lists +every client session (name and version from the MCP handshake, address, number of tool calls, last tool +and when it was last active); hover a row for details such as the last error. + +The server only listens on 127.0.0.1 and rejects requests from web pages (non-local `Origin`), so +other machines and browser tabs cannot reach it. + +## Tools + +| Tool | What it does | +|------|--------------| +| `get_build_summary` | Class, level, main skill, points, warnings, key stats of the open build | +| `list_builds`, `open_build`, `close_build` | Browse the Builds folder and open or close a saved build in the window | +| `load_build` | Load a share code, raw XML, a `.xml` path, or a pobb.in / pob.codes / poe.ninja / maxroll / pastebin / rentry / poedb link | +| `new_build`, `set_class`, `set_character` | Start from scratch; change class, ascendancy, level, bandit, pantheon | +| `save_build`, `export_build`, `decode_build_code` | Save to the Builds folder, get a share code or XML | +| `get_sidebar_stats`, `get_stats`, `get_breakdown` | What the sidebar shows, any output stat, the Calcs-tab breakdown of a stat | +| `list_skills`, `set_main_skill`, `add_skill_group`, `remove_skill_group`, `set_gem`, `set_skill_group_enabled` | Socket groups and gems | +| `list_items`, `get_item_text`, `add_item`, `equip_item`, `unequip_slot`, `delete_item` | Items and equipment | +| `get_passive_tree`, `search_passive_nodes`, `allocate_node`, `deallocate_node`, `reset_passive_tree`, `import_passive_tree` | Passive tree | +| `list_config_options`, `set_config_option` | Configuration tab | +| `set_custom_modifiers`, `get_custom_modifiers` | Free-form "what if I had +100 life" modifiers (a Custom Modifiers block named *MCP*) | + +Every tool that changes the build returns `changes`: the sidebar stats that moved, with before and after +values. Tools that would replace the open build (`load_build`, `open_build`, `new_build`, `close_build`) +refuse while it has unsaved changes unless the assistant passes `force=true`. + +## How it works + +``` +MCP client ── HTTP POST /mcp (JSON-RPC) ──► src/MCPServer.lua (sub script thread, LuaSocket) + │ MCPHandle(remote, session, method, body) blocking sub-script call + ▼ + src/Modules/MCPStatus.lua (main thread: sessions, JSON-RPC) + │ + ▼ + src/Modules/MCPTools.lua (tools acting on main.modes.BUILD) +``` + +* The server implements the MCP *Streamable HTTP* transport with plain JSON responses: `POST /mcp` + carries one JSON-RPC message (or a batch), `DELETE /mcp` ends a session, `GET` is answered with + 405 because the server never pushes messages on its own. Sessions are identified by the + `Mcp-Session-Id` header handed out by `initialize`. +* SimpleGraphic runs one blocking sub-script call per frame, so each request costs about one frame of + latency. While the server runs, PoB keeps rendering frames even when the window is not focused. +* Downloads for `load_build` links happen in the sub script (the main thread must not block on the + network); everything else runs on the main thread between frames, exactly where a mouse click would. +* After a mutating tool the build is recomputed immediately (the same work `Build:OnFrame` does when + `buildFlag` is set) so the reported `changes` are current. + +Headless runs (`HeadlessWrapper.lua`, the test suite) have no sub scripts, so the server cannot start +there; the JSON-RPC layer can still be exercised by calling `main.mcp:Handle(...)` directly. diff --git a/src/Classes/MCPConnectionListControl.lua b/src/Classes/MCPConnectionListControl.lua new file mode 100644 index 00000000000..28b349993a2 --- /dev/null +++ b/src/Classes/MCPConnectionListControl.lua @@ -0,0 +1,78 @@ +-- Path of Building +-- +-- Class: MCP Connection List +-- Lists the MCP client sessions connected to this window's MCP server (see Modules/MCPStatus.lua). +-- +local s_format = string.format +local os_time = os.time + +---@class MCPConnectionListControl: ListControl +local MCPConnectionListClass = newClass("MCPConnectionListControl", "ListControl") + +function MCPConnectionListClass:MCPConnectionListControl(anchor, rect, mcpStatus) + self:ListControl(anchor, rect, 18, "VERTICAL", false, mcpStatus.connections) + self.mcpStatus = mcpStatus + self.colLabels = true + self.showRowSeparators = true + self.defaultText = "^8No client has connected yet. Add this server to Claude Code (or another MCP client) and it will show up here." + self.colList = { + { label = "Client", width = 220 }, + { label = "From", width = 110 }, + { label = "Status", width = 90 }, + { label = "Calls", width = 60 }, + { label = "Last tool", width = 150 }, + { label = "Activity" }, + } + return self +end + +function MCPConnectionListClass:GetRowValue(column, index, session) + if column == 1 then + local name = session.title or session.client or "unknown client" + if session.version then + name = name .. " ^8" .. session.version + end + return name + elseif column == 2 then + return session.remote or "?" + elseif column == 3 then + if not self.mcpStatus:IsRunning() then + return "^8server off" + elseif os_time() - session.lastActivity <= self.mcpStatus.ACTIVE_SECONDS then + return "^x50E050active" + end + return "^8idle" + elseif column == 4 then + return tostring(session.calls or 0) + elseif column == 5 then + if not session.lastTool then + return "" + end + return (session.lastError and "^1" or "^7") .. session.lastTool + elseif column == 6 then + return self.mcpStatus.FormatAge(os_time() - session.lastActivity) + end +end + +function MCPConnectionListClass:AddValueTooltip(tooltip, index, session) + local format = self.mcpStatus.FormatTime + tooltip:Clear() + tooltip:AddLine(16, s_format("^7%s %s", session.title or session.client or "Unknown client", session.version or "")) + if session.title and session.client then + tooltip:AddLine(14, "^8Client id: ^7" .. session.client) + end + tooltip:AddLine(14, "^8Connected: ^7" .. format(session.connected) .. " ^8from ^7" .. tostring(session.remote)) + tooltip:AddLine(14, "^8Last activity: ^7" .. format(session.lastActivity)) + tooltip:AddLine(14, s_format("^8Requests: ^7%d^8, tool calls: ^7%d", session.requests or 0, session.calls or 0)) + if session.lastTool then + tooltip:AddLine(14, "^8Last tool: ^7" .. session.lastTool) + end + if session.lastError then + tooltip:AddLine(14, "^8Last error: ^1" .. session.lastError) + end + tooltip:AddLine(14, "^8Protocol: ^7" .. tostring(session.protocolVersion) .. "^8, session ^7" .. session.id) +end + +function MCPConnectionListClass:OnSelCopy(index, session) + Copy(session.lastError or session.id) +end diff --git a/src/MCPServer.lua b/src/MCPServer.lua new file mode 100644 index 00000000000..a6f802aed9d --- /dev/null +++ b/src/MCPServer.lua @@ -0,0 +1,265 @@ +-- Path of Building +-- +-- Sub script: MCP Server +-- A small HTTP server (MCP "Streamable HTTP" transport, JSON responses) that runs in a sub +-- script thread while the MCP switch in the PoB window is on. It does no PoB work itself: every +-- JSON-RPC request is handed to the main thread through MCPHandle(), which runs the tools on the +-- build open in the window and returns the response. Started by Modules/MCPStatus.lua. +-- +-- Main-thread functions available here: +-- MCPHandle(remoteAddress, sessionId, httpMethod, body) -> status, body, sessionId (blocking) +-- MCPEvent(kind, detail) (fire and forget) +-- +local host, port, maxBodySize = ... +maxBodySize = maxBodySize or 8 * 1024 * 1024 + +local socket = require("socket") +local json = require("dkjson") + +local reasons = { + [200] = "OK", [202] = "Accepted", [204] = "No Content", + [400] = "Bad Request", [403] = "Forbidden", [404] = "Not Found", [405] = "Method Not Allowed", + [411] = "Length Required", [413] = "Payload Too Large", [500] = "Internal Server Error", +} + +local function respond(client, status, body, extraHeaders) + body = body or "" + local lines = { + string.format("HTTP/1.1 %d %s", status, reasons[status] or "OK"), + "Connection: close", + "Cache-Control: no-store", + "Content-Length: " .. #body, + } + if #body > 0 then + table.insert(lines, "Content-Type: application/json") + end + for name, value in pairs(extraHeaders or { }) do + table.insert(lines, name .. ": " .. value) + end + client:send(table.concat(lines, "\r\n") .. "\r\n\r\n" .. body) +end + +local function jsonError(id, code, message) + return json.encode({ jsonrpc = "2.0", id = id, error = { code = code, message = message } }) +end + +local function readRequest(client) + local line, err = client:receive("*l") + if not line then + return nil, err + end + local method, path = line:match("^(%u+)%s+(%S+)%s+HTTP/%d%.%d$") + if not method then + return nil, "malformed request line" + end + local headers = { } + while true do + local header, herr = client:receive("*l") + if not header then + return nil, herr + end + if header == "" then + break + end + local name, value = header:match("^([^:]+):%s*(.-)%s*$") + if name then + headers[name:lower()] = value + end + end + local body = "" + if (headers["transfer-encoding"] or ""):lower():find("chunked", 1, true) then + return { method = method, path = path, headers = headers, body = "", chunked = true } + end + local length = tonumber(headers["content-length"]) + if length and length > 0 then + if length > maxBodySize then + return { method = method, path = path, headers = headers, body = "", tooLarge = true } + end + body, err = client:receive(length) + if not body then + return nil, err + end + end + return { method = method, path = path, headers = headers, body = body } +end + +-- Only pages served from this machine may talk to us (DNS-rebinding protection from the MCP spec) +local function originAllowed(origin) + if not origin or origin == "" or origin == "null" then + return true + end + return origin:match("^https?://localhost[:/]?") ~= nil or origin:match("^https?://127%.0%.0%.1[:/]?") ~= nil or origin:match("^https?://%[::1%][:/]?") ~= nil +end + +-- Build sites whose links load_build accepts; the download has to happen here because the main +-- thread cannot block on the network. Mirrors Modules/BuildSiteTools.lua. +local buildSites = { + { "maxroll%.gg/poe/pob/([^%s?#]+)", "https://maxroll.gg/poe/api/pob/%s" }, + { "pob%.codes/b/([^%s?#]+)", "https://api.pob.codes/%s/raw" }, + { "pobb%.in/([^%s?#]+)", "https://pobb.in/pob/%s" }, + { "poe%.ninja/poe1/pob/(%w+)", "https://poe.ninja/poe1/pob/raw/%s" }, + { "poe%.ninja/pob/(%w+)", "https://poe.ninja/poe1/pob/raw/%s" }, + { "pastebin%.com/raw/(%w+)", "https://pastebin.com/raw/%s" }, + { "pastebin%.com/(%w+)", "https://pastebin.com/raw/%s" }, + { "pastebinp%.com/raw/(%w+)", "https://pastebinp.com/raw/%s" }, + { "pastebinp%.com/(%w+)", "https://pastebinp.com/raw/%s" }, + { "rentry%.co/(%w+)", "https://rentry.co/paste/%s/raw" }, + { "poedb%.tw/pob/([^%s?#/]+)", "https://poedb.tw/pob/%s/raw" }, +} + +local function rawBuildURL(url) + for _, site in ipairs(buildSites) do + local id = url:match(site[1]) + if id then + return string.format(site[2], id) + end + end + return url +end + +local function download(url) + local ok, curl = pcall(require, "lcurl.safe") + if not ok or type(curl) ~= "table" then + return nil, "downloading is not available in this runtime" + end + local easy = curl.easy() + easy:setopt_url(url) + easy:setopt(curl.OPT_FOLLOWLOCATION, 1) + easy:setopt(curl.OPT_TIMEOUT, 30) + easy:setopt_useragent("Path of Building MCP") + local chunks = { } + easy:setopt_writefunction(function(data) + table.insert(chunks, data) + return true + end) + local _, err = easy:perform() + local code = easy:getinfo(curl.INFO_RESPONSE_CODE) + easy:close() + if err then + return nil, err:msg() + elseif code ~= 200 then + return nil, "HTTP " .. tostring(code) + end + return table.concat(chunks) +end + +--- load_build with a URL: fetch it here and hand the main thread the text instead. +--- Returns the (possibly rewritten) body, or a complete JSON-RPC response when the download failed. +local function prefetchBuild(body) + local request = json.decode(body) + if type(request) ~= "table" or request.method ~= "tools/call" or type(request.params) ~= "table" or request.params.name ~= "load_build" then + return body + end + local args = request.params.arguments + if type(args) ~= "table" or type(args.source) ~= "string" then + return body + end + local url = args.source:match("^%s*(https?://%S+)%s*$") + if not url then + return body + end + MCPEvent("download", url) + local text, err = download(rawBuildURL(url)) + if not text then + local message = string.format("Could not download %s: %s", url, err) + return body, json.encode({ + jsonrpc = "2.0", + id = request.id, + result = { content = { { type = "text", text = message } }, isError = true }, + }) + end + args.source = text:match("^%s*(.-)%s*$") + args.source_url = url + return json.encode(request) +end + +local function handleClient(client) + local request, err = readRequest(client) + if not request then + if err ~= "closed" then + MCPEvent("error", "could not read request: " .. tostring(err)) + end + return + end + local method, path, headers = request.method, request.path, request.headers + path = path:gsub("%?.*$", "") + if not originAllowed(headers["origin"]) then + respond(client, 403, jsonError(nil, -32600, "Origin not allowed")) + return + end + if path ~= "/mcp" and path ~= "/mcp/" and path ~= "/" then + respond(client, 404, jsonError(nil, -32600, "Not found; the MCP endpoint is /mcp")) + return + end + if method == "OPTIONS" then + respond(client, 204, nil, { ["Allow"] = "POST, DELETE, OPTIONS" }) + return + elseif method == "GET" then + -- No server-initiated stream: every response is sent with the request's reply + respond(client, 405, jsonError(nil, -32600, "This server does not offer a server-to-client stream; send JSON-RPC requests with POST"), { ["Allow"] = "POST, DELETE, OPTIONS" }) + return + elseif method ~= "POST" and method ~= "DELETE" then + respond(client, 405, jsonError(nil, -32600, "Method not allowed"), { ["Allow"] = "POST, DELETE, OPTIONS" }) + return + end + if request.chunked then + respond(client, 411, jsonError(nil, -32600, "Chunked requests are not supported; send Content-Length")) + return + elseif request.tooLarge then + respond(client, 413, jsonError(nil, -32600, "Request body too large")) + return + end + local body = request.body + local prefetchError + if method == "POST" and #body > 0 then + body, prefetchError = prefetchBuild(body) + if prefetchError then + respond(client, 200, prefetchError) + return + end + end + local remote = select(1, client:getpeername()) or "?" + local status, responseBody, sessionId = MCPHandle(remote, headers["mcp-session-id"] or "", method, body) + if status == nil then + -- The main thread is shutting the server down + return "stop" + end + local extra = { } + if sessionId and sessionId ~= "" then + extra["Mcp-Session-Id"] = sessionId + end + respond(client, status, responseBody, extra) + MCPEvent("request", string.format("%s %s -> %d (%s)", method, path, status, (body:match('"method"%s*:%s*"([^"]+)"') or "-") .. (body:match('"name"%s*:%s*"([^"]+)"') and (" " .. body:match('"name"%s*:%s*"([^"]+)"')) or ""))) +end + +local server = assert(socket.tcp()) +server:setoption("reuseaddr", true) +local bound, bindErr = server:bind(host, port) +if not bound then + server:close() + error(string.format("Could not listen on %s:%d: %s", host, port, tostring(bindErr)), 0) +end +local listening, listenErr = server:listen(16) +if not listening then + server:close() + error(string.format("Could not listen on %s:%d: %s", host, port, tostring(listenErr)), 0) +end +-- Short accept timeout: AbortSubScript() can only stop this script between Lua statements +server:settimeout(0.1) +MCPEvent("listening", string.format("%s:%d", host, port)) + +while true do + local client = server:accept() + if client then + client:settimeout(5) + local ok, result = pcall(handleClient, client) + client:close() + if not ok then + MCPEvent("error", tostring(result)) + elseif result == "stop" then + break + end + end +end +server:close() +return "stopped" diff --git a/src/Modules/MCPStatus.lua b/src/Modules/MCPStatus.lua new file mode 100644 index 00000000000..eede9230989 --- /dev/null +++ b/src/Modules/MCPStatus.lua @@ -0,0 +1,436 @@ +-- Path of Building +-- +-- Module: MCP Status +-- Runs the in-app MCP server and keeps track of the AI clients using it. +-- +-- The server itself (MCPServer.lua) is a sub script: a tiny HTTP server on 127.0.0.1 that relays +-- every JSON-RPC request to this module through the MCPHandle() global. Requests are answered +-- here, on the main thread, by Modules/MCPTools.lua acting on the build open in the window. +-- The MCP panel (main:OpenMCPPopup) is the UI for the switch, the port and the client list. +-- +local dkjson = require("dkjson") +local ipairs = ipairs +local pairs = pairs +local t_insert = table.insert +local t_sort = table.sort +local m_floor = math.floor +local m_max = math.max +local os_time = os.time +local s_format = string.format + +local MCPStatus = { } + +MCPStatus.DEFAULT_PORT = 7390 +MCPStatus.HOST = "127.0.0.1" +MCPStatus.ACTIVE_SECONDS = 600 -- sessions silent for longer are shown as idle +MCPStatus.FORGET_SECONDS = 6 * 3600 -- ... and forgotten after this long +MCPStatus.PROTOCOL_VERSIONS = { ["2025-11-25"] = true, ["2025-06-18"] = true, ["2025-03-26"] = true } +MCPStatus.DEFAULT_PROTOCOL_VERSION = "2025-06-18" +MCPStatus.MAX_LOG = 50 + +function MCPStatus:Init() + self.tools = self.tools or LoadModule("Modules/MCPTools") + self.enabled = self.enabled or false + self.port = tonumber(self.port) or self.DEFAULT_PORT + self.sessions = self.sessions or { } + self.connections = self.connections or { } + self.log = self.log or { } + self.state = self.state or "stopped" -- stopped | starting | running | error + self.requestCount = self.requestCount or 0 + if self.enabled and self.state == "stopped" then + self:Start() + end +end + +-- Server lifecycle --------------------------------------------------------------- + +function MCPStatus:Log(line) + t_insert(self.log, os.date("%H:%M:%S ") .. line) + while #self.log > self.MAX_LOG do + table.remove(self.log, 1) + end + ConPrintf("MCP: %s", line) +end + +function MCPStatus:IsRunning() + return self.subId ~= nil and (self.state == "running" or self.state == "starting") +end + +function MCPStatus:Start() + if self.subId then + return true + end + local file = io.open("MCPServer.lua", "r") + if not file then + self.state = "error" + self.errMsg = "MCPServer.lua is missing" + self:Log(self.errMsg) + return false + end + local script = file:read("*a") + file:close() + self.port = tonumber(self.port) or self.DEFAULT_PORT + local id = LaunchSubScript(script, "MCPHandle", "MCPEvent", self.HOST, self.port) + if not id then + self.state = "error" + self.errMsg = "Sub scripts are not available in this runtime" + self:Log(self.errMsg) + return false + end + self.subId = id + self.state = "starting" + self.errMsg = nil + self.startedAt = os_time() + launch:RegisterSubScript(id, function(result, errMsg) + -- The server loop ended: either we stopped it, or it could not start (port in use) + self.subId = nil + if self.stopping then + self.stopping = nil + self.state = "stopped" + self:Log("server stopped") + else + self.state = "error" + self.errMsg = errMsg or (result and tostring(result)) or "server exited" + self:Log("server exited: " .. self.errMsg) + end + end) + -- A failed bind arrives through OnSubError, which goes to the DOWNLOAD/CUSTOM callback with (nil, errMsg) + self:Log(s_format("starting on %s:%d", self.HOST, self.port)) + return true +end + +function MCPStatus:Stop() + if not self.subId then + self.state = "stopped" + return + end + self.stopping = true + AbortSubScript(self.subId) + -- Aborting discards the sub script's finish callback, so finish up here + launch.subScripts[self.subId] = nil + self.subId = nil + self.stopping = nil + self.state = "stopped" + self:Log("server stopped") +end + +function MCPStatus:Restart() + self:Stop() + if self.enabled then + self:Start() + end +end + +--- The switch in the panel: starts or stops the server and remembers the choice (saved with the settings). +function MCPStatus:SetEnabled(enabled) + self.enabled = enabled and true or false + if self.enabled then + self:Start() + else + self:Stop() + end + main:SaveSettings() +end + +function MCPStatus:SetPort(port) + port = tonumber(port) + if not port or port < 1 or port > 65535 or port ~= m_floor(port) then + return false + end + if port ~= self.port then + self.port = port + if self.enabled then + self:Restart() + end + main:SaveSettings() + end + return true +end + +function MCPStatus:GetURL() + return s_format("http://%s:%d/mcp", self.HOST, self.port) +end + +--- Called by the sub script (fire and forget) with progress and errors. +function MCPStatus:OnEvent(kind, detail) + if kind == "listening" then + self.state = "running" + self.errMsg = nil + self:Log("listening on " .. tostring(detail)) + elseif kind == "error" then + self:Log("error: " .. tostring(detail)) + elseif kind == "download" then + self:Log("downloading " .. tostring(detail)) + elseif kind == "request" then + self:Log(tostring(detail)) + end +end + +-- Sessions --------------------------------------------------------------------------- + +local function randomId() + local chars = { } + for i = 1, 32 do + chars[i] = s_format("%x", math.random(0, 15)) + end + return table.concat(chars) +end + +function MCPStatus:NewSession(remote, clientInfo, protocolVersion) + local session = { + id = randomId(), + remote = remote, + client = clientInfo and clientInfo.name or nil, + version = clientInfo and clientInfo.version or nil, + title = clientInfo and clientInfo.title or nil, + protocolVersion = protocolVersion, + connected = os_time(), + lastActivity = os_time(), + requests = 0, + calls = 0, + } + self.sessions[session.id] = session + self:Log(s_format("client connected: %s %s (%s)", session.client or "unknown", session.version or "", remote)) + self:RebuildConnectionList() + return session +end + +function MCPStatus:TouchSession(session) + session.lastActivity = os_time() + session.requests = session.requests + 1 + local now = os_time() + for id, other in pairs(self.sessions) do + if now - other.lastActivity > self.FORGET_SECONDS then + self.sessions[id] = nil + end + end +end + +function MCPStatus:CloseSession(session) + session.closed = os_time() + self:Log(s_format("client disconnected: %s", session.client or session.id)) + self.sessions[session.id] = nil + self:RebuildConnectionList() +end + +--- Rows for the panel, most recently active first (same table instance, so list controls stay bound). +function MCPStatus:RebuildConnectionList() + wipeTable(self.connections) + for _, session in pairs(self.sessions) do + t_insert(self.connections, session) + end + t_sort(self.connections, function(a, b) + if a.lastActivity ~= b.lastActivity then + return a.lastActivity > b.lastActivity + end + return a.id < b.id + end) +end + +function MCPStatus:CountActive() + local count = 0 + local now = os_time() + for _, session in pairs(self.sessions) do + if now - session.lastActivity <= self.ACTIVE_SECONDS then + count = count + 1 + end + end + return count +end + +-- JSON-RPC ------------------------------------------------------------------------------ + +local function rpcError(id, code, message) + return { jsonrpc = "2.0", id = id, error = { code = code, message = message } } +end + +local function rpcResult(id, result) + return { jsonrpc = "2.0", id = id, result = result } +end + +--- Handles one JSON-RPC message; returns the response table (or nil for notifications) and the session. +function MCPStatus:HandleMessage(message, session, remote) + local object, array = self.tools.object, self.tools.array + if type(message) ~= "table" or message.jsonrpc ~= "2.0" or type(message.method) ~= "string" then + if type(message) == "table" and message.method == nil and (message.result ~= nil or message.error ~= nil) then + return nil, session -- a response to a request we never send + end + return rpcError(type(message) == "table" and message.id or nil, -32600, "Invalid JSON-RPC request"), session + end + local method, params, id = message.method, message.params or { }, message.id + local isNotification = id == nil + + if method == "initialize" then + local wanted = type(params.protocolVersion) == "string" and params.protocolVersion or nil + local version = (wanted and self.PROTOCOL_VERSIONS[wanted]) and wanted or self.DEFAULT_PROTOCOL_VERSION + session = self:NewSession(remote, type(params.clientInfo) == "table" and params.clientInfo or nil, version) + return rpcResult(id, object({ + protocolVersion = version, + capabilities = object({ tools = object({ listChanged = false }) }), + serverInfo = object({ name = "pob", title = "Path of Building", version = tostring(launch.versionNumber) }), + instructions = self.tools.INSTRUCTIONS, + })), session + end + if isNotification then + return nil, session -- notifications/initialized, notifications/cancelled, ... + end + if method == "ping" then + return rpcResult(id, object()), session + elseif method == "tools/list" then + return rpcResult(id, object({ tools = self.tools.List() })), session + elseif method == "tools/call" then + local name = params.name + if type(name) ~= "string" or not self.tools.Has(name) then + return rpcError(id, -32602, "Unknown tool '" .. tostring(name) .. "'"), session + end + if session then + session.calls = session.calls + 1 + session.lastTool = name + end + local ok, result = self.tools.Call(name, params.arguments) + if session then + session.lastError = (not ok) and result or nil + end + if ok then + local text = dkjson.encode(result) + return rpcResult(id, object({ + content = array({ object({ type = "text", text = text }) }), + structuredContent = result, + isError = false, + })), session + end + return rpcResult(id, object({ + content = array({ object({ type = "text", text = result }) }), + isError = true, + })), session + end + return rpcError(id, -32601, "Method not found: " .. method), session +end + +--- Entry point for the sub script: one HTTP request in, (status, body, sessionId) out. +function MCPStatus:Handle(remote, sessionId, httpMethod, body) + self.requestCount = self.requestCount + 1 + local session = sessionId ~= "" and self.sessions[sessionId] or nil + if sessionId ~= "" and not session then + return 404, dkjson.encode(rpcError(nil, -32001, "Unknown or expired session; initialize again")), "" + end + if httpMethod == "DELETE" then + if session then + self:CloseSession(session) + end + return 200, "", "" + end + if session then + self:TouchSession(session) + end + local message, _, decodeErr = dkjson.decode(body) + if message == nil then + return 400, dkjson.encode(rpcError(nil, -32700, "Parse error: " .. tostring(decodeErr))), "" + end + local responses + local isBatch = type(message) == "table" and getmetatable(message) and getmetatable(message).__jsontype == "array" or (type(message) == "table" and message[1] ~= nil and message.jsonrpc == nil) + if isBatch then + responses = self.tools.array() + for _, item in ipairs(message) do + local response + response, session = self:HandleMessage(item, session, remote) + if response then + t_insert(responses, response) + end + end + if #responses == 0 then + responses = nil + end + else + responses, session = self:HandleMessage(message, session, remote) + end + local newSessionId = (session and sessionId == "" ) and session.id or "" + if responses == nil then + return 202, "", newSessionId + end + self:RebuildConnectionList() + return 200, dkjson.encode(responses), newSessionId +end + +-- Globals the sub script calls ------------------------------------------------------ + +--- Blocking call from the server sub script; must never raise (that would surface as a PoB error prompt). +function MCPHandle(remote, sessionId, httpMethod, body) + local mcp = main and main.mcp + if not mcp then + return 500, '{"jsonrpc":"2.0","id":null,"error":{"code":-32603,"message":"MCP is not initialised"}}', "" + end + local ok, status, responseBody, newSessionId = pcall(mcp.Handle, mcp, tostring(remote or "?"), tostring(sessionId or ""), tostring(httpMethod or "POST"), tostring(body or "")) + if not ok then + mcp:Log("internal error: " .. tostring(status)) + return 500, dkjson.encode(rpcError(nil, -32603, "Internal error: " .. tostring(status))), "" + end + return status, responseBody, newSessionId or "" +end + +function MCPEvent(kind, detail) + if main and main.mcp then + main.mcp:OnEvent(kind, detail) + end +end + +-- Display helpers ------------------------------------------------------------------------ + +function MCPStatus.FormatAge(seconds) + if not seconds then + return "-" + end + seconds = m_max(m_floor(seconds), 0) + if seconds < 60 then + return seconds .. "s ago" + elseif seconds < 3600 then + return m_floor(seconds / 60) .. "m ago" + elseif seconds < 86400 then + return m_floor(seconds / 3600) .. "h ago" + end + return m_floor(seconds / 86400) .. "d ago" +end + +function MCPStatus.FormatTime(epoch) + if not epoch then + return "-" + end + return os.date("%Y-%m-%d %H:%M:%S", m_floor(epoch)) +end + +--- Label for the button in the main bar. +function MCPStatus:GetButtonLabel() + if self.state == "error" then + return "^1MCP: error" + elseif not self:IsRunning() then + return "^8MCP: off" + end + local active = self:CountActive() + if active > 0 then + return s_format("^x50E050MCP: %d", active) + end + return "^7MCP: on" +end + +--- Status line shown in the panel. +function MCPStatus:GetStatusText() + if self.state == "error" then + return "^1" .. (self.errMsg or "The server could not start.") + elseif self.state == "starting" then + return "^7Starting on " .. self:GetURL() .. " ..." + elseif not self:IsRunning() then + return "^8Off. AI assistants cannot reach this window." + end + local active = self:CountActive() + if active == 0 then + return "^7Listening on ^x4040FF" .. self:GetURL() .. "^7, waiting for a client." + end + return s_format("^7Listening on ^x4040FF%s^7, ^x50E050%d^7 active client%s.", self:GetURL(), active, active == 1 and "" or "s") +end + +--- Command that registers this window's server with Claude Code. +function MCPStatus:GetSetupCommand() + return "claude mcp add --transport http pob " .. self:GetURL() +end + +return MCPStatus diff --git a/src/Modules/MCPTools.lua b/src/Modules/MCPTools.lua new file mode 100644 index 00000000000..f590e713bfa --- /dev/null +++ b/src/Modules/MCPTools.lua @@ -0,0 +1,1562 @@ +-- Path of Building +-- +-- Module: MCP Tools +-- The tools the in-app MCP server (see Modules/MCPStatus.lua and MCPServer.lua) offers to AI +-- assistants. They run on the main thread and act on the build that is open in this window, +-- exactly as if the user had made the change: the tree, items, skills and config tabs update, +-- the build is marked unsaved, and every mutating tool reports which sidebar stats moved. +-- +local dkjson = require("dkjson") +local ipairs = ipairs +local pairs = pairs +local t_insert = table.insert +local t_remove = table.remove +local t_sort = table.sort +local t_concat = table.concat +local m_min = math.min +local m_max = math.max +local m_abs = math.abs +local s_format = string.format + +local mcpTools = { } + +------------------------------------------------------------------------------ +-- JSON helpers +------------------------------------------------------------------------------ + +local objectMeta = { __jsontype = "object" } +local arrayMeta = { __jsontype = "array" } + +local function object(t) + return setmetatable(t or { }, objectMeta) +end +local function array(t) + return setmetatable(t or { }, arrayMeta) +end +mcpTools.object = object +mcpTools.array = array + +local function stripColors(s) + if type(s) ~= "string" then + return s + end + return (s:gsub("%^x%x%x%x%x%x%x", ""):gsub("%^%d", "")) +end + +--- Converts an arbitrary Lua value into something dkjson can encode (no functions, cycles, NaN). +local function sanitize(value, depth, seen) + depth = depth or 0 + local t = type(value) + if t == "number" then + if value ~= value then + return nil + elseif value == math.huge then + return "inf" + elseif value == -math.huge then + return "-inf" + end + return value + elseif t == "string" or t == "boolean" or t == "nil" then + return value + elseif t == "table" then + if depth > 6 then + return "" + end + seen = seen or { } + if seen[value] then + return "" + end + seen[value] = true + local n = 0 + local isArray = true + for k in pairs(value) do + n = n + 1 + if type(k) ~= "number" or k < 1 or k % 1 ~= 0 then + isArray = false + end + end + isArray = isArray and n == #value + local mt = getmetatable(value) + if mt and mt.__jsontype then + isArray = mt.__jsontype == "array" + end + local out + if isArray then + out = array() + for i = 1, n do + out[i] = sanitize(value[i], depth + 1, seen) + if out[i] == nil then + out[i] = dkjson.null + end + end + else + out = object() + for k, v in pairs(value) do + local sv = sanitize(v, depth + 1, seen) + if sv ~= nil then + out[tostring(k)] = sv + end + end + end + seen[value] = nil + return out + end + return nil +end +mcpTools.sanitize = sanitize + +local function scalarsOnly(tbl) + local out = object() + for k, v in pairs(tbl) do + local t = type(v) + if t == "number" or t == "string" or t == "boolean" then + out[tostring(k)] = sanitize(v) + end + end + return out +end + +local function pick(tbl, keys) + local out = object() + for _, k in ipairs(keys) do + if tbl[k] ~= nil then + out[k] = sanitize(tbl[k]) + end + end + return out +end + +------------------------------------------------------------------------------ +-- Talking to the open build +------------------------------------------------------------------------------ + +local function currentBuild() + local b = main.modes["BUILD"] + if main.mode == "BUILD" and b and b.spec then + return b + end + return nil +end + +local function build() + local b = currentBuild() + if not b then + error("No build is open in Path of Building. Use open_build, load_build or new_build first.") + end + return b +end + +--- Whether the build has unsaved changes. Build:OnFrame caches this in b.unsaved once per frame; +--- tools run between frames, so evaluate it fresh (same expression as Build:OnFrame). +local function isUnsaved(b) + local unsaved = b.modFlag or b.notesTab.modFlag or b.partyTab.modFlag or b.configTab.modFlag or b.treeTab.modFlag or b.treeTab.searchFlag or b.spec.modFlag or b.skillsTab.modFlag or b.itemsTab.modFlag or b.calcsTab.modFlag + b.unsaved = unsaved + return unsaved and true or false +end + +--- Performs any pending mode switch (main:SetMode only schedules one) right now. +local function applyModeChange() + main:ProcessModeChange() + if launch.promptMsg then + error("Path of Building reported an error: " .. stripColors(tostring(launch.promptMsg))) + end +end + +--- Closes popups that a tool caused PoB to open (confirmations, errors) and returns their text. +--- Popups the user had open before the tool ran are left alone. +local function drainNewPopups(popupsBefore) + local msgs = array() + local guard = 0 + while #main.popups > popupsBefore and guard < 20 do + local popup = main.popups[1] + local text = { } + if popup.title then + t_insert(text, stripColors(tostring(popup.title))) + end + if popup.controls then + for _, control in pairs(popup.controls) do + if type(control) == "table" and type(control.label) == "string" then + t_insert(text, stripColors(control.label)) + end + end + end + t_insert(msgs, t_concat(text, " | ")) + main:ClosePopup() + guard = guard + 1 + end + return msgs +end + +--- Recomputes the build now (what Build:OnFrame does when buildFlag is set) so results are current. +local function recalc(b) + wipeGlobalCache() + b.outputRevision = (b.outputRevision or 0) + 1 + b.buildFlag = false + b.skillsTab:UpdateSocketGroups() + b.calcsTab:BuildOutput() + b:RefreshStatList() + b.configTab.calcFunc, b.configTab.calcBase = b.calcsTab:GetMiscCalculator() + if launch.promptMsg then + error("Path of Building reported an error: " .. stripColors(tostring(launch.promptMsg))) + end +end + +local function statLabels(b) + local labels = { } + for _, statData in ipairs(b.displayStats) do + if statData.stat and not labels[statData.stat] then + labels[statData.stat] = stripColors(statData.label) + end + end + return labels +end + +local function snapshot(b) + local snap = { } + local output = b.calcsTab.mainOutput + for _, statData in ipairs(b.displayStats) do + if statData.stat and not statData.childStat then + local v = output[statData.stat] + if type(v) == "number" then + snap[statData.stat] = v + end + end + end + return snap +end + +local function diff(b, before, after) + local labels = statLabels(b) + local changes = array() + local keys = { } + for k in pairs(before) do keys[k] = true end + for k in pairs(after) do keys[k] = true end + local sorted = { } + for k in pairs(keys) do t_insert(sorted, k) end + t_sort(sorted) + for _, k in ipairs(sorted) do + local a, c = before[k], after[k] + if (a or c) and a ~= c and (a == nil or c == nil or m_abs(a - c) > 0.001) then + t_insert(changes, object({ + stat = k, + label = labels[k] or k, + before = a == nil and dkjson.null or sanitize(a), + after = c == nil and dkjson.null or sanitize(c), + delta = (a and c) and sanitize(c - a) or nil, + })) + end + end + return changes +end + +--- Runs a mutation on the open build, recomputes, marks the build unsaved and reports stat changes. +local function mutate(fn) + local b = build() + local popupsBefore = #main.popups + local before = snapshot(b) + local result = fn(b) or object() + b.modFlag = true + recalc(b) + local popups = drainNewPopups(popupsBefore) + result.changes = diff(b, before, snapshot(b)) + if #popups > 0 then + result.popups = popups + end + return result +end + +local function requireNoUnsavedChanges(args) + local b = currentBuild() + if b and isUnsaved(b) and not args.force then + error(s_format("The open build '%s' has unsaved changes. Save it in Path of Building first, or pass force=true to discard them.", b.buildName or "Unnamed build")) + end + if b then + -- Don't let dev-mode autosave write a ~~temp~~ file for a build the caller chose to discard + b.abortSave = true + end +end + +------------------------------------------------------------------------------ +-- Info builders +------------------------------------------------------------------------------ + +local function keyStats(b) + return pick(b.calcsTab.mainOutput, { + "TotalDPS", "CombinedDPS", "FullDPS", "TotalDotDPS", "WithPoisonDPS", "WithIgniteDPS", "WithBleedDPS", + "AverageHit", "AverageDamage", "Speed", "HitChance", "CritChance", "CritMultiplier", "ManaCost", "LifeCost", + "Life", "LifeUnreserved", "LifeRegenRecovery", "EnergyShield", "EnergyShieldRegenRecovery", "Mana", "ManaUnreserved", "ManaRegenRecovery", + "Armour", "Evasion", "Spirit", "Ward", "TotalEHP", "PhysicalMaximumHitTaken", "FireMaximumHitTaken", "ColdMaximumHitTaken", "LightningMaximumHitTaken", "ChaosMaximumHitTaken", + "FireResist", "ColdResist", "LightningResist", "ChaosResist", "FireResistOverCap", "ColdResistOverCap", "LightningResistOverCap", "ChaosResistOverCap", + "BlockChance", "SpellBlockChance", "AttackDodgeChance", "SpellDodgeChance", "SpellSuppressionChance", "EffectiveMovementSpeedMod", + "Str", "Dex", "Int", "ReqStr", "ReqDex", "ReqInt", + }) +end + +local function summary() + local b = build() + local spec = b.spec + local env = b.calcsTab.mainEnv + local used, ascUsed = spec:CountAllocNodes() + local mainSkill = env and env.player and env.player.mainSkill + local mainSkillName + if mainSkill and mainSkill.activeEffect and mainSkill.activeEffect.grantedEffect then + mainSkillName = mainSkill.activeEffect.grantedEffect.name + end + local warnings = array() + for _, line in ipairs(b.controls.warnings and b.controls.warnings.lines or { }) do + t_insert(warnings, stripColors(line)) + end + return object({ + name = b.buildName, + file = b.dbFileName or nil, + unsaved = isUnsaved(b), + class = spec.curClassName, + ascendancy = spec.curAscendClassName, + secondaryAscendancy = spec.curSecondaryAscendClassName, + level = b.characterLevel, + levelAutoMode = b.characterLevelAutoMode, + bandit = b.bandit, + pantheonMajorGod = b.pantheonMajorGod, + pantheonMinorGod = b.pantheonMinorGod, + treeVersion = spec.treeVersion, + passivePointsUsed = used, + ascendancyPointsUsed = ascUsed, + pointsDisplay = b.controls.pointDisplay and stripColors(b.controls.pointDisplay.str) or nil, + mainSocketGroup = b.mainSocketGroup, + mainSkill = mainSkillName, + mainSkillInfo = mainSkill and stripColors(mainSkill.infoMessage) or nil, + keyStats = keyStats(b), + warnings = warnings, + }) +end + +local function findNode(spec, ref) + local id = tonumber(ref) + if id then + local node = spec.nodes[id] + if not node then + error("No passive node with id " .. tostring(id)) + end + return node + end + local needle = tostring(ref):lower() + local exact, partial = { }, { } + for _, node in pairs(spec.nodes) do + local name = (node.dn or node.name or ""):lower() + if name == needle then + t_insert(exact, node) + elseif name:find(needle, 1, true) then + t_insert(partial, node) + end + end + local candidates = #exact > 0 and exact or partial + if #candidates == 0 then + error("No passive node matching '" .. tostring(ref) .. "'") + end + t_sort(candidates, function(a, c) + local ra, rb = a.path and 1 or 0, c.path and 1 or 0 + if ra ~= rb then return ra > rb end + local wa = (a.type == "Keystone" or a.type == "Notable") and 1 or 0 + local wb = (c.type == "Keystone" or c.type == "Notable") and 1 or 0 + if wa ~= wb then return wa > wb end + return a.id < c.id + end) + if #candidates > 1 and candidates[1].type ~= "Keystone" and candidates[1].type ~= "Notable" and #exact == 0 then + local names = { } + for i = 1, m_min(#candidates, 8) do + t_insert(names, s_format("%s (%d)", candidates[i].dn or "?", candidates[i].id)) + end + error("Ambiguous node '" .. tostring(ref) .. "'; candidates: " .. t_concat(names, ", ") .. ". Use the numeric id.") + end + return candidates[1] +end + +local function nodeInfo(node, withStats) + local info = object({ + id = node.id, + name = node.dn or node.name, + type = node.type, + allocated = node.alloc and true or false, + ascendancy = node.ascendancyName, + reachable = node.path ~= nil, + pathCost = node.path and (#node.path - (node.alloc and 1 or 0)) or nil, + }) + if withStats and node.sd then + local stats = array() + for _, line in ipairs(node.sd) do + t_insert(stats, stripColors(line)) + end + info.stats = stats + end + return info +end + +local function gemInfo(gemInstance) + local gemData = gemInstance.gemData + local grantedEffect = gemData and gemData.grantedEffect + return object({ + nameSpec = gemInstance.nameSpec, + name = gemData and gemData.name or nil, + level = gemInstance.level, + quality = gemInstance.quality, + qualityId = gemInstance.qualityId, + enabled = gemInstance.enabled and true or false, + count = gemInstance.count or 1, + isSupport = grantedEffect and grantedEffect.support or false, + recognised = gemData ~= nil, + errMsg = gemInstance.errMsg, + }) +end + +local function socketGroupInfo(index, group, mainIndex) + local gems = array() + for _, gemInstance in ipairs(group.gemList) do + t_insert(gems, gemInfo(gemInstance)) + end + return object({ + index = index, + label = group.label ~= "" and group.label or nil, + displayLabel = stripColors(group.displayLabel), + slot = group.slot, + source = group.source, + enabled = group.enabled and true or false, + includeInFullDPS = group.includeInFullDPS and true or false, + isMainSkill = index == mainIndex, + mainActiveSkill = group.mainActiveSkill, + gems = gems, + }) +end + +local function itemInfo(itemsTab, item) + local slots = array() + for _, slot in ipairs(itemsTab.orderedSlots) do + if slot.selItemId == item.id then + t_insert(slots, slot.slotName) + end + end + return object({ + id = item.id, + name = item.name, + title = item.title, + baseName = item.baseName, + type = item.type, + rarity = item.rarity, + itemLevel = item.itemLevel, + quality = item.quality, + corrupted = item.corrupted and true or false, + equippedIn = slots, + }) +end + +------------------------------------------------------------------------------ +-- Share codes and build files +------------------------------------------------------------------------------ + +local function decodeShareCode(code) + code = code:gsub("%s", "") + local xml = Inflate(common.base64.decode((code:gsub("-", "+"):gsub("_", "/")))) + if not xml or not xml:match("^%s*<") then + error("Not a valid Path of Building share code") + end + return xml +end + +local function encodeShareCode(xml) + return (common.base64.encode(Deflate(xml)):gsub("+", "-"):gsub("/", "_")) +end + +local function readFile(path) + local file = io.open(path, "r") + if not file then + return nil + end + local text = file:read("*a") + file:close() + return text +end + +local function isAbsolutePath(path) + return path:match("^[\\/]") or path:match("^%a:[\\/]") or path:match("^~") +end + +local function buildFilePath(name) + name = name:gsub("^[\\/]+", "") + if not name:lower():match("%.xml$") then + name = name .. ".xml" + end + if isAbsolutePath(name) then + return name + end + return main.buildPath .. name +end + +--- Turns the `source` of load_build into build XML: raw XML, a share code, a file path, or text +--- the server sub script downloaded from a build site. +local function resolveBuildXML(source) + local text = tostring(source or ""):match("^%s*(.-)%s*$") + assert(#text > 0, "source is empty") + if text:match("^<") then + return text, "xml" + end + if text:match("^https?://") then + error("Could not download '" .. text .. "'. Pass the share code or the build XML instead.") + end + local fileText = readFile(text) or readFile(buildFilePath(text)) + if fileText then + fileText = fileText:match("^%s*(.-)%s*$") + if fileText:match("^<") then + return fileText, text + end + return decodeShareCode(fileText), text + end + if text:find("[/ ]") then + error("'" .. text .. "' is neither an existing file, a URL, nor a share code") + end + return decodeShareCode(text), "share code" +end + +------------------------------------------------------------------------------ +-- Tool definitions +------------------------------------------------------------------------------ + +local tools = { } +local toolsByName = { } + +--- Registers a tool. `properties` is the JSON-schema property map; `handler(args)` returns a JSON-able table. +local function def(name, description, properties, required, handler) + local schema = object({ type = "object", properties = object(properties or { }) }) + if required and #required > 0 then + schema.required = array(required) + end + for _, prop in pairs(schema.properties) do + setmetatable(prop, objectMeta) + if prop.enum then + setmetatable(prop.enum, arrayMeta) + end + end + local tool = { name = name, description = description, inputSchema = schema, handler = handler } + t_insert(tools, tool) + toolsByName[name] = tool +end + +local STR = function(desc) return { type = "string", description = desc } end +local INT = function(desc) return { type = "integer", description = desc } end +local NUM = function(desc) return { type = "number", description = desc } end +local BOOL = function(desc) return { type = "boolean", description = desc } end + +-- Builds --------------------------------------------------------------------- + +def("get_build_summary", + "Class, ascendancy, level, main skill, passive points, warnings and the key offensive/defensive stats of the build open in Path of Building.", + { }, nil, + function() + return summary() + end) + +def("list_builds", + "The build files in Path of Building's Builds folder (optionally a sub-folder), for open_build.", + { folder = STR("Sub-folder of the Builds folder to list (default: the root)") }, nil, + function(args) + local sub = tostring(args.folder or ""):gsub("^[\\/]+", "") + if #sub > 0 and not sub:match("[\\/]$") then + sub = sub .. "/" + end + local builds = array() + local handle = NewFileSearch(main.buildPath .. sub .. "*.xml") + while handle do + local fileName = handle:GetFileName() + t_insert(builds, object({ + name = fileName:gsub("%.xml$", ""), + path = sub .. fileName, + modified = handle:GetFileModifiedTime(), + })) + if not handle:NextFile() then + break + end + end + local folders = array() + handle = NewFileSearch(main.buildPath .. sub .. "*", true) + while handle do + t_insert(folders, sub .. handle:GetFileName()) + if not handle:NextFile() then + break + end + end + t_sort(builds, function(a, c) return a.name:lower() < c.name:lower() end) + local open = currentBuild() + return object({ + buildsFolder = main.buildPath, + folders = folders, + builds = builds, + openBuild = open and object({ name = open.buildName, file = open.dbFileName or nil, unsaved = isUnsaved(open) }) or nil, + }) + end) + +def("open_build", + "Open a saved build from the Builds folder in the Path of Building window (name or relative path from list_builds). Refuses to replace a build with unsaved changes unless force=true.", + { name = STR("Build name or path relative to the Builds folder, e.g. \"My Build\" or \"Folder/My Build.xml\""), force = BOOL("Discard unsaved changes of the currently open build") }, { "name" }, + function(args) + local fullPath = buildFilePath(tostring(args.name)) + if not readFile(fullPath) then + error("No build file at '" .. fullPath .. "'") + end + requireNoUnsavedChanges(args) + local buildName = fullPath:match("([^\\/]+)%.xml$") + main:SetMode("BUILD", fullPath, buildName) + local popupsBefore = #main.popups + applyModeChange() + local popups = drainNewPopups(popupsBefore) + if not currentBuild() then + error("Build could not be opened" .. (#popups > 0 and (": " .. t_concat(popups, "; ")) or "")) + end + local result = summary() + if #popups > 0 then + result.popups = popups + end + return result + end) + +def("load_build", + "Load a build into the Path of Building window from a share code, raw build XML, a .xml file path, or a pobb.in / pob.codes / poe.ninja / maxroll.gg / pastebin / rentry / poedb link. " + .. "The build opens unsaved (use save_build to keep it). Refuses to replace a build with unsaved changes unless force=true.", + { source = STR("Share code, build XML, file path, or build-site URL"), name = STR("Name for the new build (default: derived from the source)"), force = BOOL("Discard unsaved changes of the currently open build") }, { "source" }, + function(args) + local xml, origin = resolveBuildXML(args.source) + requireNoUnsavedChanges(args) + local name = args.name + if not name or name == "" then + if origin ~= "xml" and origin ~= "share code" and not origin:match("^https?://") then + name = origin:match("([^\\/]+)%.xml$") or origin:match("([^\\/]+)$") + else + name = "Imported build" + end + end + main:SetMode("BUILD", false, name, xml) + local popupsBefore = #main.popups + applyModeChange() + local popups = drainNewPopups(popupsBefore) + if not currentBuild() then + error("Build could not be loaded" .. (#popups > 0 and (": " .. t_concat(popups, "; ")) or "")) + end + local result = summary() + result.source = args.source_url or origin + if #popups > 0 then + result.popups = popups + end + return result + end) + +def("new_build", + "Start a new, empty build (level 1, no items, no skills) in the Path of Building window; optionally pick class and ascendancy. Refuses to replace a build with unsaved changes unless force=true.", + { class_name = STR("Character class, e.g. \"Witch\""), ascendancy = STR("Ascendancy class, e.g. \"Necromancer\""), name = STR("Build name"), force = BOOL("Discard unsaved changes of the currently open build") }, nil, + function(args) + requireNoUnsavedChanges(args) + main:SetMode("BUILD", false, args.name or "New build") + local popupsBefore = #main.popups + applyModeChange() + drainNewPopups(popupsBefore) + local result = summary() + if args.class_name then + result = toolsByName.set_class.handler({ class_name = args.class_name, ascendancy = args.ascendancy }) + end + return result + end) + +def("close_build", + "Close the open build and return to Path of Building's build list. Refuses when there are unsaved changes unless force=true.", + { force = BOOL("Discard unsaved changes") }, nil, + function(args) + local b = build() + requireNoUnsavedChanges(args) + local name = b.buildName + b:CloseBuild() + applyModeChange() + return object({ closed = name }) + end) + +def("save_build", + "Save the open build. Without `path` it is written to its own file (the build must have been saved before); with `path` it is written there (relative paths go into the Builds folder) and the window switches to that file.", + { path = STR("Target file, e.g. \"My Build\" or \"Folder/My Build.xml\"") }, nil, + function(args) + local b = build() + if args.path and args.path ~= "" then + local fullPath = buildFilePath(tostring(args.path)) + local folder = fullPath:match("^(.*)[\\/][^\\/]+$") + if folder then + MakeDir(folder) + end + b.dbFileName = fullPath + b.buildName = fullPath:match("([^\\/]+)%.xml$") + b.dbFileSubPath = fullPath:sub(1, #main.buildPath) == main.buildPath and (fullPath:sub(#main.buildPath + 1):match("^(.*[\\/])") or "") or b.dbFileSubPath + b.spec:SetWindowTitleWithBuildClass() + elseif not b.dbFileName then + error("This build has never been saved; pass `path` to choose a file name") + end + local popupsBefore = #main.popups + local failed = b:SaveDBFile() + local popups = drainNewPopups(popupsBefore) + if failed then + error("Could not save the build" .. (#popups > 0 and (": " .. t_concat(popups, "; ")) or "")) + end + return object({ path = b.dbFileName, name = b.buildName }) + end) + +def("export_build", + "Export the open build as a share code (format=\"code\", for pobb.in or PoB's Import tab) or as XML (format=\"xml\").", + { format = { type = "string", enum = { "code", "xml" }, description = "\"code\" (default) or \"xml\"" } }, nil, + function(args) + local b = build() + local xml = b:SaveDB("export") + if not xml then + error("Could not serialise the build") + end + if args.format == "xml" then + return object({ xml = xml }) + end + return object({ code = encodeShareCode(xml) }) + end) + +def("decode_build_code", + "Decode a PoB share code into its XML without loading it.", + { code = STR("The share code") }, { "code" }, + function(args) + return object({ xml = decodeShareCode(tostring(args.code or "")) }) + end) + +def("set_class", + "Change the character class and/or ascendancy of the open build. The tree is kept when the new class start is connected; otherwise PoB connects it along the shortest path.", + { class_name = STR("Character class"), ascendancy = STR("Ascendancy class") }, nil, + function(args) + return mutate(function(b) + local spec = b.spec + if args.class_name then + local classId + for id, class in pairs(spec.tree.classes) do + if class.name:lower() == tostring(args.class_name):lower() then + classId = id + end + end + assert(classId, "Unknown class '" .. tostring(args.class_name) .. "'") + if classId ~= spec.curClassId then + if spec:CountAllocNodes() == 0 or spec:IsClassConnected(classId) then + spec:SelectClass(classId) + elseif spec:ConnectToClass(classId) then + spec:SelectClass(classId) + else + error("Cannot change class without resetting the tree; use reset_passive_tree first") + end + end + end + if args.ascendancy then + local ascendId + local class = spec.tree.classes[spec.curClassId] + for id, ascend in pairs(class.classes) do + if ascend.name:lower() == tostring(args.ascendancy):lower() then + ascendId = id + end + end + assert(ascendId, "Unknown ascendancy '" .. tostring(args.ascendancy) .. "' for class " .. class.name) + spec:SelectAscendClass(ascendId) + end + spec:AddUndoState() + return summary() + end) + end) + +def("set_character", + "Set character level (1-100, turns off auto level unless level_auto=true), bandit choice (\"None\", \"Oak\", \"Kraityn\", \"Alira\") and pantheon gods (e.g. \"TheBrineKing\", \"Lunaris\", \"Solaris\", \"Arakaali\", \"Gruthkul\", \"Yugul\", \"Abberath\", \"Tukohama\", \"Garukhan\", \"Ralakesh\", \"Ryslatha\", \"Shakari\").", + { level = INT("Character level 1-100"), level_auto = BOOL("Let PoB pick the level from the allocated points"), bandit = STR("Bandit choice"), pantheon_major_god = STR("Major god"), pantheon_minor_god = STR("Minor god") }, nil, + function(args) + return mutate(function(b) + if args.level ~= nil then + local level = tonumber(args.level) + assert(level and level >= 1 and level <= 100, "level must be between 1 and 100") + b.characterLevel = level + b.characterLevelAutoMode = false + if b.controls.characterLevel then + b.controls.characterLevel:SetText(tostring(level)) + end + end + if args.level_auto ~= nil then + b.characterLevelAutoMode = args.level_auto and true or false + end + local map = { bandit = "bandit", pantheon_major_god = "pantheonMajorGod", pantheon_minor_god = "pantheonMinorGod" } + for argKey, key in pairs(map) do + if args[argKey] ~= nil then + b[key] = tostring(args[argKey]) + b.configTab.input[key] = tostring(args[argKey]) + end + end + b.configTab:BuildModList() + return summary() + end) + end) + +-- Stats ---------------------------------------------------------------------- + +def("get_stats", + "Raw calculated stats from PoB's output table. Pass `names` (e.g. [\"TotalDPS\", \"Life\", \"FireResist\"]) for specific values, or omit to get every scalar stat (several hundred). `actor` may be \"player\" or \"minion\".", + { names = { type = "array", items = { type = "string" }, description = "Stat names to return" }, actor = { type = "string", enum = { "player", "minion" } } }, nil, + function(args) + local b = build() + local env = b.calcsTab.mainEnv + local actor = env.player + if args.actor == "minion" then + actor = env.minion + if not actor then + error("This build has no minion actor") + end + end + local output = actor.output + if args.names then + local out = object() + local missing = array() + for _, name in ipairs(args.names) do + local v = output[name] + if v == nil then + t_insert(missing, name) + else + out[name] = sanitize(v) + end + end + return object({ stats = out, missing = missing }) + end + return object({ stats = scalarsOnly(output) }) + end) + +def("get_sidebar_stats", + "The stats exactly as PoB shows them in the left-hand sidebar (formatted strings, in display order).", + { }, nil, + function() + local b = build() + local lines = array() + for _, line in ipairs(b.controls.statBox.list) do + if type(line[1]) == "string" then + t_insert(lines, object({ + label = (stripColors(line[1]):gsub(":%s*$", "")), + value = stripColors(line[2]), + })) + end + end + return object({ lines = lines }) + end) + +def("get_breakdown", + "PoB's calculation breakdown for one stat (what the Calcs tab shows on hover), e.g. \"Life\", \"TotalDPS\", \"Speed\", \"Armour\". PoB only builds a breakdown for stats that modifiers affect; the error lists similar keys when one is missing.", + { stat = STR("Stat name"), actor = { type = "string", enum = { "player", "minion" } } }, { "stat" }, + function(args) + local b = build() + local env = b.calcsTab.calcsEnv + local actor = args.actor == "minion" and env.minion or env.player + if not actor or not actor.breakdown then + error("No breakdown data available") + end + local data = actor.breakdown[args.stat] + if data == nil then + local needle = tostring(args.stat):lower() + local similar = { } + for key in pairs(actor.breakdown) do + if type(key) == "string" and key:lower():find(needle, 1, true) then + t_insert(similar, key) + end + end + t_sort(similar) + local hint = #similar > 0 and (" Similar breakdowns: " .. t_concat(similar, ", ", 1, m_min(#similar, 15))) + or " (PoB only generates a breakdown when modifiers affect the stat.)" + error("No breakdown for stat '" .. tostring(args.stat) .. "'." .. hint) + end + local function strip(v) + if type(v) == "string" then + return stripColors(v) + elseif type(v) == "table" then + local out = { } + for k, x in pairs(v) do + out[k] = strip(x) + end + return out + end + return v + end + local mainActor = args.actor == "minion" and b.calcsTab.mainEnv.minion or b.calcsTab.mainEnv.player + return object({ + stat = args.stat, + value = sanitize(actor.output[args.stat]), + sidebarValue = mainActor and sanitize(mainActor.output[args.stat]) or nil, + breakdown = sanitize(strip(data)), + }) + end) + +-- Skills --------------------------------------------------------------------- + +def("list_skills", + "All socket groups (skill setups) with their gems, levels, qualities and which one is the main skill.", + { }, nil, + function() + local b = build() + local groups = array() + for i, group in ipairs(b.skillsTab.socketGroupList) do + t_insert(groups, socketGroupInfo(i, group, b.mainSocketGroup)) + end + return object({ mainSocketGroup = b.mainSocketGroup, groups = groups }) + end) + +def("set_main_skill", + "Select which socket group (1-based index from list_skills) is the main skill used for DPS; `active_skill` picks the active gem within the group when it has several.", + { index = INT("Socket group index"), active_skill = INT("Active skill index within the group") }, { "index" }, + function(args) + return mutate(function(b) + local index = tonumber(args.index) + assert(index and b.skillsTab.socketGroupList[index], "Invalid socket group index") + b.mainSocketGroup = index + if args.active_skill then + b.skillsTab.socketGroupList[index].mainActiveSkill = tonumber(args.active_skill) + end + return object({ mainSocketGroup = index }) + end) + end) + +def("add_skill_group", + "Add a socket group. `gems` is one gem per line as \" /\" (e.g. \"Fireball 21/20\"; the Support suffix is optional). `slot` (e.g. \"Body Armour\", \"Weapon 1\") links the group to an item slot.", + { gems = STR("Gem lines"), slot = STR("Item slot"), label = STR("Group label"), make_main = BOOL("Make this the main skill group") }, { "gems" }, + function(args) + assert(type(args.gems) == "string", "gems text is required") + local lines = { } + for line in args.gems:gmatch("[^\r\n]+") do + line = line:match("^%s*(.-)%s*$") + if #line > 0 then + if line:match("^[ %a']+ %d+/%d+$") then + line = line .. " 1" + elseif line:match("^[ %a']+$") then + line = line .. " 20/0 1" + end + t_insert(lines, line) + end + end + if args.slot then + t_insert(lines, 1, "Slot: " .. args.slot) + end + if args.label then + t_insert(lines, 1, "Label: " .. args.label) + end + local text = t_concat(lines, "\n") .. "\n" + return mutate(function(b) + local count = #b.skillsTab.socketGroupList + b.skillsTab:PasteSocketGroup(text) + if #b.skillsTab.socketGroupList == count then + error("No gems were recognised in the supplied text") + end + local index = #b.skillsTab.socketGroupList + if args.make_main then + b.mainSocketGroup = index + end + local group = b.skillsTab.socketGroupList[index] + b.skillsTab:ProcessSocketGroup(group) + local retry = false + for _, gemInstance in ipairs(group.gemList) do + if gemInstance.errMsg and gemInstance.nameSpec:match(" [Ss]upport$") then + gemInstance.nameSpec = gemInstance.nameSpec:gsub(" [Ss]upport$", "") + retry = true + end + end + if retry then + b.skillsTab:ProcessSocketGroup(group) + end + return object({ index = index, group = socketGroupInfo(index, group, b.mainSocketGroup) }) + end) + end) + +def("remove_skill_group", + "Remove a socket group by its 1-based index.", + { index = INT("Socket group index") }, { "index" }, + function(args) + return mutate(function(b) + local index = tonumber(args.index) + assert(index and b.skillsTab.socketGroupList[index], "Invalid socket group index") + t_remove(b.skillsTab.socketGroupList, index) + if b.mainSocketGroup > #b.skillsTab.socketGroupList then + b.mainSocketGroup = m_max(1, #b.skillsTab.socketGroupList) + elseif b.mainSocketGroup > index then + b.mainSocketGroup = b.mainSocketGroup - 1 + end + b.skillsTab:AddUndoState() + return object({ removed = index }) + end) + end) + +def("set_gem", + "Change a gem inside a socket group (both indices 1-based, from list_skills). `name` swaps the gem for another.", + { group_index = INT("Socket group index"), gem_index = INT("Gem index within the group"), level = INT("Gem level"), quality = INT("Gem quality"), enabled = BOOL("Enable or disable the gem"), count = INT("Number of copies"), name = STR("Replacement gem name") }, { "group_index", "gem_index" }, + function(args) + return mutate(function(b) + local group = b.skillsTab.socketGroupList[tonumber(args.group_index) or 0] + assert(group, "Invalid socket group index") + local gem = group.gemList[tonumber(args.gem_index) or 0] + assert(gem, "Invalid gem index") + if args.level ~= nil then gem.level = tonumber(args.level) end + if args.quality ~= nil then gem.quality = tonumber(args.quality) end + if args.enabled ~= nil then gem.enabled = args.enabled and true or false end + if args.count ~= nil then gem.count = tonumber(args.count) end + if args.name ~= nil then gem.nameSpec = tostring(args.name) end + b.skillsTab:ProcessSocketGroup(group) + b.skillsTab:AddUndoState() + return object({ gem = gemInfo(gem) }) + end) + end) + +def("set_skill_group_enabled", + "Enable/disable a socket group, or toggle whether it counts towards Full DPS.", + { index = INT("Socket group index"), enabled = BOOL("Enabled"), include_in_full_dps = BOOL("Include in Full DPS") }, { "index" }, + function(args) + return mutate(function(b) + local group = b.skillsTab.socketGroupList[tonumber(args.index) or 0] + assert(group, "Invalid socket group index") + if args.enabled ~= nil then group.enabled = args.enabled and true or false end + if args.include_in_full_dps ~= nil then group.includeInFullDPS = args.include_in_full_dps and true or false end + return object({ group = socketGroupInfo(tonumber(args.index), group, b.mainSocketGroup) }) + end) + end) + +-- Items ---------------------------------------------------------------------- + +def("list_items", + "Equipment slots with what is equipped, plus every item in the build's item list (equipped or not).", + { }, nil, + function() + local b = build() + local itemsTab = b.itemsTab + local slots = array() + for _, slot in ipairs(itemsTab.orderedSlots) do + local item = slot.selItemId ~= 0 and itemsTab.items[slot.selItemId] or nil + local shown = true + if slot.IsShown then + local ok, res = pcall(slot.IsShown, slot) + shown = (not ok) or res + end + t_insert(slots, object({ + slot = slot.slotName, + isJewelSocket = slot.nodeId ~= nil, + nodeId = slot.nodeId, + active = shown and true or false, + itemId = item and item.id or nil, + itemName = item and item.name or nil, + })) + end + local items = array() + for _, id in ipairs(itemsTab.itemOrderList) do + local item = itemsTab.items[id] + if item then + t_insert(items, itemInfo(itemsTab, item)) + end + end + return object({ slots = slots, items = items }) + end) + +def("get_item_text", + "Full item text (PoB/in-game format) for an item from list_items.", + { item_id = INT("Item id") }, { "item_id" }, + function(args) + local b = build() + local item = b.itemsTab.items[tonumber(args.item_id) or 0] + assert(item, "No item with id " .. tostring(args.item_id)) + return object({ id = item.id, name = item.name, text = item:BuildRaw() }) + end) + +def("add_item", + "Add an item from its text (in-game Ctrl+C format or PoB item format) and, by default, equip it. Without `slot` PoB picks the first valid empty slot; with `slot` (e.g. \"Ring 2\", \"Weapon 1\", \"Body Armour\", \"Flask 3\") it replaces what is equipped there.", + { item_text = STR("Item text"), slot = STR("Slot to equip into"), equip = BOOL("Equip the item (default true)") }, { "item_text" }, + function(args) + assert(type(args.item_text) == "string" and #args.item_text > 0, "item text is required") + return mutate(function(b) + local itemsTab = b.itemsTab + itemsTab:CreateDisplayItemFromRaw(args.item_text, false) + local item = itemsTab.displayItem + if not item then + error("Could not parse item text (unknown base type?)") + end + local equip = args.equip ~= false + itemsTab:AddDisplayItem((not equip) or args.slot ~= nil) + if equip and args.slot then + local slot = itemsTab.slots[args.slot] + assert(slot, "Unknown slot '" .. tostring(args.slot) .. "'") + if not itemsTab:IsItemValidForSlot(item, args.slot) then + error("Item '" .. item.name .. "' cannot be equipped in slot " .. args.slot) + end + slot:SetSelItemId(item.id) + itemsTab:PopulateSlots() + end + return object({ item = itemInfo(itemsTab, item) }) + end) + end) + +def("equip_item", + "Equip an existing item (from list_items) into a slot.", + { item_id = INT("Item id"), slot = STR("Slot name") }, { "item_id", "slot" }, + function(args) + return mutate(function(b) + local itemsTab = b.itemsTab + local item = itemsTab.items[tonumber(args.item_id) or 0] + assert(item, "No item with id " .. tostring(args.item_id)) + local slot = itemsTab.slots[args.slot or ""] + assert(slot, "Unknown slot '" .. tostring(args.slot) .. "'") + if not itemsTab:IsItemValidForSlot(item, args.slot) then + error("Item '" .. item.name .. "' cannot be equipped in slot " .. args.slot) + end + slot:SetSelItemId(item.id) + itemsTab:PopulateSlots() + itemsTab:AddUndoState() + return object({ item = itemInfo(itemsTab, item) }) + end) + end) + +def("unequip_slot", + "Empty an equipment slot (the item stays in the item list).", + { slot = STR("Slot name") }, { "slot" }, + function(args) + return mutate(function(b) + local itemsTab = b.itemsTab + local slot = itemsTab.slots[args.slot or ""] + assert(slot, "Unknown slot '" .. tostring(args.slot) .. "'") + local previous = slot.selItemId + slot:SetSelItemId(0) + itemsTab:PopulateSlots() + itemsTab:AddUndoState() + return object({ slot = args.slot, previousItemId = previous ~= 0 and previous or nil }) + end) + end) + +def("delete_item", + "Delete an item from the build entirely.", + { item_id = INT("Item id") }, { "item_id" }, + function(args) + return mutate(function(b) + local itemsTab = b.itemsTab + local item = itemsTab.items[tonumber(args.item_id) or 0] + assert(item, "No item with id " .. tostring(args.item_id)) + itemsTab:DeleteItem(item) + return object({ deleted = item.id, name = item.name }) + end) + end) + +-- Passive tree --------------------------------------------------------------- + +def("get_passive_tree", + "Allocated passive nodes, mastery choices, point counts and a pathofexile.com tree link. By default only notables, keystones and masteries are listed; set notables_only=false for every node.", + { notables_only = BOOL("Only notables, keystones and masteries (default true)"), with_stats = BOOL("Include each node's stat lines") }, nil, + function(args) + local b = build() + local spec = b.spec + local used, ascUsed, secondaryAscUsed, sockets = spec:CountAllocNodes() + local notablesOnly = args.notables_only ~= false + local nodes = array() + local ids = { } + for id, node in pairs(spec.allocNodes) do + if node.type ~= "ClassStart" and node.type ~= "AscendClassStart" then + t_insert(ids, id) + end + end + t_sort(ids) + local withStats = args.with_stats and true or false + for _, id in ipairs(ids) do + local node = spec.allocNodes[id] + if not notablesOnly or node.type == "Notable" or node.type == "Keystone" or node.type == "Mastery" then + t_insert(nodes, nodeInfo(node, withStats or node.type == "Keystone" or node.type == "Notable")) + end + end + local masteries = array() + for nodeId, effectId in pairs(spec.masterySelections) do + local node = spec.nodes[nodeId] + local effectText + if node and node.masteryEffects then + for _, effect in ipairs(node.masteryEffects) do + if effect.effect == effectId then + effectText = stripColors(t_concat(effect.sd or { }, "; ")) + end + end + end + t_insert(masteries, object({ nodeId = nodeId, mastery = node and node.dn or nil, effectId = effectId, effect = effectText })) + end + local url + pcall(function() + url = spec:EncodeURL("https://www.pathofexile.com/passive-skill-tree/") + end) + return object({ + class = spec.curClassName, + ascendancy = spec.curAscendClassName, + treeVersion = spec.treeVersion, + passivePointsUsed = used, + ascendancyPointsUsed = ascUsed, + secondaryAscendancyPointsUsed = secondaryAscUsed, + jewelSocketsAllocated = sockets, + pointsDisplay = b.controls.pointDisplay and stripColors(b.controls.pointDisplay.str) or nil, + nodes = nodes, + masteries = masteries, + url = url, + }) + end) + +def("search_passive_nodes", + "Find passive nodes whose name or stat text contains `query`; reports whether each is allocated and how many points it costs to reach.", + { query = STR("Text to search for"), allocated_only = BOOL("Only allocated nodes"), limit = INT("Maximum results (default 40)") }, { "query" }, + function(args) + local b = build() + local spec = b.spec + local needle = tostring(args.query or ""):lower() + assert(#needle > 0, "query is required") + local limit = tonumber(args.limit) or 40 + local matches = { } + for _, node in pairs(spec.nodes) do + if node.type ~= "ClassStart" and node.type ~= "AscendClassStart" and (not args.allocated_only or node.alloc) then + local hit = (node.dn or ""):lower():find(needle, 1, true) ~= nil + if not hit and node.sd then + for _, line in ipairs(node.sd) do + if line:lower():find(needle, 1, true) then + hit = true + break + end + end + end + if hit then + t_insert(matches, node) + end + end + end + t_sort(matches, function(a, c) + local ra, rb = a.alloc and 2 or (a.path and 1 or 0), c.alloc and 2 or (c.path and 1 or 0) + if ra ~= rb then return ra > rb end + local pa, pb = a.path and #a.path or 1e9, c.path and #c.path or 1e9 + if pa ~= pb then return pa < pb end + return a.id < c.id + end) + local nodes = array() + for i = 1, m_min(#matches, limit) do + t_insert(nodes, nodeInfo(matches[i], true)) + end + return object({ total = #matches, nodes = nodes }) + end) + +def("allocate_node", + "Allocate a passive node (by numeric id or name, e.g. \"Elemental Overload\") plus the shortest path to it.", + { node = STR("Node id or name") }, { "node" }, + function(args) + local b = build() + local node = findNode(b.spec, args.node) + if node.alloc then + return object({ node = nodeInfo(node, true), alreadyAllocated = true, changes = array() }) + end + if not node.path then + error(s_format("Node '%s' (%d) cannot be reached from the current tree", node.dn or "?", node.id)) + end + return mutate(function() + local pathNodes = array() + for _, pathNode in ipairs(node.path) do + if not pathNode.alloc then + t_insert(pathNodes, nodeInfo(pathNode, false)) + end + end + b.spec:AllocNode(node) + b.spec:AddUndoState() + return object({ node = nodeInfo(node, true), allocatedPath = pathNodes }) + end) + end) + +def("deallocate_node", + "Deallocate a passive node (by id or name) and any nodes that were only connected through it.", + { node = STR("Node id or name") }, { "node" }, + function(args) + local b = build() + local node = findNode(b.spec, args.node) + if not node.alloc then + error(s_format("Node '%s' (%d) is not allocated", node.dn or "?", node.id)) + end + return mutate(function() + local removed = array() + for _, depNode in ipairs(node.depends) do + t_insert(removed, nodeInfo(depNode, false)) + end + b.spec:DeallocNode(node) + b.spec:AddUndoState() + return object({ node = nodeInfo(node, true), deallocated = removed }) + end) + end) + +def("reset_passive_tree", + "Remove every allocated passive (keeps class and ascendancy).", + { }, nil, + function() + return mutate(function(b) + b.spec:ResetNodes() + b.spec:BuildAllDependsAndPaths() + b.spec:AddUndoState() + return object({ reset = true }) + end) + end) + +def("import_passive_tree", + "Replace the passive tree from a pathofexile.com or poeplanner passive tree URL.", + { url = STR("Passive tree URL") }, { "url" }, + function(args) + assert(type(args.url) == "string", "url is required") + return mutate(function(b) + local err = b.spec:DecodeURL(args.url) + if err then + error("Could not decode tree URL: " .. tostring(err)) + end + b.spec:AddUndoState() + return object({ imported = true }) + end) + end) + +-- Configuration -------------------------------------------------------------- + +local varList = LoadModule("Modules/ConfigOptions") + +local function configEntry(configTab, varData, section) + local entry = object({ + var = varData.var, + type = varData.type, + label = stripColors(varData.label), + section = section, + value = sanitize(configTab.input[varData.var]), + placeholder = sanitize(configTab.placeholder[varData.var]), + }) + if type(varData.tooltip) == "string" then + entry.tooltip = stripColors(varData.tooltip) + end + if varData.type == "list" and varData.list then + local options = array() + for _, opt in ipairs(varData.list) do + t_insert(options, object({ value = sanitize(opt.val), label = stripColors(opt.label) })) + end + entry.options = options + if varData.defaultIndex and varData.list[varData.defaultIndex] then + entry.default = sanitize(varData.list[varData.defaultIndex].val) + end + end + return entry +end + +def("list_config_options", + "Configuration tab options (buffs, charges, enemy settings, conditions...) with their current values. Filter with `query` (variable name or label, e.g. \"frenzy\", \"boss\", \"shock\") or `section` (\"General\", \"Skill Options\", \"Map Modifiers and Player Debuffs\", \"When In Combat\", \"For Effective DPS\"); only_set=true shows just the options changed from default.", + { query = STR("Text filter"), section = STR("Section filter"), only_set = BOOL("Only options with a value set"), limit = INT("Maximum results (default 150)") }, nil, + function(args) + local b = build() + local configTab = b.configTab + local query = args.query and tostring(args.query):lower() or nil + local wantSection = args.section and tostring(args.section):lower() or nil + local limit = tonumber(args.limit) or 150 + local section + local entries = array() + local total = 0 + for _, varData in ipairs(varList) do + if varData.section then + section = varData.section + elseif varData.var then + local ok = true + if wantSection and not (section or ""):lower():find(wantSection, 1, true) then + ok = false + end + if ok and args.only_set and configTab.input[varData.var] == nil then + ok = false + end + if ok and query then + local hay = (varData.var .. " " .. stripColors(varData.label or "")):lower() + if not hay:find(query, 1, true) then + ok = false + end + end + if ok then + total = total + 1 + if #entries < limit then + t_insert(entries, configEntry(configTab, varData, section)) + end + end + end + end + return object({ total = total, options = entries }) + end) + +def("set_config_option", + "Set a Configuration tab option by variable name (from list_config_options). Checkboxes take true/false, counters take numbers, lists take an option value or label; pass null or \"\" to reset to default. Examples: (\"usePowerCharges\", true), (\"enemyIsBoss\", \"Pinnacle\"), (\"multiplierRage\", 50).", + { var = STR("Option variable name"), value = { description = "New value (boolean, number, string or null)" } }, { "var" }, + function(args) + assert(args.var, "var is required") + local varData + for _, data in ipairs(varList) do + if data.var == args.var then + varData = data + break + end + end + assert(varData, "Unknown config option '" .. tostring(args.var) .. "'") + local value = args.value + if value == dkjson.null then + value = nil + end + if value ~= nil and value ~= "" then + if varData.type == "check" then + if type(value) == "string" then + value = value:lower() == "true" or value == "1" or value:lower() == "yes" + else + value = value and true or false + end + if not value then + value = nil + end + elseif varData.type == "count" or varData.type == "integer" or varData.type == "countAllowZero" or varData.type == "float" then + value = tonumber(value) + assert(value, "Option '" .. varData.var .. "' expects a number") + elseif varData.type == "list" then + local matched + for _, opt in ipairs(varData.list) do + if opt.val == value or tostring(opt.val) == tostring(value) or (type(value) == "string" and stripColors(opt.label):lower() == value:lower()) then + matched = opt.val + break + end + end + if matched == nil then + local labels = { } + for _, opt in ipairs(varData.list) do + t_insert(labels, tostring(opt.val) .. " (" .. stripColors(opt.label) .. ")") + end + error("Invalid value for '" .. varData.var .. "'. Options: " .. t_concat(labels, ", ")) + end + value = matched + else + value = tostring(value) + end + else + value = nil + end + return mutate(function(b) + local configTab = b.configTab + configTab.input[varData.var] = value + if varData.var == "bandit" or varData.var == "pantheonMajorGod" or varData.var == "pantheonMinorGod" then + b[varData.var] = value or "None" + end + configTab:BuildModList() + configTab:AddUndoState() + return object({ option = configEntry(configTab, varData, nil) }) + end) + end) + +local CUSTOM_BLOCK_TITLE = "MCP" + +def("set_custom_modifiers", + "Apply free-form modifiers to the character, one per line, in item/passive wording - e.g. \"+100 to maximum Life\", \"20% increased Attack Speed\", \"Chaos Inoculation\". Useful for what-if questions. Replaces modifiers previously set by this tool (they live in a Custom Modifiers block named \"MCP\" on the Configuration tab); pass \"\" to clear. Lines PoB cannot parse are returned in `unparsed`.", + { text = STR("Modifier lines") }, { "text" }, + function(args) + local text = args.text and tostring(args.text) or "" + return mutate(function(b) + local configTab = b.configTab + local set = configTab.configSets[configTab.activeConfigSetId] + set.customModsList = set.customModsList or { } + local block + for _, existing in ipairs(set.customModsList) do + if existing.title == CUSTOM_BLOCK_TITLE then + block = existing + end + end + if #text:gsub("%s", "") == 0 then + if block then + for i, existing in ipairs(set.customModsList) do + if existing == block then + t_remove(set.customModsList, i) + break + end + end + end + else + if not block then + block = { title = CUSTOM_BLOCK_TITLE, enabled = true } + t_insert(set.customModsList, block) + end + block.text = text + block.enabled = true + end + configTab:BuildModList() + local unparsed = array() + for line in text:gmatch("([^\n]*)\n?") do + local stripped = line:match("^%s*(.-)%s*$") + if #stripped > 0 then + local mods, extra = modLib.parseMod(stripped) + if not mods or extra then + t_insert(unparsed, stripped) + end + end + end + return object({ text = text, unparsed = unparsed }) + end) + end) + +def("get_custom_modifiers", + "Custom modifier blocks currently applied to the build (Configuration tab > Custom Modifiers).", + { }, nil, + function() + local b = build() + local set = b.configTab.configSets[b.configTab.activeConfigSetId] + local blocks = array() + for _, block in ipairs(set.customModsList or { }) do + t_insert(blocks, object({ title = block.title, enabled = block.enabled ~= false, text = block.text })) + end + return object({ blocks = blocks, legacyCustomMods = b.configTab.input.customMods }) + end) + +------------------------------------------------------------------------------ +-- Public API +------------------------------------------------------------------------------ + +mcpTools.INSTRUCTIONS = [[ +Path of Building (Path of Exile 1 build planner). These tools act on the build that is open in the +Path of Building window; the user sees every change immediately and can undo or save it there. + +Typical workflow: +1. `get_build_summary` to see what is open (or `list_builds` + `open_build`, `load_build` with a share + code / build-site link, or `new_build`). +2. `get_sidebar_stats` / `get_stats` / `get_breakdown` to read what PoB calculates. +3. Change things - `add_item`, `set_config_option`, `allocate_node`, `set_gem`, `set_custom_modifiers` + and friends. Every mutating tool returns `changes`: the sidebar stats that moved, with before/after values. +4. The build is marked unsaved; `save_build` writes it, `export_build` gives a share code. + +Stat names (`get_stats`, `get_breakdown`) are PoB's internal output keys, e.g. TotalDPS, CombinedDPS, +FullDPS, Life, EnergyShield, Mana, Armour, Evasion, TotalEHP, FireResist, ColdResist, LightningResist, +ChaosResist, Speed, CritChance, AverageHit. `get_stats` with no names lists everything. + +Item text uses the in-game copy format (Ctrl+C on an item): rarity line, name, base, then one modifier +per line. Gem lists use one gem per line as " /", e.g. "Fireball 21/20". +]] + +--- The tool list as `tools/list` returns it. +function mcpTools.List() + local list = array() + for _, tool in ipairs(tools) do + t_insert(list, object({ name = tool.name, description = tool.description, inputSchema = tool.inputSchema })) + end + return list +end + +function mcpTools.Has(name) + return toolsByName[name] ~= nil +end + +--- Runs a tool. Returns `true, result` or `false, errorMessage`. +function mcpTools.Call(name, args) + local tool = toolsByName[name] + if not tool then + return false, "Unknown tool '" .. tostring(name) .. "'" + end + args = type(args) == "table" and args or { } + local ok, result = xpcall(function() + return tool.handler(args) + end, function(err) + return debug.traceback(tostring(err), 2) + end) + if ok then + return true, sanitize(result) + end + ConPrintf("MCP tool %s failed: %s", name, tostring(result)) + local firstLine = tostring(result):match("^[^\n]*") + firstLine = firstLine:gsub("^.-%.lua:%d+: ", "") + return false, stripColors(firstLine) +end + +return mcpTools diff --git a/src/Modules/Main.lua b/src/Modules/Main.lua index 7ce2c87cb85..40a4c48c0e5 100644 --- a/src/Modules/Main.lua +++ b/src/Modules/Main.lua @@ -61,6 +61,7 @@ function main:Init() self.modes = { } self.modes["LIST"] = LoadModule("Modules/BuildList") self.modes["BUILD"] = LoadModule("Modules/Build") + self.mcp = LoadModule("Modules/MCPStatus") self.popups = { } self.sharedItemList = { } @@ -246,6 +247,13 @@ function main:Init() self.controls.about = new("ButtonControl"):ButtonControl({"BOTTOMLEFT",self.anchorMain,"BOTTOMLEFT"}, {72, 0, 68, 20}, "About", function() self:OpenAboutPopup() end) + self.controls.mcp = new("ButtonControl"):ButtonControl({"BOTTOMLEFT",self.anchorMain,"BOTTOMLEFT"}, {144, 0, 68, 20}, "MCP", function() + self:OpenMCPPopup() + end) + self.controls.mcp.label = function() + return self.mcp:GetButtonLabel() + end + self.controls.mcp.tooltipText = "Run an MCP server inside this window so AI assistants (Claude Code, Claude Desktop, Cursor, ...)\ncan read and change the open build, and see which clients are connected." self.controls.applyUpdate = new("ButtonControl"):ButtonControl({"BOTTOMLEFT",self.anchorMain,"BOTTOMLEFT"}, {0, -24, 140, 20}, "^x50E050Update Ready", function() self:OpenUpdatePopup() end) @@ -387,18 +395,15 @@ function main:CanExit() end function main:Shutdown() + if self.mcp then + self.mcp:Stop() + end self:CallMode("Shutdown") self:SaveSettings() end -function main:OnFrame() - self.screenW, self.screenH = GetVirtualScreenSize() - - if self.screenH > self.screenW then - self.portraitMode = true - else - self.portraitMode = false - end +--- Performs the mode switch scheduled by main:SetMode (normally once per frame; the MCP tools call it directly) +function main:ProcessModeChange() while self.newMode do if self.mode then self:CallMode("Shutdown") @@ -411,6 +416,17 @@ function main:OnFrame() end self.newModeChangeToTree = false end +end + +function main:OnFrame() + self.screenW, self.screenH = GetVirtualScreenSize() + + if self.screenH > self.screenW then + self.portraitMode = true + else + self.portraitMode = false + end + self:ProcessModeChange() self.viewPort = { x = 0, y = 0, width = self.screenW, height = self.screenH } @@ -690,6 +706,12 @@ function main:LoadSettings(ignoreBuild) self.dpiScaleOverridePercent = tonumber(node.attrib.dpiScaleOverridePercent) or 0 SetDPIScaleOverridePercent(self.dpiScaleOverridePercent) end + if node.attrib.mcpEnabled then + self.mcp.enabled = node.attrib.mcpEnabled == "true" + end + if node.attrib.mcpPort then + self.mcp.port = tonumber(node.attrib.mcpPort) or self.mcp.DEFAULT_PORT + end end end end @@ -823,6 +845,8 @@ function main:SaveSettings() showAllItemAffixes = tostring(self.showAllItemAffixes), disableScrollControlInteraction = tostring(self.disableScrollControlInteraction), dpiScaleOverridePercent = tostring(self.dpiScaleOverridePercent), + mcpEnabled = tostring(self.mcp.enabled and true or false), + mcpPort = tostring(self.mcp.port or self.mcp.DEFAULT_PORT), } }) local res, errMsg = common.xml.SaveXMLFile(setXML, self.userPath.."Settings.xml") if not res then @@ -874,6 +898,9 @@ function main:ChangeUserPath(newUserPath, ignoreBuild) MakeDir(self.buildPath) self:LoadSettings(ignoreBuild) self:LoadSharedItems() + if self.mcp then + self.mcp:Init() + end end --- Opens the popup for the "Options" menu --- @param savedState table|nil optional passing of saved values, in case of reopening `{nodePowerTheme, colorPositive, ...}` @@ -1349,6 +1376,68 @@ function main:SetManifestBranch(branchName) xml.SaveXMLFile(localManXML[1], manifestLocation) end +--- Opens the MCP panel: the switch for the in-app MCP server, its port, and the list of connected clients +function main:OpenMCPPopup() + local mcp = self.mcp + mcp:RebuildConnectionList() + local popupWidth, popupHeight = 760, 430 + local controls = { } + + controls.enabled = new("CheckBoxControl"):CheckBoxControl({ "TOPLEFT", nil, "TOPLEFT" }, { 130, 22, 20 }, "^7MCP server:", function(state) + mcp:SetEnabled(state) + end, "Runs an MCP server on this computer (127.0.0.1 only) that lets AI assistants read and change\nthe build open in this window. The choice is remembered.", mcp.enabled) + controls.portLabel = new("LabelControl"):LabelControl({ "LEFT", controls.enabled, "RIGHT" }, { 24, 0, 0, 16 }, "^7Port:") + controls.port = new("EditControl"):EditControl({ "LEFT", controls.portLabel, "RIGHT" }, { 6, 0, 70, 18 }, tostring(mcp.port), nil, "%D", 5) + controls.applyPort = new("ButtonControl"):ButtonControl({ "LEFT", controls.port, "RIGHT" }, { 6, 0, 60, 18 }, "Apply", function() + if not mcp:SetPort(controls.port.buf) then + self:OpenMessagePopup("Invalid port", "Enter a port number between 1 and 65535.") + controls.port:SetText(tostring(mcp.port)) + end + end) + controls.applyPort.enabled = function() + return tonumber(controls.port.buf) ~= mcp.port + end + controls.restart = new("ButtonControl"):ButtonControl({ "TOPRIGHT", nil, "TOPRIGHT" }, { -10, 20, 100, 20 }, "Restart server", function() + mcp:Restart() + end) + controls.restart.enabled = function() + return mcp.enabled + end + + controls.status = new("LabelControl"):LabelControl({ "TOPLEFT", nil, "TOPLEFT" }, { 10, 52, 0, 16 }, "") + controls.status.label = function() + return mcp:GetStatusText() + end + + controls.listLabel = new("LabelControl"):LabelControl({ "TOPLEFT", nil, "TOPLEFT" }, { 10, 80, 0, 16 }, "^7Connected clients:") + controls.list = new("MCPConnectionListControl"):MCPConnectionListControl({ "TOPLEFT", nil, "TOPLEFT" }, { 10, 100, popupWidth - 20, 200 }, mcp) + + controls.setup = new("LabelControl"):LabelControl({ "TOPLEFT", nil, "TOPLEFT" }, { 10, 310, 0, 14 }, "") + controls.setup.label = function() + return "^8Connect Claude Code: ^7" .. mcp:GetSetupCommand() + end + controls.copySetup = new("ButtonControl"):ButtonControl({ "TOPLEFT", nil, "TOPLEFT" }, { 10, 332, 200, 20 }, "Copy Claude Code command", function() + Copy(mcp:GetSetupCommand()) + ToastNotification:Add("Copied to clipboard:\n" .. mcp:GetSetupCommand()) + end) + controls.copyURL = new("ButtonControl"):ButtonControl({ "LEFT", controls.copySetup, "RIGHT" }, { 8, 0, 110, 20 }, "Copy server URL", function() + Copy(mcp:GetURL()) + ToastNotification:Add("Copied to clipboard:\n" .. mcp:GetURL()) + end) + controls.copyURL.tooltipText = "For other MCP clients (Claude Desktop, Cursor, ...): add a remote/HTTP MCP server with this URL." + + controls.logLabel = new("LabelControl"):LabelControl({ "TOPLEFT", nil, "TOPLEFT" }, { 10, 362, 0, 14 }, "") + controls.logLabel.label = function() + return "^8Log: ^7" .. (mcp.log[#mcp.log] or "-") + end + controls.hint = new("LabelControl"):LabelControl({ "TOPLEFT", nil, "TOPLEFT" }, { 10, 380, 0, 14 }, "^8Tools act on the build open in this window; changes show up immediately and mark the build unsaved.") + + controls.close = new("ButtonControl"):ButtonControl({ "BOTTOM", nil, "BOTTOM" }, { 0, -10, 80, 20 }, "Close", function() + self:ClosePopup() + end) + self:OpenPopup(popupWidth, popupHeight, "MCP", controls, nil, nil, "close") +end + function main:OpenUpdatePopup() local changeList = { } local changelogName = launch.devMode and "../changelog.txt" or "changelog.txt"