Skip to content

Commit 44eaf21

Browse files
stolinskiclaude
andcommitted
Let agents sign in to Syntax Lab's local MCP through local Syntax Auth
Lab Dex task td21hm43. Local mode now accepts http://localhost:1337/mcp as a token audience (get_oauth_valid_audiences takes is_local_development from the existing env) and seeds one public OAuth client, syntax-lab-mcp-local, with the redirects production's Syntax Lab MCP client has (localhost:41337 for Claude Code, 127.0.0.1 on any port for pi). The seed upserts on the client ID, refuses a deployed env, uses only the local wrangler env, and runs after local migrations in pnpm dev and in the Docker image. Deployed audiences and clients are unchanged. Tests: oauth_audiences.test.ts pins production's exact audience list and the local one. pnpm test 90/90, pnpm lint, pnpm check 0 errors. Live, 2026-10-29, Claude Code 2.1.312 against this branch's pnpm dev: claude mcp add --client-id syntax-lab-mcp-local --callback-port 41337 for http://localhost:1337/mcp, sign-in as Local Developer and consent, "Authentication successful", status Connected, listLabOperations returned 122 operations. A local token (iss localhost:37960, aud localhost:1337/mcp) sent to https://lab.syntax.fm/mcp got 401 invalid_token. A locally built image seeded the client on start. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9468252 commit 44eaf21

9 files changed

Lines changed: 115 additions & 8 deletions

File tree

‎CONSUMING_AUTH.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,13 @@ instead. A request from an origin it doesn't trust gets a 403 with code `INVALID
379379
message naming that origin and the origins it accepts. Its sessions are meaningless to production.
380380
The container publishes its port on `127.0.0.1` only.
381381

382+
Local mode also lets a member's own agent (Claude Code, pi) sign in to Syntax Lab's local MCP
383+
endpoint as the local developer. It issues access tokens for `http://localhost:1337/mcp`, and it
384+
seeds one public OAuth client, `syntax-lab-mcp-local` (`pnpm oauth:seed-local`, run on every start
385+
of the container and of `pnpm dev`), with the same redirects as production's Syntax Lab MCP
386+
client. Deployed Syntax Auth has neither, and production refuses its tokens: their issuer and keys
387+
are local.
388+
382389
### Leaving the old `.syntax.test` setup
383390

384391
Earlier versions set up `https://syntax.test`, `https://lab.syntax.test`, and

‎Dockerfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ COPY . .
1212
RUN pnpm build
1313
EXPOSE 37960
1414
VOLUME /app/.wrangler
15-
CMD ["sh", "-c", "pnpm d1:migrate:local && exec node_modules/.bin/vite preview --host 0.0.0.0 --port 37960 --strictPort"]
15+
CMD ["sh", "-c", "pnpm d1:migrate:local && pnpm oauth:seed-local && exec node_modules/.bin/vite preview --host 0.0.0.0 --port 37960 --strictPort"]

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"d1:migrate:local": "wrangler d1 migrations apply syntax-auth --local --env local",
1818
"d1:migrate:remote": "wrangler d1 migrations apply syntax-auth --remote",
1919
"deploy": "pnpm build && wrangler deploy --env=\"\"",
20-
"oauth:register": "tsx scripts/register_oauth_client.ts"
20+
"oauth:register": "tsx scripts/register_oauth_client.ts",
21+
"oauth:seed-local": "tsx scripts/seed_local_oauth_clients.ts"
2122
},
2223
"dependencies": {
2324
"@better-auth/drizzle-adapter": "1.6.23",

‎scripts/local_server.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from '../packages/auth-local/container.js';
1919

2020
const SERVER_COMMANDS = {
21-
dev: 'pnpm d1:migrate:local && exec vite dev',
21+
dev: 'pnpm d1:migrate:local && pnpm oauth:seed-local && exec vite dev',
2222
preview: `pnpm build && exec wrangler dev --env local --ip 127.0.0.1 --port ${SYNTAX_AUTH_LOCAL_PORT}`
2323
};
2424
// Vite restarts its server within about a second when its config changes, so only a longer
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Seeds the OAuth clients only local Syntax Auth has (src/lib/server/local_oauth_clients.ts) into
2+
// the local D1, after `pnpm d1:migrate:local` in `pnpm dev` and in the Docker image. It writes the
3+
// row itself because Better Auth's `adminCreateOAuthClient` always generates a random client ID.
4+
// An upsert on the client ID, so it can run on every start and later changes reach existing local
5+
// databases.
6+
import { drizzle } from 'drizzle-orm/d1';
7+
import { getPlatformProxy } from 'wrangler';
8+
9+
import { oauthClient } from '../src/lib/server/db/schema';
10+
import { get_auth_environment } from '../src/lib/server/env';
11+
import { LOCAL_SYNTAX_LAB_MCP_CLIENT } from '../src/lib/server/local_oauth_clients';
12+
13+
// Only the `local` env, never remote bindings: this script must never write a deployed database.
14+
const platform = await getPlatformProxy<App.Platform['env']>({
15+
configPath: 'wrangler.jsonc',
16+
environment: 'local',
17+
persist: true,
18+
remoteBindings: false
19+
});
20+
21+
try {
22+
const env = get_auth_environment(platform.env);
23+
if (!env.is_local_development) {
24+
throw new Error(
25+
`Local OAuth clients are seeded only into local Syntax Auth, not ${env.BETTER_AUTH_URL}.`
26+
);
27+
}
28+
29+
const client = LOCAL_SYNTAX_LAB_MCP_CLIENT;
30+
const now = new Date();
31+
const client_fields = {
32+
name: client.client_name,
33+
redirectUris: [...client.redirect_uris],
34+
scopes: [...client.scopes],
35+
tokenEndpointAuthMethod: client.token_endpoint_auth_method,
36+
type: client.type,
37+
public: true,
38+
grantTypes: [...client.grant_types],
39+
responseTypes: [...client.response_types],
40+
requirePKCE: client.require_pkce,
41+
skipConsent: client.skip_consent,
42+
enableEndSession: client.enable_end_session,
43+
disabled: false,
44+
updatedAt: now
45+
};
46+
47+
await drizzle(env.DB)
48+
.insert(oauthClient)
49+
.values({ id: client.client_id, clientId: client.client_id, createdAt: now, ...client_fields })
50+
.onConflictDoUpdate({ target: oauthClient.clientId, set: client_fields });
51+
52+
console.log(`Seeded the local OAuth client ${client.client_id}`);
53+
} catch (seed_error) {
54+
console.error('Seeding local OAuth clients failed', seed_error);
55+
process.exitCode = 1;
56+
} finally {
57+
await platform.dispose();
58+
}

‎src/lib/server/auth.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function create_auth(env: AuthEnvironment) {
9191
allowDynamicClientRegistration: false,
9292
allowUnauthenticatedClientRegistration: false,
9393
// Without this, Better Auth accepts only its own base URL as a token `resource`.
94-
validAudiences: get_oauth_valid_audiences(better_auth_origin),
94+
validAudiences: get_oauth_valid_audiences(better_auth_origin, env.is_local_development),
9595
silenceWarnings: {
9696
oauthAuthServerConfig: true
9797
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// OAuth clients that only local Syntax Auth has, seeded by scripts/seed_local_oauth_clients.ts.
2+
// Deployed Syntax Auth registers its clients with scripts/register_oauth_client.ts instead.
3+
//
4+
// Syntax Lab MCP (local): members' own agents (Claude Code, pi) sign in with it to Lab's dev server
5+
// at `http://localhost:1337/mcp`. Its ID is fixed so every machine and Lab's docs use the same
6+
// `claude mcp add` command; a public client's ID is not a secret. The other values are the ones
7+
// production's Syntax Lab MCP client was registered with (`pnpm oauth:register --public`):
8+
// Claude Code's fixed callback port 41337, and `127.0.0.1` on any port (RFC 8252) for pi.
9+
export const LOCAL_SYNTAX_LAB_MCP_CLIENT = {
10+
client_id: 'syntax-lab-mcp-local',
11+
client_name: 'Syntax Lab MCP (local)',
12+
redirect_uris: ['http://localhost:41337/callback', 'http://127.0.0.1/callback'],
13+
scopes: ['openid', 'profile', 'email', 'offline_access'],
14+
token_endpoint_auth_method: 'none',
15+
type: 'user-agent-based',
16+
grant_types: ['authorization_code', 'refresh_token'],
17+
response_types: ['code'],
18+
require_pkce: true,
19+
skip_consent: false,
20+
enable_end_session: false
21+
} as const;

‎src/lib/server/oauth_audiences.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ import { test } from 'node:test';
33

44
import { get_oauth_valid_audiences } from './oauth_audiences';
55

6-
test("tokens can be issued for Syntax Auth's own API, as before, and for Syntax Lab's MCP endpoint only", () => {
7-
assert.deepEqual(get_oauth_valid_audiences('https://auth.syntax.fm'), [
6+
test("deployed Syntax Auth issues tokens for its own API and Syntax Lab's MCP endpoint only", () => {
7+
assert.deepEqual(get_oauth_valid_audiences('https://auth.syntax.fm', false), [
88
'https://auth.syntax.fm/api/auth',
99
'https://lab.syntax.fm/mcp'
1010
]);
1111
});
12+
13+
test("local Syntax Auth also issues tokens for Lab's local MCP endpoint", () => {
14+
assert.deepEqual(get_oauth_valid_audiences('http://localhost:37960', true), [
15+
'http://localhost:37960/api/auth',
16+
'https://lab.syntax.fm/mcp',
17+
'http://localhost:1337/mcp'
18+
]);
19+
});

‎src/lib/server/oauth_audiences.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
// (syntax-lab docs/tech/agent-access.md, "Built: the MCP endpoint").
77
export const SYNTAX_LAB_MCP_AUDIENCE = 'https://lab.syntax.fm/mcp';
88

9-
export function get_oauth_valid_audiences(better_auth_origin: string): string[] {
10-
return [`${better_auth_origin}/api/auth`, SYNTAX_LAB_MCP_AUDIENCE];
9+
// Lab's dev server, on its documented port 1337. Agents send the protected-resource URL as
10+
// `resource`, so a Lab dev server on another port or host gets "requested resource invalid".
11+
// Only local Syntax Auth issues tokens for it.
12+
export const SYNTAX_LAB_LOCAL_MCP_AUDIENCE = 'http://localhost:1337/mcp';
13+
14+
export function get_oauth_valid_audiences(
15+
better_auth_origin: string,
16+
is_local_development: boolean
17+
): string[] {
18+
return [
19+
`${better_auth_origin}/api/auth`,
20+
SYNTAX_LAB_MCP_AUDIENCE,
21+
...(is_local_development ? [SYNTAX_LAB_LOCAL_MCP_AUDIENCE] : [])
22+
];
1123
}

0 commit comments

Comments
 (0)