FoundryVTT MCP Server Documentation - v1.5.3
    Preparing search index...

    Class FoundryClient

    Index

    Constructors

    Methods

    • Connects to FoundryVTT. REST API mode: tests /api/status endpoint. Socket.IO mode: authenticates and loads full worldData.

      Returns Promise<void>

    • Reports whether the client currently has a live link to FoundryVTT (#217).

      Socket.IO mode answers from the socket itself rather than from a latched flag, so a link that dropped without an explicit disconnect() — server restart, network loss — reads as disconnected immediately, even if the disconnect event has not been delivered yet. It tracks the socket in both directions: an automatic reconnect (onSocketConnect) reads as connected again rather than staying latched off.

      REST API mode (FOUNDRY_API_KEY) has no socket to ask, so it answers from the last REST request that actually happened: a request that failed to reach FoundryVTT (connection refused, reset, timed out) reads as disconnected from then on, and the next request that gets through reads as connected again. An HTTP error status does not count as a drop — the server answered. This is a last observed outcome, not a live probe: it cannot notice a server that went away between requests, and it never does I/O of its own, because this accessor is synchronous and widely called.

      Returns boolean

    • True when the cached snapshot is no longer being kept live by broadcasts — i.e. the socket dropped after a world load (#217). Reads still answer from the cache; this flags that the answer is a point-in-time copy.

      Returns boolean

    • Re-emits 'world' on the existing socket to refresh the cached snapshot.

      Registers a one-shot 'world' listener and cleans it up on every exit path (success, error, timeout) via socket.off() so that repeated refreshes over a long-running session do not leak listener handles.

      Returns Promise<void>

    • Returns the raw WorldActor with the full system data (game-system specific).

      Parameters

      • actorId: string

      Returns WorldActor | undefined

    • Patches attributes on an actor's system object (#143). WRITE — Socket.IO.

      patch keys are dot-paths into actor.system (e.g. attributes.hp.value, currency.gp, spells.spell1.value, attributes.exhaustion). Each key is prefixed with system. and sent through the Socket.IO modifyDocument write protocol as an Actor update — matching FoundryVTT's own document model (Actor#update). No REST call and no apiKey are involved.

      Client-side validation, using the actor's current data, rejects:

      • HP value exceeding max + temp,
      • spell-slot value exceeding its max,
      • exhaustion outside 0–10 (2024 rules) or 0–6 (2014 rules).

      Parameters

      Returns Promise<ActorAttributeUpdateResult>

      via assertWriteable() if writeEnabled is false or the socket is not connected; also if the id is malformed, the actor/path is missing, or a validation rule is violated.

    • Creates a new item on an actor via the modifyDocument socket protocol.

      Inline sources are created directly. Compendium sources are NOT yet supported over Socket.IO — copying a pack entry needs a compendium read that modifyDocument does not provide (tracked in issue #159).

      Parameters

      • actorId: string

        16-char alphanumeric actor document id

      • source: ActorItemCreateSource

        inline item document (compendium source throws)

      Returns Promise<FoundryItem>

      the newly created item document

    • Applies a JSON merge patch to an item owned by an actor.

      The patch is merged into the item's system data (recursively, so nested paths like the D&D 5e v4+ activities.{id}.consumption.targets are preserved). Performed via the modifyDocument socket protocol.

      Parameters

      • actorId: string

        16-char alphanumeric actor document id

      • itemId: string

        16-char alphanumeric item document id

      • patch: Record<string, unknown>

        shallow/nested JSON merge patch applied to item.system

      Returns Promise<FoundryItem>

      the updated item document

    • Deletes an item owned by an actor via the modifyDocument socket protocol.

      Parameters

      • actorId: string

        16-char alphanumeric actor document id

      • itemId: string

        16-char alphanumeric item document id

      Returns Promise<void>

    • Updates the active combat's turn/round pointers (FR-018).

      Combat is a top-level document, so the update carries no parentUuid. The patch fields map directly onto the Combat document (turn, round).

      Parameters

      • combatId: string

        16-char alphanumeric Combat document id

      • patch: { turn?: number; round?: number }

        turn and/or round to set on the combat

      Returns Promise<unknown>

      the updated combat document

    • Ends (deletes) the active combat encounter (FR-018).

      Parameters

      • combatId: string

        16-char alphanumeric Combat document id

      Returns Promise<void>

    • Sets a combatant's initiative (FR-018).

      Combatant is an embedded document inside Combat, so the update is sent with parentUuid: "Combat.<combatId>".

      Parameters

      • combatId: string

        16-char alphanumeric Combat document id (the parent)

      • combatantId: string

        16-char alphanumeric Combatant document id

      • initiative: number

        finite initiative value to assign

      Returns Promise<unknown>

      the updated combatant document

    • Starts a new combat encounter and seeds its combatants (FR-018, #172).

      Two-step modifyDocument flow:

      1. Create the top-level Combat document (no parentUuid), activated on the given scene, and read its _id from the response.
      2. Create the embedded Combatant documents with parentUuid: "Combat.<combatId>" (mirrors the Combatant→Combat embed used by setCombatantInitiative).

      The create wire shape is verified against the v13.348 client source per .claude/rules/foundry-write-protocol.md; smoke-test one live round-trip when changing it.

      Parameters

      • sceneId: string

        16-char alphanumeric Scene document id the combat runs on

      • combatants: { tokenId: string; sceneId: string; actorId?: string }[]

        combatant seeds ({ tokenId, sceneId, actorId? })

      Returns Promise<{ combatId: string; combatantCount: number }>

      the new combat id and the number of combatants created

    • Locates a token (and the scene it lives on) in the cached worldData.

      Token is an embedded document of Scene; worldData carries each scene's tokens as raw records. When sceneId is omitted the search spans every scene, so a token can be moved/affected without first resolving its scene.

      Parameters

      • tokenId: string

        16-char alphanumeric Token document id

      • OptionalsceneId: string

        optional Scene id to scope the search to

      Returns { scene: WorldScene; token: Record<string, unknown> } | null

      the owning scene and the raw token record, or null if not found

    • Moves a token to new x/y coordinates (FR-019).

      Token is an embedded document of Scene, so the update is sent with parentUuid: "Scene.<sceneId>" (mirrors the Combatant→Combat embed). The wire shape is verified against the v13.348 client source per .claude/rules/foundry-write-protocol.md.

      Parameters

      • sceneId: string

        16-char alphanumeric Scene document id (the parent)

      • tokenId: string

        16-char alphanumeric Token document id

      • x: number

        target x pixel coordinate (finite number)

      • y: number

        target y pixel coordinate (finite number)

      Returns Promise<unknown>

      the updated token document

    • Creates a status-effect ActiveEffect on a token's actor (FR-019).

      ActiveEffect is an embedded document of Actor, so the create is sent with the actor's parent UUID:

      • Actor.<id> for a world-linked actor (actorLink: true)
      • Scene.<sid>.Token.<tid>.Actor.<aid> for an unlinked token's synthetic actor (the per-token delta).

      The effect carries a statuses array, matching how FoundryVTT v11+ models conditions (Actor#toggleStatusEffect toggles by this field).

      Parameters

      • parentActorUuid: string

        the token actor's parent UUID (see forms above)

      • statusId: string

        condition id (e.g. "prone", "stunned")

      • options: { name?: string; img?: string } = {}

        optional display name (defaults to statusId) and img

      Returns Promise<WorldEffect>

      the newly created ActiveEffect document

    • Deletes an ActiveEffect from a token's actor (FR-019), e.g. to clear a status condition. Accepts the same parent-UUID forms as createActorStatusEffect.

      Parameters

      • parentActorUuid: string

        the token actor's parent UUID

      • effectId: string

        16-char alphanumeric ActiveEffect document id

      Returns Promise<void>

    • Creates a new JournalEntry with one or more text pages.

      JournalEntry is a top-level document (unlike Item/ActiveEffect, which are embedded in an Actor), so the create carries no parentUuid — mirrors startCombat's top-level Combat create. Each entry in pages is mapped to Foundry's native JournalEntryPage text-page shape, with an explicit sort so the pages render in the order supplied.

      Parameters

      • name: string

        journal entry title

      • pages: JournalPageCreateSource[]

        one or more pages (name + content); at least one required

      • Optionalfolder: string

        optional 16-char Folder document id to file the entry under

      • Optionalvisibility: DocumentVisibility

        who can see the entry (#204); omitted means GM-only, which is FoundryVTT's default for a newly created document

      Returns Promise<WorldJournal>

      the newly created journal entry document

    • Rolls a dice formula.

      Validation is deliberately per transport, because the two transports are not equally capable (#219):

      • REST (FOUNDRY_API_KEY) posts the formula to /api/dice/roll, where FoundryVTT's own Roll engine evaluates it. That engine understands more than this module does — parentheses, for one — so only the DICE_FORMULA_ALPHABET check applies here. Imposing the local parser's narrower grammar would take away a capability the transport has. What the alphabet does refuse is refused by name and position (unexpected "k" at position 3, via alphabetViolation), so the two transports are equally specific about what they would not evaluate.
      • Socket.IO / no API key has no remote evaluator: fallbackDiceRoll is the roller, so the grammar its parser can represent is the grammar accepted, and that parser is the only gate. No alphabet pre-check runs ahead of it, so its specific message (unexpected "k" at position 3) reaches the caller instead of a generic Invalid dice formula: 4d6kh3. Nothing is ever dropped from a total in silence.

      The length cap is common to both. A REST roll that cannot reach FoundryVTT falls through to the local roller, which then applies the strict grammar — a formula only Foundry could evaluate errors out rather than being mis-totalled locally.

      Parameters

      • formula: string
      • Optionalreason: string

      Returns Promise<DiceRoll>

    • Type Parameters

      • T = unknown

      Parameters

      • url: string
      • Optionalconfig: AxiosRequestConfig<any>

      Returns Promise<AxiosResponse<T, any, {}>>

    post

    • post<T = unknown>(
          url: string,
          data?: unknown,
          config?: AxiosRequestConfig<any>,
      ): Promise<AxiosResponse<T, any, {}>>

      Type Parameters

      • T = unknown

      Parameters

      • url: string
      • Optionaldata: unknown
      • Optionalconfig: AxiosRequestConfig<any>

      Returns Promise<AxiosResponse<T, any, {}>>

    • Type Parameters

      • T = unknown

      Parameters

      • url: string
      • Optionaldata: unknown
      • Optionalconfig: AxiosRequestConfig<any>

      Returns Promise<AxiosResponse<T, any, {}>>

    • Type Parameters

      • T = unknown

      Parameters

      • url: string
      • Optionalconfig: AxiosRequestConfig<any>

      Returns Promise<AxiosResponse<T, any, {}>>