11import { z } from 'zod'
22
33import { fetchCloudflareApi } from '@repo/mcp-common/src/cloudflare-api'
4+
45import type { LogsMCP } from '../index'
56
6- const zJobIdentifier = z . number ( ) . int ( ) . min ( 1 ) . optional ( ) . describe ( "Unique id of the job." ) ;
7- const zEnabled = z . boolean ( ) . optional ( ) . describe ( "Flag that indicates if the job is enabled." ) ;
8- const zName = z . string ( )
9- . regex ( / ^ [ a - z A - Z 0 - 9 \- \. ] * $ / )
10- . max ( 512 )
11- . nullable ( )
12- . optional ( )
13- . describe ( "Optional human readable job name. Not unique." ) ;
14- const zDataset = z . string ( )
15- . regex ( / ^ [ a - z A - Z 0 - 9 _ \- ] * $ / )
16- . max ( 256 )
17- . nullable ( )
18- . optional ( )
19- . describe ( "Name of the dataset." ) ;
20- const zLastComplete = z . string ( )
21- . datetime ( )
22- . nullable ( )
23- . optional ( )
24- . describe ( "Records the last time for which logs have been successfully pushed." ) ;
25- const zLastError = z . string ( )
26- . datetime ( )
27- . nullable ( )
28- . optional ( )
29- . describe ( "Records the last time the job failed." ) ;
30- const zErrorMessage = z . string ( )
31- . nullable ( )
32- . optional ( )
33- . describe ( "If not null, the job is currently failing." ) ;
7+ const zJobIdentifier = z . number ( ) . int ( ) . min ( 1 ) . optional ( ) . describe ( 'Unique id of the job.' )
8+ const zEnabled = z . boolean ( ) . optional ( ) . describe ( 'Flag that indicates if the job is enabled.' )
9+ const zName = z
10+ . string ( )
11+ . regex ( / ^ [ a - z A - Z 0 - 9 \- \. ] * $ / )
12+ . max ( 512 )
13+ . nullable ( )
14+ . optional ( )
15+ . describe ( 'Optional human readable job name. Not unique.' )
16+ const zDataset = z
17+ . string ( )
18+ . regex ( / ^ [ a - z A - Z 0 - 9 _ \- ] * $ / )
19+ . max ( 256 )
20+ . nullable ( )
21+ . optional ( )
22+ . describe ( 'Name of the dataset.' )
23+ const zLastComplete = z
24+ . string ( )
25+ . datetime ( )
26+ . nullable ( )
27+ . optional ( )
28+ . describe ( 'Records the last time for which logs have been successfully pushed.' )
29+ const zLastError = z
30+ . string ( )
31+ . datetime ( )
32+ . nullable ( )
33+ . optional ( )
34+ . describe ( 'Records the last time the job failed.' )
35+ const zErrorMessage = z
36+ . string ( )
37+ . nullable ( )
38+ . optional ( )
39+ . describe ( 'If not null, the job is currently failing.' )
40+
41+ export const zLogpushJob = z
42+ . object ( {
43+ id : zJobIdentifier ,
44+ enabled : zEnabled ,
45+ name : zName ,
46+ dataset : zDataset ,
47+ last_complete : zLastComplete ,
48+ last_error : zLastError ,
49+ error_message : zErrorMessage ,
50+ } )
51+ . nullable ( )
52+ . optional ( )
3453
35- export const zLogpushJob = z . object ( {
36- id : zJobIdentifier ,
37- enabled : zEnabled ,
38- name : zName ,
39- dataset : zDataset ,
40- last_complete : zLastComplete ,
41- last_error : zLastError ,
42- error_message : zErrorMessage ,
43- } ) . nullable ( ) . optional ( ) ;
44-
45- const zApiResponseCommon = z . object ( {
54+ const zApiResponseCommon = z . object ( {
4655 success : z . literal ( true ) ,
47- errors : z . array ( z . object ( { message : z . string ( ) } ) ) . optional ( )
48- } ) ;
56+ errors : z . array ( z . object ( { message : z . string ( ) } ) ) . optional ( ) ,
57+ } )
4958
5059const zLogPushJobResults = z . array ( zLogpushJob ) . optional ( )
51-
52- // The complete schema for zone_logpush_job_response_collection
60+
61+ // The complete schema for zone_logpush_job_response_collection
5362export const zLogpushJobResponseCollection = zApiResponseCommon . extend ( {
54- result : zLogPushJobResults
55- } ) ;
63+ result : zLogPushJobResults ,
64+ } )
5665
5766/**
5867 * Fetches available telemetry keys for a specified Cloudflare Worker
@@ -63,7 +72,7 @@ export const zLogpushJobResponseCollection = zApiResponseCommon.extend({
6372
6473export async function handleGetAccountLogPushJobs (
6574 accountId : string ,
66- apiToken : string ,
75+ apiToken : string
6776) : Promise < z . infer < typeof zLogPushJobResults > > {
6877 // Call the Public API
6978 const data = await fetchCloudflareApi ( {
@@ -81,17 +90,16 @@ export async function handleGetAccountLogPushJobs(
8190 } )
8291
8392 const res = data as z . infer < typeof zLogpushJobResponseCollection >
84- return ( res . result . slice ( 0 , 100 ) || [ ] )
93+ return res . result ? .slice ( 0 , 100 ) || [ ]
8594}
8695
87-
8896/**
8997 * Registers the logs analysis tool with the MCP server
9098 * @param server The MCP server instance
9199 * @param accountId Cloudflare account ID
92100 * @param apiToken Cloudflare API token
93101 */
94- export function registerLogsTools ( agent : LogsMCP ) {
102+ export function registerLogsTools ( agent : LogsMCP , devToken : string ) {
95103 // Register the worker logs analysis tool by worker name
96104 agent . server . tool (
97105 'logpush_jobs_by_account_id' ,
@@ -117,8 +125,8 @@ export function registerLogsTools(agent: LogsMCP) {
117125 }
118126 }
119127 try {
120- const token = agent . props . apiToken . length > 0 ? agent . props . apiToken : agent . props . accessToken
121- const result = await handleGetAccountLogPushJobs ( accountId , token )
128+ const token = devToken . length > 0 ? devToken : agent . props . accessToken
129+ const result = await handleGetAccountLogPushJobs ( accountId , token )
122130 return {
123131 content : [
124132 {
0 commit comments