Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(oauth): publish every issuable scope in discovery so agent-wallet tools are reachable
nirholasc3df3c10ac27
diff --git a/api/_lib/oauth-scopes.js b/api/_lib/oauth-scopes.jsnew file mode 100644index 000000000..386301d4f--- /dev/null+++ b/api/_lib/oauth-scopes.js@@ -0,0 +1,63 @@+// OAuth scope sets, in one place, because two surfaces have to agree on them.+//+// A dynamically-registered MCP client learns what it may ask for by following+// the chain the 401 hands it: `WWW-Authenticate: Bearer resource_metadata=...`+// points at /.well-known/oauth-protected-resource (RFC 9728), and the client+// registers for the scopes that document lists. So the scope list published+// there and the scope list the registration endpoint actually keeps+// (`filterRegisterableScope`) are the same contract seen from two sides: any+// scope missing from the metadata is a scope no discovery-driven client will+// ever request, and any scope listed there but dropped at registration is a+// promise the server breaks.+//+// They drifted once already. The wallet/services scopes were added to+// registration to make the agent-wallet MCP server (api/mcp-agent) reachable,+// and the published metadata kept advertising the original eight, so a client+// that discovered its scopes the documented way still could not reach+// wallet_status, pay_and_call, provision_wallet or monetize_endpoint. Both+// sides now read this array, and tests/api/oauth-scope-discovery.test.js+// pins them together.++// Scopes a dynamically-registered client may request. Anything outside this set+// (notably privileged scopes like `permissions:redeem`, which authorizes+// gas-spending on-chain redemption) is silently dropped at registration so a+// self-registering client can never mint a token carrying it.+export const REGISTERABLE_SCOPES = Object.freeze([+ 'avatars:read',+ 'avatars:write',+ 'avatars:delete',+ 'profile',+ 'offline_access',+ // Agent memory MCP tools (remember / recall / forget).+ 'memory:read',+ 'memory:write',+ // On-chain agent identity MCP tools (register_agent / identity_check).+ 'agents:read',+ 'agents:write',+ // The wallet/services scopes gate the agent-wallet MCP server+ // (api/mcp-agent). They are registerable because the user approves each one+ // by name on the consent screen and every spend they authorize is still+ // bounded by the server-side caps and THREEWS_AGENT_PAY_ENABLED.+ 'wallet:read',+ 'wallet:write',+ 'services:write',+]);++// Scopes that exist on the authorization server but are NOT self-registerable:+// they gate paid x402 endpoints for first-party and API-key principals via the+// auth-hints extension (installAccessControl({ requiredScope })), never the MCP+// resource. They belong in RFC 8414 authorization-server metadata and must stay+// out of the RFC 9728 protected-resource document for /api/mcp.+export const AUTH_HINT_SCOPES = Object.freeze(['read:agent-reputation', 'x402:bypass']);++// Everything the authorization server can issue, for RFC 8414 scopes_supported.+export const SUPPORTED_SCOPES = Object.freeze([...REGISTERABLE_SCOPES, ...AUTH_HINT_SCOPES]);++const REGISTERABLE = new Set(REGISTERABLE_SCOPES);++export function filterRegisterableScope(requested) {+ const kept = String(requested || '')+ .split(/\s+/)+ .filter((s) => REGISTERABLE.has(s));+ return kept.length ? kept.join(' ') : 'avatars:read';+}diff --git a/api/oauth/[action].js b/api/oauth/[action].jsindex 83b975b9e..517ed7be1 100644--- a/api/oauth/[action].js+++ b/api/oauth/[action].js@@ -14,6 +14,7 @@ import { limits, clientIp } from '../_lib/rate-limit.js';import { env } from '../_lib/env.js';import { z } from 'zod';import { parse } from '../_lib/validate.js';+import { filterRegisterableScope } from '../_lib/oauth-scopes.js';// ── authorize ─────────────────────────────────────────────────────────────────@@ -51,26 +52,10 @@ function canonicalResource(requested) {return strip(requested) === strip(env.MCP_RESOURCE) ? env.MCP_RESOURCE : null;}-// Scopes a dynamically-registered client may request. Anything outside this set-// (notably privileged scopes like `permissions:redeem`, which authorizes-// gas-spending on-chain redemption) is silently dropped at registration so a-// self-registering client can never mint a token carrying it.-const REGISTERABLE_SCOPES = new Set([- 'avatars:read', 'avatars:write', 'avatars:delete', 'profile',- 'memory:read', 'memory:write', 'agents:read', 'agents:write', 'offline_access',- // The wallet/services scopes gate the agent-wallet MCP server- // (api/mcp-agent). They are registerable because the user approves each one- // by name on the consent screen and every spend they authorize is still- // bounded by the server-side caps and THREEWS_AGENT_PAY_ENABLED. Omitting- // them left wallet_status, pay_and_call, provision_wallet and- // monetize_endpoint unreachable for every dynamically-registered client.- 'wallet:read', 'wallet:write', 'services:write',-]);--function filterRegisterableScope(requested) {- const kept = String(requested || '').split(/\s+/).filter((s) => REGISTERABLE_SCOPES.has(s));- return kept.length ? kept.join(' ') : 'avatars:read';-}+// The registerable scope set and the filter that enforces it live in+// ../_lib/oauth-scopes.js, shared with api/wk.js so the scopes this endpoint+// keeps and the scopes /.well-known/oauth-protected-resource advertises are+// read from one array and cannot drift apart.function scopeLabel(s) {const labels = { 'avatars:read': 'Read your avatars', 'avatars:write': 'Create and update avatars', 'avatars:delete': 'Delete your avatars', profile: 'See your name and email', offline_access: 'Stay signed in across sessions', 'memory:read': 'Recall your agents’ memories', 'memory:write': 'Store and forget your agents’ memories', 'agents:read': 'Screen your agents’ identities', 'agents:write': 'Register your agents on-chain', 'wallet:read': 'See your agent wallet balance and spending caps', 'wallet:write': 'Spend USDC from your agent wallet, within your caps', 'services:write': 'Publish paid services that earn USDC to your agent wallet' };diff --git a/api/wk.js b/api/wk.jsindex 2475df57d..f2df4e5e4 100644--- a/api/wk.js+++ b/api/wk.js@@ -5,6 +5,7 @@ import { readFileSync } from 'node:fs';import { join } from 'node:path';import { cors, json, method, wrap, error } from './_lib/http.js';import { env } from './_lib/env.js';+import { REGISTERABLE_SCOPES, SUPPORTED_SCOPES } from './_lib/oauth-scopes.js';import {paymentRequirements,bazaarExtension,@@ -162,28 +163,9 @@ function handleOauthAuthServer(req, res) {'client_secret_basic','client_secret_post',],- scopes_supported: [- 'avatars:read',- 'avatars:write',- 'avatars:delete',- 'profile',- 'offline_access',- // Agent memory MCP tools (remember / recall / forget).- 'memory:read',- 'memory:write',- // On-chain agent identity MCP tools (register_agent / identity_check).- 'agents:read',- 'agents:write',- // USE-21 auth-hints: paid endpoints advertise these scopes for- // Bearer-token bypass via the auth-hints extension.- 'read:agent-reputation',- 'x402:bypass',- // Agent wallet MCP (api/mcp-agent): read status, provision a wallet,- // and publish a paid endpoint to earn USDC.- 'wallet:read',- 'wallet:write',- 'services:write',- ],+ // Everything this AS can issue: the self-registerable scopes plus the+ // USE-21 auth-hints scopes paid endpoints accept for Bearer bypass.+ scopes_supported: [...SUPPORTED_SCOPES],service_documentation: `${base}/docs/mcp`,ui_locales_supported: ['en'],},@@ -202,16 +184,13 @@ function handleOauthProtectedResource(req, res) {authorization_servers: [env.APP_ORIGIN],bearer_methods_supported: ['header'],resource_documentation: `${env.APP_ORIGIN}/docs/mcp`,- scopes_supported: [- 'avatars:read',- 'avatars:write',- 'avatars:delete',- 'profile',- 'memory:read',- 'memory:write',- 'agents:read',- 'agents:write',- ],+ // Exactly what /api/oauth/register will keep for a self-registering+ // client. A discovery-driven MCP client reads its scope list from+ // here, so a scope missing from this array is a scope it never asks+ // for: publishing fewer than the endpoint grants left the whole+ // agent-wallet server (wallet:read / wallet:write / services:write)+ // unreachable through the documented flow.+ scopes_supported: [...REGISTERABLE_SCOPES],},{ 'cache-control': 'public, max-age=300' },);diff --git a/tests/api/oauth-scope-discovery.test.js b/tests/api/oauth-scope-discovery.test.jsnew file mode 100644index 000000000..569bc0c94--- /dev/null+++ b/tests/api/oauth-scope-discovery.test.js@@ -0,0 +1,102 @@+/**+ * The two OAuth discovery documents and the registration endpoint describe one+ * contract from three sides, and a client that follows the documented path+ * touches all three: the 401 from any MCP server carries+ * `WWW-Authenticate: Bearer resource_metadata=".../.well-known/oauth-protected-resource"`,+ * the client reads `scopes_supported` from that document, and it registers for+ * exactly those scopes at /api/oauth/register.+ *+ * So a scope the registration endpoint grants but the protected-resource+ * document omits is a scope no discovery-driven client ever asks for. That is+ * how the whole agent-wallet MCP server (wallet:read / wallet:write /+ * services:write) became unreachable through the documented flow while the+ * registration endpoint happily granted those scopes to anyone who knew to name+ * them. These assertions pin the surfaces to the shared array so the next scope+ * added on one side cannot silently skip the other.+ */+import { describe, it, expect } from 'vitest';++process.env.PUBLIC_APP_ORIGIN ||= 'https://three.ws';++const {+ REGISTERABLE_SCOPES,+ AUTH_HINT_SCOPES,+ SUPPORTED_SCOPES,+ filterRegisterableScope,+} = await import('../../api/_lib/oauth-scopes.js');+const { default: wkHandler } = await import('../../api/wk.js');++function makeRes() {+ return {+ statusCode: 200,+ headers: {},+ body: null,+ setHeader(name, value) {+ this.headers[String(name).toLowerCase()] = value;+ },+ end(body) {+ this.body = body ?? null;+ },+ };+}++async function fetchWellKnown(name) {+ const res = makeRes();+ const req = { method: 'GET', url: `/api/wk?name=${name}`, headers: {}, query: { name } };+ await wkHandler(req, res);+ return { res, doc: JSON.parse(res.body) };+}++describe('OAuth discovery scope parity', () => {+ it('serves the protected-resource document with the registerable scopes', async () => {+ const { res, doc } = await fetchWellKnown('oauth-protected-resource');+ expect(res.statusCode).toBe(200);+ expect(doc.scopes_supported).toEqual([...REGISTERABLE_SCOPES]);+ });++ it('publishes every scope the registration endpoint would keep', async () => {+ const { doc } = await fetchWellKnown('oauth-protected-resource');+ // Ask for everything the metadata advertises; registration must keep it+ // all, or a compliant client is being told to request scopes it loses.+ const kept = filterRegisterableScope(doc.scopes_supported.join(' '));+ expect(kept.split(' ')).toEqual(doc.scopes_supported);+ });++ it('reaches the agent-wallet scopes through the documented flow', async () => {+ const { doc } = await fetchWellKnown('oauth-protected-resource');+ for (const scope of ['wallet:read', 'wallet:write', 'services:write']) {+ expect(doc.scopes_supported).toContain(scope);+ }+ });++ it('serves authorization-server metadata covering the resource scopes', async () => {+ const { res, doc } = await fetchWellKnown('oauth-authorization-server');+ expect(res.statusCode).toBe(200);+ expect(doc.scopes_supported).toEqual([...SUPPORTED_SCOPES]);+ const { doc: resourceDoc } = await fetchWellKnown('oauth-protected-resource');+ for (const scope of resourceDoc.scopes_supported) {+ expect(doc.scopes_supported).toContain(scope);+ }+ });++ it('keeps the auth-hints scopes out of the protected-resource document', async () => {+ // These gate paid x402 endpoints for first-party principals, not the MCP+ // resource, and registration drops them. Advertising them to MCP clients+ // would promise a scope every registration silently strips.+ const { doc } = await fetchWellKnown('oauth-protected-resource');+ for (const scope of AUTH_HINT_SCOPES) {+ expect(doc.scopes_supported).not.toContain(scope);+ expect(filterRegisterableScope(scope)).toBe('avatars:read');+ }+ });++ it('points the protected-resource document at the MCP resource', async () => {+ const { doc } = await fetchWellKnown('oauth-protected-resource');+ expect(doc.resource).toBe('https://three.ws/api/mcp');+ expect(doc.authorization_servers).toContain('https://three.ws');+ const { doc: asDoc } = await fetchWellKnown('oauth-authorization-server');+ // The issuer named by the resource must be the issuer the AS claims,+ // or the client walks the chain into a document it will reject.+ expect(doc.authorization_servers).toContain(asDoc.issuer);+ });+});