Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/workers-observability/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"@cloudflare/workers-oauth-provider": "0.0.5",
"@fast-csv/format": "^5.0.2",
"@fast-csv/format": "5.0.2",
"@hono/zod-validator": "0.4.3",
"@modelcontextprotocol/sdk": "1.10.2",
"@repo/mcp-common": "workspace:*",
Expand Down
13 changes: 5 additions & 8 deletions apps/workers-observability/src/tools/observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ This tool provides three primary views of your Worker data:
const response = await queryWorkersObservability(agent.props.accessToken, accountId, query)

if (query.view === 'calculations') {
let data = 'DISPLAY THE CALCULATIONS IN A GRAPHICAL WAY'
let data = ''
for (const calculation of response?.calculations || []) {
const alias = calculation.alias || calculation.calculation
const aggregates = calculation.aggregates.map((agg) => {
Expand Down Expand Up @@ -128,7 +128,7 @@ This tool provides three primary views of your Worker data:
}
}

if(query.view === 'events') {
if (query.view === 'events') {
const events = response?.events?.events
return {
content: [
Expand All @@ -140,16 +140,13 @@ This tool provides three primary views of your Worker data:
}
}

if(query.view === 'invocations') {
if (query.view === 'invocations') {
const invocations = Object.entries(response?.invocations || {}).map(([_, logs]) => {
const invocationLog = logs.find((log) => log.$metadata.type === 'cf-worker-event')
return invocationLog?.$metadata ?? logs[0]?.$metadata
});
})

const tsv = await writeToString(
invocations,
{ headers: true, delimiter: '\t' }
)
const tsv = await writeToString(invocations, { headers: true, delimiter: '\t' })
return {
content: [
{
Expand Down
4 changes: 4 additions & 0 deletions packages/mcp-common/src/api/workers-observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { V4Schema } from '../v4-api'

import type { z } from 'zod'
import type { zKeysRequest, zQueryRunRequest, zValuesRequest } from '../types/workers-logs-schemas'
import { env } from 'cloudflare:workers'

type QueryRunRequest = z.infer<typeof zQueryRunRequest>

Expand All @@ -23,6 +24,8 @@ export async function queryWorkersObservability(
accountId: string,
query: QueryRunRequest
): Promise<z.infer<typeof zReturnedQueryRunResult> | null> {
// @ts-expect-error We don't have actual env in this package
const environment = env.ENVIRONMENT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably also pass through this env in the params so we don't need to expect error here.

Kinda like what do here https://github.com/cloudflare/mcp-server-cloudflare/blob/main/packages/mcp-common/src/dev-mode.ts#L6

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix in a follow up on monday :)

const data = await fetchCloudflareApi({
endpoint: '/workers/observability/telemetry/query',
accountId,
Expand All @@ -32,6 +35,7 @@ export async function queryWorkersObservability(
method: 'POST',
headers: {
'Content-Type': 'application/json',
'workers-observability-origin': `workers-observability-mcp-${environment}`,
Copy link
Member

@Maximo-Guk Maximo-Guk May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can come up with a general header name, which every mcp server uses?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is specific to our backend, happy to add a general one too

Copy link
Member

@Maximo-Guk Maximo-Guk May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, maybe we can set our user agent to the server name / version ( the ones pulled from package.json )

Also doesn't have to be in this PR!

},
body: JSON.stringify({ ...query, timeframe: fixTimeframe(query.timeframe) }),
},
Expand Down
10 changes: 10 additions & 0 deletions packages/mcp-common/src/cloudflare-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export async function fetchCloudflareApi<T>({
}): Promise<T> {
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}${endpoint}`

// @ts-expect-error We don't have actual env in this package
if (env.DEV_DISABLE_OAUTH) {
options.headers = {
...options.headers,
// @ts-expect-error We don't have actual env in this package
'X-Auth-Email': env.DEV_CLOUDFLARE_EMAIL,
// @ts-expect-error We don't have actual env in this package
'X-Auth-Key': env.DEV_CLOUDFLARE_API_TOKEN,
}
}
const response = await fetch(url, {
...options,
headers: {
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading