Skip to content

Add regenerate command and auto-regenerate stale codegen on startup#6

Merged
gabrielbauman merged 2 commits into
mainfrom
gbauman/regenerate-codegen
Jun 3, 2026
Merged

Add regenerate command and auto-regenerate stale codegen on startup#6
gabrielbauman merged 2 commits into
mainfrom
gbauman/regenerate-codegen

Conversation

@gabrielbauman

Copy link
Copy Markdown
Owner

Summary

Re-running an API's code generation after an anyapi-mcp upgrade previously meant re-adding it, which destroys stored credentials: a bearer API re-added without --token resolves to auth: none, and cleanupOrphanedSecrets then deletes its keystore token. This adds a codegen-only path that preserves auth.

What's new

  • anyapi-mcp regenerate [id ...] [--stale-only] — re-fetches each API's saved source and rebuilds its typed client + operation index in place. Auth, baseUrl, and hosts are preserved verbatim; no secret is written or deleted (a bearer token is only read, to re-introspect a source that needs it). Per-API isolation: one unreachable source never blocks the rest.
  • CODEGEN_VERSION stamp — every generated artifact records the codegen-contract version it was built under (a deliberate counter, not the git hash, so doc-only releases don't force needless re-fetches). serve regenerates any entry with an older or missing stamp on startup, before connecting, logging only to stderr. A re-fetch failure is logged and keeps the existing (working) code. Entries written before this field count as stale, so they migrate on first upgraded boot.

Test plan

Verified against a live registry (OpenAPI no-auth/OAuth + GraphQL):

  • regenerate rebuilt all entries incl. a 3068-op spec; an OAuth API (Strava) stayed logged in throughout — credentials untouched.
  • serve startup regenerated only stale entries; a second boot regenerated zero (idempotent — no steady-state network cost).
  • stdout stayed exactly 0 bytes during a startup regen (MCP frame-stream hygiene preserved).
  • deno task check && lint && fmt clean; 9/9 tests pass.

Re-running an API's code generation after an anyapi-mcp upgrade previously
required re-adding it, which destroys stored credentials: a bearer API
re-added without --token resolves to auth:none, and its keystore token is
then deleted. Add a codegen-only path that preserves auth instead.

- regenerate [id ...] [--stale-only]: re-fetch each API's saved source and
  rebuild its typed client + operation index in place. Auth, baseUrl, and
  hosts are preserved; no secret is written or deleted (a bearer token is
  only read, to re-introspect a source that needs it). Each API is
  independent, so one unreachable source never blocks the rest.
- Stamp generated artifacts with CODEGEN_VERSION. serve regenerates any
  entry with an older or missing stamp on startup, before connecting, with
  all logging on stderr. A re-fetch failure is logged and keeps the old code.
Copilot AI review requested due to automatic review settings June 3, 2026 06:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a “codegen refresh” workflow so users can rebuild cached generated clients/operation indexes after upgrading anyapi-mcp without re-registering (and therefore without clobbering/deleting stored credentials). It also teaches serve to auto-regenerate stale entries on startup based on a stored codegen contract version.

Changes:

  • Introduce CODEGEN_VERSION + per-entry codegenVersion to detect stale generated artifacts.
  • Add regenerateApi(s) core logic and a new anyapi-mcp regenerate CLI subcommand.
  • Auto-regenerate stale entries on serve startup while keeping MCP stdout clean (stderr-only diagnostics).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/registry.ts Adds codegenVersion field to registry entries for staleness detection.
src/register.ts Defines CODEGEN_VERSION and implements regenerateApi(s) to rebuild codegen in place.
src/main.ts Wires the new regenerate subcommand into CLI dispatch/usage.
src/commands/serve.ts Regenerates stale entries on startup before serving (stderr-only).
src/commands/regenerate.ts New CLI command for targeted/all regeneration with --stale-only.
README.md Documents the new regenerate command and serve startup regeneration behavior.
CLAUDE.md Updates contributor/architecture docs to include regeneration behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/registry.ts Outdated
Comment on lines 59 to 60
/** Absolute path to the generated .d.ts in the cache dir. */
typesPath: string;
Comment thread CLAUDE.md Outdated
Comment on lines +77 to +79
generated artifact is stamped with `CODEGEN_VERSION`; bump that constant when
a generator change makes old artifacts stale, and `serve` regenerates anything
older on startup (an entry missing the stamp counts as stale).
Address review: typesPath is a .d.ts only for OpenAPI/GraphQL (SOAP emits a
.ts runtime module), and the version stamp lives on the registry entry's
codegenVersion field, not embedded in the generated artifacts themselves.
@gabrielbauman gabrielbauman merged commit cfb3b92 into main Jun 3, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants