Skip to content

Commit fa4a1b5

Browse files
committed
feat: Improved pagination support and typing on API calls.
1 parent b7b87a4 commit fa4a1b5

File tree

3 files changed

+177
-94
lines changed

3 files changed

+177
-94
lines changed

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

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

19+
import type { zReturnedAssetResult } from '@repo/mcp-common/src/schemas/cf1-integrations'
1920
import type { ToolDefinition } from '@repo/mcp-common/src/types/tools'
2021
import type { CASBMCP } from '../index'
2122

22-
const PAGE_SIZE = 3
23-
2423
const integrationIdParam = z.string().describe('The UUID of the integration to analyze')
2524
const assetSearchTerm = z.string().describe('The search keyword for assets')
2625
const assetIdParam = z.string().describe('The UUID of the asset to analyze')
2726
const assetCategoryIdParam = z.string().describe('The UUID of the asset category to analyze')
2827

28+
const paginationParams = z
29+
.object({
30+
page: z.number().optional().describe('Page number for pagination'),
31+
pageSize: z.number().optional().describe('Number of items per page'),
32+
})
33+
.optional()
34+
35+
const assetLite = ({ fields: _, integration, ...asset }: zReturnedAssetResult) => ({
36+
...asset,
37+
integration: {
38+
id: integration.id,
39+
name: integration.name,
40+
},
41+
})
42+
2943
const toolDefinitions: Array<ToolDefinition<any>> = [
3044
{
3145
name: 'integration_by_id',
3246
description: 'Analyze Cloudflare One Integration by ID',
33-
params: { integrationIdParam },
47+
params: { integrationIdParam, paginationParams },
3448
handler: async ({
3549
integrationIdParam,
3650
accountId,
3751
apiToken,
52+
paginationParams = {},
3853
}: {
3954
integrationIdParam: string
4055
accountId: string
4156
apiToken: string
57+
paginationParams?: { page?: number; pageSize?: number }
4258
}) => {
4359
const { integration } = await handleIntegrationById({
4460
integrationIdParam,
4561
accountId,
4662
apiToken,
63+
...paginationParams,
4764
})
4865
return { integration }
4966
},
5067
},
5168
{
5269
name: 'integrations_list',
5370
description: 'List all Cloudflare One Integrations in a given account',
54-
params: {},
55-
handler: async ({ accountId, apiToken }: { accountId: string; apiToken: string }) => {
56-
const { integrations } = await handleIntegrations({ accountId, apiToken })
57-
return { integrations }
71+
params: { paginationParams },
72+
handler: async ({
73+
accountId,
74+
apiToken,
75+
paginationParams = {},
76+
}: {
77+
accountId: string
78+
apiToken: string
79+
paginationParams?: { page?: number; pageSize?: number }
80+
}) => {
81+
const { integrations, result_info } = await handleIntegrations({
82+
accountId,
83+
apiToken,
84+
...paginationParams,
85+
})
86+
return { integrations, result_info }
5887
},
5988
},
6089
{
6190
name: 'assets_search',
6291
description: 'Search Assets by keyword',
63-
params: { assetSearchTerm },
92+
params: { assetSearchTerm, paginationParams },
6493
handler: async ({
6594
assetSearchTerm,
6695
accountId,
6796
apiToken,
97+
paginationParams = {},
6898
}: {
6999
assetSearchTerm: string
70100
accountId: string
71101
apiToken: string
102+
paginationParams?: { page?: number; pageSize?: number }
72103
}) => {
73-
const { assets } = await handleAssetsSearch({
104+
const { assets, result_info } = await handleAssetsSearch({
74105
accountId,
75106
apiToken,
76107
searchTerm: assetSearchTerm,
77-
pageSize: PAGE_SIZE,
108+
...paginationParams,
78109
})
79-
return { assets }
110+
return { assets: assets.map(assetLite), result_info }
80111
},
81112
},
82113
{
83114
name: 'asset_by_id',
84115
description: 'Search Assets by ID',
85-
params: { assetIdParam },
116+
params: { assetIdParam, paginationParams },
86117
handler: async ({
87118
assetIdParam,
88119
accountId,
89120
apiToken,
121+
paginationParams = {},
90122
}: {
91123
assetIdParam: string
92124
accountId: string
93125
apiToken: string
126+
paginationParams?: { page?: number; pageSize?: number }
94127
}) => {
95128
const { asset } = await handleAssetById({
96129
accountId,
97130
apiToken,
98131
assetId: assetIdParam,
132+
...paginationParams,
99133
})
100134
return { asset }
101135
},
102136
},
103137
{
104138
name: 'assets_by_integration_id',
105139
description: 'Search Assets by Integration ID',
106-
params: { integrationIdParam },
140+
params: { integrationIdParam, paginationParams },
107141
handler: async ({
108142
integrationIdParam,
109143
accountId,
110144
apiToken,
145+
paginationParams = {},
111146
}: {
112147
integrationIdParam: string
113148
accountId: string
114149
apiToken: string
150+
paginationParams?: { page?: number; pageSize?: number }
115151
}) => {
116-
const { assets } = await handleAssetsByIntegrationId({
152+
const { assets, result_info } = await handleAssetsByIntegrationId({
117153
accountId,
118154
apiToken,
119155
integrationId: integrationIdParam,
120-
pageSize: PAGE_SIZE,
156+
...paginationParams,
121157
})
122-
return { assets }
158+
return { assets: assets.map(assetLite), result_info }
123159
},
124160
},
125161
{
126162
name: 'assets_by_category_id',
127163
description: 'Search Assets by Asset Category ID',
128-
params: { assetCategoryIdParam },
164+
params: { assetCategoryIdParam, paginationParams },
129165
handler: async ({
130166
assetCategoryIdParam,
131167
accountId,
132168
apiToken,
169+
paginationParams = {},
133170
}: {
134171
assetCategoryIdParam: string
135172
accountId: string
136173
apiToken: string
174+
paginationParams?: { page?: number; pageSize?: number }
137175
}) => {
138-
const { assets } = await handleAssetsByAssetCategoryId({
176+
const { assets, result_info } = await handleAssetsByAssetCategoryId({
139177
accountId,
140178
apiToken,
141179
categoryId: assetCategoryIdParam,
142-
pageSize: PAGE_SIZE,
180+
...paginationParams,
143181
})
144-
return { assets }
182+
return { assets: assets.map(assetLite), result_info }
145183
},
146184
},
147185
{
148186
name: 'assets_list',
149187
description: 'Paginated list of Assets',
150-
params: {},
151-
handler: async ({ accountId, apiToken }: { accountId: string; apiToken: string }) => {
152-
const { assets } = await handleAssets({
188+
params: { paginationParams },
189+
handler: async ({
190+
accountId,
191+
apiToken,
192+
paginationParams = {},
193+
}: {
194+
accountId: string
195+
apiToken: string
196+
paginationParams?: { page?: number; pageSize?: number }
197+
}) => {
198+
const { assets, result_info } = await handleAssets({
153199
accountId,
154200
apiToken,
155-
pageSize: PAGE_SIZE,
201+
...paginationParams,
156202
})
157-
return { assets }
203+
return { assets: assets.map(assetLite), result_info }
158204
},
159205
},
160206
{
161207
name: 'asset_categories_list',
162208
description: 'List Asset Categories',
163-
params: {},
164-
handler: async ({ accountId, apiToken }: { accountId: string; apiToken: string }) => {
165-
const { categories } = await handleAssetCategories({
209+
params: { paginationParams },
210+
handler: async ({
211+
accountId,
212+
apiToken,
213+
paginationParams = {},
214+
}: {
215+
accountId: string
216+
apiToken: string
217+
paginationParams?: { page?: number; pageSize?: number }
218+
}) =>
219+
await handleAssetCategories({
166220
accountId,
167221
apiToken,
168-
})
169-
return { categories }
170-
},
222+
...paginationParams,
223+
}),
171224
},
172225
{
173226
name: 'asset_categories_by_vendor',
174227
description: 'List asset categories by vendor',
175-
params: { assetCategoryVendorParam },
228+
params: { assetCategoryVendorParam, paginationParams },
176229
handler: async ({
177230
assetCategoryVendorParam,
178231
accountId,
179232
apiToken,
233+
paginationParams = {},
180234
}: {
181235
assetCategoryVendorParam: string
182236
accountId: string
183237
apiToken: string
184-
}) => {
185-
const { categories } = await handleAssetCategories({
238+
paginationParams?: { page?: number; pageSize?: number }
239+
}) =>
240+
await handleAssetCategories({
186241
accountId,
187242
apiToken,
188243
vendor: assetCategoryVendorParam,
189-
})
190-
return { categories }
191-
},
244+
...paginationParams,
245+
}),
192246
},
193247
{
194248
name: 'asset_categories_by_type',
195249
description: 'Search Asset Categories by type',
196-
params: { assetCategoryTypeParam },
250+
params: { assetCategoryTypeParam, paginationParams },
197251
handler: async ({
198252
assetCategoryTypeParam,
199253
accountId,
200254
apiToken,
255+
paginationParams = {},
201256
}: {
202257
assetCategoryTypeParam?: string
203258
accountId: string
204259
apiToken: string
205-
}) => {
206-
const { categories } = await handleAssetCategories({
260+
paginationParams?: { page?: number; pageSize?: number }
261+
}) =>
262+
await handleAssetCategories({
207263
accountId,
208264
apiToken,
209265
type: assetCategoryTypeParam,
210-
})
211-
return { categories }
212-
},
266+
...paginationParams,
267+
}),
213268
},
214269
{
215270
name: 'asset_categories_by_vendor_and_type',
216271
description: 'Search Asset Categories by vendor and type',
217-
params: { assetCategoryTypeParam, assetCategoryVendorParam },
272+
params: { assetCategoryTypeParam, assetCategoryVendorParam, paginationParams },
218273
handler: async ({
219274
assetCategoryTypeParam,
220275
assetCategoryVendorParam,
221276
accountId,
222277
apiToken,
278+
paginationParams = {},
223279
}: {
224280
assetCategoryTypeParam?: string
225281
assetCategoryVendorParam: string
226282
accountId: string
227283
apiToken: string
228-
}) => {
229-
const { categories } = await handleAssetCategories({
284+
paginationParams?: { page?: number; pageSize?: number }
285+
}) =>
286+
await handleAssetCategories({
230287
accountId,
231288
apiToken,
232289
type: assetCategoryTypeParam,
233290
vendor: assetCategoryVendorParam,
234-
})
235-
return { categories }
236-
},
291+
...paginationParams,
292+
}),
237293
},
238294
]
239295

240-
/**
241-
* Registers the logs analysis tool with the MCP server
242-
* @param agent The MCP server instance
243-
*/
244296
export function registerIntegrationsTools(agent: CASBMCP) {
245297
toolDefinitions.forEach(({ name, description, params, handler }) => {
246298
agent.server.tool(name, description, params, withAccountCheck(agent, handler))

0 commit comments

Comments
 (0)