This file provides guidance to coding agents (Claude Code, Codex, and others) when working with code in this repository.
Discogs MCP — a Cloudflare Workers MCP server that lets AI assistants interact with users' Discogs music collections. Implements OAuth 1.0a authentication against the Discogs API, intelligent search with mood mapping, and context-aware recommendations. Deployed at discogs-mcp.com (locked to maintainer; users must self-host).
npm run dev # Local dev server on port 8787 npm run build # Dry-run build (wrangler deploy --dry-run) npm test # Vitest with Cloudflare Workers pool npm test -- path/to/file # Single test file npm test -- --watch # Watch mode npm run lint # ESLint (must pass before every commit) npm run format # Prettier --write npm run format:check # Prettier --check npm run deploy # Deploy to dev environment npm run deploy:prod # Deploy to production npm run cf-typegen # Regenerate Cloudflare Workers types
Entry point: src/index-oauth.ts — routes requests through OAuthProvider which handles OAuth endpoints, then delegates MCP traffic to the server created by src/mcp/server.ts.
src/
├── auth/ # OAuth 1.0a flow against Discogs
├── clients/ # DiscogsClient (API calls + retry) → CachedDiscogsClient (KV cache wrapper)
├── mcp/
│ ├── server.ts # MCP server factory — registers all tools, resources, prompts
│ ├── tools/
│ │ ├── public.ts # Unauthenticated: ping, server_info, auth_status
│ │ └── authenticated.ts # Requires auth: search_collection, search_discogs, get_release, etc.
│ ├── resources/ # MCP resource definitions
│ └── prompts/ # MCP prompt templates
├── rate-limiter/ # Durable Object for coordinated rate limiting across Workers
├── types/ # Shared TypeScript types
├── utils/
│ ├── moodMapping.ts # Maps emotional descriptors ("mellow", "Sunday vibes") → genres/styles
│ ├── searchRanking.ts # Deduplication, scoring, ranking of search results
│ ├── searchQueryParser.ts # Classifies queries (mood, specific, genre, etc.)
│ ├── cache.ts # KV-based caching utilities
│ └── kvLogger.ts # Structured logging to KV
└── marketing-page.ts # HTML landing page
- MCP transport:
createMcpHandlerfromagents/mcp/serverwith a per-request server factory (@modelcontextprotocol/serverv2, protocol 2026-07-28, legacyinitializeclients served by the handler's compatibility lane).allowedOriginHostnamesreplaces the handler defaults and is built per request from the request hostname plus localhost, so self-hosted copies accept their own browser clients. The repo's own?session_id=parameter selects the manual-login session path; there is noMcp-Session-Idheader. Hand-rolled requests to the modern lane fail (missing _meta); use an SDK client - Request context injection: MCP server injects auth context before each tool handler call — tools receive the authenticated user's Discogs client
- Two-layer caching: CachedDiscogsClient wraps DiscogsClient; cache keys in KV with TTLs to stay within Discogs' 60 req/min rate limit
- Rate limiting via Durable Objects:
src/rate-limiter/durable-object.tscoordinates rate limits across Worker instances. Its queue holds 20 requests (MAX_QUEUE_DEPTH), so a tool that makes many Discogs calls in one invocation issues them sequentially - Mood intelligence:
moodMapping.tsconverts emotional/situational descriptors into genre and style filters — core differentiator of the search experience - Adding or renaming a tool: the catalogue is listed by hand in several places that must move together: the
auth_statuslists insrc/mcp/tools/public.ts(signed-in and signed-out), the tool count and annotation map intest/mcp/server.test.ts, the tool count intest/integration/mcp-client.test.ts, the catalogue snapshot (npm test -- test/mcp/server.test.ts -u, then read the diff), and the tables inREADME.md.test/mcp/tools/authStatusToolList.spec.tsfails ifauth_statusdrifts - OAuth dance: Discogs uses OAuth 1.0a (not 2.0);
oauth-1.0alibrary signs requests. The Workers OAuth Provider wraps this with OAuth 2.1 for MCP clients
- Runtime: Cloudflare Workers (ES2021 target, Node.js compat enabled)
- Storage: KV namespaces for sessions (
MCP_SESSIONS) and OAuth state (OAUTH_KV); sync outcomes structured-log to console (Workers Observability) - Durable Objects:
DiscogsRateLimiterclass - Secrets (via
wrangler secret put):DISCOGS_CONSUMER_KEY,DISCOGS_CONSUMER_SECRET - CI: GitHub Actions — lint → test → build → auto-deploy to prod on main push
- ABOUTME comments: Every code file starts with a 2-line comment where each line begins with
ABOUTME:— explains what the file does - Formatting: Prettier — 140 char width, single quotes, no semicolons, tabs
- TypeScript: Strict mode, explicit types, avoid
any(ESLint warns) - Naming: No temporal names like "improved", "new", "enhanced" — names must be evergreen
- Evergreen comments: Describe code as-is, never reference refactors or recent changes
- Error handling: Fail fast for programming errors, recover gracefully for user/external errors
- Changes: Make the smallest reasonable change. Never rewrite from scratch without explicit permission
- Unrelated fixes: Don't fix unrelated issues in a task — document them in
development/todo.mdwith priority (P0/P1/P2) - Schema validation: Zod for runtime validation of API responses and tool inputs
- Framework: Vitest 4 with
@cloudflare/vitest-pool-workers0.22 — tests run in a Workers-compatible environment. Requires Node 22 (see.nvmrc) - A mock that the source constructs with
newmust be implemented as afunctionor a class, not an arrow function:vi.fn().mockImplementation(function () { return { ... } }) wrangleris pinned to the exact version@cloudflare/vitest-pool-workersdepends on, so tests,npm run devand deploys share one wrangler and one workerd. When bumping vitest-pool-workers, move the pin tonpm view @cloudflare/vitest-pool-workers@<version> dependencies.wranglerin the same change- Change dependencies with
npx npm@11 install. npm 10 crashes on this dependency graph (Cannot read properties of null (reading 'edgesOut'));npm cion npm 10 works from the resulting lockfile.zodis held at 4.1.13 at the top level while vitest-pool-workers nests its own copy - Mock Discogs API responses for unit tests; real protocol compliance checks for integration tests
- Test output must be clean — no warnings, no unexpected errors in logs
- Tests live in
test/mirroringsrc/structure
GitHub releases follow Rian's side-project release notes contract: ~/git/product-ai/05-personal/side-projects/release-notes-contract.md. Read it before drafting. The name is discogs-mcp vX.Y.Z — <tagline> (em dash), and the body is a 1-3 sentence opening paragraph, then only ## Breaking changes / ## What's new / ## Fixes / ## Under the hood, written from the user's side.
A release stays on GitHub unless Rian explicitly asks for an elezea.com cross-post. Only then, put <!-- post-to-elezea --> on its own line at the end of the body: .github/workflows/elezea-release-post.yml posts a release that carries the marker to elezea.com, and the endpoint rejects anything off-contract. Adding the marker after publishing does nothing, because the workflow runs only when a release is published. A release runs the workflow from its tagged commit, so tag a commit that includes this version of the workflow.
Check every claim against the commits since the last tag, show Rian the final text, and wait for a go before gh release create.