Skip to content

Commit 2e97917

Browse files
committed
add radar tools (wip)
1 parent 2d76979 commit 2e97917

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ yarn-error.log*
4848
.sentryclirc.lock/
4949
tmp.json
5050
tmp.ts
51+
.idea
5152

52-
apps/sandbox-container/workdir
53+
apps/sandbox-container/workdir

apps/workers-observability/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { registerWorkersTools } from '@repo/mcp-common/src/tools/worker'
1313
import { registerLogsTools } from './tools/logs'
1414

1515
import type { AccountSchema, UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
16+
import { registerRadarTools } from "@repo/mcp-common/src/tools/radar";
1617

1718
// Context from the auth process, encrypted & stored in the auth token
1819
// and provided to the DurableMCP as this.props
@@ -42,6 +43,9 @@ export class ObservabilityMCP extends McpAgent<Env, State, Props> {
4243

4344
// Register Cloudflare Workers logs tools
4445
registerLogsTools(this)
46+
47+
// Register Cloudflare Radar tools
48+
registerRadarTools(this)
4549
}
4650

4751
getActiveAccountId() {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { getCloudflareClient } from '../cloudflare-api'
2+
import { type CloudflareMcpAgent } from '../types/cloudflare-mcp-agent'
3+
import { AsOrderByParam, SingleLocationParam } from '../types/radar'
4+
import { PaginationLimitParam, PaginationOffsetParam } from '../types/shared'
5+
6+
export function registerRadarTools(agent: CloudflareMcpAgent) {
7+
agent.server.tool(
8+
'radar_ases_list',
9+
'List Autonomous Systems',
10+
{
11+
limit: PaginationLimitParam,
12+
offset: PaginationOffsetParam,
13+
location: SingleLocationParam,
14+
orderBy: AsOrderByParam,
15+
},
16+
async ({ limit, offset, location, orderBy }) => {
17+
try {
18+
const client = getCloudflareClient(agent.props.accessToken)
19+
const r = await client.radar.entities.asns.list({
20+
limit: limit ?? undefined,
21+
offset: offset ?? undefined,
22+
location: location ?? undefined,
23+
orderBy: orderBy ?? undefined,
24+
})
25+
26+
return {
27+
content: [
28+
{
29+
type: 'text',
30+
text: JSON.stringify({
31+
result: r,
32+
}),
33+
},
34+
],
35+
}
36+
} catch (error) {
37+
return {
38+
content: [
39+
{
40+
type: 'text',
41+
text: `Error listing ASes: ${error instanceof Error && error.message}`,
42+
},
43+
],
44+
}
45+
}
46+
}
47+
)
48+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* This file contains the validators for the radar tools.
3+
*/
4+
import { z } from 'zod'
5+
6+
import type { ASNListParams } from 'cloudflare/resources/radar/entities/asns.mjs'
7+
8+
export const AsnParam: z.ZodType<ASNListParams['asn']> = z
9+
.string()
10+
.optional()
11+
.refine((val) => val?.split(',').every((asn) => parseInt(asn, 10) > 0), {
12+
message: 'ASNs must be positive integers',
13+
})
14+
15+
export const SingleLocationParam: z.ZodType<ASNListParams['location']> = z
16+
.string()
17+
.regex(/^[a-zA-Z]{2}$/, {
18+
message:
19+
'Invalid location code. Must be a valid alpha-2 location code (two letters, case insensitive).',
20+
})
21+
.optional()
22+
23+
export const AsOrderByParam: z.ZodType<ASNListParams['orderBy']> = z
24+
.enum(['ASN', 'POPULATION'])
25+
.optional()

packages/mcp-common/src/types/shared.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ import { z } from 'zod'
22

33
export const PaginationPerPageParam = z.number().nullable().optional()
44
export const PaginationPageParam = z.number().nullable().optional()
5+
6+
export const PaginationLimitParam = z.number().nullable().optional()
7+
export const PaginationOffsetParam = z.number().nullable().optional()

0 commit comments

Comments
 (0)