Skip to content

Commit a3b44b3

Browse files
committed
Add Cloudflare OAUTH to sandbox container
1 parent 3390201 commit a3b44b3

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

apps/sandbox-container/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"build": "docker build .",
1111
"start": "wrangler dev",
1212
"start:container": "tsx container/index.ts",
13-
"postinstall": "mkdir workdir"
13+
"postinstall": "mkdir -p workdir"
1414
},
1515
"dependencies": {
16+
"@cloudflare/workers-oauth-provider": "0.0.2",
1617
"@cloudflare/workers-types": "^4.20250320.0",
1718
"@hono/node-server": "^1.13.8",
1819
"@hono/zod-validator": "^0.4.3",
@@ -27,7 +28,8 @@
2728
"partyserver": "^0.0.65",
2829
"tsx": "^4.19.3",
2930
"workers-mcp": "0.1.0-3",
30-
"zod": "^3.24.2"
31+
"zod": "^3.24.2",
32+
"@repo/mcp-common": "workspace:*"
3133
},
3234
"devDependencies": {
3335
"concurrently": "^9.1.2",
Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
import { Hono } from 'hono'
2-
import { Octokit } from 'octokit'
3-
import OAuthProvider, {
4-
AuthRequest,
5-
OAuthHelpers,
6-
} from 'workers-mcp/vendor/workers-oauth-provider/oauth-provider.js'
1+
import OAuthProvider from '@cloudflare/workers-oauth-provider';
2+
import {
3+
AccountSchema,
4+
CloudflareAuthHandler,
5+
handleTokenExchangeCallback,
6+
UserSchema,
7+
} from '@repo/mcp-common/src/cloudflare-oauth-handler';
78

8-
import { ContainerManager } from './containerManager'
9-
import { ContainerMcpAgent } from './containerMcp'
9+
import { ContainerManager } from './containerManager';
10+
import { ContainerMcpAgent } from './containerMcp';
1011

11-
export { ContainerManager, ContainerMcpAgent }
12+
export { ContainerManager, ContainerMcpAgent };
1213

1314
export type Env = {
1415
CONTAINER_MCP_AGENT: DurableObjectNamespace<ContainerMcpAgent>
1516
CONTAINER_MANAGER: DurableObjectNamespace<ContainerManager>
1617
ENVIRONMENT: 'dev' | 'prod'
1718
}
1819

19-
// TODO: add user specific props
20-
export type Props = {}
21-
22-
const app = new Hono<{ Bindings: Env }>()
20+
// Context from the auth process, encrypted & stored in the auth token
21+
// and provided to the DurableMCP as this.props
22+
export type Props = {
23+
accessToken: string
24+
user: UserSchema['result']
25+
accounts: AccountSchema['result']
26+
}
2327

24-
export default ContainerMcpAgent.mount('/sse', { binding: 'CONTAINER_MCP_AGENT' })
28+
export default new OAuthProvider({
29+
apiRoute: '/workers/sandbox/sse',
30+
// @ts-ignore
31+
apiHandler: ContainerMcpAgent.mount('/workers/sandbox/sse', { binding: 'CONTAINER_MCP_AGENT' }),
32+
// @ts-ignore
33+
defaultHandler: CloudflareAuthHandler,
34+
authorizeEndpoint: '/oauth/authorize',
35+
tokenEndpoint: '/token',
36+
tokenExchangeCallback: handleTokenExchangeCallback,
37+
// Cloudflare access token TTL
38+
accessTokenTTL: 3600,
39+
clientRegistrationEndpoint: '/register',
40+
})

apps/sandbox-container/wrangler.jsonc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,14 @@
3232
],
3333
"observability": {
3434
"enabled": true
35+
},
36+
"kv_namespaces": [
37+
{
38+
"binding": "OAUTH_KV",
39+
"id": "DEV_KV"
40+
}
41+
],
42+
"dev": {
43+
"port": 8976
3544
}
3645
}

pnpm-lock.yaml

Lines changed: 6 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)