Skip to content

Commit dcb0810

Browse files
committed
WIP more scaffolding
1 parent 1952669 commit dcb0810

File tree

4 files changed

+101
-183
lines changed

4 files changed

+101
-183
lines changed

apps/workers-builds/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
1515
import { registerWorkersTools } from '@repo/mcp-common/src/tools/worker'
1616

1717
import { MetricsTracker } from '../../../packages/mcp-observability/src'
18-
import { registerBuildsTools } from './tools/observability'
18+
import { registerBuildsTools } from './tools/builds'
1919

2020
import type { AuthProps } from '@repo/mcp-common/src/cloudflare-oauth-handler'
2121
import type { Env } from './context'
@@ -99,12 +99,12 @@ export class BuildsMCP extends McpAgent<Env, State, Props> {
9999
}
100100
}
101101

102-
const ObservabilityScopes = {
102+
const BuildsScopes = {
103103
...RequiredScopes,
104104
'account:read': 'See your account info such as account details, analytics, and memberships.',
105105
'workers:write':
106106
'See and change Cloudflare Workers data such as zones, KV storage, namespaces, scripts, and routes.',
107-
'workers_observability:read': 'See observability logs for your account',
107+
// TODO: add builds
108108
} as const
109109

110110
export default {
@@ -119,7 +119,7 @@ export default {
119119
'/sse': BuildsMCP.serveSSE('/sse'),
120120
},
121121
// @ts-ignore
122-
defaultHandler: createAuthHandlers({ scopes: ObservabilityScopes, metrics }),
122+
defaultHandler: createAuthHandlers({ scopes: BuildsScopes, metrics }),
123123
authorizeEndpoint: '/oauth/authorize',
124124
tokenEndpoint: '/token',
125125
tokenExchangeCallback: (options) =>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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'
11+
12+
import type { BuildsMCP } from '../index'
13+
14+
/**
15+
* Registers the logs analysis tool with the MCP server
16+
* @param server The MCP server instance
17+
* @param accountId Cloudflare account ID
18+
* @param apiToken Cloudflare API token
19+
*/
20+
export function registerBuildsTools(agent: BuildsMCP) {
21+
// Register the worker logs analysis tool by worker name
22+
agent.server.tool(
23+
'list_workers_builds',
24+
`
25+
Query the Workers Builds API to view builds from your Cloudflare Workers.
26+
`.trim(),
27+
28+
{
29+
query: zQueryRunRequest,
30+
},
31+
async ({ query }) => {
32+
const accountId = await agent.getActiveAccountId()
33+
if (!accountId) {
34+
return {
35+
content: [
36+
{
37+
type: 'text',
38+
text: 'No currently active accountId. Try listing your accounts (accounts_list) and then setting an active account (set_active_account)',
39+
},
40+
],
41+
}
42+
}
43+
try {
44+
const res = await queryWorkersObservability(agent.props.accessToken, accountId, query)
45+
return {
46+
content: [
47+
{
48+
type: 'text',
49+
text: JSON.stringify(res),
50+
},
51+
],
52+
}
53+
} catch (error) {
54+
return {
55+
content: [
56+
{
57+
type: 'text',
58+
text: JSON.stringify({
59+
error: `Error analyzing worker logs: ${error instanceof Error && error.message}`,
60+
}),
61+
},
62+
],
63+
}
64+
}
65+
}
66+
)
67+
}

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

Lines changed: 0 additions & 179 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { fetchCloudflareApi } from '../cloudflare-api'
2+
import {
3+
zKeysResponse,
4+
zReturnedQueryRunResult,
5+
zValuesResponse,
6+
} from '../types/workers-logs-schemas'
7+
import { V4Schema } from '../v4-api'
8+
9+
import type { z } from 'zod'
10+
import type { zKeysRequest, zQueryRunRequest, zValuesRequest } from '../types/workers-logs-schemas'
11+
12+
export async function listWorkersBuilds(
13+
apiToken: string,
14+
accountId: string
15+
): Promise<z.infer<typeof zReturnedQueryRunResult> | null> {
16+
const data = await fetchCloudflareApi({
17+
endpoint: '/workers/observability/telemetry/query',
18+
accountId,
19+
apiToken,
20+
responseSchema: V4Schema(zReturnedQueryRunResult),
21+
options: {
22+
method: 'POST',
23+
headers: {
24+
'Content-Type': 'application/json',
25+
},
26+
},
27+
})
28+
29+
return data.result
30+
}

0 commit comments

Comments
 (0)