Skip to content

Commit d99224e

Browse files
authored
Try fix the workers observability prompts to make Claude Sonet more effective (cloudflare#118)
Thanks @mattzcarey you prompting wizard
1 parent 38aa001 commit d99224e

File tree

3 files changed

+22
-88
lines changed

3 files changed

+22
-88
lines changed

apps/workers-observability/src/index.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,13 @@ export class ObservabilityMCP extends McpAgent<Env, State, Props> {
5959
sentry: initSentryWithUser(env, this.ctx, this.props.user.id),
6060
options: {
6161
instructions: `# Cloudflare Workers Observability Tool
62+
* A cloudflare worker is a serverless function
63+
* Workers Observability is the tool to inspect the logs for your cloudflare Worker
64+
* Each log is a structured JSON payload with keys and values
6265
63-
This tool provides powerful capabilities to analyze and troubleshoot your Cloudflare Workers through logs and metrics. Here's how to use it effectively:
6466
65-
## IMPORTANT: Query Discipline
66-
67-
**STOP after the first successful query if it answers the user's question.** Do not run multiple queries unless absolutely necessary. The first query often contains the answer - review it thoroughly before running additional queries.
68-
69-
## Best Practices
70-
71-
### Efficient Querying
72-
- Start with a focused query that's most likely to answer the question
73-
- Review results completely before deciding if additional queries are needed
74-
- If the first query provides the answer, STOP and present it to the user
75-
- Only run additional queries when specifically directed or when essential information is missing
76-
77-
### When to STOP Querying
78-
- STOP after presenting meaningful results from the first query
79-
- STOP when you've answered the user's specific question
80-
- STOP when the user hasn't requested additional exploration
81-
- Only continue if explicitly directed with "EXPLORE" or similar instruction
82-
`,
67+
This server allows you to analyze your Cloudflare Workers logs and metrics.
68+
`,
8369
},
8470
})
8571

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

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,8 @@ export function registerObservabilityTools(agent: ObservabilityMCP) {
2323
'query_worker_observability',
2424
`Query the Workers Observability API to analyze logs and metrics from your Cloudflare Workers.
2525
26-
The resulting information should answer the users query. STOP HERE and show them the answer to there question.
27-
If you can't answer the question ask for a follow up.
28-
29-
## Output handling
30-
31-
Once you have ran this query you must IMMEDIATELY present the user with this information.
32-
33-
- **Events**: Display as a table with key fields. For detailed inspection, show full JSON of individual events.
34-
- **Calculations**: Use appropriate charts based on the data (bar charts for comparisons, line charts for time series)
35-
- **Invocations**: Show full request/response details with syntax highlighting for important fields
36-
37-
## When to Use This tool
38-
39-
- Investigate errors or performance issues in your Cloudflare Workers
40-
- Monitor Worker usage patterns and resource consumption
41-
- Debug specific request failures or unexpected behaviors
42-
- Verify recent deployments are working as expected
43-
- Generate performance reports for specific Workers or endpoints
44-
- Track down user-reported issues with request ID or user information
45-
- Analyze trends in response times, error rates, or request volumes
46-
26+
* A query typical query looks like this:
27+
{"view":"events","queryId":"workers-logs-events","limit":5,"dry":true,"parameters":{"datasets":["cloudflare-workers"],"filters":[{"id":"520","key":"message","operation":"eq","type":"string","value":"Clickhouse Statistics"},{"id":"2088","key":"statistics.elapsed","operation":"gt","type":"number","value":"0.269481519"}],"calculations":[],"groupBys":[],"havings":[]},"timeframe":{"to":"2025-04-30T20:53:15Z","from":" ""2025-04-30T19:53:15Z"}}
4728
## Core Capabilities
4829
This tool provides three primary views of your Worker data:
4930
1. **List Events** - Browse individual request logs and errors
@@ -104,31 +85,11 @@ This tool provides three primary views of your Worker data:
10485

10586
agent.server.tool(
10687
'observability_keys',
107-
`Find keys in the Workers Observability Data.
108-
109-
## When to Use This Tool
110-
- Before creating new queries to discover available data fields
111-
- When building complex filters to verify field names exist
112-
- To explore the schema of your Workers data
113-
- When troubleshooting "invalid field" errors in queries
114-
- To discover metrics fields available for calculations
115-
116-
## Core Capabilities
117-
This tool provides a comprehensive view of available data fields:
118-
1. **Discover Schema** - Explore what fields exist in your Workers data
119-
2. **Validate Fields** - Confirm field names before using them in filters
120-
3. **Understand Data Types** - Learn the type of each field for proper filtering
88+
`Find keys in the Workers Observability Data
12189
12290
## Best Practices
12391
- Set a high limit (1000+) to ensure you see all available keys
12492
- Add the $metadata.service filter to narrow results to a specific Worker
125-
- Use this tool before a query with unfamiliar fields
126-
- Pay attention to field data types when crafting filters
127-
128-
## Common Key Categories
129-
- $metadata.* fields: Core Worker metadata including service name, level, etc.
130-
- $workers.* fields: Worker-specific metadata like request ID, trigger type, etc.
131-
- custom fields: Any fields added via console.log in your Worker code
13293
13394
## Troubleshooting
13495
- If expected fields are missing, verify the Worker is actively logging
@@ -176,15 +137,6 @@ This tool provides a comprehensive view of available data fields:
176137
'observability_values',
177138
`Find values in the Workers Observability Data.
178139
179-
## When to Use This Tool
180-
- When building complex queries requiring exact value matches
181-
182-
## Best Practices
183-
- Always specify the correct data type (string, number, boolean)
184-
- Use needle parameter with matchCase:false for case-insensitive searches
185-
- Combine with filters to focus on specific Workers or time periods
186-
- When dealing with high-cardinality fields, use more specific filters
187-
188140
## Troubleshooting
189141
- For no results, verify the field exists using observability_keys first
190142
- If expected values are missing, try broadening your time range`,

packages/mcp-common/src/types/workers-logs-schemas.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ export const zSearchNeedle = z.object({
117117
matchCase: z.boolean().optional(),
118118
})
119119

120-
const zViews = z
121-
.enum(['traces', 'events', 'calculations', 'invocations', 'requests', 'patterns'])
122-
.optional()
120+
const zViews = z.enum(['events', 'calculations', 'invocations'])
123121

124122
export const zAggregateResult = z.object({
125123
groups: z.array(z.object({ key: z.string(), value: zPrimitiveUnion })).optional(),
@@ -306,6 +304,19 @@ export const zReturnedQueryRunEvents = z.object({
306304
export const zQueryRunRequest = z.object({
307305
// TODO: Fix these types
308306
queryId: z.string(),
307+
view: zViews.optional().default('calculations').describe(`## Examples by View Type
308+
### Events View
309+
- "Show me all errors for the worker api-proxy in the last 30 minutes"
310+
- "Show events from worker auth-service where the path contains /login"
311+
312+
### Calculation View
313+
- "What is the p99 of wall time for worker api-proxy?"
314+
- "What's the count of requests by status code for worker cdn-router?"
315+
316+
### Invocation View
317+
- "Find a request to worker api-proxy that resulted in a 500 error"
318+
- "List successful requests for the image-resizer worker with status code 200"
319+
`),
309320
parameters: z.object({
310321
datasets: z
311322
.array(z.string())
@@ -348,22 +359,7 @@ export const zQueryRunRequest = z.object({
348359
.describe(
349360
'Use this limit to limit the number of events returned when the view is events. 5 is a sensible default'
350361
),
351-
view: zViews.optional().default('calculations').describe(`## Examples by View Type
352-
### Events View
353-
- "Show me all errors for the worker api-proxy in the last 30 minutes"
354-
- "List successful requests for the image-resizer worker with status code 200"
355-
- "Show events from worker auth-service where the path contains /login"
356-
357-
### Calculation View
358-
- "What is the p99 of wall time for worker api-proxy?"
359-
- "What's the count of requests by status code for worker cdn-router?"
360-
361-
### Invocation View
362-
- "Find a request to worker api-proxy that resulted in a 500 error"
363-
- "Find the slowest request to worker image-processor in the last hour"
364362

365-
TRACES AND PATTERNS ARE NOT CURRENTLY SUPPORTED
366-
`),
367363
dry: z.boolean().optional().default(true),
368364
offset: z
369365
.string()

0 commit comments

Comments
 (0)