@chrischall/warapp-mcp 0.0.0 → 0.1.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.
- package/.claude-plugin/marketplace.json +4 -4
- package/.claude-plugin/plugin.json +2 -2
- package/README.md +199 -8
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -0
- package/dist/analysis/card-support.js +329 -0
- package/dist/analysis/card-threats.js +234 -0
- package/dist/analysis/cards-in-effect.js +130 -0
- package/dist/analysis/chains.js +59 -0
- package/dist/analysis/chokepoints.js +140 -0
- package/dist/analysis/combat.js +231 -0
- package/dist/analysis/commander-safety.js +149 -0
- package/dist/analysis/commitments.js +338 -0
- package/dist/analysis/exchange.js +155 -0
- package/dist/analysis/graph.js +82 -0
- package/dist/analysis/guard-off.js +54 -0
- package/dist/analysis/guess.js +351 -0
- package/dist/analysis/headroom.js +241 -0
- package/dist/analysis/hold-diff.js +64 -0
- package/dist/analysis/how-lost.js +130 -0
- package/dist/analysis/index.js +14 -0
- package/dist/analysis/local-deploy.js +159 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +551 -0
- package/dist/analysis/opponents.js +1245 -0
- package/dist/analysis/outlook.js +633 -0
- package/dist/analysis/overrides.js +60 -0
- package/dist/analysis/picks.js +1879 -0
- package/dist/analysis/plan/after-leftover.js +140 -0
- package/dist/analysis/plan/cap-spend.js +242 -0
- package/dist/analysis/plan/captures.js +1195 -0
- package/dist/analysis/plan/commander.js +236 -0
- package/dist/analysis/plan/completed.js +29 -0
- package/dist/analysis/plan/conceded-out.js +131 -0
- package/dist/analysis/plan/estimates.js +715 -0
- package/dist/analysis/plan/guard-off.js +232 -0
- package/dist/analysis/plan/holds.js +1631 -0
- package/dist/analysis/plan/leftover.js +760 -0
- package/dist/analysis/plan/move-order.js +319 -0
- package/dist/analysis/plan/orders.js +317 -0
- package/dist/analysis/plan/relations.js +702 -0
- package/dist/analysis/plan/report.js +672 -0
- package/dist/analysis/plan/shared.js +1218 -0
- package/dist/analysis/plan/stand-still.js +823 -0
- package/dist/analysis/plan/state.js +44 -0
- package/dist/analysis/plan/strike.js +1416 -0
- package/dist/analysis/plan/targets.js +651 -0
- package/dist/analysis/plan/team.js +299 -0
- package/dist/analysis/plan/threats.js +927 -0
- package/dist/analysis/plan/transfers.js +328 -0
- package/dist/analysis/plan/util.js +5 -0
- package/dist/analysis/plan/vacated.js +285 -0
- package/dist/analysis/planner.js +2922 -0
- package/dist/analysis/player-record.js +144 -0
- package/dist/analysis/player.js +268 -0
- package/dist/analysis/profile.js +347 -0
- package/dist/analysis/relations.js +1030 -0
- package/dist/analysis/schedule.js +88 -0
- package/dist/analysis/since-last.js +50 -0
- package/dist/analysis/static-seats.js +71 -0
- package/dist/analysis/stress.js +577 -0
- package/dist/analysis/team-brief.js +151 -0
- package/dist/analysis/tripwire.js +644 -0
- package/dist/analysis/turn-sim.js +259 -0
- package/dist/analysis/whereabouts.js +1109 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +117959 -36172
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +192 -0
- package/dist/client.js +361 -0
- package/dist/config.js +62 -0
- package/dist/direct/link.js +102 -0
- package/dist/direct/session.js +267 -0
- package/dist/direct/transport.js +424 -0
- package/dist/guess-salt.js +65 -0
- package/dist/hosted.js +24 -0
- package/dist/index.js +33 -0
- package/dist/join-color.js +140 -0
- package/dist/live.js +278 -0
- package/dist/map-stats.js +152 -0
- package/dist/map-store.js +204 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +180 -0
- package/dist/normalize.js +411 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +138 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +229 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +677 -0
- package/dist/protocol/bundle.js +375 -0
- package/dist/protocol/codec.js +556 -0
- package/dist/protocol/decoded.js +12 -0
- package/dist/protocol/interp.js +1904 -0
- package/dist/protocol/methods.js +531 -0
- package/dist/protocol/rpc.js +202 -0
- package/dist/push/listener.js +393 -0
- package/dist/push/packets.js +66 -0
- package/dist/push/socketio.js +333 -0
- package/dist/push/websocket.js +179 -0
- package/dist/redact.js +62 -0
- package/dist/relations-store.js +242 -0
- package/dist/replay.js +215 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +269 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2168 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -0
- package/dist/tools/award.js +206 -0
- package/dist/tools/begin-turn.js +162 -0
- package/dist/tools/board.js +77 -0
- package/dist/tools/card-advice.js +256 -0
- package/dist/tools/card-effects.js +49 -0
- package/dist/tools/card-plays.js +434 -0
- package/dist/tools/card-support-advice.js +140 -0
- package/dist/tools/chat-inbox.js +146 -0
- package/dist/tools/chat.js +483 -0
- package/dist/tools/check-plan.js +688 -0
- package/dist/tools/commander-check.js +102 -0
- package/dist/tools/commitments.js +216 -0
- package/dist/tools/common.js +143 -0
- package/dist/tools/diplomacy.js +342 -0
- package/dist/tools/game.js +126 -0
- package/dist/tools/guess-view.js +56 -0
- package/dist/tools/healthcheck.js +90 -0
- package/dist/tools/hints.js +34 -0
- package/dist/tools/how-lost.js +154 -0
- package/dist/tools/index.js +142 -0
- package/dist/tools/join-game.js +327 -0
- package/dist/tools/leave-game.js +181 -0
- package/dist/tools/live-context.js +339 -0
- package/dist/tools/live.js +404 -0
- package/dist/tools/map-stats.js +117 -0
- package/dist/tools/map.js +133 -0
- package/dist/tools/my-games.js +123 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +151 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/picks.js +292 -0
- package/dist/tools/profile-hook.js +156 -0
- package/dist/tools/profile-opponents.js +121 -0
- package/dist/tools/profile-reads.js +99 -0
- package/dist/tools/push.js +385 -0
- package/dist/tools/relations.js +611 -0
- package/dist/tools/resolve.js +45 -0
- package/dist/tools/route.js +36 -0
- package/dist/tools/scout.js +87 -0
- package/dist/tools/session.js +163 -0
- package/dist/tools/strategy.js +200 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender.js +240 -0
- package/dist/tools/survival.js +189 -0
- package/dist/tools/team-brief.js +266 -0
- package/dist/tools/team-source.js +222 -0
- package/dist/tools/team.js +182 -0
- package/dist/tools/turn.js +2761 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +275 -0
- package/dist/tools/write-kit.js +158 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +452 -0
- package/dist/version.js +4 -0
- package/manifest.json +174 -4
- package/mint.yaml +101 -14
- package/package.json +11 -6
- package/server.json +40 -3
- package/skills/warapp-api/SKILL.md +95 -4
- package/skills/warapp-api/references/scout.mjs +348 -0
- package/skills/warapp-api/scripts/warapp.sh +69 -8
- package/skills/warapp-mcp/SKILL.md +969 -21
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// warapp_uncommit_turn: withdraw the signed-in account's committed orders for
|
|
2
|
+
// the current turn with the web client's UnCommit (method 80) — the client's
|
|
3
|
+
// "Modify orders" — gated on the user's confirmation. The game is read from
|
|
4
|
+
// GameFeed first (refused outside a turn phase, for a game the account is not
|
|
5
|
+
// playing, or when nothing is committed), and the outcome is read back from
|
|
6
|
+
// GameFeed afterwards: UnCommit's reply carries no result.
|
|
7
|
+
import { CONFIRM_INJECTION_RULE, toolAnnotations } from '@chrischall/mcp-utils';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { parseGameId } from '../client.js';
|
|
10
|
+
import { Method, writeUnCommitParams } from '../protocol/methods.js';
|
|
11
|
+
import { readLedger } from '../relations-store.js';
|
|
12
|
+
import { dropOrdersCache } from '../trend.js';
|
|
13
|
+
import { gameIdArg, shownAccount, UNTRUSTED_PREVIEW_NOTE, untrustedText } from './common.js';
|
|
14
|
+
import { mintMeta } from './hints.js';
|
|
15
|
+
import { requireApprovedAccount, writeAccount } from './live-context.js';
|
|
16
|
+
import { BRIDGE_NOTE } from './live.js';
|
|
17
|
+
import { currentTurn, requireTurnPhase } from './turn.js';
|
|
18
|
+
import { defineGatedWrite, mySeat, refuse, requireMe, requirePlaying, sendAndVerify, worded } from './write-kit.js';
|
|
19
|
+
/** The UnCommit refusals the client handles, in its own terms (read from the bundle). */
|
|
20
|
+
const UNCOMMIT_ERRORS = {
|
|
21
|
+
99: 'The other players committed their orders first, so the turn is resolving with yours; they could not be withdrawn.',
|
|
22
|
+
4: 'The game is over, so there are no orders to withdraw.',
|
|
23
|
+
7: 'You are no longer in the game, so your orders could not be withdrawn.',
|
|
24
|
+
230: 'You are no longer in the game, so your orders could not be withdrawn.',
|
|
25
|
+
};
|
|
26
|
+
/** The signed-in account's seat in `game`, refused unless it has orders committed this turn. */
|
|
27
|
+
function uncommittable(game, token) {
|
|
28
|
+
requireTurnPhase(game);
|
|
29
|
+
const seat = mySeat(game, token);
|
|
30
|
+
requirePlaying(game, seat);
|
|
31
|
+
if (!seat.hasCommittedOrders) {
|
|
32
|
+
refuse(`Your orders for this turn of game ${game.id} are not committed, so there is nothing to withdraw.`, 'Submit orders with warapp_submit_turn; check warapp_get_game for your committed flag.');
|
|
33
|
+
}
|
|
34
|
+
return seat;
|
|
35
|
+
}
|
|
36
|
+
export function registerUncommitTool(server, ctx) {
|
|
37
|
+
defineGatedWrite(server, {
|
|
38
|
+
title: 'Withdraw my committed orders in a war.app game',
|
|
39
|
+
_meta: mintMeta('change', ['uncommit my turn', 'modify my orders', 'withdraw my orders', 'undo my commit']),
|
|
40
|
+
}, {
|
|
41
|
+
name: 'warapp_uncommit_turn',
|
|
42
|
+
description: 'Withdraw your committed orders for the current turn of a war.app (Warzone) game (war.app\'s UnCommit, ' +
|
|
43
|
+
'the client\'s "Modify orders"), so a corrected turn can be submitted with warapp_submit_turn before the ' +
|
|
44
|
+
'turn resolves. Refuses outside a turn phase, for a game you are not playing, or when your orders are not ' +
|
|
45
|
+
'committed. The game then waits on you again, so recommit before the boot timer runs out. Always as the ' +
|
|
46
|
+
'signed-in war.app account; the outcome is read back from GameFeed (`uncommitted`, `unconfirmed`, or ' +
|
|
47
|
+
`\`unknown\` when war.app did not answer — never resend blindly). A truce recorded broken-by-me on that ` +
|
|
48
|
+
`turn stays recorded (\`agreementsStillBroken\`). ${BRIDGE_NOTE} ${CONFIRM_INJECTION_RULE}`,
|
|
49
|
+
annotations: toolAnnotations({
|
|
50
|
+
title: 'Withdraw committed war.app orders',
|
|
51
|
+
readOnly: false,
|
|
52
|
+
destructive: true,
|
|
53
|
+
idempotent: false,
|
|
54
|
+
openWorld: true,
|
|
55
|
+
}),
|
|
56
|
+
inputSchema: z.object({ game_id: gameIdArg }),
|
|
57
|
+
async plan({ game_id }) {
|
|
58
|
+
const game = await ctx.client.getGame(parseGameId(game_id), { map: false });
|
|
59
|
+
requireTurnPhase(game);
|
|
60
|
+
const token = await requireMe(ctx);
|
|
61
|
+
const seat = uncommittable(game, token);
|
|
62
|
+
const name = game.name ?? `game ${game.id}`;
|
|
63
|
+
const turn = currentTurn(game);
|
|
64
|
+
const turnLabel = turn === null ? 'this turn' : `turn ${turn}`;
|
|
65
|
+
return {
|
|
66
|
+
gate: {
|
|
67
|
+
action: 'warapp.uncommit_turn',
|
|
68
|
+
// By id only: the game and player names (other people's words) go, delimited,
|
|
69
|
+
// in labelled fields.
|
|
70
|
+
summary: `Withdraw your committed orders for ${turnLabel} of war.app game ${game.id}`,
|
|
71
|
+
account: writeAccount(seat.id),
|
|
72
|
+
target: game.id,
|
|
73
|
+
payload: { method: 'UnCommit', gameId: game.id },
|
|
74
|
+
willSend: { game: game.id, player: untrustedText(seat.name), turn },
|
|
75
|
+
preview: {
|
|
76
|
+
warning: `This withdraws your committed orders for ${turnLabel} of game ${game.id}. The game then waits on you ` +
|
|
77
|
+
'again: submit corrected orders with warapp_submit_turn before the boot timer runs out, or you ' +
|
|
78
|
+
'may be booted.',
|
|
79
|
+
account: shownAccount(seat.name, seat.id),
|
|
80
|
+
game: { id: game.id, name: game.name === undefined ? null : untrustedText(game.name), turn },
|
|
81
|
+
untrusted: UNTRUSTED_PREVIEW_NOTE,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
state: {
|
|
85
|
+
gameId: game.id,
|
|
86
|
+
name,
|
|
87
|
+
me: seat.id,
|
|
88
|
+
turnsBefore: game.numberOfTurns ?? null,
|
|
89
|
+
turn,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
},
|
|
93
|
+
async execute({ gameId, name, me, turnsBefore, turn }) {
|
|
94
|
+
await requireApprovedAccount(ctx, writeAccount(me));
|
|
95
|
+
const base = { game: { id: gameId, name }, turn };
|
|
96
|
+
const check = 'Check warapp_get_game (your committed flag, the turn count) before doing anything else.';
|
|
97
|
+
const unconfirmed = (warning) => ({ status: 'unconfirmed', outcome: 'unconfirmed', ...base, warning });
|
|
98
|
+
return sendAndVerify({
|
|
99
|
+
send: async () => {
|
|
100
|
+
await ctx.rpc.call('UnCommit', Method.UnCommit, (w) => writeUnCommitParams(w, gameId));
|
|
101
|
+
},
|
|
102
|
+
refused: worded(UNCOMMIT_ERRORS),
|
|
103
|
+
unanswered: (failure) => ({
|
|
104
|
+
status: 'unknown',
|
|
105
|
+
outcome: 'unknown',
|
|
106
|
+
...base,
|
|
107
|
+
warning: `war.app did not confirm the withdrawal (${failure}), so it may already have gone through. ${check}`,
|
|
108
|
+
}),
|
|
109
|
+
readBack: () => ctx.client.getGame(gameId, { map: false }),
|
|
110
|
+
unreadable: (failure) => unconfirmed(`war.app accepted the request, but GameFeed could not be read back (${failure}). ${check}`),
|
|
111
|
+
classify: async (after) => {
|
|
112
|
+
// A resolved turn also clears everyone's committed flag, so without both turn
|
|
113
|
+
// counts "not committed" cannot be told apart from "already played".
|
|
114
|
+
if (turnsBefore === null || after.numberOfTurns === undefined) {
|
|
115
|
+
return unconfirmed("war.app accepted the request, but GameFeed gave no turn count, so a withdrawal cannot be told " +
|
|
116
|
+
'apart from a turn that already resolved. Check warapp_get_game and the live board.');
|
|
117
|
+
}
|
|
118
|
+
if (after.numberOfTurns > turnsBefore) {
|
|
119
|
+
return unconfirmed(`The turn already resolved (the game is now on turn ${after.numberOfTurns + 1}), so the orders ` +
|
|
120
|
+
'were most likely played as committed. Check warapp_get_game and the live board.');
|
|
121
|
+
}
|
|
122
|
+
const seat = after.players.find((p) => p.id === me);
|
|
123
|
+
if (seat && !seat.hasCommittedOrders) {
|
|
124
|
+
// The orders cached when they were sent (#119) may never run now: a resend from the web
|
|
125
|
+
// client would leave them standing in for that turn, so they are forgotten.
|
|
126
|
+
if (ctx.dataDir && turn !== null)
|
|
127
|
+
await dropOrdersCache(ctx.dataDir, gameId, me, turn);
|
|
128
|
+
// B2: warapp_submit_turn's break_agreement recorded a truce broken-by-me when these orders
|
|
129
|
+
// were committed. They never ran now, but broken-by-me is final: say so, never undo it.
|
|
130
|
+
const stillBroken = ctx.dataDir && turn !== null
|
|
131
|
+
? ((await readLedger(ctx.dataDir, gameId, me))?.agreements ?? [])
|
|
132
|
+
.filter((a) => a.status === 'broken-by-me' && a.endedTurn === turn)
|
|
133
|
+
.map((a) => a.id)
|
|
134
|
+
.sort()
|
|
135
|
+
: [];
|
|
136
|
+
return {
|
|
137
|
+
status: 'uncommitted',
|
|
138
|
+
outcome: 'confirmed',
|
|
139
|
+
...base,
|
|
140
|
+
detail: 'Your orders are withdrawn and the game is waiting on you again: submit corrected orders with ' +
|
|
141
|
+
'warapp_submit_turn (e.g. from warapp_plan_turn) before the boot timer runs out.',
|
|
142
|
+
...(stillBroken.length
|
|
143
|
+
? {
|
|
144
|
+
agreementsStillBroken: stillBroken,
|
|
145
|
+
warning: `${stillBroken.length === 1 ? 'Agreement' : 'Agreements'} ${stillBroken.join(', ')} ${stillBroken.length === 1 ? 'is' : 'are'} ` +
|
|
146
|
+
`recorded broken-by-me on turn ${turn} (warapp_submit_turn's break_agreement, when these orders were committed). ` +
|
|
147
|
+
'If the withdrawn orders were that break, they never ran, but broken-by-me is final: the ledger and the ' +
|
|
148
|
+
"partner's player record keep it, and this withdrawal does not undo it. Tell the owner; record a new " +
|
|
149
|
+
'agreement (warapp_record_agreement) if the truce is to go on.',
|
|
150
|
+
}
|
|
151
|
+
: {}),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return unconfirmed('war.app accepted the request, but GameFeed still shows your orders committed. ' +
|
|
155
|
+
'Check warapp_get_game shortly before submitting again.');
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { trackGame, trackOpponents } from '../analysis/index.js';
|
|
3
|
+
import { locateOpponents, MAX_APPROACHES, MAX_PREPARE, PREPARE_TURNS, } from '../analysis/whereabouts.js';
|
|
4
|
+
import { boardStateFromLive } from '../live.js';
|
|
5
|
+
import { MIN_SAMPLE } from '../map-stats.js';
|
|
6
|
+
import { readPickRecord, writePickable } from '../pick-list.js';
|
|
7
|
+
import { cachedRecords, STATS_ENV, statsGate } from '../stats.js';
|
|
8
|
+
import { CACHED_TURNS, writeBoardCache } from '../trend.js';
|
|
9
|
+
import { writeGameRecord } from '../replay.js';
|
|
10
|
+
import { loadAward } from './award.js';
|
|
11
|
+
import { gameIdArg, graphOf, namer, readAnnotations, respond, round2, untrustedDescription, viewArg } from './common.js';
|
|
12
|
+
import { lookupMeta } from './hints.js';
|
|
13
|
+
import { loadLiveGame } from './live-context.js';
|
|
14
|
+
import { BRIDGE_NOTE, playerArg, resolveMe } from './live.js';
|
|
15
|
+
import { mapOf } from './map.js';
|
|
16
|
+
import { recordProfile } from './profile-hook.js';
|
|
17
|
+
import { boardHistory, currentTurn, plannerSettings, requireTurnPhase } from './turn.js';
|
|
18
|
+
import { resolveTeamsFor } from './team-source.js';
|
|
19
|
+
const VIEWS = ['compact', 'full'];
|
|
20
|
+
/** The most entries any one list names on the compact view; the rest are a count. */
|
|
21
|
+
const LIST_MAX = 10;
|
|
22
|
+
/** Approaches listed per opponent on the compact view (the full view lists the model's maximum). */
|
|
23
|
+
const COMPACT_APPROACHES = 3;
|
|
24
|
+
/** The least likelihood the full view lists under `presence`. */
|
|
25
|
+
const PRESENCE_LISTED = 0.15;
|
|
26
|
+
/**
|
|
27
|
+
* Keep the territories the live board shows as pickable, for the signed-in account when it is a
|
|
28
|
+
* player of `game`: later turns have no other way to know where a player could have started; and
|
|
29
|
+
* (#201) the picking board's neutrals with their armies, for the turn planner's fogged neutrals.
|
|
30
|
+
* Best effort: without a data directory or a readable account nothing is kept. Never throws.
|
|
31
|
+
*/
|
|
32
|
+
export async function keepPickable(ctx, game, pickable, neutrals) {
|
|
33
|
+
if (!ctx.dataDir || !pickable.length)
|
|
34
|
+
return;
|
|
35
|
+
try {
|
|
36
|
+
const account = await ctx.myInviteToken();
|
|
37
|
+
if (account !== undefined && game.players.some((p) => p.id === account))
|
|
38
|
+
await writePickable(ctx.dataDir, game.id, account, pickable, neutrals);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Best effort: the picks are planned either way.
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Where a player could have started, and how the candidates weigh: the pickable territories kept
|
|
46
|
+
* during distribution, else (statistics on) every territory a player started on in the finished
|
|
47
|
+
* games cached for the map; the prior counts those starts, plus one, when statistics are on and
|
|
48
|
+
* some game is cached. Only files this server already wrote are read.
|
|
49
|
+
*/
|
|
50
|
+
async function startKnowledge(ctx, env, game, map, me) {
|
|
51
|
+
if (!ctx.dataDir)
|
|
52
|
+
return {};
|
|
53
|
+
const onMap = new Set(map.territories.map((t) => t.id));
|
|
54
|
+
const kept = (await readPickRecord(ctx.dataDir, game.id, me))?.pickable?.filter((t) => onMap.has(t)) ?? [];
|
|
55
|
+
const records = statsGate(env).enabled ? await cachedRecords(ctx.dataDir, map.id) : [];
|
|
56
|
+
/** Territory → the players who started on it, over the cached games. */
|
|
57
|
+
const started = new Map();
|
|
58
|
+
for (const r of records)
|
|
59
|
+
for (const p of r.players)
|
|
60
|
+
for (const t of new Set(p.starts))
|
|
61
|
+
if (onMap.has(t))
|
|
62
|
+
started.set(t, (started.get(t) ?? 0) + 1);
|
|
63
|
+
const candidates = kept.length
|
|
64
|
+
? { territories: kept, source: 'pickable' }
|
|
65
|
+
: started.size
|
|
66
|
+
? { territories: [...started.keys()].sort((a, b) => a - b), source: 'statistics' }
|
|
67
|
+
: undefined;
|
|
68
|
+
if (!candidates)
|
|
69
|
+
return {};
|
|
70
|
+
if (!records.length)
|
|
71
|
+
return { candidates };
|
|
72
|
+
return {
|
|
73
|
+
candidates,
|
|
74
|
+
prior: {
|
|
75
|
+
// Plus one, so a candidate nobody started on in the sample is unlikely, not impossible.
|
|
76
|
+
weights: new Map(candidates.territories.map((t) => [t, (started.get(t) ?? 0) + 1])),
|
|
77
|
+
source: 'statistics',
|
|
78
|
+
sample: records.length,
|
|
79
|
+
weak: records.length < MIN_SAMPLE,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export function registerWhereTool(server, ctx, deps = {}) {
|
|
84
|
+
server.registerTool('warapp_locate_opponents', {
|
|
85
|
+
title: 'Locate my opponents in a war.app game',
|
|
86
|
+
description: untrustedDescription('Where each opponent of an in-progress war.app (Warzone) game probably is and what it may be doing, so you ' +
|
|
87
|
+
'can pre-empt or prepare. Per opponent, by name: `seen` holds the only facts — its territories in view on ' +
|
|
88
|
+
'the live board, and territories now hidden that your earlier boards last showed as its own. Everything ' +
|
|
89
|
+
'else is a guess, kept apart: `starts` (the starts the award of your picks proves, then the likely ones ' +
|
|
90
|
+
'among the territories a player could start on, less those a board showed as a neutral or as yours), ' +
|
|
91
|
+
'`presence` (hidden territories it may hold, each with a `likelihood` and its `basis`: last-seen; start; ' +
|
|
92
|
+
'corridor, the shortest ways between a likely start and a sighting; growth, what a player could plausibly ' +
|
|
93
|
+
'have taken from that start by now; income, bonuses that would explain its tracked income), `bonuses` it ' +
|
|
94
|
+
'probably holds in full, its tracked `income` and `hiddenArmies` (`unexplainedArmies` in warapp_track_opponents), `approaches` ' +
|
|
95
|
+
'(the earliest turn it could attack each border territory of yours: from a stack in view, with the armies ' +
|
|
96
|
+
'that could arrive, or from a territory it only probably holds, marked `guessed`) and `doing` (plain ' +
|
|
97
|
+
`sentences, each from a named rule), with a one-line \`summary\`. Then \`prepare\`, most urgent first (at most ${MAX_PREPARE}): ` +
|
|
98
|
+
`\`hold\` (a territory of a bonus you hold in full, or your commander's, that a stack in view can attack within ${PREPARE_TURNS} ` +
|
|
99
|
+
'turns, with the armies it needs to hold), `contested` (a bonus you hold part of whose missing territory it ' +
|
|
100
|
+
'holds, may hold or can take in that time) and `scout` (a territory whose capture would show a guess that ' +
|
|
101
|
+
'could go either way). The likelihoods are estimates of a simple model, not measured probabilities: present ' +
|
|
102
|
+
'them as guesses, and `seen` as what is known. The model assumes one capture per turn along a path, pooled ' +
|
|
103
|
+
'armies when it grows a start, that a start is never abandoned, and ignores cards (the full view lists its ' +
|
|
104
|
+
'assumptions). The start candidates are the territories the live board showed as pickable, kept by ' +
|
|
105
|
+
'warapp_plan_picks and by warapp_submit_picks; with none kept and no statistics, no start is guessed ' +
|
|
106
|
+
'(`starts.source` is "none") and the rest works from what was seen. The start prior is uniform, unless the ' +
|
|
107
|
+
`server runs locally with ${STATS_ENV}=1: then each candidate weighs by how often players started on it in the ` +
|
|
108
|
+
'finished games warapp_map_stats already cached for the map (the sample is reported, and called weak under ' +
|
|
109
|
+
`${MIN_SAMPLE} games), and with no pickable territories kept those starts are the candidates. It reads only boards ` +
|
|
110
|
+
`this server already saw (the last ${CACHED_TURNS} turns warapp_plan_turn, warapp_track_opponents and this tool cached, ` +
|
|
111
|
+
'or the game history when war.app sends it) plus those files: no war.app request beyond the live board and ' +
|
|
112
|
+
`the game feed warapp_track_opponents reads. Refuses outside a turn phase. ${BRIDGE_NOTE} Read-only on war.app; it ` +
|
|
113
|
+
'caches the board it read. warapp_plan_turn does not use these guesses yet.'),
|
|
114
|
+
_meta: lookupMeta(['where is my opponent', 'locate opponents', 'where did they start', 'what is my opponent doing', 'what should i prepare for']),
|
|
115
|
+
annotations: readAnnotations('Locate war.app opponents'),
|
|
116
|
+
inputSchema: z.object({
|
|
117
|
+
game_id: gameIdArg,
|
|
118
|
+
player: playerArg,
|
|
119
|
+
view: viewArg(VIEWS, `compact (default) gives each opponent's facts, its likely starts, the ${LIST_MAX} likeliest hidden territories, the bonuses it probably ` +
|
|
120
|
+
`holds, its ${COMPACT_APPROACHES} soonest approaches and what it is doing, then what to prepare for; "full" adds every other hidden ` +
|
|
121
|
+
`territory at a likelihood of ${PRESENCE_LISTED} or more (the rest are counted), every approach (up to ${MAX_APPROACHES}), the candidates the boards ruled out and the model's assumptions.`),
|
|
122
|
+
}),
|
|
123
|
+
}, async ({ game_id, player, view }) => {
|
|
124
|
+
const feed = await ctx.client.getGame(game_id, { settings: true });
|
|
125
|
+
requireTurnPhase(feed);
|
|
126
|
+
const seat = await resolveMe(ctx, feed.players, player);
|
|
127
|
+
// The teams, once my seat is found: GameFeed's, else the game list's (asked once per game, kept).
|
|
128
|
+
const { game, me, known: teamsKnown } = await resolveTeamsFor(ctx, feed, seat);
|
|
129
|
+
const map = mapOf(game);
|
|
130
|
+
const { territories } = await loadLiveGame(ctx, game);
|
|
131
|
+
const graph = graphOf(map);
|
|
132
|
+
// The board war.app showed: the model takes the starts the award proves as `proven`, never
|
|
133
|
+
// as territories standing on the board.
|
|
134
|
+
const board = boardStateFromLive(territories, me, game.players);
|
|
135
|
+
const settings = plannerSettings(game.settings);
|
|
136
|
+
const turn = currentTurn(game);
|
|
137
|
+
const history = await boardHistory(ctx, game, me, turn);
|
|
138
|
+
const boards = turn !== null ? [...history.boards, { turn, board }] : [{ turn: 0, board }];
|
|
139
|
+
const about = trackGame(game.players, game.settings, map, game.endRules);
|
|
140
|
+
const tracks = trackOpponents(graph, boards, me.id, settings, about);
|
|
141
|
+
const award = await loadAward(ctx.dataDir, game, me, board, turn);
|
|
142
|
+
const proven = new Map();
|
|
143
|
+
if (award?.opponent && award.reading.opponentStarts.length)
|
|
144
|
+
proven.set(award.opponent.id, award.reading.opponentStarts);
|
|
145
|
+
const { candidates, prior } = await startKnowledge(ctx, deps.env, game, map, me.id);
|
|
146
|
+
const s = game.settings;
|
|
147
|
+
const whereSettings = {
|
|
148
|
+
...settings,
|
|
149
|
+
// How many territories a player started on, when the distribution says (not under full
|
|
150
|
+
// distribution or a scenario: `trackGame`).
|
|
151
|
+
...(about.start
|
|
152
|
+
? { startsPerPlayer: about.start.territories, startingArmies: about.start.armiesPerTerritory }
|
|
153
|
+
: s?.initialPlayerArmiesPerTerritory !== undefined
|
|
154
|
+
? { startingArmies: s.initialPlayerArmiesPerTerritory }
|
|
155
|
+
: {}),
|
|
156
|
+
...(s?.initialNonDistributionArmies !== undefined ? { neutralArmies: s.initialNonDistributionArmies } : {}),
|
|
157
|
+
...(s?.initialNeutralsInDistribution !== undefined ? { distributionNeutralArmies: s.initialNeutralsInDistribution } : {}),
|
|
158
|
+
};
|
|
159
|
+
const result = locateOpponents({
|
|
160
|
+
graph,
|
|
161
|
+
me: me.id,
|
|
162
|
+
boards,
|
|
163
|
+
players: about.players,
|
|
164
|
+
settings: whereSettings,
|
|
165
|
+
...(candidates ? { candidates } : {}),
|
|
166
|
+
...(prior ? { prior } : {}),
|
|
167
|
+
proven,
|
|
168
|
+
tracks,
|
|
169
|
+
});
|
|
170
|
+
if (ctx.dataDir && turn !== null) {
|
|
171
|
+
await writeBoardCache(ctx.dataDir, game.id, me.id, turn, board);
|
|
172
|
+
await writeGameRecord(ctx.dataDir, game);
|
|
173
|
+
await recordProfile(ctx.dataDir, deps.env, game, me, boards, teamsKnown);
|
|
174
|
+
}
|
|
175
|
+
const name = namer(map);
|
|
176
|
+
const playerNames = new Map(game.players.map((p) => [p.id, p.name]));
|
|
177
|
+
const playerOf = (id) => ({ id, name: playerNames.get(id) ?? String(id) });
|
|
178
|
+
const bonusOf = (id) => {
|
|
179
|
+
const b = map.bonuses.find((x) => x.id === id);
|
|
180
|
+
return { id, name: b?.name ?? String(id), value: b?.value ?? 0 };
|
|
181
|
+
};
|
|
182
|
+
/** One approach in a sentence, by id (the names are in the fields beside it). */
|
|
183
|
+
const approachText = (o, a) => `Player ${o} could attack territory ${a.target} on turn ${a.turn} from territory ${a.from} (${a.source})` +
|
|
184
|
+
`${a.armies !== null ? ` with up to ${a.armies} armies` : ''}.`;
|
|
185
|
+
const prepareView = (p) => ({
|
|
186
|
+
kind: p.kind,
|
|
187
|
+
text: p.text,
|
|
188
|
+
turn: p.turn,
|
|
189
|
+
opponent: playerOf(p.opponent),
|
|
190
|
+
territory: name(p.territory),
|
|
191
|
+
...(p.bonusId !== undefined ? { bonus: bonusOf(p.bonusId) } : {}),
|
|
192
|
+
...(p.need !== undefined ? { need: p.need, have: p.have, armies: p.armies } : {}),
|
|
193
|
+
...(p.from !== undefined ? { from: name(p.from) } : {}),
|
|
194
|
+
...(p.likelihood !== undefined ? { likelihood: round2(p.likelihood) } : {}),
|
|
195
|
+
...(p.reveals ? { reveals: p.reveals.map(name) } : {}),
|
|
196
|
+
});
|
|
197
|
+
return respond(view, VIEWS, (rung) => {
|
|
198
|
+
const full = rung === 'full';
|
|
199
|
+
/** The first entries of a list, and how many more there are (none on the full view). */
|
|
200
|
+
const some = (rows, max = LIST_MAX) => (full ? [...rows] : rows.slice(0, max));
|
|
201
|
+
const more = (rows, key, max = LIST_MAX) => (!full && rows.length > max ? { [key]: rows.length - max } : {});
|
|
202
|
+
const opponentView = (o) => {
|
|
203
|
+
// Likeliest first: the full view keeps the compact view's first entries, however unlikely,
|
|
204
|
+
// and adds every other one at PRESENCE_LISTED or more.
|
|
205
|
+
const presence = full
|
|
206
|
+
? o.presence.filter((p, i) => i < LIST_MAX || p.likelihood >= PRESENCE_LISTED)
|
|
207
|
+
: o.presence.slice(0, LIST_MAX);
|
|
208
|
+
return {
|
|
209
|
+
player: playerOf(o.playerId),
|
|
210
|
+
summary: o.summary,
|
|
211
|
+
// FACTS: what the boards showed.
|
|
212
|
+
seen: {
|
|
213
|
+
inView: some(o.seen.inView).map((t) => ({ territory: name(t.territoryId), armies: t.armies })),
|
|
214
|
+
...more(o.seen.inView, 'inViewMore'),
|
|
215
|
+
lastSeen: some(o.seen.lastSeen).map((t) => ({ territory: name(t.territoryId), turn: t.turn, armies: t.armies })),
|
|
216
|
+
...more(o.seen.lastSeen, 'lastSeenMore'),
|
|
217
|
+
bonuses: some(o.seen.bonuses).map(bonusOf),
|
|
218
|
+
...more(o.seen.bonuses, 'bonusesMore'),
|
|
219
|
+
},
|
|
220
|
+
// GUESSES from here on: each likelihood is the model's estimate.
|
|
221
|
+
starts: {
|
|
222
|
+
source: o.starts.source,
|
|
223
|
+
prior: o.starts.prior,
|
|
224
|
+
proven: o.starts.proven.map(name),
|
|
225
|
+
likely: some(o.starts.likely).map((x) => ({ territory: name(x.territoryId), likelihood: round2(x.likelihood) })),
|
|
226
|
+
...more(o.starts.likely, 'likelyMore'),
|
|
227
|
+
unknown: o.starts.unknown,
|
|
228
|
+
method: o.starts.method,
|
|
229
|
+
...(o.starts.priorOnly ? { priorOnly: true } : {}),
|
|
230
|
+
...(full ? { ruledOut: o.starts.ruledOut.map(name) } : {}),
|
|
231
|
+
},
|
|
232
|
+
presence: presence.map((p) => ({ territory: name(p.territoryId), likelihood: round2(p.likelihood), basis: p.basis })),
|
|
233
|
+
...(o.presence.length > presence.length ? { presenceMore: o.presence.length - presence.length } : {}),
|
|
234
|
+
bonuses: some(o.bonuses).map((b) => ({
|
|
235
|
+
bonus: bonusOf(b.bonusId),
|
|
236
|
+
likelihood: round2(b.likelihood),
|
|
237
|
+
basis: b.basis,
|
|
238
|
+
inView: b.inView,
|
|
239
|
+
hidden: b.hidden,
|
|
240
|
+
})),
|
|
241
|
+
...more(o.bonuses, 'bonusesMore'),
|
|
242
|
+
income: o.income,
|
|
243
|
+
hiddenArmies: o.hiddenArmies,
|
|
244
|
+
approaches: some(o.approaches, COMPACT_APPROACHES).map((a) => ({
|
|
245
|
+
text: approachText(o.playerId, a),
|
|
246
|
+
turn: a.turn,
|
|
247
|
+
inTurns: a.inTurns,
|
|
248
|
+
target: name(a.target),
|
|
249
|
+
from: name(a.from),
|
|
250
|
+
source: a.source,
|
|
251
|
+
path: a.path.map(name),
|
|
252
|
+
armies: a.armies,
|
|
253
|
+
valued: a.valued,
|
|
254
|
+
})),
|
|
255
|
+
...more(o.approaches, 'approachesMore', COMPACT_APPROACHES),
|
|
256
|
+
doing: o.doing.map((d) => ({
|
|
257
|
+
rule: d.rule,
|
|
258
|
+
text: d.text,
|
|
259
|
+
...(d.territoryIds.length ? { territories: d.territoryIds.map(name) } : {}),
|
|
260
|
+
...(d.bonusId !== undefined ? { bonus: bonusOf(d.bonusId) } : {}),
|
|
261
|
+
})),
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
return {
|
|
265
|
+
game: { id: game.id, name: game.name ?? null, turn },
|
|
266
|
+
player: { id: me.id, name: me.name },
|
|
267
|
+
// The turns whose boards the model read (yours, as war.app showed them).
|
|
268
|
+
turns: boards.map((b) => b.turn).sort((a, b) => a - b),
|
|
269
|
+
opponents: result.opponents.map(opponentView),
|
|
270
|
+
prepare: result.prepare.map(prepareView),
|
|
271
|
+
...(full ? { assumptions: result.assumptions } : {}),
|
|
272
|
+
};
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// The write kit (#133): what every confirm-gated write shares — one refusal,
|
|
2
|
+
// one check that the signed-in account holds the seat being acted for, one
|
|
3
|
+
// send / classify / read-back, one outcome vocabulary, and one registration.
|
|
4
|
+
//
|
|
5
|
+
// Every write result carries `outcome`, from one vocabulary:
|
|
6
|
+
// confirmed — the effect was read back from game state.
|
|
7
|
+
// accepted — war.app accepted the request, and nothing more can be read back
|
|
8
|
+
// (a chat message; a surrender that now waits on the others).
|
|
9
|
+
// unconfirmed — war.app accepted the request, but the read-back failed or does not
|
|
10
|
+
// show the effect yet. Check before acting again.
|
|
11
|
+
// unknown — war.app did not answer, so the request may or may not have gone
|
|
12
|
+
// through. Never resend blindly. A failure before the request left this
|
|
13
|
+
// process (#272: no email, the bridge not ready) is no result but an error
|
|
14
|
+
// saying it was not sent.
|
|
15
|
+
// not_applied — the read-back shows the request did not take effect.
|
|
16
|
+
// A refusal from war.app (nothing changed) is an error, not a result.
|
|
17
|
+
//
|
|
18
|
+
// Each tool keeps its own `status` (and picks its `sent` / `verified`) beside it, since
|
|
19
|
+
// callers read them:
|
|
20
|
+
// warapp_begin_turn begun → confirmed; not_begun → not_applied; unconfirmed; unknown
|
|
21
|
+
// warapp_join_game joined → confirmed; unconfirmed; unknown
|
|
22
|
+
// warapp_leave_game left → confirmed; unconfirmed; unknown
|
|
23
|
+
// warapp_surrender surrendered → confirmed; pending, still_playing → accepted;
|
|
24
|
+
// unchanged → unconfirmed; unknown
|
|
25
|
+
// warapp_uncommit_turn uncommitted → confirmed; unconfirmed; unknown
|
|
26
|
+
// warapp_submit_turn resolved, committed → confirmed; unknown after a send war.app
|
|
27
|
+
// accepted (read-back failed or shows nothing yet) → unconfirmed;
|
|
28
|
+
// unknown (no answer) → unknown
|
|
29
|
+
// warapp_submit_picks verified → confirmed; unconfirmed; unknown
|
|
30
|
+
// warapp_send_chat sent → accepted; unknown
|
|
31
|
+
// warapp_link_session linked → confirmed
|
|
32
|
+
import { defineWrite, McpToolError, registerWrite, } from '@chrischall/mcp-utils';
|
|
33
|
+
import { notSent, WarappServerError } from '../protocol/rpc.js';
|
|
34
|
+
import { respond, untrustedText } from './common.js';
|
|
35
|
+
import { withToolConfig } from './hints.js';
|
|
36
|
+
import { failureText } from './live-context.js';
|
|
37
|
+
import { resolvePlayer } from './resolve.js';
|
|
38
|
+
/** Refuse a write (or a read) before anything is sent. */
|
|
39
|
+
export function refuse(message, hint) {
|
|
40
|
+
throw new McpToolError(message, { kind: 'unknown', hint });
|
|
41
|
+
}
|
|
42
|
+
/** The hint when the signed-in account cannot be told: linking a session is suggested only locally. */
|
|
43
|
+
function signedInHint(hosted) {
|
|
44
|
+
return hosted
|
|
45
|
+
? 'Make sure a war.app tab signed in to your account is open and paired (reload or focus it if it is frozen), then retry.'
|
|
46
|
+
: 'Make sure a war.app tab signed in to your account is open and paired (reload or focus it if it is frozen), or link ' +
|
|
47
|
+
'a session with warapp_link_session, then retry.';
|
|
48
|
+
}
|
|
49
|
+
/** The one refusal when the signed-in account cannot be read; `consequence` ends the sentence. */
|
|
50
|
+
export function accountUnknown(hosted, consequence = 'nothing was sent') {
|
|
51
|
+
return new McpToolError(`Could not tell which war.app account is signed in, so ${consequence}.`, {
|
|
52
|
+
kind: 'unknown',
|
|
53
|
+
hint: signedInHint(hosted),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The signed-in account's GameFeed id (its invite token), read fresh unless
|
|
58
|
+
* `fresh: false`; refused when it cannot be read. Writes always act as that
|
|
59
|
+
* account, so a write never goes ahead without it.
|
|
60
|
+
*/
|
|
61
|
+
export async function requireMe(ctx, opts = {}) {
|
|
62
|
+
const token = await ctx.myInviteToken({ fresh: opts.fresh ?? true });
|
|
63
|
+
if (token === undefined)
|
|
64
|
+
throw accountUnknown(ctx.hosted, opts.consequence);
|
|
65
|
+
return token;
|
|
66
|
+
}
|
|
67
|
+
/** The seat of account `token` in `game`; refused when it holds none. */
|
|
68
|
+
export function mySeat(game, token) {
|
|
69
|
+
const seat = game.players.find((p) => p.id === token);
|
|
70
|
+
if (!seat) {
|
|
71
|
+
refuse(`The signed-in war.app account is not a player in game ${game.id}, so nothing was sent.`, 'Check the game id with warapp_my_games.');
|
|
72
|
+
}
|
|
73
|
+
return seat;
|
|
74
|
+
}
|
|
75
|
+
/** Refuse a seat that is no longer playing (eliminated, booted, surrendered, …). */
|
|
76
|
+
export function requirePlaying(game, seat) {
|
|
77
|
+
if (seat.state !== 'Playing') {
|
|
78
|
+
refuse(`You are no longer playing in game ${game.id} (your state: ${seat.state ?? 'unknown'}).`, 'Check warapp_get_game.');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** Refuse an explicit `player` that is not the signed-in account `token`. */
|
|
82
|
+
function requireNamedIsMe(game, token, input) {
|
|
83
|
+
if (input === undefined || String(input).trim() === '')
|
|
84
|
+
return;
|
|
85
|
+
const named = resolvePlayer(game.players, input);
|
|
86
|
+
if (named.id !== token) {
|
|
87
|
+
refuse(`${untrustedText(named.name)} is not the signed-in war.app account, so nothing was sent: writes always act as the signed-in account.`, 'Omit `player` (it defaults to the signed-in account), or sign in to war.app as that player.');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The signed-in account's seat in `game`, read fresh: refused when the account
|
|
92
|
+
* cannot be read, when an explicit `player` names someone else, when it holds
|
|
93
|
+
* no seat, and (unless `playing: false`) when the seat is no longer playing.
|
|
94
|
+
*/
|
|
95
|
+
export async function requireMySeat(ctx, game, opts = {}) {
|
|
96
|
+
const token = await requireMe(ctx);
|
|
97
|
+
requireNamedIsMe(game, token, opts.player);
|
|
98
|
+
const seat = mySeat(game, token);
|
|
99
|
+
if (opts.playing ?? true)
|
|
100
|
+
requirePlaying(game, seat);
|
|
101
|
+
return seat;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Send a write, then read its effect back: a refusal from war.app throws
|
|
105
|
+
* (nothing changed); no answer is `unanswered` (it may have gone through, so
|
|
106
|
+
* nothing is read back and nothing is resent); otherwise the read-back is
|
|
107
|
+
* classified, and a failed read-back is `unreadable`, never a success.
|
|
108
|
+
*/
|
|
109
|
+
export async function sendAndVerify(spec) {
|
|
110
|
+
let ignored;
|
|
111
|
+
try {
|
|
112
|
+
await spec.send();
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
// #272: a failure before the request left (no email, the bridge not ready) changed nothing.
|
|
116
|
+
if (notSent(e)) {
|
|
117
|
+
throw new McpToolError(`The request was not sent (${failureText(e)}), so nothing changed.`, {
|
|
118
|
+
kind: 'unknown',
|
|
119
|
+
hint: e instanceof McpToolError && e.hint
|
|
120
|
+
? e.hint
|
|
121
|
+
: 'Fix what stopped it (a signed-in war.app tab open and paired, or the configured email), then confirm the action again.',
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
if (!(e instanceof WarappServerError))
|
|
125
|
+
return spec.unanswered(failureText(e));
|
|
126
|
+
const decided = spec.refused?.(e);
|
|
127
|
+
if (decided !== 'ignore')
|
|
128
|
+
throw decided ?? e;
|
|
129
|
+
ignored = e;
|
|
130
|
+
}
|
|
131
|
+
let after;
|
|
132
|
+
try {
|
|
133
|
+
after = await spec.readBack();
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
136
|
+
return spec.unreadable(failureText(e), ignored);
|
|
137
|
+
}
|
|
138
|
+
return await spec.classify(after, ignored);
|
|
139
|
+
}
|
|
140
|
+
/** Rethrow a refusal in the client's own wording, when `wordings` has one for its code. */
|
|
141
|
+
export function worded(wordings) {
|
|
142
|
+
return (e) => {
|
|
143
|
+
const wording = wordings[e.code];
|
|
144
|
+
return wording ? new WarappServerError(e.code, `${wording} (war.app error ${e.code})`) : undefined;
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Register a confirm-gated write (mcp-utils' `defineWrite` / `registerWrite`)
|
|
149
|
+
* with its `title` and `_meta`, answered compact inside the untrusted envelope
|
|
150
|
+
* and never batched, unless the definition says otherwise.
|
|
151
|
+
*/
|
|
152
|
+
export function defineGatedWrite(server, config, def) {
|
|
153
|
+
withToolConfig(server, config, () => registerWrite(server, defineWrite({
|
|
154
|
+
render: (r) => respond(undefined, ['compact'], () => r),
|
|
155
|
+
batchable: false,
|
|
156
|
+
...def,
|
|
157
|
+
})));
|
|
158
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The fetchproxy browser bridge to war.app.
|
|
2
|
+
//
|
|
3
|
+
// The web-client protocol (docs/WEB-PROTOCOL.md) is authenticated by the
|
|
4
|
+
// browser's war.app cookies, not by anything in the frame, so every
|
|
5
|
+
// `/Services` call is relayed through the user's signed-in war.app tab by the
|
|
6
|
+
// ContextMint Bridge extension. The GameFeed API (src/client.ts) does not use
|
|
7
|
+
// this — it authenticates with the API token and is called directly.
|
|
8
|
+
import { readPortEnv } from '@chrischall/mcp-utils';
|
|
9
|
+
import { createFetchproxyTransport, createLazyTransport, } from '@chrischall/mcp-utils/fetchproxy';
|
|
10
|
+
import { VERSION } from './version.js';
|
|
11
|
+
export const SERVER_NAME = 'warapp-mcp';
|
|
12
|
+
/** Env var overriding the fetchproxy concentrator port. */
|
|
13
|
+
export const WS_PORT_ENV = 'WARAPP_WS_PORT';
|
|
14
|
+
/** The fleet's shared fetchproxy concentrator port. */
|
|
15
|
+
export const DEFAULT_WS_PORT = 37149;
|
|
16
|
+
/** The extension's trust boundary: requests may only target war.app. */
|
|
17
|
+
export const BRIDGE_DOMAINS = ['war.app'];
|
|
18
|
+
/** Build (but do not start) the war.app bridge transport. */
|
|
19
|
+
export function createBridgeTransport(opts = {}) {
|
|
20
|
+
const env = opts.env ?? process.env;
|
|
21
|
+
return createFetchproxyTransport({
|
|
22
|
+
serverName: SERVER_NAME,
|
|
23
|
+
version: VERSION,
|
|
24
|
+
domains: [...BRIDGE_DOMAINS],
|
|
25
|
+
port: readPortEnv(WS_PORT_ENV, DEFAULT_WS_PORT, { env }),
|
|
26
|
+
debugEnvVar: 'WARAPP_DEBUG',
|
|
27
|
+
env,
|
|
28
|
+
...(opts.createServer ? { createServer: opts.createServer } : {}),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
export function createWarappBridge(opts = {}) {
|
|
32
|
+
return createLazyTransport(() => createBridgeTransport(opts));
|
|
33
|
+
}
|