Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
71 changes: 71 additions & 0 deletions docs/mcp.md
Original file line number Diff line number Diff line change
@@ -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.
78 changes: 78 additions & 0 deletions src/Classes/MCPConnectionListControl.lua
Original file line number Diff line number Diff line change
@@ -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
Loading