Skip to content

Commit cf813a6

Browse files
committed
fix: Packages, Typing, and Env Oh My
1 parent 3c834d3 commit cf813a6

File tree

6 files changed

+53
-27
lines changed

6 files changed

+53
-27
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { CASBMCP, UserDetails } from './index'
2+
3+
export interface Env {
4+
ENVIRONMENT: 'development' | 'staging' | 'production'
5+
MCP_SERVER_NAME: string
6+
MCP_SERVER_VERSION: string
7+
MCP_OBJECT: DurableObjectNamespace<CASBMCP>
8+
MCP_METRICS: AnalyticsEngineDataset
9+
AI: Ai
10+
11+
CLOUDFLARE_CLIENT_ID: string
12+
CLOUDFLARE_CLIENT_SECRET: string
13+
14+
USER_DETAILS: DurableObjectNamespace<UserDetails>
15+
}

apps/cloudflare-one-casb/src/index.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import OAuthProvider from '@cloudflare/workers-oauth-provider'
22
import { McpAgent } from 'agents/mcp'
3-
import { env } from 'cloudflare:workers'
43

54
import {
65
createAuthHandlers,
76
handleTokenExchangeCallback,
87
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
8+
import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details'
9+
import { getEnv } from '@repo/mcp-common/src/env'
910
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
1011
import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
1112

1213
import { MetricsTracker } from '../../../packages/mcp-observability/src'
1314
import { registerIntegrationsTools } from './tools/integrations'
1415

1516
import type { AccountSchema, UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
16-
import type { CloudflareMcpAgent } from '@repo/mcp-common/src/types/cloudflare-mcp-agent'
17+
import type { Env } from './context'
18+
19+
export { UserDetails }
20+
21+
const env = getEnv<Env>()
1722

1823
const metrics = new MetricsTracker(env.MCP_METRICS, {
1924
name: env.MCP_SERVER_NAME,
@@ -46,32 +51,37 @@ export class CASBMCP extends McpAgent<Env, State, Props> {
4651
}
4752

4853
async init() {
49-
this.server = new CloudflareMCPServer(this.props.user.id, this.env.MCP_METRICS, {
50-
name: this.env.MCP_SERVER_NAME,
51-
version: this.env.MCP_SERVER_VERSION,
54+
this.server = new CloudflareMCPServer({
55+
userId: this.props.user.id,
56+
wae: this.env.MCP_METRICS,
57+
serverInfo: {
58+
name: this.env.MCP_SERVER_NAME,
59+
version: this.env.MCP_SERVER_VERSION,
60+
},
5261
})
5362

5463
registerAccountTools(this)
5564
registerIntegrationsTools(this)
5665
}
5766

58-
getActiveAccountId() {
67+
async getActiveAccountId() {
5968
try {
60-
return this.state.activeAccountId ?? null
69+
// Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it
70+
// we do this so we can persist activeAccountId across sessions
71+
const userDetails = getUserDetails(env, this.props.user.id)
72+
return await userDetails.getActiveAccountId()
6173
} catch (e) {
62-
console.error('getActiveAccountId failured: ', e)
74+
this.server.recordError(e)
6375
return null
6476
}
6577
}
6678

67-
setActiveAccountId(accountId: string) {
79+
async setActiveAccountId(accountId: string) {
6880
try {
69-
this.setState({
70-
...this.state,
71-
activeAccountId: accountId,
72-
})
81+
const userDetails = getUserDetails(env, this.props.user.id)
82+
await userDetails.setActiveAccountId(accountId)
7383
} catch (e) {
74-
return null
84+
this.server.recordError(e)
7585
}
7686
}
7787
}

apps/cloudflare-one-casb/src/tools/integrations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
assetCategoryVendorParam,
1717
} from '@repo/mcp-common/src/schemas/cf1-integrations'
1818

19-
import type { McpAgentWithAccount } from '@repo/mcp-common/src/api/account'
19+
import type { CloudflareMcpAgent } from '@repo/mcp-common/src/types/cloudflare-mcp-agent'
2020
import type { ToolDefinition } from '@repo/mcp-common/src/types/tools'
2121
import type { CASBMCP } from '../index'
2222

@@ -248,7 +248,7 @@ export function registerIntegrationsTools(agent: CASBMCP) {
248248
name,
249249
description,
250250
params,
251-
withAccountCheck(agent as unknown as McpAgentWithAccount, handler)
251+
withAccountCheck(agent as unknown as CloudflareMcpAgent, handler)
252252
)
253253
})
254254
}

apps/cloudflare-one-casb/worker-configuration.d.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
// Generated by Wrangler by running `wrangler types` (hash: 085ce38c6421603ed1a73fa9678ae0a7)
1+
// Generated by Wrangler by running `wrangler types` (hash: 16591b3e7a816201c4869a97f0b7602f)
22
// Runtime types generated with [email protected] 2025-03-10 nodejs_compat
33
declare namespace Cloudflare {
44
interface Env {
55
OAUTH_KV: KVNamespace;
6-
CLOUDFLARE_CLIENT_ID: string;
7-
CLOUDFLARE_CLIENT_SECRET: string;
8-
MCP_OBJECT: DurableObjectNamespace<import("./src/index").CASBMCP>;
9-
10-
MCP_SERVER_NAME: "PLACEHOLDER";
6+
MCP_SERVER_NAME: "PLACEHOLDER";
117
MCP_SERVER_VERSION: "PLACEHOLDER";
12-
MCP_METRICS: AnalyticsEngineDataset;
8+
CLOUDFLARE_CLIENT_ID: "<PLACEHOLDER>";
9+
CLOUDFLARE_CLIENT_SECRET: "<PLACEHOLDER>";
10+
MCP_OBJECT: DurableObjectNamespace<import("./src/index").CASBMCP>;
11+
MCP_METRICS: AnalyticsEngineDataset;
1312
}
1413
}
1514
interface Env extends Cloudflare.Env {}

apps/cloudflare-one-casb/wrangler.jsonc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
},
1919
"vars": {
2020
"MCP_SERVER_NAME": "PLACEHOLDER",
21-
"MCP_SERVER_VERSION": "PLACEHOLDER"
21+
"MCP_SERVER_VERSION": "PLACEHOLDER",
22+
"CLOUDFLARE_CLIENT_ID": "<PLACEHOLDER>",
23+
"CLOUDFLARE_CLIENT_SECRET": "<PLACEHOLDER>"
2224
},
25+
2326
"dev": {
2427
"port": 8976
2528
},

packages/mcp-common/src/schemas/cf1-integrations.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export type zReturnedAssetsResult = z.infer<typeof AssetsResponse>
7373
export const AssetCategoriesResponse = z.array(AssetCategory)
7474
export type zReturnedAssetCategoriesResult = z.infer<typeof AssetCategoriesResponse>
7575

76-
7776
export const assetCategoryTypeParam = z
7877
.enum([
7978
'Account',
@@ -127,7 +126,7 @@ export const assetCategoryTypeParam = z
127126
.optional()
128127
.describe('Type of cloud resource or service category')
129128

130-
export const assetCategoryVendorParam = z
129+
export const assetCategoryVendorParam = z
131130
.enum([
132131
'AWS',
133132
'Bitbucket',
@@ -147,4 +146,4 @@ export const assetCategoryTypeParam = z
147146
'Workday',
148147
'Zoom',
149148
])
150-
.describe('Vendor of the cloud service or resource')
149+
.describe('Vendor of the cloud service or resource')

0 commit comments

Comments
 (0)