Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

feat(mcp): read the MCP endpoint settings from the Worker env

spacesdrivebf08a7a806fd
diff --git a/backend/lib/config.ts b/backend/lib/config.ts
index 569c5da..e1a74f3 100644
--- a/backend/lib/config.ts
+++ b/backend/lib/config.ts
@@ -50,6 +50,17 @@ export interface AutomationConfig {
batchSize: number;
}
+// the MCP endpoint: where Claude clients reach it, who signs them in, and the secret behind its confirmation tokens
+export interface McpConfig {
+ serverUrl: string;
+ resourceMetadataUrl: string;
+ // Supabase Auth's issuer, named in the protected resource metadata as the authorization server
+ authorizationServer: string;
+ // undefined when TOKEN_ENCRYPTION_KEY is missing or too short; tools that need a confirmation then refuse
+ signingSecret: string | undefined;
+ oauthEnabled: boolean;
+}
+
export interface AppConfig {
youtubeApiKey: string | undefined;
instagram: InstagramOAuthConfig;
@@ -57,6 +68,7 @@ export interface AppConfig {
allowedOrigins: string[];
authEmail: AuthEmailConfig;
automation: AutomationConfig;
+ mcp: McpConfig;
}
// pasted secrets can carry a byte order mark or stray whitespace
@@ -162,6 +174,22 @@ export function readAutomationConfig(env: Env): AutomationConfig {
};
}
+// a shorter key could be guessed, so it counts as not set
+const MIN_SIGNING_SECRET_LENGTH = 32;
+const MCP_PATH = '/mcp';
+
+export function readMcpConfig(env: Env): McpConfig {
+ const backendUrl = cleanUrl(env.BACKEND_URL, DEFAULT_BACKEND_URL).replace(/\/$/, '');
+ const secret = cleanSecret(env.TOKEN_ENCRYPTION_KEY);
+ return {
+ serverUrl: `${backendUrl}${MCP_PATH}`,
+ resourceMetadataUrl: `${backendUrl}/.well-known/oauth-protected-resource${MCP_PATH}`,
+ authorizationServer: `${cleanUrl(env.SUPABASE_URL, '').replace(/\/$/, '')}/auth/v1`,
+ signingSecret: secret && secret.length >= MIN_SIGNING_SECRET_LENGTH ? secret : undefined,
+ oauthEnabled: cleanSecret(env.MCP_OAUTH_ENABLED)?.toLowerCase() === 'true',
+ };
+}
+
export function readConfig(env: Env): AppConfig {
const cloudName = cleanSecret(env.CLOUDINARY_CLOUD_NAME);
const uploadPreset = cleanSecret(env.CLOUDINARY_UPLOAD_PRESET);
@@ -172,5 +200,6 @@ export function readConfig(env: Env): AppConfig {
allowedOrigins: allowedOrigins(env),
authEmail: readAuthEmailConfig(env),
automation: readAutomationConfig(env),
+ mcp: readMcpConfig(env),
};
}
diff --git a/backend/tests/lib/config.test.ts b/backend/tests/lib/config.test.ts
index 94cd502..89753fa 100644
--- a/backend/tests/lib/config.test.ts
+++ b/backend/tests/lib/config.test.ts
@@ -88,3 +88,19 @@ test('the missing secrets message names each secret and says how to set it', ()
assert.equal(coreSecretsMessage(['SUPABASE_URL']), 'The server is not set up: the SUPABASE_URL Worker secret is missing or invalid. Set it with wrangler secret put, then deploy again.');
assert.match(coreSecretsMessage(['SUPABASE_URL', 'SUPABASE_ANON_KEY']), /the SUPABASE_URL, SUPABASE_ANON_KEY Worker secrets are missing or invalid\. Set them with/);
});
+
+test('the MCP endpoint and its metadata live on the Worker, and Supabase Auth is the authorization server', () => {
+ const { mcp } = config({ BACKEND_URL: 'https://api.example.com/', SUPABASE_URL: 'https://ref.supabase.co/' });
+ assert.equal(mcp.serverUrl, 'https://api.example.com/mcp');
+ assert.equal(mcp.resourceMetadataUrl, 'https://api.example.com/.well-known/oauth-protected-resource/mcp');
+ assert.equal(mcp.authorizationServer, 'https://ref.supabase.co/auth/v1');
+ assert.equal(mcp.oauthEnabled, false);
+});
+
+test('the MCP signing secret counts as unset below 32 characters, and only "true" turns OAuth on', () => {
+ assert.equal(config({ TOKEN_ENCRYPTION_KEY: 'short' }).mcp.signingSecret, undefined);
+ const key = 'k'.repeat(32);
+ assert.equal(config({ TOKEN_ENCRYPTION_KEY: ` ${key} ` }).mcp.signingSecret, key);
+ assert.equal(config({ MCP_OAUTH_ENABLED: 'TRUE' }).mcp.oauthEnabled, true);
+ assert.equal(config({ MCP_OAUTH_ENABLED: 'yes' }).mcp.oauthEnabled, false);
+});
diff --git a/backend/types/env.ts b/backend/types/env.ts
index dd696e9..14ed9d4 100644
--- a/backend/types/env.ts
+++ b/backend/types/env.ts
@@ -36,4 +36,8 @@ export interface Env {
AUTOMATION_PUBLIC_ACCOUNT_HOURLY_LIMIT?: string;
// replies sent by one webhook request or one sweep
AUTOMATION_BATCH_SIZE?: string;
+ // at least 32 random characters; keys derived from it sign MCP confirmations and encrypt stored platform tokens
+ TOKEN_ENCRYPTION_KEY?: string;
+ // "true" once the owner has turned on Supabase Auth's OAuth 2.1 server, so Claude's apps can sign in to /mcp
+ MCP_OAUTH_ENABLED?: string;
}