Skip to content

Commit 4c7ef2f

Browse files
Merge pull request #45 from cloudflare/wp-1587-courtney
Add Cloudflare OAUTH to Sandbox Container
2 parents e25f4a3 + 50da7db commit 4c7ef2f

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

apps/sandbox-container/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
"dev": "concurrently \"tsx container/index.ts\" \"wrangler dev --var \"ENVIRONMENT:dev\"\"",
1010
"build": "docker build .",
1111
"start": "wrangler dev",
12-
"start:container": "tsx container/index.ts"
12+
"start:container": "tsx container/index.ts",
13+
"postinstall": "mkdir -p workdir"
1314
},
1415
"dependencies": {
16+
"@cloudflare/workers-oauth-provider": "0.0.2",
1517
"@cloudflare/workers-types": "^4.20250320.0",
1618
"@hono/node-server": "^1.13.8",
1719
"@hono/zod-validator": "^0.4.3",
@@ -26,7 +28,8 @@
2628
"partyserver": "^0.0.65",
2729
"tsx": "^4.19.3",
2830
"workers-mcp": "0.1.0-3",
29-
"zod": "^3.24.2"
31+
"zod": "^3.24.2",
32+
"@repo/mcp-common": "workspace:*"
3033
},
3134
"devDependencies": {
3235
"concurrently": "^9.1.2",
Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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+
3+
import {
4+
AccountSchema,
5+
CloudflareAuthHandler,
6+
handleTokenExchangeCallback,
7+
UserSchema,
8+
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
79

810
import { ContainerManager } from './containerManager'
911
import { ContainerMcpAgent } from './containerMcp'
@@ -16,9 +18,24 @@ export type Env = {
1618
ENVIRONMENT: 'dev' | 'prod'
1719
}
1820

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

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

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)