|
| 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 | +} |
0 commit comments