1
1
import { z } from 'zod'
2
2
3
+ import { withAccountCheck } from '@repo/mcp-common/src/api/account'
3
4
import {
4
5
handleAssetById ,
5
6
handleAssetCategories ,
@@ -15,73 +16,17 @@ import {
15
16
assetCategoryVendorParam ,
16
17
} from '@repo/mcp-common/src/schemas/cf1-integrations'
17
18
19
+ import type { McpAgentWithAccount } from '@repo/mcp-common/src/api/account'
20
+ import type { ToolDefinition } from '@repo/mcp-common/src/types/tools'
18
21
import type { CASBMCP } from '../index'
19
22
20
23
const PAGE_SIZE = 3
21
24
22
- // CF1INT Integration Params
23
25
const integrationIdParam = z . string ( ) . describe ( 'The UUID of the integration to analyze' )
24
-
25
- // CF1INT Asset Params
26
26
const assetSearchTerm = z . string ( ) . describe ( 'The search keyword for assets' )
27
27
const assetIdParam = z . string ( ) . describe ( 'The UUID of the asset to analyze' )
28
28
const assetCategoryIdParam = z . string ( ) . describe ( 'The UUID of the asset category to analyze' )
29
29
30
- // Define types for our tool handlers
31
- type ToolHandler < T extends Record < string , any > > = (
32
- params : T & { accountId : string ; apiToken : string }
33
- ) => Promise < any >
34
-
35
- interface ToolDefinition < T extends Record < string , any > > {
36
- name : string
37
- description : string
38
- params : Record < string , z . ZodType >
39
- handler : ToolHandler < T >
40
- }
41
-
42
- // Helper function to handle common error cases and account ID checks
43
- const withAccountCheck = < T extends Record < string , any > > (
44
- agent : CASBMCP ,
45
- handler : ToolHandler < T >
46
- ) => {
47
- return async ( params : T ) => {
48
- const accountId = agent . getActiveAccountId ( )
49
- if ( ! accountId ) {
50
- return {
51
- content : [
52
- {
53
- type : 'text' as const ,
54
- text : 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)' ,
55
- } ,
56
- ] ,
57
- }
58
- }
59
-
60
- try {
61
- const result = await handler ( {
62
- ...params ,
63
- accountId,
64
- apiToken : agent . props . accessToken ,
65
- } )
66
- return {
67
- content : [ { type : 'text' as const , text : JSON . stringify ( result ) } ] ,
68
- }
69
- } catch ( error ) {
70
- return {
71
- content : [
72
- {
73
- type : 'text' as const ,
74
- text : JSON . stringify ( {
75
- error : `Error processing request: ${ error instanceof Error ? error . message : 'Unknown error' } ` ,
76
- } ) ,
77
- } ,
78
- ] ,
79
- }
80
- }
81
- }
82
- }
83
-
84
- // Tool definitions with their handlers
85
30
const toolDefinitions : Array < ToolDefinition < any > > = [
86
31
{
87
32
name : 'integration_by_id' ,
@@ -109,7 +54,6 @@ const toolDefinitions: Array<ToolDefinition<any>> = [
109
54
description : 'List all Cloudflare One Integrations in a given account' ,
110
55
params : { } ,
111
56
handler : async ( { accountId, apiToken } : { accountId : string ; apiToken : string } ) => {
112
- console . log ( 'integrations_list' , accountId , apiToken )
113
57
const { integrations } = await handleIntegrations ( { accountId, apiToken } )
114
58
return { integrations }
115
59
} ,
@@ -300,6 +244,11 @@ const toolDefinitions: Array<ToolDefinition<any>> = [
300
244
*/
301
245
export function registerIntegrationsTools ( agent : CASBMCP ) {
302
246
toolDefinitions . forEach ( ( { name, description, params, handler } ) => {
303
- agent . server . tool ( name , description , params , withAccountCheck ( agent , handler ) )
247
+ agent . server . tool (
248
+ name ,
249
+ description ,
250
+ params ,
251
+ withAccountCheck ( agent as unknown as McpAgentWithAccount , handler )
252
+ )
304
253
} )
305
254
}
0 commit comments