Skip to content

Commit faf6dca

Browse files
committed
Add api to get worker details
1 parent 903b6e9 commit faf6dca

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

packages/mcp-common/src/api/workers.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { fetchCloudflareApi } from '../cloudflare-api'
2+
import { WorkersService } from '../types/worker'
3+
import { V4Schema } from '../v4-api'
4+
15
import type { Cloudflare } from 'cloudflare'
26

37
/**
@@ -16,6 +20,33 @@ export async function handleWorkersList({
1620
return (await client.workers.scripts.list({ account_id: accountId })).result
1721
}
1822

23+
/**
24+
* Get details of a worker script from Cloudflare API
25+
* @param client Cloudflare API Client
26+
* @param scriptName Name of the worker script to download
27+
* @param accountId Cloudflare account ID
28+
* @returns The script name and id
29+
*/
30+
export async function handleGetWorkersService({
31+
apiToken,
32+
scriptName,
33+
accountId,
34+
}: {
35+
apiToken: string
36+
scriptName: string
37+
accountId: string
38+
}) {
39+
return await fetchCloudflareApi({
40+
endpoint: `/workers/services/${scriptName}`,
41+
accountId,
42+
apiToken,
43+
responseSchema: V4Schema(WorkersService),
44+
options: {
45+
method: 'GET',
46+
},
47+
})
48+
}
49+
1950
/**
2051
* Downloads a specific worker script from Cloudflare API
2152
* @param client Cloudflare API Client

packages/mcp-common/src/tools/worker.ts

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

3-
import { handleWorkerScriptDownload, handleWorkersList } from '../api/workers'
3+
import {
4+
handleGetWorkersService,
5+
handleWorkerScriptDownload,
6+
handleWorkersList,
7+
} from '../api/workers'
48
import { getCloudflareClient } from '../cloudflare-api'
59

610
import 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({
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { z } from 'zod'
2+
3+
export type WorkersService = z.infer<typeof WorkersService>
4+
export const WorkersService = z.object({
5+
id: z.string(),
6+
default_environment: z.object({
7+
environment: z.string(),
8+
script_tag: z.string(),
9+
created_on: z.string(),
10+
modified_on: z.string(),
11+
script: z.object({
12+
created_on: z.string(),
13+
modified_on: z.string(),
14+
id: z.string(),
15+
tag: z.string(),
16+
tags: z.array(z.string()),
17+
deployment_id: z.string(),
18+
tail_consumers: z.null(),
19+
logpush: z.boolean(),
20+
has_assets: z.boolean(),
21+
has_modules: z.boolean(),
22+
etag: z.string(),
23+
handlers: z.array(z.string()),
24+
last_deployed_from: z.string(),
25+
compatibility_date: z.string(),
26+
compatibility_flags: z.array(z.string()),
27+
usage_model: z.string(),
28+
}),
29+
}),
30+
created_on: z.string(),
31+
modified_on: z.string(),
32+
usage_model: z.string(),
33+
environments: z.array(
34+
z.object({
35+
environment: z.string(),
36+
script_tag: z.string(),
37+
created_on: z.string(),
38+
modified_on: z.string(),
39+
})
40+
),
41+
})

0 commit comments

Comments
 (0)