Skip to content

Commit 411f493

Browse files
committed
Use getProps() which throws an error if props isn't defined.
- In new versions of agents package, props might be undefined, so we now always have to check that's defined
1 parent 241f1ab commit 411f493

File tree

35 files changed

+337
-186
lines changed

35 files changed

+337
-186
lines changed

apps/ai-gateway/src/ai-gateway.app.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
99
import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details.do'
1010
import { getEnv } from '@repo/mcp-common/src/env'
11+
import { getProps } from '@repo/mcp-common/src/get-props'
1112
import { RequiredScopes } from '@repo/mcp-common/src/scopes'
1213
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
1314
import { registerAccountTools } from '@repo/mcp-common/src/tools/account.tools'
@@ -51,7 +52,8 @@ export class AIGatewayMCP extends McpAgent<Env, State, Props> {
5152

5253
async init() {
5354
// TODO: Probably we'll want to track account tokens usage through an account identifier at some point
54-
const userId = this.props.type === 'user_token' ? this.props.user.id : undefined
55+
const props = getProps(this)
56+
const userId = props.type === 'user_token' ? props.user.id : undefined
5557

5658
this.server = new CloudflareMCPServer({
5759
userId,
@@ -70,13 +72,14 @@ export class AIGatewayMCP extends McpAgent<Env, State, Props> {
7072

7173
async getActiveAccountId() {
7274
try {
75+
const props = getProps(this)
7376
// account tokens are scoped to one account
74-
if (this.props.type === 'account_token') {
75-
return this.props.account.id
77+
if (props.type === 'account_token') {
78+
return props.account.id
7679
}
7780
// Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it
7881
// we do this so we can persist activeAccountId across sessions
79-
const userDetails = getUserDetails(env, this.props.user.id)
82+
const userDetails = getUserDetails(env, props.user.id)
8083
return await userDetails.getActiveAccountId()
8184
} catch (e) {
8285
this.server.recordError(e)
@@ -86,11 +89,12 @@ export class AIGatewayMCP extends McpAgent<Env, State, Props> {
8689

8790
async setActiveAccountId(accountId: string) {
8891
try {
92+
const props = getProps(this)
8993
// account tokens are scoped to one account
90-
if (this.props.type === 'account_token') {
94+
if (props.type === 'account_token') {
9195
return
9296
}
93-
const userDetails = getUserDetails(env, this.props.user.id)
97+
const userDetails = getUserDetails(env, props.user.id)
9498
await userDetails.setActiveAccountId(accountId)
9599
} catch (e) {
96100
this.server.recordError(e)

apps/ai-gateway/src/tools/ai-gateway.tools.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getCloudflareClient } from '@repo/mcp-common/src/cloudflare-api'
2+
import { getProps } from '@repo/mcp-common/src/get-props'
23

34
import { GatewayIdParam, ListLogsParams, LogIdParam, pageParam, perPageParam } from '../types'
45

@@ -26,7 +27,8 @@ export function registerAIGatewayTools(agent: AIGatewayMCP) {
2627
}
2728
}
2829
try {
29-
const client = getCloudflareClient(agent.props.accessToken)
30+
const props = getProps(agent)
31+
const client = getCloudflareClient(props.accessToken)
3032
const r = await client.aiGateway.list({
3133
account_id: accountId,
3234
page: params.page,
@@ -73,7 +75,8 @@ export function registerAIGatewayTools(agent: AIGatewayMCP) {
7375

7476
const { gateway_id, ...filters } = params
7577

76-
const client = getCloudflareClient(agent.props.accessToken)
78+
const props = getProps(agent)
79+
const client = getCloudflareClient(props.accessToken)
7780
const r = await client.aiGateway.logs.list(gateway_id, {
7881
...filters,
7982
account_id: accountId,
@@ -123,7 +126,8 @@ export function registerAIGatewayTools(agent: AIGatewayMCP) {
123126
}
124127

125128
try {
126-
const client = getCloudflareClient(agent.props.accessToken)
129+
const props = getProps(agent)
130+
const client = getCloudflareClient(props.accessToken)
127131
const r = await client.aiGateway.logs.get(params.gateway_id, params.log_id, {
128132
account_id: accountId,
129133
})
@@ -172,7 +176,8 @@ export function registerAIGatewayTools(agent: AIGatewayMCP) {
172176
}
173177

174178
try {
175-
const client = getCloudflareClient(agent.props.accessToken)
179+
const props = getProps(agent)
180+
const client = getCloudflareClient(props.accessToken)
176181
const r = await client.aiGateway.logs.request(params.gateway_id, params.log_id, {
177182
account_id: accountId,
178183
})
@@ -221,7 +226,8 @@ export function registerAIGatewayTools(agent: AIGatewayMCP) {
221226
}
222227

223228
try {
224-
const client = getCloudflareClient(agent.props.accessToken)
229+
const props = getProps(agent)
230+
const client = getCloudflareClient(props.accessToken)
225231
const r = await client.aiGateway.logs.response(params.gateway_id, params.log_id, {
226232
account_id: accountId,
227233
})

apps/auditlogs/src/auditlogs.app.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
99
import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details.do'
1010
import { getEnv } from '@repo/mcp-common/src/env'
11+
import { getProps } from '@repo/mcp-common/src/get-props'
1112
import { RequiredScopes } from '@repo/mcp-common/src/scopes'
1213
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
1314
import { registerAccountTools } from '@repo/mcp-common/src/tools/account.tools'
@@ -52,7 +53,8 @@ export class AuditlogMCP extends McpAgent<Env, State, Props> {
5253

5354
async init() {
5455
// TODO: Probably we'll want to track account tokens usage through an account identifier at some point
55-
const userId = this.props.type === 'user_token' ? this.props.user.id : undefined
56+
const props = getProps(this)
57+
const userId = props.type === 'user_token' ? props.user.id : undefined
5658

5759
this.server = new CloudflareMCPServer({
5860
userId,
@@ -70,13 +72,14 @@ export class AuditlogMCP extends McpAgent<Env, State, Props> {
7072

7173
async getActiveAccountId() {
7274
try {
75+
const props = getProps(this)
7376
// account tokens are scoped to one account
74-
if (this.props.type === 'account_token') {
75-
return this.props.account.id
77+
if (props.type === 'account_token') {
78+
return props.account.id
7679
}
7780
// Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it
7881
// we do this so we can persist activeAccountId across sessions
79-
const userDetails = getUserDetails(env, this.props.user.id)
82+
const userDetails = getUserDetails(env, props.user.id)
8083
return await userDetails.getActiveAccountId()
8184
} catch (e) {
8285
this.server.recordError(e)
@@ -86,11 +89,12 @@ export class AuditlogMCP extends McpAgent<Env, State, Props> {
8689

8790
async setActiveAccountId(accountId: string) {
8891
try {
92+
const props = getProps(this)
8993
// account tokens are scoped to one account
90-
if (this.props.type === 'account_token') {
94+
if (props.type === 'account_token') {
9195
return
9296
}
93-
const userDetails = getUserDetails(env, this.props.user.id)
97+
const userDetails = getUserDetails(env, props.user.id)
9498
await userDetails.setActiveAccountId(accountId)
9599
} catch (e) {
96100
this.server.recordError(e)

apps/auditlogs/src/tools/auditlogs.tools.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from 'zod'
22

33
import { fetchCloudflareApi } from '@repo/mcp-common/src/cloudflare-api'
4+
import { getProps } from '@repo/mcp-common/src/get-props'
45

56
import type { AuditlogMCP } from '../auditlogs.app'
67

@@ -253,7 +254,8 @@ export function registerAuditLogTools(agent: AuditlogMCP) {
253254
}
254255
}
255256
try {
256-
const result = await handleGetAuditLogs(accountId, agent.props.accessToken, params)
257+
const props = getProps(agent)
258+
const result = await handleGetAuditLogs(accountId, props.accessToken, params)
257259
return {
258260
content: [
259261
{

apps/autorag/src/autorag.app.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
99
import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details.do'
1010
import { getEnv } from '@repo/mcp-common/src/env'
11+
import { getProps } from '@repo/mcp-common/src/get-props'
1112
import { RequiredScopes } from '@repo/mcp-common/src/scopes'
1213
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
1314
import { registerAccountTools } from '@repo/mcp-common/src/tools/account.tools'
@@ -51,7 +52,8 @@ export class AutoRAGMCP extends McpAgent<Env, State, Props> {
5152

5253
async init() {
5354
// TODO: Probably we'll want to track account tokens usage through an account identifier at some point
54-
const userId = this.props.type === 'user_token' ? this.props.user.id : undefined
55+
const props = getProps(this)
56+
const userId = props.type === 'user_token' ? props.user.id : undefined
5557

5658
this.server = new CloudflareMCPServer({
5759
userId,
@@ -70,13 +72,14 @@ export class AutoRAGMCP extends McpAgent<Env, State, Props> {
7072

7173
async getActiveAccountId() {
7274
try {
75+
const props = getProps(this)
7376
// account tokens are scoped to one account
74-
if (this.props.type === 'account_token') {
75-
return this.props.account.id
77+
if (props.type === 'account_token') {
78+
return props.account.id
7679
}
7780
// Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it
7881
// we do this so we can persist activeAccountId across sessions
79-
const userDetails = getUserDetails(env, this.props.user.id)
82+
const userDetails = getUserDetails(env, props.user.id)
8083
return await userDetails.getActiveAccountId()
8184
} catch (e) {
8285
this.server.recordError(e)
@@ -86,11 +89,12 @@ export class AutoRAGMCP extends McpAgent<Env, State, Props> {
8689

8790
async setActiveAccountId(accountId: string) {
8891
try {
92+
const props = getProps(this)
8993
// account tokens are scoped to one account
90-
if (this.props.type === 'account_token') {
94+
if (props.type === 'account_token') {
9195
return
9296
}
93-
const userDetails = getUserDetails(env, this.props.user.id)
97+
const userDetails = getUserDetails(env, props.user.id)
9498
await userDetails.setActiveAccountId(accountId)
9599
} catch (e) {
96100
this.server.recordError(e)

apps/autorag/src/tools/autorag.tools.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { V4PagePaginationArray } from 'cloudflare/src/pagination.js'
22
import { z } from 'zod'
33

44
import { getCloudflareClient } from '@repo/mcp-common/src/cloudflare-api'
5+
import { getProps } from '@repo/mcp-common/src/get-props'
56

67
import { pageParam, perPageParam } from '../types'
78

@@ -28,7 +29,8 @@ export function registerAutoRAGTools(agent: AutoRAGMCP) {
2829
}
2930
}
3031
try {
31-
const client = getCloudflareClient(agent.props.accessToken)
32+
const props = getProps(agent)
33+
const client = getCloudflareClient(props.accessToken)
3234
const r = (await client.getAPIList(
3335
`/accounts/${accountId}/autorag/rags`,
3436
// @ts-ignore
@@ -90,7 +92,8 @@ export function registerAutoRAGTools(agent: AutoRAGMCP) {
9092
}
9193
}
9294

93-
const client = getCloudflareClient(agent.props.accessToken)
95+
const props = getProps(agent)
96+
const client = getCloudflareClient(props.accessToken)
9497
const r = (await client.post(
9598
`/accounts/${accountId}/autorag/rags/${params.rag_id}/search`,
9699
{
@@ -155,7 +158,8 @@ export function registerAutoRAGTools(agent: AutoRAGMCP) {
155158
}
156159
}
157160

158-
const client = getCloudflareClient(agent.props.accessToken)
161+
const props = getProps(agent)
162+
const client = getCloudflareClient(props.accessToken)
159163
const r = (await client.post(
160164
`/accounts/${accountId}/autorag/rags/${params.rag_id}/ai-search`,
161165
{

apps/browser-rendering/src/browser.app.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
99
import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details.do'
1010
import { getEnv } from '@repo/mcp-common/src/env'
11+
import { getProps } from '@repo/mcp-common/src/get-props'
1112
import { RequiredScopes } from '@repo/mcp-common/src/scopes'
1213
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
1314
import { registerAccountTools } from '@repo/mcp-common/src/tools/account.tools'
@@ -51,7 +52,8 @@ export class BrowserMCP extends McpAgent<Env, State, Props> {
5152

5253
async init() {
5354
// TODO: Probably we'll want to track account tokens usage through an account identifier at some point
54-
const userId = this.props.type === 'user_token' ? this.props.user.id : undefined
55+
const props = getProps(this)
56+
const userId = props.type === 'user_token' ? props.user.id : undefined
5557

5658
this.server = new CloudflareMCPServer({
5759
userId,
@@ -70,13 +72,14 @@ export class BrowserMCP extends McpAgent<Env, State, Props> {
7072

7173
async getActiveAccountId() {
7274
try {
75+
const props = getProps(this)
7376
// account tokens are scoped to one account
74-
if (this.props.type === 'account_token') {
75-
return this.props.account.id
77+
if (props.type === 'account_token') {
78+
return props.account.id
7679
}
7780
// Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it
7881
// we do this so we can persist activeAccountId across sessions
79-
const userDetails = getUserDetails(env, this.props.user.id)
82+
const userDetails = getUserDetails(env, props.user.id)
8083
return await userDetails.getActiveAccountId()
8184
} catch (e) {
8285
this.server.recordError(e)
@@ -86,11 +89,12 @@ export class BrowserMCP extends McpAgent<Env, State, Props> {
8689

8790
async setActiveAccountId(accountId: string) {
8891
try {
92+
const props = getProps(this)
8993
// account tokens are scoped to one account
90-
if (this.props.type === 'account_token') {
94+
if (props.type === 'account_token') {
9195
return
9296
}
93-
const userDetails = getUserDetails(env, this.props.user.id)
97+
const userDetails = getUserDetails(env, props.user.id)
9498
await userDetails.setActiveAccountId(accountId)
9599
} catch (e) {
96100
this.server.recordError(e)

apps/browser-rendering/src/tools/browser.tools.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from 'zod'
22

33
import { getCloudflareClient } from '@repo/mcp-common/src/cloudflare-api'
4+
import { getProps } from '@repo/mcp-common/src/get-props'
45

56
import type { BrowserMCP } from '../browser.app'
67

@@ -24,7 +25,8 @@ export function registerBrowserTools(agent: BrowserMCP) {
2425
}
2526
}
2627
try {
27-
const client = getCloudflareClient(agent.props.accessToken)
28+
const props = getProps(agent)
29+
const client = getCloudflareClient(props.accessToken)
2830
const r = await client.browserRendering.content.create({
2931
account_id: accountId,
3032
url: params.url,
@@ -72,7 +74,8 @@ export function registerBrowserTools(agent: BrowserMCP) {
7274
}
7375
}
7476
try {
75-
const client = getCloudflareClient(agent.props.accessToken)
77+
const props = getProps(agent)
78+
const client = getCloudflareClient(props.accessToken)
7679
const r = (await client.post(`/accounts/${accountId}/browser-rendering/markdown`, {
7780
body: {
7881
url: params.url,
@@ -127,8 +130,9 @@ export function registerBrowserTools(agent: BrowserMCP) {
127130
}
128131
}
129132
try {
133+
const props = getProps(agent)
130134
// Cf client appears to be broken, so we use the raw API instead.
131-
// const client = getCloudflareClient(agent.props.accessToken)
135+
// const client = getCloudflareClient(props.accessToken)
132136
// const r = await client.browserRendering.screenshot.create({
133137
// account_id: accountId,
134138
// url: params.url,
@@ -141,7 +145,7 @@ export function registerBrowserTools(agent: BrowserMCP) {
141145
method: 'POST',
142146
headers: {
143147
'Content-Type': 'application/json',
144-
Authorization: `Bearer ${agent.props.accessToken}`,
148+
Authorization: `Bearer ${props.accessToken}`,
145149
},
146150
body: JSON.stringify({
147151
url: params.url,

0 commit comments

Comments
 (0)