Skip to content

Commit 8fcb1a1

Browse files
committed
Setup auth-server to handle callbacks for sandbox-container and workers-bindings
1 parent 831d1e8 commit 8fcb1a1

File tree

10 files changed

+45
-24
lines changed

10 files changed

+45
-24
lines changed

apps/sandbox-container/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@hono/node-server": "^1.13.8",
2020
"@hono/zod-validator": "^0.4.3",
2121
"@modelcontextprotocol/sdk": "^1.7.0",
22+
"@repo/mcp-common": "workspace:*",
2223
"@types/node": "^22.13.10",
2324
"agents": "^0.0.42",
2425
"cron-schedule": "^5.0.4",
@@ -29,10 +30,10 @@
2930
"partyserver": "^0.0.65",
3031
"tsx": "^4.19.3",
3132
"workers-mcp": "0.1.0-3",
32-
"zod": "^3.24.2",
33-
"@repo/mcp-common": "workspace:*"
33+
"zod": "^3.24.2"
3434
},
3535
"devDependencies": {
36+
"@cloudflare/vitest-pool-workers": "0.8.14",
3637
"concurrently": "^9.1.2",
3738
"wrangler": "^4.9.1"
3839
}

apps/sandbox-container/server/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import OAuthProvider from '@cloudflare/workers-oauth-provider'
22
import { env } from 'cloudflare:workers'
33

44
import {
5-
CloudflareAuthHandler,
5+
createAuthHandlers,
66
handleTokenExchangeCallback,
77
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
8+
import { getEnvironment } from '@repo/mcp-common/src/config'
89

910
import { ContainerManager } from './containerManager'
1011
import { ContainerMcpAgent } from './containerMcp'
@@ -32,7 +33,10 @@ export default new OAuthProvider({
3233
// @ts-ignore
3334
apiHandler: ContainerMcpAgent.mount('/workers/sandbox/sse', { binding: 'CONTAINER_MCP_AGENT' }),
3435
// @ts-ignore
35-
defaultHandler: CloudflareAuthHandler,
36+
defaultHandler: createAuthHandlers({
37+
serverPath: 'workers/containers',
38+
environment: getEnvironment(env.ENVIRONMENT),
39+
}),
3640
authorizeEndpoint: '/oauth/authorize',
3741
tokenEndpoint: '/token',
3842
tokenExchangeCallback: (options) =>
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
11
{
2-
"compilerOptions": {
3-
"target": "ESNext",
4-
"lib": ["ESNext", "DOM"],
5-
"jsx": "react-jsx",
6-
"module": "ESNext",
7-
"moduleResolution": "bundler",
8-
"types": ["./worker-configuration.d.ts", "@cloudflare/workers-types/2023-07-01"],
9-
"noEmit": true,
10-
"esModuleInterop": true,
11-
"forceConsistentCasingInFileNames": true,
12-
"strict": true,
13-
"skipLibCheck": true
14-
},
15-
"include": ["server/**.ts", "shared/**.ts"]
2+
"extends": "@repo/typescript-config/workers.json"
163
}

apps/sandbox-container/worker-configuration.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
declare namespace Cloudflare {
44
interface Env {
55
OAUTH_KV: KVNamespace;
6+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
67
CONTAINER_MCP_AGENT: DurableObjectNamespace<import("./server/index").ContainerMcpAgent>;
8+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
79
CONTAINER_MANAGER: DurableObjectNamespace<import("./server/index").ContainerManager>;
810
CLOUDFLARE_CLIENT_ID: string;
911
CLOUDFLARE_CLIENT_SECRET: string;
12+
ENVIRONMENT: MCPEnvironment
1013
}
1114
}
1215
interface Env extends Cloudflare.Env {}

apps/sandbox-container/wrangler.jsonc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "node_modules/wrangler/config-schema.json",
3-
"name": "sandbox-container",
3+
"name": "sandbox-container-dev",
44
"main": "server/index.ts",
55
"compatibility_date": "2025-04-03",
66
"containers": [
@@ -41,5 +41,8 @@
4141
],
4242
"dev": {
4343
"port": 8976
44-
}
44+
},
45+
"vars": {
46+
"ENVIRONMENT": "development"
47+
},
4548
}

apps/workers-bindings/src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import { McpAgent } from 'agents/mcp'
44
import { env } from 'cloudflare:workers'
55

66
import {
7-
CloudflareAuthHandler,
7+
createAuthHandlers,
88
handleTokenExchangeCallback,
99
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
10+
import { getEnvironment } from '@repo/mcp-common/src/config'
1011
import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
1112
import { registerKVTools } from '@repo/mcp-common/src/tools/kv_namespace'
1213
import { registerWorkersTools } from '@repo/mcp-common/src/tools/worker'
1314

1415
import type { AccountSchema, UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
16+
import type { Env } from '../worker-configuration'
1517

1618
export type WorkersBindingsMCPState = { activeAccountId: string | null }
1719

@@ -66,11 +68,19 @@ export default new OAuthProvider({
6668
// @ts-ignore
6769
apiHandler: WorkersBindingsMCP.mount('/workers/bindings/sse'),
6870
// @ts-ignore
69-
defaultHandler: CloudflareAuthHandler,
71+
defaultHandler: createAuthHandlers({
72+
serverPath: 'workers/containers',
73+
// TOOD: Why do we need to cast as Env for typescript to pick up types?
74+
environment: getEnvironment((env as Env).ENVIRONMENT),
75+
}),
7076
authorizeEndpoint: '/oauth/authorize',
7177
tokenEndpoint: '/token',
7278
tokenExchangeCallback: (options) =>
73-
handleTokenExchangeCallback(options, env.CLOUDFLARE_CLIENT_ID, env.CLOUDFLARE_CLIENT_SECRET),
79+
handleTokenExchangeCallback(
80+
options,
81+
(env as Env).CLOUDFLARE_CLIENT_ID,
82+
(env as Env).CLOUDFLARE_CLIENT_SECRET
83+
),
7484
// Cloudflare access token TTL
7585
accessTokenTTL: 3600,
7686
clientRegistrationEndpoint: '/register',

apps/workers-bindings/vitest.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'
22

3+
import type { Env } from './worker-configuration'
4+
35
export interface TestEnv extends Env {
46
CLOUDFLARE_MOCK_ACCOUNT_ID: string
57
CLOUDFLARE_MOCK_API_TOKEN: string

apps/workers-bindings/worker-configuration.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// Generated by Wrangler by running `wrangler types` (hash: 83b65e4226fcf5092146edc626681982)
2+
3+
import type { MCPEnvironment } from "@repo/mcp-common/src/config";
4+
25
// Runtime types generated with [email protected] 2025-03-10 nodejs_compat
36
declare namespace Cloudflare {
47
interface Env {
58
OAUTH_KV: KVNamespace;
69
CLOUDFLARE_CLIENT_ID: string;
710
CLOUDFLARE_CLIENT_SECRET: string;
11+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
812
MCP_OBJECT: DurableObjectNamespace<import("./src/index").WorkersBindingsMCP>;
13+
ENVIRONMENT: MCPEnvironment
914
}
1015
}
1116
interface Env extends Cloudflare.Env {}

apps/workers-bindings/wrangler.jsonc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
{
66
"$schema": "node_modules/wrangler/config-schema.json",
7-
"name": "workers-bindings",
7+
"name": "workers-bindings-dev",
88
"main": "src/index.ts",
99
"compatibility_date": "2025-03-10",
1010
"compatibility_flags": ["nodejs_compat"],
@@ -33,6 +33,9 @@
3333
},
3434
"dev": {
3535
"port": 8976
36+
},
37+
"vars": {
38+
"ENVIRONMENT": "development"
3639
}
3740
/**
3841
* Smart Placement

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)