@chrischall/warapp-mcp 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/.claude-plugin/marketplace.json +4 -4
  2. package/.claude-plugin/plugin.json +2 -2
  3. package/README.md +211 -9
  4. package/dist/analysis/accept-surrender.js +56 -0
  5. package/dist/analysis/award.js +159 -0
  6. package/dist/analysis/bonuses.js +98 -0
  7. package/dist/analysis/breach.js +62 -0
  8. package/dist/analysis/calibrate.js +239 -0
  9. package/dist/analysis/card-support.js +329 -0
  10. package/dist/analysis/card-threats.js +234 -0
  11. package/dist/analysis/cards-in-effect.js +130 -0
  12. package/dist/analysis/chains.js +59 -0
  13. package/dist/analysis/chokepoints.js +140 -0
  14. package/dist/analysis/combat.js +231 -0
  15. package/dist/analysis/commander-safety.js +149 -0
  16. package/dist/analysis/commitments.js +338 -0
  17. package/dist/analysis/exchange.js +155 -0
  18. package/dist/analysis/finished-game.js +136 -0
  19. package/dist/analysis/game-lessons.js +217 -0
  20. package/dist/analysis/graph.js +82 -0
  21. package/dist/analysis/guard-off.js +54 -0
  22. package/dist/analysis/guess.js +351 -0
  23. package/dist/analysis/headroom.js +241 -0
  24. package/dist/analysis/hold-diff.js +64 -0
  25. package/dist/analysis/how-lost.js +130 -0
  26. package/dist/analysis/index.js +14 -0
  27. package/dist/analysis/local-deploy.js +179 -0
  28. package/dist/analysis/lookahead.js +108 -0
  29. package/dist/analysis/opponent-profile.js +552 -0
  30. package/dist/analysis/opponents.js +1253 -0
  31. package/dist/analysis/outlook.js +695 -0
  32. package/dist/analysis/overrides.js +60 -0
  33. package/dist/analysis/picks.js +1879 -0
  34. package/dist/analysis/plan/after-leftover.js +140 -0
  35. package/dist/analysis/plan/cap-spend.js +242 -0
  36. package/dist/analysis/plan/captures.js +1195 -0
  37. package/dist/analysis/plan/commander.js +236 -0
  38. package/dist/analysis/plan/completed.js +29 -0
  39. package/dist/analysis/plan/conceded-out.js +131 -0
  40. package/dist/analysis/plan/estimates.js +715 -0
  41. package/dist/analysis/plan/guard-off.js +232 -0
  42. package/dist/analysis/plan/holds.js +1631 -0
  43. package/dist/analysis/plan/leftover.js +760 -0
  44. package/dist/analysis/plan/move-order.js +319 -0
  45. package/dist/analysis/plan/orders.js +317 -0
  46. package/dist/analysis/plan/relations.js +702 -0
  47. package/dist/analysis/plan/report.js +672 -0
  48. package/dist/analysis/plan/shared.js +1405 -0
  49. package/dist/analysis/plan/stand-still.js +823 -0
  50. package/dist/analysis/plan/state.js +44 -0
  51. package/dist/analysis/plan/strike.js +1416 -0
  52. package/dist/analysis/plan/targets.js +651 -0
  53. package/dist/analysis/plan/team.js +299 -0
  54. package/dist/analysis/plan/threats.js +1001 -0
  55. package/dist/analysis/plan/transfers.js +328 -0
  56. package/dist/analysis/plan/util.js +5 -0
  57. package/dist/analysis/plan/vacated.js +285 -0
  58. package/dist/analysis/planner.js +2927 -0
  59. package/dist/analysis/player-record.js +144 -0
  60. package/dist/analysis/player.js +268 -0
  61. package/dist/analysis/profile.js +347 -0
  62. package/dist/analysis/relations.js +1030 -0
  63. package/dist/analysis/schedule.js +88 -0
  64. package/dist/analysis/since-last.js +50 -0
  65. package/dist/analysis/static-seats.js +71 -0
  66. package/dist/analysis/stress.js +584 -0
  67. package/dist/analysis/team-brief.js +151 -0
  68. package/dist/analysis/tripwire.js +644 -0
  69. package/dist/analysis/turn-sim.js +259 -0
  70. package/dist/analysis/whereabouts.js +1112 -0
  71. package/dist/board.js +105 -0
  72. package/dist/bundle.js +121082 -36148
  73. package/dist/calibration-source.js +75 -0
  74. package/dist/cards.js +435 -0
  75. package/dist/chat-cursor.js +135 -0
  76. package/dist/chat.js +265 -0
  77. package/dist/client.js +439 -0
  78. package/dist/config.js +62 -0
  79. package/dist/direct/link.js +102 -0
  80. package/dist/direct/session.js +267 -0
  81. package/dist/direct/transport.js +424 -0
  82. package/dist/finished-store.js +357 -0
  83. package/dist/guess-salt.js +65 -0
  84. package/dist/hosted.js +24 -0
  85. package/dist/index.js +33 -0
  86. package/dist/join-color.js +140 -0
  87. package/dist/lessons-store.js +356 -0
  88. package/dist/live.js +287 -0
  89. package/dist/map-details.js +127 -0
  90. package/dist/map-stats.js +160 -0
  91. package/dist/map-store.js +242 -0
  92. package/dist/move-order.js +141 -0
  93. package/dist/my-games.js +218 -0
  94. package/dist/normalize.js +443 -0
  95. package/dist/notifications.js +143 -0
  96. package/dist/open-games.js +537 -0
  97. package/dist/outlook-log.js +165 -0
  98. package/dist/path-lock.js +22 -0
  99. package/dist/pick-list.js +193 -0
  100. package/dist/player-record-store.js +231 -0
  101. package/dist/player-states.js +31 -0
  102. package/dist/profile-store.js +727 -0
  103. package/dist/protocol/bundle.js +375 -0
  104. package/dist/protocol/codec.js +556 -0
  105. package/dist/protocol/decoded.js +12 -0
  106. package/dist/protocol/interp.js +1904 -0
  107. package/dist/protocol/methods.js +615 -0
  108. package/dist/protocol/rpc.js +202 -0
  109. package/dist/push/listener.js +393 -0
  110. package/dist/push/packets.js +66 -0
  111. package/dist/push/socketio.js +333 -0
  112. package/dist/push/websocket.js +179 -0
  113. package/dist/redact.js +62 -0
  114. package/dist/relations-store.js +242 -0
  115. package/dist/replay.js +227 -0
  116. package/dist/scalars.js +28 -0
  117. package/dist/scout.js +346 -0
  118. package/dist/stats.js +291 -0
  119. package/dist/strategy/card-clock.js +185 -0
  120. package/dist/strategy/map-notes.js +128 -0
  121. package/dist/strategy/notes.js +2223 -0
  122. package/dist/surrender-pending.js +126 -0
  123. package/dist/team-cards.js +84 -0
  124. package/dist/team-orders.js +150 -0
  125. package/dist/teams.js +19 -0
  126. package/dist/tools/accept-surrender.js +311 -0
  127. package/dist/tools/award.js +206 -0
  128. package/dist/tools/begin-turn.js +162 -0
  129. package/dist/tools/board.js +77 -0
  130. package/dist/tools/card-advice.js +256 -0
  131. package/dist/tools/card-effects.js +49 -0
  132. package/dist/tools/card-plays.js +434 -0
  133. package/dist/tools/card-support-advice.js +140 -0
  134. package/dist/tools/chat-inbox.js +158 -0
  135. package/dist/tools/chat-read-mark.js +179 -0
  136. package/dist/tools/chat.js +525 -0
  137. package/dist/tools/check-plan.js +689 -0
  138. package/dist/tools/commander-check.js +103 -0
  139. package/dist/tools/commitments.js +216 -0
  140. package/dist/tools/common.js +143 -0
  141. package/dist/tools/diplomacy.js +342 -0
  142. package/dist/tools/end-of-game.js +384 -0
  143. package/dist/tools/game.js +151 -0
  144. package/dist/tools/guess-view.js +56 -0
  145. package/dist/tools/healthcheck.js +90 -0
  146. package/dist/tools/hints.js +34 -0
  147. package/dist/tools/how-lost.js +154 -0
  148. package/dist/tools/index.js +152 -0
  149. package/dist/tools/join-game.js +327 -0
  150. package/dist/tools/leave-game.js +181 -0
  151. package/dist/tools/live-context.js +339 -0
  152. package/dist/tools/live.js +404 -0
  153. package/dist/tools/map-details.js +88 -0
  154. package/dist/tools/map-stats.js +130 -0
  155. package/dist/tools/map.js +141 -0
  156. package/dist/tools/my-games.js +193 -0
  157. package/dist/tools/notifications.js +484 -0
  158. package/dist/tools/open-games.js +104 -0
  159. package/dist/tools/opponents.js +163 -0
  160. package/dist/tools/order-options.js +135 -0
  161. package/dist/tools/past-games.js +94 -0
  162. package/dist/tools/picks.js +305 -0
  163. package/dist/tools/profile-hook.js +156 -0
  164. package/dist/tools/profile-opponents.js +121 -0
  165. package/dist/tools/profile-reads.js +99 -0
  166. package/dist/tools/push.js +445 -0
  167. package/dist/tools/relations.js +611 -0
  168. package/dist/tools/resolve.js +45 -0
  169. package/dist/tools/route.js +36 -0
  170. package/dist/tools/scout.js +87 -0
  171. package/dist/tools/session.js +163 -0
  172. package/dist/tools/strategy.js +257 -0
  173. package/dist/tools/stress-opponents.js +83 -0
  174. package/dist/tools/surrender-pending.js +74 -0
  175. package/dist/tools/surrender.js +247 -0
  176. package/dist/tools/survival.js +189 -0
  177. package/dist/tools/team-brief.js +266 -0
  178. package/dist/tools/team-source.js +222 -0
  179. package/dist/tools/team.js +178 -0
  180. package/dist/tools/turn.js +2825 -0
  181. package/dist/tools/uncommit.js +160 -0
  182. package/dist/tools/where.js +300 -0
  183. package/dist/tools/write-kit.js +164 -0
  184. package/dist/transport-bridge.js +33 -0
  185. package/dist/trend.js +577 -0
  186. package/dist/version.js +4 -0
  187. package/manifest.json +194 -4
  188. package/mint.yaml +106 -14
  189. package/package.json +11 -6
  190. package/server.json +40 -3
  191. package/skills/warapp-api/SKILL.md +95 -4
  192. package/skills/warapp-api/references/scout.mjs +348 -0
  193. package/skills/warapp-api/scripts/warapp.sh +69 -8
  194. package/skills/warapp-mcp/SKILL.md +1096 -21
@@ -0,0 +1,267 @@
1
+ // The signed-in war.app session for the chrome-less "direct" transport.
2
+ //
3
+ // The AuthToken cookie is a FULL-ACCOUNT credential (live-verified 2026-11-12
4
+ // to authenticate both the server-rendered pages and the binary /Services
5
+ // protocol on its own). It is stored at `<dataDir>/session.json`
6
+ // (`WARAPP_SESSION_FILE` overrides): directory `0700` (an existing directory
7
+ // of ours is tightened to it; another user's is refused, and so is root's when
8
+ // others can write to it; on macOS any ACL on our directory and on the file is
9
+ // removed, since mode bits do not govern ACLs), file `0600`, written atomically through a randomly named tmp file
10
+ // created exclusively (never an existing file, never through a symlink) and
11
+ // renamed into place, so a crash never leaves a half-written token.
12
+ //
13
+ // SECURITY: treat the AuthToken exactly like WARAPP_API_TOKEN — never log it,
14
+ // never put it in a tool result, never leave it in an error or hint string.
15
+ // {@link scrubToken} removes it from any upstream string BY VALUE (reusing
16
+ // mcp-utils `stripBearer`, so a `Cookie:`/`Bearer`-prefixed form is caught too,
17
+ // and src/redact.ts, so its encoded spellings are too).
18
+ //
19
+ // Deferred, like every other credential in this server: nothing here runs at
20
+ // import time, the server boots without a session, and a missing OR corrupt
21
+ // file reads as `null` — never a throw.
22
+ //
23
+ // LOCAL-ONLY: on a hosted child (src/hosted.ts) the store reads as empty — a
24
+ // session file already in the data dir is never read — and refuses to write.
25
+ import { execFile } from 'node:child_process';
26
+ import { randomBytes } from 'node:crypto';
27
+ import { readFileSync } from 'node:fs';
28
+ import { chmod, mkdir, open, readFile, rename, rm, stat } from 'node:fs/promises';
29
+ import { dirname, join, resolve } from 'node:path';
30
+ import { promisify } from 'node:util';
31
+ import { McpToolError, readEnvVar } from '@chrischall/mcp-utils';
32
+ import { stripBearer } from '@chrischall/mcp-utils/fetchproxy';
33
+ import { dataDir } from '../config.js';
34
+ import { isHosted } from '../hosted.js';
35
+ import { scrubSecrets } from '../redact.js';
36
+ /** The only host a session is ever stored for. */
37
+ export const SESSION_HOST = 'war.app';
38
+ const isRec = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
39
+ /**
40
+ * Where the session is stored: `WARAPP_SESSION_FILE` if set, else
41
+ * `<dataDir>/session.json` (the same `dataDir` the client bundle cache uses).
42
+ * {@link writeSession} makes the containing directory `0700`, so the variable
43
+ * should name a file in a directory of its own, not a shared folder.
44
+ */
45
+ export function sessionFilePath(env = process.env) {
46
+ return readEnvVar('WARAPP_SESSION_FILE', { env }) ?? join(dataDir(env), 'session.json');
47
+ }
48
+ /** Parse a decoded session record, returning `null` for any unusable shape. */
49
+ function parseSession(raw) {
50
+ if (!isRec(raw))
51
+ return null;
52
+ const { authToken, capturedAt, host } = raw;
53
+ if (typeof authToken !== 'string' || authToken.length === 0)
54
+ return null;
55
+ if (typeof capturedAt !== 'string' || capturedAt.length === 0)
56
+ return null;
57
+ if (host !== SESSION_HOST)
58
+ return null;
59
+ return { authToken, capturedAt, host };
60
+ }
61
+ /**
62
+ * The linked session, or `null` when none is linked or the file is missing or
63
+ * unusable (wrong shape, wrong host, not JSON). Never throws.
64
+ */
65
+ export async function readSession(env = process.env) {
66
+ if (isHosted(env))
67
+ return null;
68
+ try {
69
+ return parseSession(JSON.parse(await readFile(sessionFilePath(env), 'utf8')));
70
+ }
71
+ catch {
72
+ return null;
73
+ }
74
+ }
75
+ /**
76
+ * {@link readSession}'s synchronous twin, for the one caller that needs the
77
+ * session inside a synchronous getter (the adaptive healthcheck's `path`, which
78
+ * reports only the non-secret linked/`capturedAt` facts — never the token).
79
+ * Same guarantees: a missing or corrupt file reads as `null`, never a throw.
80
+ */
81
+ export function readSessionSync(env = process.env) {
82
+ if (isHosted(env))
83
+ return null;
84
+ try {
85
+ return parseSession(JSON.parse(readFileSync(sessionFilePath(env), 'utf8')));
86
+ }
87
+ catch {
88
+ return null;
89
+ }
90
+ }
91
+ /**
92
+ * Judge the session directory from its `stat` and this process's uid
93
+ * (`undefined` where there are no POSIX owners: nothing to check). Ours with
94
+ * any group/other bit → `tighten` (to `0700`). Root's and writable by group or
95
+ * others → `refuse` (someone else could plant or swap files in it). Another
96
+ * user's → `refuse` whatever its mode bits say: we could write into it only
97
+ * through an ACL its owner controls, and the owner could add an inheritable
98
+ * entry that reaches the session file.
99
+ */
100
+ export function sessionDirVerdict(st, myUid) {
101
+ if (myUid === undefined)
102
+ return 'ok';
103
+ if (st.uid === myUid)
104
+ return (st.mode & 0o077) !== 0 ? 'tighten' : 'ok';
105
+ if (st.uid !== 0)
106
+ return 'refuse';
107
+ return (st.mode & 0o022) !== 0 ? 'refuse' : 'ok';
108
+ }
109
+ const run = promisify(execFile);
110
+ /**
111
+ * macOS ACLs: the mode bits do not govern them, so `chmod 0700` leaves an
112
+ * "everyone allow read" entry in place, and an inheritable one on the
113
+ * directory is copied onto every file created in it — the session file
114
+ * included, whatever mode it was created with. On darwin these strip and
115
+ * detect an extended ACL with the system tools (Node has no ACL API). Linux
116
+ * POSIX ACLs need nothing here: their named entries are capped by the mask,
117
+ * which `0700`/`0600` set to nothing. Paths are absolute (never read as flags).
118
+ */
119
+ const MACOS_ACLS = process.platform === 'darwin';
120
+ /**
121
+ * Whether `ls -led` output lists an ACL entry (` 0: group:everyone allow …` on
122
+ * a line after the first). The mode string's `+` is not enough: with extended
123
+ * attributes as well, macOS prints `@` there instead.
124
+ */
125
+ export function lsShowsAcl(stdout) {
126
+ return stdout
127
+ .split('\n')
128
+ .slice(1)
129
+ .some((line) => /^\s*\d+: /.test(line));
130
+ }
131
+ async function hasAcl(path) {
132
+ const { stdout } = await run('/bin/ls', ['-led', path]);
133
+ return lsShowsAcl(stdout);
134
+ }
135
+ /** Remove every ACL entry from `path` (ours), then refuse unless none is left. */
136
+ async function stripAcl(path, what) {
137
+ await run('/bin/chmod', ['-N', path]);
138
+ if (await hasAcl(path)) {
139
+ throw new McpToolError(`Refusing to store the war.app session: could not remove the access control list on ${what}.`, {
140
+ hint: 'Point WARAPP_SESSION_FILE (or MCP_DATA_DIR) at a directory only you can read, with no ACL (`ls -le`).',
141
+ });
142
+ }
143
+ }
144
+ /** A fresh, unpredictable tmp file name next to `path`. */
145
+ export function sessionTmpPath(path) {
146
+ return `${path}.${randomBytes(16).toString('hex')}.tmp`;
147
+ }
148
+ /**
149
+ * Store a session: the directory is created `0700` (an existing one of ours is
150
+ * tightened to `0700`; one others can write to that is not ours is refused),
151
+ * and the file is written `0600` atomically — a randomly named tmp file,
152
+ * created exclusively (it never opens an existing file or follows a symlink),
153
+ * then renamed over the target; the tmp file is removed if anything fails.
154
+ * Unlike {@link readSession} this propagates I/O errors, so a deliberate link
155
+ * action can report that it failed.
156
+ */
157
+ export async function writeSession(session, env = process.env) {
158
+ if (isHosted(env)) {
159
+ throw new McpToolError('The direct transport is local-only: a hosted server never stores a war.app session.', {
160
+ kind: 'unknown',
161
+ });
162
+ }
163
+ const path = resolve(sessionFilePath(env));
164
+ const dir = dirname(path);
165
+ await mkdir(dir, { recursive: true, mode: 0o700 });
166
+ // mkdir's mode applies only to a directory it creates.
167
+ const st = await stat(dir);
168
+ const myUid = process.getuid?.();
169
+ const verdict = sessionDirVerdict(st, myUid);
170
+ if (verdict === 'refuse') {
171
+ throw new McpToolError(`Refusing to store the war.app session in ${dir}: it is not yours, and others could reach files in it.`, {
172
+ hint: 'Point WARAPP_SESSION_FILE (or MCP_DATA_DIR) at a directory of your own that only you can write to.',
173
+ });
174
+ }
175
+ if (verdict === 'tighten')
176
+ await chmod(dir, 0o700);
177
+ if (MACOS_ACLS) {
178
+ // Before the file exists: an inheritable entry would be copied onto it at
179
+ // creation, and a reader that opened it then would keep that access.
180
+ if (st.uid === myUid)
181
+ await stripAcl(dir, dir);
182
+ else if (await hasAcl(dir)) {
183
+ throw new McpToolError(`Refusing to store the war.app session in ${dir}: it carries an access control list and is not yours.`, {
184
+ hint: 'Point WARAPP_SESSION_FILE (or MCP_DATA_DIR) at a directory of your own that only you can read.',
185
+ });
186
+ }
187
+ }
188
+ const tmp = sessionTmpPath(path);
189
+ // 'wx' = O_CREAT | O_EXCL: fails on any existing entry, a symlink included.
190
+ const file = await open(tmp, 'wx', 0o600);
191
+ try {
192
+ try {
193
+ // Still empty: make sure it carries nothing but 0600 before the token goes in.
194
+ await file.chmod(0o600);
195
+ if (MACOS_ACLS)
196
+ await stripAcl(tmp, 'the session file');
197
+ await file.writeFile(JSON.stringify(session));
198
+ }
199
+ finally {
200
+ await file.close();
201
+ }
202
+ await rename(tmp, path);
203
+ }
204
+ catch (e) {
205
+ await rm(tmp, { force: true }).catch(() => { });
206
+ throw e;
207
+ }
208
+ }
209
+ /** Remove the stored session. A no-op (never throws) when none is linked. */
210
+ export async function clearSession(env = process.env) {
211
+ await rm(sessionFilePath(env), { force: true });
212
+ }
213
+ /** The marker a scrubbed AuthToken leaves behind. */
214
+ const SCRUBBED = '[warapp-session]';
215
+ /**
216
+ * Remove `token` from `s` by value — the raw value and its
217
+ * {@link stripBearer}-normalised form (so a `Cookie: AuthToken=…` or
218
+ * `Bearer …` slice is caught), each in every spelling src/redact.ts knows
219
+ * (JSON-escaped, percent-encoded, form-encoded), so a token echoed in a URL,
220
+ * a form body or a JSON string is caught. Wire this around any upstream string
221
+ * that could carry the session, the way the GameFeed client scrubs the email
222
+ * and API token. A falsy token leaves `s` unchanged.
223
+ */
224
+ export function scrubToken(s, token) {
225
+ if (!token)
226
+ return s;
227
+ return scrubSecrets(s, [token, stripBearer(token)], SCRUBBED);
228
+ }
229
+ /** Every string an error carries that could reach a caller: message, hint, stack, and its cause chain. */
230
+ function errorStrings(err, depth = 0) {
231
+ if (depth > 4 || typeof err !== 'object' || err === null)
232
+ return typeof err === 'string' ? [err] : [];
233
+ const e = err;
234
+ const own = [e.message, e.hint, e.stack].filter((v) => typeof v === 'string');
235
+ return [...own, ...errorStrings(e.cause, depth + 1)];
236
+ }
237
+ /**
238
+ * Whether any of `tokens` (in any {@link scrubToken} form) appears anywhere in
239
+ * `err` — its message, hint, stack, or a short `cause` chain.
240
+ */
241
+ function errorCarriesToken(err, tokens) {
242
+ const strings = errorStrings(err);
243
+ return tokens.some((t) => !!t && strings.some((s) => scrubToken(s, t) !== s));
244
+ }
245
+ /**
246
+ * `err` with every form of each of `tokens` removed: returned unchanged when
247
+ * none appears anywhere in it (message, hint, stack, cause chain); otherwise
248
+ * rebuilt as an {@link McpToolError} carrying the scrubbed message and hint and
249
+ * the original `kind`/`status`, with NO `cause` (the cause is where a token
250
+ * could still hide) and a fresh stack.
251
+ */
252
+ export function scrubError(err, tokens) {
253
+ if (!errorCarriesToken(err, tokens))
254
+ return err;
255
+ // Longest first, so a token inside a longer one (the AuthToken inside the
256
+ // whole captured Cookie header) never leaves the longer one half-scrubbed.
257
+ const ordered = tokens.filter((t) => !!t).sort((a, b) => b.length - a.length);
258
+ const scrub = (s) => ordered.reduce((acc, t) => scrubToken(acc, t), s);
259
+ const e = (typeof err === 'object' && err !== null ? err : {});
260
+ const message = scrub(typeof e.message === 'string' ? e.message : String(err));
261
+ const kind = err instanceof McpToolError && err.kind ? err.kind : 'transport';
262
+ return new McpToolError(message, {
263
+ kind,
264
+ ...(typeof e.status === 'number' ? { status: e.status } : {}),
265
+ ...(typeof e.hint === 'string' ? { hint: scrub(e.hint) } : {}),
266
+ });
267
+ }