11import { z } from 'zod'
22
3- import { handleWorkerScriptDownload , handleWorkersList } from '../api/workers'
3+ import {
4+ handleGetWorkersService ,
5+ handleWorkerScriptDownload ,
6+ handleWorkersList ,
7+ } from '../api/workers'
48import { getCloudflareClient } from '../cloudflare-api'
59
610import type { CloudflareMcpAgent } from '../types/cloudflare-mcp-agent'
@@ -28,6 +32,7 @@ export function registerWorkersTools(agent: CloudflareMcpAgent) {
2832 ] ,
2933 }
3034 }
35+
3136 try {
3237 const results = await handleWorkersList ( {
3338 client : getCloudflareClient ( agent . props . accessToken ) ,
@@ -38,7 +43,7 @@ export function registerWorkersTools(agent: CloudflareMcpAgent) {
3843 . map ( ( worker ) => ( {
3944 name : worker . id ,
4045 // The API client doesn't know tag exists. The tag is needed in other places such as Workers Builds
41- script_id : z . object ( { tag : z . string ( ) } ) . parse ( worker ) ,
46+ id : z . object ( { tag : z . string ( ) } ) . parse ( worker ) ,
4247 modified_on : worker . modified_on || null ,
4348 created_on : worker . created_on || null ,
4449 } ) )
@@ -73,6 +78,60 @@ export function registerWorkersTools(agent: CloudflareMcpAgent) {
7378 }
7479 } )
7580
81+ // Tool to get a specific worker's script details
82+ agent . server . tool (
83+ 'worker_get_worker_details' ,
84+ 'Get the name and id of the Cloudflare Worker' ,
85+ { scriptName : workerNameParam } ,
86+ async ( params ) => {
87+ const accountId = await agent . getActiveAccountId ( )
88+ console . log ( params )
89+ if ( ! accountId ) {
90+ return {
91+ content : [
92+ {
93+ type : 'text' ,
94+ text : 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)' ,
95+ } ,
96+ ] ,
97+ }
98+ }
99+
100+ try {
101+ const { scriptName } = params
102+ const worker = await handleGetWorkersService ( {
103+ apiToken : agent . props . accessToken ,
104+ scriptName,
105+ accountId,
106+ } )
107+ const text = worker . result
108+ ? JSON . stringify ( {
109+ name : worker . result . id ,
110+ id : worker . result ?. default_environment . script_tag ,
111+ } )
112+ : 'Worker not found'
113+ return {
114+ content : [
115+ {
116+ type : 'text' ,
117+ text,
118+ } ,
119+ ] ,
120+ }
121+ } catch ( e ) {
122+ agent . server . recordError ( e )
123+ return {
124+ content : [
125+ {
126+ type : 'text' ,
127+ text : `Error retrieving worker script: ${ e instanceof Error && e . message } ` ,
128+ } ,
129+ ] ,
130+ }
131+ }
132+ }
133+ )
134+
76135 // Tool to get a specific worker's script content
77136 agent . server . tool (
78137 'worker_get_worker' ,
@@ -90,6 +149,7 @@ export function registerWorkersTools(agent: CloudflareMcpAgent) {
90149 ] ,
91150 }
92151 }
152+
93153 try {
94154 const { scriptName } = params
95155 const scriptContent = await handleWorkerScriptDownload ( {
0 commit comments