|
1 | | -import { queryWorkersObservability, handleWorkerLogsKeys, handleWorkerLogsValues } from '@repo/mcp-common/src/api/workers-observability' |
2 | | -import { zKeysRequest, zQueryRunRequest, zValuesRequest } from '@repo/mcp-common/src/types/workers-logs-schemas' |
| 1 | +import { |
| 2 | + handleWorkerLogsKeys, |
| 3 | + handleWorkerLogsValues, |
| 4 | + queryWorkersObservability, |
| 5 | +} from '@repo/mcp-common/src/api/workers-observability' |
| 6 | +import { |
| 7 | + zKeysRequest, |
| 8 | + zQueryRunRequest, |
| 9 | + zValuesRequest, |
| 10 | +} from '@repo/mcp-common/src/types/workers-logs-schemas' |
3 | 11 |
|
4 | 12 | import type { ObservabilityMCP } from '../index' |
5 | 13 |
|
@@ -88,49 +96,92 @@ Once you have ran this query you must IMMEDIATELY present the user with this inf |
88 | 96 | } |
89 | 97 | ) |
90 | 98 |
|
91 | | - agent.server.tool('observability_keys', ` |
92 | | -Find keys in the workers observability Data. This tool should be used to ensure that the filters or calculations that you are adding to your query are valid. |
93 | | -Filters can be added to this query but because it is faster to return lots of keys set a high limit and only add the filter $metadata.service to filter by worker name. |
94 | | - `, { keysQuery: zKeysRequest }, async ({ keysQuery }) => { |
95 | | - const accountId = await agent.getActiveAccountId() |
96 | | - if (!accountId) { |
97 | | - return { |
98 | | - content: [ |
99 | | - { |
100 | | - type: 'text', |
101 | | - text: 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)', |
102 | | - }, |
103 | | - ], |
104 | | - } |
105 | | - } |
106 | | - try { |
107 | | - |
108 | | - const result = await handleWorkerLogsKeys(agent.props.accessToken, accountId, keysQuery) |
109 | | - return { |
110 | | - content: [ |
111 | | - { |
112 | | - type: 'text', |
113 | | - text: JSON.stringify(result), |
114 | | - }, |
115 | | - ], |
| 99 | + agent.server.tool( |
| 100 | + 'observability_keys', |
| 101 | + `Find keys in the Workers Observability Data. |
| 102 | +
|
| 103 | +## When to Use This Tool |
| 104 | +- Before creating new queries to discover available data fields |
| 105 | +- When building complex filters to verify field names exist |
| 106 | +- To explore the schema of your Workers data |
| 107 | +- When troubleshooting "invalid field" errors in queries |
| 108 | +- To discover metrics fields available for calculations |
| 109 | +
|
| 110 | +## Core Capabilities |
| 111 | +This tool provides a comprehensive view of available data fields: |
| 112 | +1. **Discover Schema** - Explore what fields exist in your Workers data |
| 113 | +2. **Validate Fields** - Confirm field names before using them in filters |
| 114 | +3. **Understand Data Types** - Learn the type of each field for proper filtering |
| 115 | +
|
| 116 | +## Best Practices |
| 117 | +- Set a high limit (1000+) to ensure you see all available keys |
| 118 | +- Add the $metadata.service filter to narrow results to a specific Worker |
| 119 | +- Use this tool before a query with unfamiliar fields |
| 120 | +- Pay attention to field data types when crafting filters |
| 121 | +
|
| 122 | +## Common Key Categories |
| 123 | +- $metadata.* fields: Core Worker metadata including service name, level, etc. |
| 124 | +- $workers.* fields: Worker-specific metadata like request ID, trigger type, etc. |
| 125 | +- custom fields: Any fields added via console.log in your Worker code |
| 126 | +
|
| 127 | +## Troubleshooting |
| 128 | +- If expected fields are missing, verify the Worker is actively logging |
| 129 | +- For empty results, try broadening your time range |
| 130 | +`, |
| 131 | + { keysQuery: zKeysRequest }, |
| 132 | + async ({ keysQuery }) => { |
| 133 | + const accountId = await agent.getActiveAccountId() |
| 134 | + if (!accountId) { |
| 135 | + return { |
| 136 | + content: [ |
| 137 | + { |
| 138 | + type: 'text', |
| 139 | + text: 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)', |
| 140 | + }, |
| 141 | + ], |
| 142 | + } |
116 | 143 | } |
117 | | - } catch (error) { |
118 | | - return { |
119 | | - content: [ |
120 | | - { |
121 | | - type: 'text', |
122 | | - text: JSON.stringify({ |
123 | | - error: `Error retrieving worker telemetry keys: ${error instanceof Error && error.message}`, |
124 | | - }), |
125 | | - }, |
126 | | - ], |
| 144 | + try { |
| 145 | + const result = await handleWorkerLogsKeys(agent.props.accessToken, accountId, keysQuery) |
| 146 | + return { |
| 147 | + content: [ |
| 148 | + { |
| 149 | + type: 'text', |
| 150 | + text: JSON.stringify(result), |
| 151 | + }, |
| 152 | + ], |
| 153 | + } |
| 154 | + } catch (error) { |
| 155 | + return { |
| 156 | + content: [ |
| 157 | + { |
| 158 | + type: 'text', |
| 159 | + text: JSON.stringify({ |
| 160 | + error: `Error retrieving worker telemetry keys: ${error instanceof Error && error.message}`, |
| 161 | + }), |
| 162 | + }, |
| 163 | + ], |
| 164 | + } |
127 | 165 | } |
128 | 166 | } |
129 | | - }) |
| 167 | + ) |
130 | 168 |
|
131 | | - agent.server.tool('observability_values', ` |
132 | | -Find values in the workers observability Data. This tool should be used to ensure that the filters that you are adding to your query are valid. |
133 | | -`, |
| 169 | + agent.server.tool( |
| 170 | + 'observability_values', |
| 171 | + `Find values in the Workers Observability Data. |
| 172 | +
|
| 173 | +## When to Use This Tool |
| 174 | +- When building complex queries requiring exact value matches |
| 175 | +
|
| 176 | +## Best Practices |
| 177 | +- Always specify the correct data type (string, number, boolean) |
| 178 | +- Use needle parameter with matchCase:false for case-insensitive searches |
| 179 | +- Combine with filters to focus on specific Workers or time periods |
| 180 | +- When dealing with high-cardinality fields, use more specific filters |
| 181 | +
|
| 182 | +## Troubleshooting |
| 183 | +- For no results, verify the field exists using observability_keys first |
| 184 | +- If expected values are missing, try broadening your time range`, |
134 | 185 | { valuesQuery: zValuesRequest }, |
135 | 186 | async ({ valuesQuery }) => { |
136 | 187 | const accountId = await agent.getActiveAccountId() |
@@ -166,5 +217,6 @@ Find values in the workers observability Data. This tool should be used to ensur |
166 | 217 | ], |
167 | 218 | } |
168 | 219 | } |
169 | | - }) |
| 220 | + } |
| 221 | + ) |
170 | 222 | } |
0 commit comments