|
| 1 | +// EXAMPLE TOOLS — Edit this to create your own tools |
| 2 | + |
| 3 | +import { z } from 'zod' |
| 4 | + |
| 5 | +import { handleWorkerLogs, handleWorkerLogsKeys } from '@repo/mcp-common/src/api/workers-logs' |
| 6 | + |
| 7 | +import type { MyMCP } from '../index' |
| 8 | + |
| 9 | +// Worker logs parameter schema |
| 10 | +const workerNameParam = z.string().describe('The name of the worker to analyze logs for') |
| 11 | +const filterErrorsParam = z.boolean().default(false).describe('If true, only shows error logs') |
| 12 | +const limitParam = z |
| 13 | + .number() |
| 14 | + .min(1) |
| 15 | + .max(100) |
| 16 | + .default(100) |
| 17 | + .describe('Maximum number of logs to retrieve (1-100, default 100)') |
| 18 | +const minutesAgoParam = z |
| 19 | + .number() |
| 20 | + .min(1) |
| 21 | + .max(10080) |
| 22 | + .default(30) |
| 23 | + .describe('Minutes in the past to look for logs (1-10080, default 30)') |
| 24 | +const rayIdParam = z.string().optional().describe('Filter logs by specific Cloudflare Ray ID') |
| 25 | + |
| 26 | +/** |
| 27 | + * Registers the logs analysis tool with the MCP server |
| 28 | + * @param server The MCP server instance |
| 29 | + * @param accountId Cloudflare account ID |
| 30 | + * @param apiToken Cloudflare API token |
| 31 | + */ |
| 32 | +export function registerLogsTools(agent: MyMCP) { |
| 33 | + // Register the worker logs analysis tool by worker name |
| 34 | + agent.server.tool( |
| 35 | + 'worker_logs_by_worker_name', |
| 36 | + 'Analyze recent logs for a Cloudflare Worker by worker name', |
| 37 | + { |
| 38 | + scriptName: workerNameParam, |
| 39 | + shouldFilterErrors: filterErrorsParam, |
| 40 | + limitParam, |
| 41 | + minutesAgoParam, |
| 42 | + rayId: rayIdParam, |
| 43 | + }, |
| 44 | + async (params) => { |
| 45 | + const accountId = agent.getActiveAccountId() |
| 46 | + if (!accountId) { |
| 47 | + return { |
| 48 | + content: [ |
| 49 | + { |
| 50 | + type: 'text', |
| 51 | + text: 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)', |
| 52 | + }, |
| 53 | + ], |
| 54 | + } |
| 55 | + } |
| 56 | + try { |
| 57 | + const { scriptName, shouldFilterErrors, limitParam, minutesAgoParam, rayId } = params |
| 58 | + const { logs, from, to } = await handleWorkerLogs({ |
| 59 | + scriptName, |
| 60 | + limit: limitParam, |
| 61 | + minutesAgo: minutesAgoParam, |
| 62 | + accountId, |
| 63 | + apiToken: agent.props.accessToken, |
| 64 | + shouldFilterErrors, |
| 65 | + rayId, |
| 66 | + }) |
| 67 | + return { |
| 68 | + content: [ |
| 69 | + { |
| 70 | + type: 'text', |
| 71 | + text: JSON.stringify({ |
| 72 | + logs, |
| 73 | + stats: { |
| 74 | + timeRange: { |
| 75 | + from, |
| 76 | + to, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }), |
| 80 | + }, |
| 81 | + ], |
| 82 | + } |
| 83 | + } catch (error) { |
| 84 | + return { |
| 85 | + content: [ |
| 86 | + { |
| 87 | + type: 'text', |
| 88 | + text: JSON.stringify({ |
| 89 | + error: `Error analyzing worker logs: ${error instanceof Error && error.message}`, |
| 90 | + }), |
| 91 | + }, |
| 92 | + ], |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + ) |
| 97 | + |
| 98 | + // Register tool to search logs by Ray ID across all workers |
| 99 | + agent.server.tool( |
| 100 | + 'worker_logs_by_rayid', |
| 101 | + 'Analyze recent logs across all workers for a specific request by Cloudflare Ray ID', |
| 102 | + { |
| 103 | + rayId: z.string().describe('Filter logs by specific Cloudflare Ray ID'), |
| 104 | + shouldFilterErrors: filterErrorsParam, |
| 105 | + limitParam, |
| 106 | + minutesAgoParam, |
| 107 | + }, |
| 108 | + async (params) => { |
| 109 | + const accountId = agent.getActiveAccountId() |
| 110 | + if (!accountId) { |
| 111 | + return { |
| 112 | + content: [ |
| 113 | + { |
| 114 | + type: 'text', |
| 115 | + text: 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)', |
| 116 | + }, |
| 117 | + ], |
| 118 | + } |
| 119 | + } |
| 120 | + try { |
| 121 | + const { rayId, shouldFilterErrors, limitParam, minutesAgoParam } = params |
| 122 | + const { logs, from, to } = await handleWorkerLogs({ |
| 123 | + limit: limitParam, |
| 124 | + minutesAgo: minutesAgoParam, |
| 125 | + accountId, |
| 126 | + apiToken: agent.props.accessToken, |
| 127 | + shouldFilterErrors, |
| 128 | + rayId, |
| 129 | + }) |
| 130 | + return { |
| 131 | + content: [ |
| 132 | + { |
| 133 | + type: 'text', |
| 134 | + text: JSON.stringify({ |
| 135 | + logs, |
| 136 | + stats: { |
| 137 | + timeRange: { |
| 138 | + from, |
| 139 | + to, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }), |
| 143 | + }, |
| 144 | + ], |
| 145 | + } |
| 146 | + } catch (error) { |
| 147 | + return { |
| 148 | + content: [ |
| 149 | + { |
| 150 | + type: 'text', |
| 151 | + text: JSON.stringify({ |
| 152 | + error: `Error analyzing logs by Ray ID: ${error instanceof Error && error.message}`, |
| 153 | + }), |
| 154 | + }, |
| 155 | + ], |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + ) |
| 160 | + |
| 161 | + // Register the worker telemetry keys tool |
| 162 | + agent.server.tool( |
| 163 | + 'worker_logs_keys', |
| 164 | + 'Get available telemetry keys for a Cloudflare Worker', |
| 165 | + { scriptName: workerNameParam, minutesAgoParam }, |
| 166 | + async (params) => { |
| 167 | + const accountId = agent.getActiveAccountId() |
| 168 | + if (!accountId) { |
| 169 | + return { |
| 170 | + content: [ |
| 171 | + { |
| 172 | + type: 'text', |
| 173 | + text: 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)', |
| 174 | + }, |
| 175 | + ], |
| 176 | + } |
| 177 | + } |
| 178 | + try { |
| 179 | + const { scriptName, minutesAgoParam } = params |
| 180 | + const keys = await handleWorkerLogsKeys( |
| 181 | + scriptName, |
| 182 | + minutesAgoParam, |
| 183 | + accountId, |
| 184 | + agent.props.accessToken |
| 185 | + ) |
| 186 | + |
| 187 | + return { |
| 188 | + content: [ |
| 189 | + { |
| 190 | + type: 'text', |
| 191 | + text: JSON.stringify({ |
| 192 | + keys: keys.map((key) => ({ |
| 193 | + key: key.key, |
| 194 | + type: key.type, |
| 195 | + lastSeenAt: key.lastSeenAt ? new Date(key.lastSeenAt).toISOString() : null, |
| 196 | + })), |
| 197 | + stats: { |
| 198 | + total: keys.length, |
| 199 | + byType: keys.reduce( |
| 200 | + (acc, key) => { |
| 201 | + acc[key.type] = (acc[key.type] || 0) + 1 |
| 202 | + return acc |
| 203 | + }, |
| 204 | + {} as Record<string, number> |
| 205 | + ), |
| 206 | + }, |
| 207 | + }), |
| 208 | + }, |
| 209 | + ], |
| 210 | + } |
| 211 | + } catch (error) { |
| 212 | + return { |
| 213 | + content: [ |
| 214 | + { |
| 215 | + type: 'text', |
| 216 | + text: JSON.stringify({ |
| 217 | + error: `Error retrieving worker telemetry keys: ${error instanceof Error && error.message}`, |
| 218 | + }), |
| 219 | + }, |
| 220 | + ], |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + ) |
| 225 | +} |
0 commit comments