agentleFS
CloudflareKnown publishercloudflare/workers-oauth-provider/AGENTS.md

workers-oauth-provider

AGENTS.md1.9k starsChanged 2 months agoMITView on GitHub

@cloudflare/workers-oauth-provider is a production-grade OAuth 2.1 provider library for Cloudflare Workers. It implements authorization code flow with PKCE, OAuth discovery, client registration, token exchange, audience validation, and encryption of sensitive data stored in KV. Primary use case: This library powers authorization for MCP (Model Context Protocol) servers. MCP servers are OAuth Resource Servers, and this library provides both protected-resource middleware and the authorization server functionality needed to secure them. This library was largely written with Claude AI assistance, with all…

  • Installs packages

Trending

More talk lately.about the whole repository

Dec 2025: 1 mentionJan 2026: 0 mentionsFeb 2026: 0 mentionsMar 2026: 0 mentionsApr 2026: 0 mentionsMay 2026: 0 mentionsJun 2026: 4 mentionsJul 2026: 2 mentionsAug 2026: 7 mentionsSep 2026: 9 mentionsOct 2026: 20 mentionsNov 2026: 0 mentions
Dec 2025Jun 2026Nov 2026
Mentions a month of the cloudflare/workers-oauth-provider repository that our weekly crawl found. There is no adoption data for this file yet.

Who's talking about cloudflare/workers-oauth-provider

The biggest 5 of 53 mentions linking to the repository that our crawl found. Each line is their own headline.

See all 53

Talked about on GitHub, Bluesky and Hacker News (53 times).

On GitHub · 44

39 more on GitHub

On Bluesky · 8

3 more on Bluesky

On Hacker News · 1

  • Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.

    Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.

  • In this repositoryOne other file it gives its agents.

The file

We split this AGENTS.md into the rules it gives an agent

11 rules. About 0 are common, 9 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈9

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When modifying OAuth functionality, always check the latest published MCP specification (not drafts):

    line 13
  2. Distinctive:

    When in doubt about OAuth behavior, the MCP specification takes precedence for MCP-related use cases.

    line 30
  3. Distinctive:

    All public methods and interfaces must have JSDoc documentation

    line 104
  4. Distinctive:

    Only token holders can decrypt their associated props

    line 140
  5. Distinctive:

    All checks must pass before merge.

    line 223
  6. Very specific:

    Run npm run check before considering work done

    line 263
  7. Distinctive:

    Add tests for new functionality

    line 264
  8. Distinctive:

    Document public APIs with JSDoc

    line 265
  9. Distinctive:

    Consider security implications of changes

    line 266
  10. Very specific:

    Use any type without explicit justification

    line 282
  11. Distinctive:

    Update this file when:

    line 288

≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

AGENTS.md

Project overview

@cloudflare/workers-oauth-provider is a production-grade OAuth 2.1 provider library for Cloudflare Workers. It implements authorization code flow with PKCE, OAuth discovery, client registration, token exchange, audience validation, and encryption of sensitive data stored in KV.

Primary use case: This library powers authorization for MCP (Model Context Protocol) servers. MCP servers are OAuth Resource Servers, and this library provides both protected-resource middleware and the authorization server functionality needed to secure them.

This library was largely written with Claude AI assistance, with all code thoroughly reviewed by Cloudflare security engineers.

MCP specification compliance

When modifying OAuth functionality, always check the latest published MCP specification (not drafts):

This library must be feature-complete with the latest published MCP spec version. Key MCP auth requirements:

  • MCP servers are OAuth Resource Servers with protected resource metadata (RFC 9728)
  • MCP clients use Resource Indicators (RFC 8707), and servers validate token audience
  • Client registration supports pre-registration and Client ID Metadata Documents (CIMD)
  • Dynamic Client Registration (DCR) is deprecated in MCP 2026-07-28 and retained for compatibility
  • Authorization responses use issuer identification (RFC 9207)
  • Streamable HTTP transport uses OAuth 2.1 for authentication

When in doubt about OAuth behavior, the MCP specification takes precedence for MCP-related use cases.

Repository structure

workers-oauth-provider/
├── src/
│   ├── oauth-provider.ts      # Core provider implementation
│   ├── oauth-capabilities.ts  # Pure server/client metadata capability policy
│   ├── oauth-client-metadata.ts # Typed DCR parsing and CIMD resolution pipeline
│   ├── oauth-http.ts          # CORS and scope helpers shared by both hosts
│   ├── oauth-error.ts         # OAuthError: what callbacks and validators throw
│   ├── oauth-consent.ts       # Consent and upstream sign-in transactions (MCP confused-deputy MUSTs)
│   └── ema/                   # Enterprise-Managed Authorization pipeline
├── tests/
│   ├── oauth-provider.test.ts # Comprehensive provider integration suite
│   ├── oauth-capabilities.test.ts # Pure capability policy tests
│   ├── public-api.test.ts     # Pins the package's runtime exports
│   ├── setup.ts               # Vitest setup and mocking
│   └── mocks/
│       └── cloudflare-workers.ts
├── examples/                  # Runnable Workers, typechecked and e2e-tested in workerd
│   └── split-workers/         # The README quick start in full, e2e-tested
├── conformance/               # Black-box MCP authorization conformance matrix
│   ├── README.md              # Scope, revision coverage, and traceability
│   ├── shared.ts              # Typed Worker RPC contract and fixture constants
│   ├── spec-versions.ts       # Ordered MCP authorization revision timeline
│   ├── support/               # Workerd lifecycle and OAuth client
│   ├── upstream/              # Official @modelcontextprotocol/conformance runner, run in CI
│   └── worker/                # Real Wrangler Worker with local KV
├── dist/                      # Build output (tsdown)
├── docs/
│   ├── advanced-configuration.md
│   └── resource-servers.md    # Hosting resources in the same Worker or over a Service Binding
├── .github/workflows/
│   ├── ci.yml                 # PR validation
│   ├── release.yml            # Changesets-based npm publishing
│   └── pkg-pr-new.yml         # PR preview packages
├── storage-schema.md          # KV namespace data structure docs
├── HISTORY.md                 # Kenton's original project history
├── SECURITY.md                # Vulnerability reporting
└── README.md                  # Usage documentation

Audit-oriented architecture: Request orchestration and storage-backed OAuth behavior remain in src/oauth-provider.ts. Typed OAuth client metadata parsing plus CIMD fetching and resolution live in src/oauth-client-metadata.ts; pure authorization-server/client capability policy lives in src/oauth-capabilities.ts. Consent pages and third-party sign-in transactions (the MCP confused-deputy protections: __Host- browser binding, single-use state, signed remembered approvals) live in src/oauth-consent.ts. The experimental Enterprise-Managed Authorization validation pipeline is isolated in src/ema/ so its JWT and trust-boundary code can be reviewed independently.

Setup

npm install    # Install dependencies
npm run build  # Build with tsdown

Node 24+ required.

Commands

CommandWhat it does
npm run buildBuilds single-file ESM bundle with tsdown
npm run checkRuns typecheck + tests
npm run typecheckTypeScript type checking (no emit)
npm run testRuns vitest test suite
npm run test:conformanceRuns MCP auth conformance tests
npm run test:conformance:upstreamRuns the official MCP conformance runner against the conformance Worker
npm run test:watchRuns vitest in watch mode
npm run prettierFormats all files with Prettier

Code standards

TypeScript

  • Strict mode enabled
  • Target: ES2021, Module: ES2022
  • All public methods and interfaces must have JSDoc documentation
  • Private fields use # prefix (modern TS private fields)

Naming conventions

  • PascalCase for classes, interfaces, enums
  • camelCase for methods, variables
  • SCREAMING_SNAKE_CASE for constants
  • Impl suffix for internal implementations
  • Options suffix for configuration interfaces

Architecture patterns

PImpl pattern: The public OAuthProvider class wraps a private OAuthProviderImpl. This prevents TypeScript private methods from being accidentally exposed over RPC in Cloudflare Workers.

export class OAuthProvider {
  #impl: OAuthProviderImpl;
  fetch(...) { return this.#impl.fetch(...); }
}

Dual handler support: The library supports both ExportedHandler (plain objects) and WorkerEntrypoint (classes) patterns. Maintain both for backwards compatibility.

Formatting

Prettier with 120 character line width. Run npm run prettier before committing.

Security considerations

This is a security-critical OAuth library. All changes must consider:

Token storage:

  • Secrets (tokens, authorization codes) are stored as SHA-256 hashes only
  • Props are encrypted with AES-GCM, key wrapped with the token itself
  • Only token holders can decrypt their associated props

Validation:

  • Redirect URIs validated against XSS payloads
  • Client IDs validated (including CIMD URL validation)
  • PKCE enforced for public clients (S256 method)
  • Scope downscoping validated per RFC 6749 Section 3.3

Refresh token rotation:

  • Dual refresh tokens: current + previous both valid
  • Handles network failure cases gracefully
  • Previous token invalidated only after new token first used

Testing

Tests use vitest with custom mocks for Cloudflare Workers APIs.

npm run test               # Full suite, including conformance
npm run test:conformance   # MCP authorization conformance only
npm run test:watch         # Watch mode

Main integration test: tests/oauth-provider.test.ts

MCP authorization conformance: conformance/ contains black-box tests for every dated authorization revision represented by the official MCP conformance timeline. Wrangler's createTestHarness() runs a real Worker in Workerd with a local KV binding; tests exercise public OAuthProvider and OAuthHelpers interfaces and include requirement traceability in conformance/README.md.

Examples: examples/ Workers import @cloudflare/workers-oauth-provider, which tsconfig.json paths and each wrangler.jsonc alias map to src/, so they are built from source and need no package.json of their own. The README shows short excerpts and links here for the full code; keep the two consistent.

Mock implementations:

  • MockKV — In-memory KV with TTL simulation
  • MockExecutionContext — ctx.props support
  • createMockRequest() — HTTP request builder

Coverage areas:

  • OAuth metadata discovery endpoints
  • Authorization code flow with PKCE
  • Token exchange and refresh flows
  • Client registration (RFC 7591)
  • Grant management and revocation
  • Error responses and validation
  • Scope downscoping
  • Resource-aware audience validation

Test pattern:

beforeEach(() => {
  mockEnv = createMockEnv();
  mockCtx = new MockExecutionContext();
  oauthProvider = new OAuthProvider(options);
});

afterEach(() => {
  mockEnv.OAUTH_KV.clear();
});

Contributing

Changesets

Changes affecting the public API or bug fixes need a changeset:

npx changeset    # Interactive: select semver bump, write description

The changesets bot keeps a Version Packages PR (changeset-release/*) open on main and v0. Merging it publishes to npm, moves dist-tags and creates the git tag and GitHub release, none of which can be undone. Only a human maintainer merges it. An agent may read it, check that its version and changelog are right, and report what it would publish, but must never merge, approve, or auto-merge it, even with admin rights or when asked to "finish the release". Hand it back to a human instead.

Pull request process

CI runs on every PR:

  1. npm ci — Clean install
  2. npm run build — Build with tsdown
  3. npm run check — Typecheck + tests
  4. Prettier format check

All checks must pass before merge.

0.x maintenance branch (v0)

main carries the 1.0 line. Fixes that 0.x users need are cherry-picked to v0 in a PR against that branch, with their changeset. v0 carries its own ci.yml and a push-triggered release.yml (a workflow_run release would give changesets/action the default branch's github.sha), and publishes through .github/changeset-publish.sh, which uses the v0 dist-tag once npm's latest is 1.x so a hotfix never displaces latest. Both release workflows share the npm-publish concurrency group.

Bonk (AI code review)

Mention /bonk or @ask-bonk in PR comments to get AI-powered code review and suggestions. Bonk can analyze code, suggest fixes, and even auto-commit improvements.

Semver: changes that invalidate tokens or refresh tokens must be minor

Any change that alters the format of data stored on grants (e.g. the resource field) can silently invalidate existing refresh tokens. These changes must be released as a minor version bump, not a patch. This applies to any change where tokens issued by the previous version would fail validation against the new version.

RFC compliance

This library implements multiple OAuth/security RFCs. When making changes, maintain compliance with:

  • OAuth 2.1 (draft-ietf-oauth-v2-1-13)
  • OAuth 2.0 Bearer Token Usage (RFC 6750)
  • OAuth 2.0 Token Revocation (RFC 7009)
  • OAuth 2.0 Dynamic Client Registration (RFC 7591, deprecated by MCP 2026-07-28)
  • PKCE (RFC 7636)
  • OAuth 2.0 Authorization Server Metadata (RFC 8414)
  • OAuth 2.0 Token Exchange (RFC 8693)
  • Resource Indicators for OAuth 2.0 (RFC 8707)
  • OAuth 2.0 Authorization Server Issuer Identification (RFC 9207)
  • OAuth 2.0 Protected Resource Metadata (RFC 9728)
  • Client ID Metadata Documents (draft spec)
  • OpenID Connect RP Metadata Choices 1.0

Generated files

  • dist/ — Generated by npm run build, don't hand-edit
  • package-lock.json — Generated by npm install, don't hand-edit

Boundaries

Always:

  • Run npm run check before considering work done
  • Add tests for new functionality
  • Document public APIs with JSDoc
  • Consider security implications of changes
  • Maintain backwards compatibility for handler patterns

Ask first:

  • Adding new dependencies (this ships to users with zero runtime deps)
  • Changing KV storage schema (requires migration planning)
  • Modifying OAuth endpoints or flows
  • Adding new feature flags

Never:

  • Hardcode secrets or API keys
  • Bypass constructor validation
  • Store unhashed tokens or secrets in KV
  • Break existing handler patterns
  • Use any type without explicit justification
  • Force push to main
  • Merge, approve, or enable auto-merge on a Version Packages PR: releases are for humans only

Keeping AGENTS.md updated

Update this file when:

  • Adding new modules or significant features
  • Changing project structure
  • Modifying build/test tooling
  • Adding new code patterns or conventions
  • Changing contribution workflows

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.