# AGENTS.md Single source of truth for working on this repository — for **any** model or agent; Claude Code reads it directly too, so there is no `CLAUDE.md`. If one is ever added, it must import this file (`@AGENTS.md`): Claude Code reads AGENTS.md on its own only when no CLAUDE.md exists. (For end-user/runtime docs, see [README.md](README.md).) ## Project shape A TypeScript MCP server for the [AniList](https://anilist.co) GraphQL API. Reads (search/details/genres/tags/recommendations/threads/activity/public user data) call the public GraphQL endpoint directly and need no credentials. Personal-list and social tools (add/update/remove list entries, favourites, follow, posting/deleting activity or threads, `update_user`, `get_notifications`) act on the authenticated user's own AniList account and require a user access token — see [docs/auth.md](docs/auth.md) for the OAuth model and [docs/api-references.md](docs/api-references.md) for AniList's API specifics (rate limits, mutation semantics, considerations) before changing the client. ``` src/ index.ts # bin entry — calls start() server.ts # buildServer() + start(); registers everything config.ts # env → validated Config (zod) version.ts # VERSION constant, kept in sync with package.json (checked by a test) lib/ # http, rateLimit, cache, tokenStore, oauthLogin, errors, logger, # result, cancel (the call's AbortSignal), graphql (the # GraphQL request/error-mapping layer) clients/ # anilist.ts — thin AniListClient facade (auth/config/login, # exposes ctx(): AniListContext); anilist/*.ts — one file per # domain (activity, context, favourites, fields, list, media, # misc, notification, people, recommendation, search, thread, # user), matching src/tools/* (plus fuzzyDate.ts: the # date-filter encoders several domains share, and # deleteById.ts: the four Delete* mutations' shared call). # Tool handlers call these domain functions directly # (e.g. `media.getMedia(client.ctx(), ...)`) rather than through a same-named method on AniListClient. # anilist/ids.ts — branded numeric-ID type aliases (MediaId, # ListEntryId, etc.), one per domain concept; every domain # file's exported functions take these instead of plain # `number` so e.g. a MediaId can't compile where a # ListEntryId is expected. Compile-time only — see the # file's own comment for why it has no zod runtime import. # anilist/enums.ts — single source for the AniList enums # re-typed on both sides of the boundary (MediaType, # MediaListStatus, FavouriteKind, birthday kinds): one # `as const` array per enum, feeding z.enum() in tools/ and # the derived `(typeof X)[number]` type in these domain # signatures, so runtime validation and the compile-time # types can't drift. Lives here (not tools/) because # clients/ can't import from tools/. tools/ # misc.ts, activity.ts, list.ts, media.ts, notification.ts, # people.ts, recommendation.ts, search.ts, thread.ts, user.ts, # login.ts, register.ts (the one tool-registration wrapper), # outputSchemas.ts # (shared output-schema fragments, incl. anilistId and its # per-domain branded variants — mediaId, listEntryId, etc.) prompts.ts # MCP Prompts: multi-step plans that orchestrate the read tools __tests__/ # node:test (*.test.ts) + helpers.ts scripts/ # run-tests.ts, check-api.ts, sync-version.ts, # audit-bundle.ts (CI: npm audit over the inlined packages), # preversion-check.ts (npm `preversion` gate — see the `release` skill) .agents/skills/ # reusable agent workflows for this repo (e.g. live-audit/) — # plain Markdown with a YAML frontmatter name/description, # not tied to any one tool's orchestration features, per # this file's agent-agnostic policy; same skill name/layout # as this project's sibling MCP servers (tmdb-mcp, mal-mcp, # steam-games-mcp) — sync improvements both ways rather # than letting them drift. `.claude/skills` is a symlink # here (Codex CLI/Gemini CLI read `.agents/skills` # directly, Claude Code the symlink), so no skill content # is duplicated per client path. ``` ## Why this server exists The pre-existing third-party `anilist-mcp` (npm) cannot write list entries at all — its `add_list_entry`/`update_list_entry` require nested-object input fields, but its GraphQL-mutation builder throws on any nested value (a confirmed upstream bug, [anilist-mcp#13](https://github.com/yuna0x0/anilist-mcp/issues/13)). This server avoids that whole bug class by talking to AniList's GraphQL API directly through a small first-party client (`lib/graphql.ts` + `clients/anilist.ts`) instead of a third-party wrapper library: it builds its own `variables` object and only includes keys that are actually set — GraphQL naturally omits absent optional arguments, no dummy placeholder values needed. ## Commands ```sh npm run build # tsc --noEmit + tsdown → dist/index.js (single ESM bundle) npm test # node:test straight on src/**/*.test.ts (native type stripping) npm run test:coverage # same, with coverage (gate: ~80%) npm run lint # eslint npm run format # prettier --write npm run check:api # live upstream health-check (network) ``` ## Conventions - **Docs and in-code text are English** (README, docs, comments, tool descriptions, error messages). - Runtime floor is **Node ≥ 22.18.0** — the first 22.x with TypeScript type stripping on by default (what runs the tests and `scripts/*.ts`). tsdown reads its target from `engines.node`; `tsconfig.json` uses `module: "node20"` with an ES2024 target/lib (Node 22's V8 12.4 has all of ES2024, e.g. `Promise.withResolvers()`, `Object.groupBy()`, but not ES2025's `Promise.try()`/`RegExp.escape()`). Keep `engines`, `manifest.json`'s `compatibility.runtimes.node` and the CI matrix's lowest entry in sync. Tests may run on newer Node but must not raise the runtime floor. - Log to **stderr only** — stdout is the MCP protocol channel. Use the logger; it redacts credentials. - **Tool failures: throw, never build an error result.** Every tool is registered through `tools/register.ts`'s `tool(name, config, handler)` (each `registerXTools()` gets it instead of the McpServer); it runs the handler with the call's cancellation signal, checks a success against the tool's own `outputSchema` (a mismatch is `internal`) and turns any throw into an `isError` result via `lib/result.ts`. Throw `ApiError` for an upstream failure (facts: code, status, AniList's own `detail`, and this server's one-sentence `note` naming the occurrence) and `ToolError` for this server's own decisions (`invalid_arguments`, `login_required`, `not_configured`) with the sentence the model should read; anything else is `internal`. `toProblem()` is the only place tool-facing prose is written: the text is the problem's `detail`, and the RFC 9457 problem rides in `_meta["io.github.Grinv.anilist-mcp-server/problem"]` with no `structuredContent`. A new code needs a `PROBLEMS` entry and a `docs/errors.md` section (a test checks both). Mutation/personal tools are gated by `ctx.requireAuth()` (defined in `clients/anilist/context.ts`, implemented in `clients/anilist.ts`), called at the top of each domain function that needs it — it throws `login_required` before any network call rather than making a doomed authenticated request. - Write tool `description`s and per-field `.describe()` text for the calling model: explain when to use a tool and what each parameter means. Check new or edited descriptions against the `tool-description-check` skill (Glama's TDQS rubric) before committing. - **Name a field for what it actually accepts, not a generic ID suffix** — e.g. `user` (accepts either a numeric AniList ID or an exact username, see `outputSchemas.ts`'s shared `userIdOrName`) rather than `user_id`/`userId`, which would wrongly imply numbers only. Keep the same field name for the same concept across every tool that takes it — grep sibling tools before naming a new field for an existing concept. A name that silently diverges (e.g. `search_activity`'s old `userId` vs every other user-scoped tool's `user`) once made the mismatched key silently dropped, so the tool fell back to whatever the field being absent means instead of erroring (see `CHANGELOG.md` 0.2.2 for the bug this caused live). Every tool `inputSchema` is now a `z.strictObject()` — an unknown key is rejected ("Unrecognized key") rather than dropped; keep new ones strict too (a test in `tools.test.ts` checks every registered tool). - Every tool declares an `outputSchema` alongside `inputSchema`, describing the `structuredContent` shape `jsonResult()` returns. Model top-level keys precisely; for arrays of AniList media/character/staff/etc. objects and other deeply-nested GraphQL substructures, a loose `.loose()` shape is fine (`z.json()` for a field that's genuinely AniList's own untyped GraphQL `Json` scalar, e.g. `advancedScores`/`childComments`/`theme`) — the SDK validates `structuredContent` against `outputSchema` at runtime, so a schema that's too strict fails real tool calls (a good signal, not just a style nit — but verify a bound against live AniList data before adding it, the same way `anilistId`/`fuzzyDateOut`'s `.positive()` are documented as confirmed live; a plausible-sounding bound like "an episode number is always ≥1" can be wrong — AniList's `AiringSchedule.episode` does return `0` for some titles). - **Never use `z.date()`/`z.bigint()`/`z.nan()`/`.transform()`/`z.map()`/ `z.set()`/`z.symbol()`/`z.void()`/`z.custom()` in a tool's `inputSchema` or `outputSchema`.** `@modelcontextprotocol/server` v2 converts every registered schema to JSON Schema via Zod's own `~standard.jsonSchema` bridge (`standardSchemaToJsonSchema()` in `node_modules/@modelcontextprotocol/server/dist/src-*.mjs`) with no `unrepresentable` override — and that bridge, like `z.toJSONSchema()` itself, defaults `unrepresentable` to `"throw"` (`zod/v4/core/to-json-schema.js`), not `"any"`. Since SDK 2.3.0 that conversion is lazy (at `tools/list`, not `registerTool()`), so the server now starts fine — but one of these types anywhere in any tool schema makes every `tools/list` call throw (confirmed on 2.3.1: "Date cannot be represented in JSON Schema"), i.e. the client sees no tools at all, not just a broken one. Worse than the old startup crash, since nothing fails until a client connects. A reach for `z.date()` on a "since"-style parameter is the likely way this bites; use a date/time **string** format instead (e.g. `z.iso.datetime()`). - Mocked-`fetch` test fixtures must mirror the real upstream response shape for that exact query, not just whatever fields make the current code pass — see the `fixture-accuracy-check` skill. - **Schema-first for hand-built internal shapes** (config, persisted state — not GraphQL passthrough objects, which stay loose per the `outputSchema` rule above): when a type describes an object the code itself constructs from external input (env vars, an on-disk file, an OAuth response), define the Zod schema first and derive the type via `z.infer`, then build the value through `schema.parse()`/`.safeParse()`/`.transform()` rather than a hand-written `interface` kept in sync by convention with a separately-built object literal — see `Config`/`AniListAuth` (`config.ts`) and `TokenState` (`lib/tokenStore.ts`). This is a house style going forward, not (yet) retrofitted onto every existing hand-written interface over external input (e.g. `GraphQLResponse` in `lib/graphql.ts` still isn't converted) — migrate an existing one opportunistically when you're already touching it, don't do a drive-by rewrite. - Keep dependencies minimal. New deps need a clear justification (supply-chain). Runtime packages (`@modelcontextprotocol/server`, `zod`) live in `devDependencies`, not `dependencies`: tsdown inlines them into `dist/index.js`, so `npx` installs nothing. A new runtime package must be added to `tsdown.config.ts`'s `bundledDeps` or the build fails; CI audits the bundled packages with `scripts/audit-bundle.ts`. - `manifest.json` lists no `prompts`, only `prompts_generated: true`: mcpb's static prompt text can't express the argument handling in `src/prompts.ts` (e.g. a numeric user ID vs a quoted username), so clients take the prompts from the server's own `prompts/list`. Its `tools` list stays hand-written (short descriptions) and is checked against the registered tools. - **Tests and tooling files run as TypeScript directly** through Node's native type stripping (`node --test src/**/*.test.ts`, `node scripts/foo.ts` — no compile step), so all of `src/` and `scripts/` follow its rules: only erasable syntax (no `enum`/`namespace`/parameter properties; `erasableSyntaxOnly` enforces it), `import type` for type-only imports (`verbatimModuleSyntax`), and relative imports name the real `.ts` file (`allowImportingTsExtensions`; tsdown bundles them the same way). `eslint.config.mjs` stays plain JS with `// @ts-check` + JSDoc: ESLint loads a `.ts` config only through `jiti` (an extra dependency) or an unstable flag editors don't pass. These, plus `tsdown.config.ts`, are type-checked via a separate `tsconfig.scripts.json` (`npm run typecheck:scripts`, folded into `npm run lint`) instead of the main `tsconfig.json`, which only covers `src/`. - **Never commit secrets.** Credentials come from env vars, the `login_anilist` OAuth flow, or the on-disk token store (`tokenStore.ts`, `0600`) — never hardcoded or committed. Unlike MAL, AniList issues a `client_secret` for every app (no "public vs confidential app type" pitfall) and does **not** support refresh tokens — access tokens are long-lived JWTs (~1 year); re-authentication via `login_anilist` is the only way to renew one. - **MCP SDK**: this project runs on stable **`@modelcontextprotocol/server` v2** (`2.4.0`, pinned exact — not a `^range`), like the sibling servers (`mal-mcp`/`tmdb-mcp`/`steam-games-mcp`). Re-run the full test suite, including `e2e.test.ts`, before merging any future SDK bump. - **No `logging` MCP capability.** `notifications/message`/`logging/setLevel` were deprecated in protocol version 2026-07-28 (SEP-2577) in favor of stderr/OpenTelemetry — which `lib/logger.ts` already does (stderr-only, redacted). Don't re-add the capability; if a future need for structured client-visible logs comes up, look at whatever SEP-2577's eventual replacement is, not the deprecated push-style API. The same applies to sampling/elicitation/roots (also deprecated in that revision) — none of them are used here, and the deprecated push-style versions aren't worth building on. - Cross-platform: macOS, Linux and Windows. Avoid POSIX-only shell in npm scripts (use the Node helper scripts). - **Commits:** author/committer `Grinv <4070730+Grinv@users.noreply.github.com>`; do **not** add a `Co-Authored-By` trailer. This repo has no local/global git `user.name`/`user.email` configured, so a bare `git commit` silently falls back to whatever the OS/environment auto-detects (confirmed: has produced a wrong-author commit before) — before your **first** commit each session, check `git log -1 --format='%an <%ae>'`, and if it isn't the identity above, pass `GIT_AUTHOR_NAME`/`GIT_AUTHOR_EMAIL`/`GIT_COMMITTER_NAME`/ `GIT_COMMITTER_EMAIL` as env vars on every `git commit`/`npm version` invocation rather than relying on ambient config. Don't fix this by running `git config` yourself — that's a standing instruction outside this file. - **CodeQL** (`.github/workflows/codeql.yml`) scans `javascript-typescript` on push/PR to main plus a weekly cron — no local equivalent command; findings surface under the repo's **Security → Code scanning** tab. ## Testing the live/published server For a full audit of the currently published (or just-fixed) package — build/test/lint plus hammering the live MCP tools with edge cases, cross-checked against source — follow [.agents/skills/live-audit/SKILL.md](.agents/skills/live-audit/SKILL.md). It covers the safety rules for testing mutation tools against a real authenticated account, what edge cases to cover, and known bug classes found in past passes worth checking don't recur. ## Before opening a PR Run `npm run build && npm test && npm run lint && npm run format:check`. Update `CHANGELOG.md` (Unreleased section) — see the `changelog-style` skill for entry style. ## Releasing `package.json` is the single source of truth for the version; `npm version` bumps + syncs every derived file + tags the release. See the `release` skill for the full steps (including the `preversion` gate on `CHANGELOG.md` and tool descriptions) and MCP Registry details. ## Reuse / shared architecture This server follows the same reusable shape as its siblings: a generic carcass (`src/lib/` + build tooling, tests infra, CI) and a thin domain layer (`config.ts`, `clients/`, domain `tools/`, `prompts.ts`, `check-api.ts`), bootstrapped from the **`mcp-server-template`** repository. Extract `lib/` into a shared npm package only once cross-server duplication actually hurts (YAGNI) — not before.