Skip to content

Commit 0d21d99

Browse files
committed
Update Radar scopes and improve scan_url tool
1 parent 88ad4ce commit 0d21d99

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

apps/radar/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ export class RadarMCP extends McpAgent<Env, State, Props> {
6666
}
6767
}
6868

69-
// TODO add radar:read and url_scanner:write scopes once they are available
70-
// Also remove URL_SCANNER_API_TOKEN env var
71-
const RadarScopes = { ...RequiredScopes } as const
69+
const RadarScopes = {
70+
...RequiredScopes,
71+
// TODO 'radar:read': 'Grants access to read Cloudflare Radar data.',
72+
// TODO 'url_scanner:write': 'Grants write level access to URL Scanner', // Remove URL_SCANNER_API_TOKEN env var
73+
} as const
7274

7375
export default {
7476
fetch: async (req: Request, env: Env, ctx: ExecutionContext) => {

apps/radar/src/tools/url-scanner.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,40 @@ export function registerUrlScannerTools(agent: RadarMCP) {
2323
Authorization: `Bearer ${agent.env.URL_SCANNER_API_TOKEN}`,
2424
}
2525

26-
// TODO investigate why this does not work
27-
// const scan = await (client.urlScanner.scans.create({ account_id, url: "https://www.example.com" }, { headers })).withResponse()
28-
29-
const res = await fetch(
30-
`https://api.cloudflare.com/client/v4/accounts/${account_id}/urlscanner/v2/scan`,
26+
// TODO search if there are recent scans for the URL
27+
const scans = await client.urlScanner.scans.list(
3128
{
32-
method: 'POST',
33-
headers,
34-
body: JSON.stringify({ url }),
35-
}
29+
account_id,
30+
size: 1,
31+
q: `page.url:${url}`,
32+
},
33+
{ headers }
3634
)
37-
if (!res.ok) {
38-
throw new Error('Failed to submit scan')
39-
}
35+
console.log(scans)
36+
37+
let scanId = scans.results.length > 0 ? scans.results[0]._id : null
38+
39+
if (!scanId) {
40+
// Submit scan
41+
// TODO investigate why this does not work
42+
// const scan = await (client.urlScanner.scans.create({ account_id, url: "https://www.example.com" }, { headers })).withResponse()
4043

41-
const scan = CreateScanResult.parse(await res.json())
42-
const scanId = scan?.uuid
44+
const res = await fetch(
45+
`https://api.cloudflare.com/client/v4/accounts/${account_id}/urlscanner/v2/scan`,
46+
{
47+
method: 'POST',
48+
headers,
49+
body: JSON.stringify({ url }),
50+
}
51+
)
52+
53+
if (!res.ok) {
54+
throw new Error('Failed to submit scan')
55+
}
56+
57+
const scan = CreateScanResult.parse(await res.json())
58+
scanId = scan?.uuid
59+
}
4360

4461
const r = await pollUntilReady({
4562
taskFn: () => client.urlScanner.scans.get(scanId, { account_id }, { headers }),
@@ -52,7 +69,7 @@ export function registerUrlScannerTools(agent: RadarMCP) {
5269
{
5370
type: 'text',
5471
text: JSON.stringify({
55-
result: r, // TODO select what is more relevant, or add a param to allow the agent to select a set of metrics
72+
result: { verdicts: r.verdicts, stats: r.stats, page: r.page }, // TODO select what is more relevant, or add a param to allow the agent to select a set of metrics
5673
}),
5774
},
5875
],

0 commit comments

Comments
 (0)