Skip to content

Commit 786bf46

Browse files
committed
IT WORKS
1 parent 0cc2f2f commit 786bf46

File tree

4 files changed

+157
-66
lines changed

4 files changed

+157
-66
lines changed

apps/workers-builds/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
1515
import { registerWorkersTools } from '@repo/mcp-common/src/tools/worker'
1616

1717
import { MetricsTracker } from '../../../packages/mcp-observability/src'
18-
import { registerBuildsTools } from './tools/builds'
18+
import { registerBuildsTools } from './tools/workers-builds.tools'
1919

2020
import type { AuthProps } from '@repo/mcp-common/src/cloudflare-oauth-handler'
2121
import type { Env } from './context'

apps/workers-builds/src/tools/builds.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { z } from 'zod'
2+
3+
import {
4+
getBuildErrorReason,
5+
getLatestWorkersBuild,
6+
} from '@repo/mcp-common/src/api/workers-builds.api'
7+
8+
import type { BuildsMCP } from '../index'
9+
10+
/**
11+
* Registers the logs analysis tool with the MCP server
12+
* @param server The MCP server instance
13+
* @param accountId Cloudflare account ID
14+
* @param apiToken Cloudflare API token
15+
*/
16+
export function registerBuildsTools(agent: BuildsMCP) {
17+
agent.server.tool(
18+
'get_latest_workers_build',
19+
`
20+
Use the Workers Builds API to get the latest build for a Cloudflare Worker.
21+
22+
If the build failed, use get_workers_build_failure_reason to determine why it failed.
23+
`.trim(),
24+
25+
{
26+
scriptId: z.string(),
27+
},
28+
async ({ scriptId }) => {
29+
const accountId = await agent.getActiveAccountId()
30+
if (!accountId) {
31+
return {
32+
content: [
33+
{
34+
type: 'text',
35+
text: 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)',
36+
},
37+
],
38+
}
39+
}
40+
41+
try {
42+
const res = await getLatestWorkersBuild({
43+
apiToken: agent.props.accessToken,
44+
accountId,
45+
scriptId,
46+
})
47+
return {
48+
content: [
49+
{
50+
type: 'text',
51+
text: JSON.stringify(res),
52+
},
53+
],
54+
}
55+
} catch (error) {
56+
return {
57+
content: [
58+
{
59+
type: 'text',
60+
text: JSON.stringify({
61+
error: `Error getting build: ${error instanceof Error && error.message}`,
62+
}),
63+
},
64+
],
65+
}
66+
}
67+
}
68+
)
69+
70+
agent.server.tool(
71+
'get_workers_build_failure_reason',
72+
`
73+
Use the Workers Builds API to get the latest build for a Cloudflare Worker.
74+
75+
If the build failed, use get_build_failure_reason to determine why it failed.
76+
`.trim(),
77+
78+
{
79+
buildUUID: z.string(),
80+
},
81+
async ({ buildUUID }) => {
82+
const accountId = await agent.getActiveAccountId()
83+
if (!accountId) {
84+
return {
85+
content: [
86+
{
87+
type: 'text',
88+
text: 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)',
89+
},
90+
],
91+
}
92+
}
93+
94+
try {
95+
const res = await getBuildErrorReason({
96+
apiToken: agent.props.accessToken,
97+
accountId,
98+
buildUUID,
99+
})
100+
return {
101+
content: [
102+
{
103+
type: 'text',
104+
text: res,
105+
},
106+
],
107+
}
108+
} catch (error) {
109+
return {
110+
content: [
111+
{
112+
type: 'text',
113+
text: JSON.stringify({
114+
error: `Error getting build failure reason: ${error instanceof Error && error.message}`,
115+
}),
116+
},
117+
],
118+
}
119+
}
120+
}
121+
)
122+
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,37 @@ export async function getLatestWorkersBuild({
2929
}
3030
return build
3131
}
32+
33+
export async function getBuildErrorReason({
34+
apiToken,
35+
accountId,
36+
buildUUID,
37+
}: {
38+
apiToken: string
39+
accountId: string
40+
buildUUID: string
41+
}) {
42+
return `
43+
You are a debugging machine!
44+
45+
You will be provided information about a Cloudflare Workers Build and you will need to debug it.
46+
47+
For now, you can only validate that the user has remembered to provide a wrangler configuration file.
48+
If the build failed for an unrelated reason, just say "build failed for an unknown reason. check logs for more info".
49+
50+
If the user forgot the wrangler file, suggest a new 'wrangler.jsonc' based on the following template.
51+
Use the build details and local files (if available) to determine the correct entrypoint.
52+
53+
If you have access to create the file, go ahead and create it instead of just suggesting it.
54+
55+
{
56+
"name": "<fill in the name of the worker>",
57+
"main": "<find the correct main entrypoint file based on the framework>",
58+
"compatibility_date": "2025-04-28",
59+
"compatibility_flags": ["nodejs_compat"],
60+
"observability": {
61+
"enabled": true
62+
}
63+
}
64+
`.trim()
65+
}

0 commit comments

Comments
 (0)