Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions .changeset/rare-paws-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'cloudflare-ai-gateway-mcp-server': patch
'auditlogs': patch
'cloudflare-autorag-mcp-server': patch
'cloudflare-browser-mcp-server': patch
'cloudflare-casb-mcp-server': patch
'demo-day': patch
'dex-analysis': patch
'dns-analytics': patch
'docs-ai-search': patch
'docs-autorag': patch
'docs-vectorize': patch
'graphql-mcp-server': patch
'logpush': patch
'cloudflare-radar-mcp-server': patch
'containers-mcp': patch
'workers-bindings': patch
'workers-builds': patch
'workers-observability': patch
'@repo/eslint-config': patch
'@repo/eval-tools': patch
'@repo/mcp-common': patch
'@repo/mcp-observability': patch
'@repo/tools': patch
'@repo/typescript-config': patch
---

Move docs MCP server to use AI Search
65 changes: 33 additions & 32 deletions packages/mcp-common/src/tools/docs-ai-search.tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@ interface RequiredEnv {
const AiSearchResponseSchema = z.object({
object: z.string(),
search_query: z.string(),
data: z.array(z.object({
file_id: z.string(),
filename: z.string(),
score: z.number(),
attributes: z.object({
modified_date: z.number().optional(),
folder: z.string().optional(),
}).catchall(z.any()),
content: z.array(z.object({
id: z.string(),
type: z.string(),
text: z.string(),
})),
})),
data: z.array(
z.object({
file_id: z.string(),
filename: z.string(),
score: z.number(),
attributes: z
.object({
modified_date: z.number().optional(),
folder: z.string().optional(),
})
.catchall(z.any()),
content: z.array(
z.object({
id: z.string(),
type: z.string(),
text: z.string(),
})
),
})
),
has_more: z.boolean(),
next_page: z.string().nullable(),
})


/**
* Registers the docs search tool with the MCP server using AI Search
* @param server The MCP server instance
Expand Down Expand Up @@ -114,7 +119,7 @@ ${result.text}

async function queryAiSearch(ai: Ai, query: string) {
const rawResponse = await doWithRetries(() =>
ai.autorag("docs-mcp-rag").search({
ai.autorag('docs-mcp-rag').search({
query,
})
)
Expand All @@ -127,7 +132,7 @@ async function queryAiSearch(ai: Ai, query: string) {
id: item.file_id,
url: sourceToUrl(item.filename),
title: extractTitle(item.filename),
text: item.content.map(c => c.text).join('\n'),
text: item.content.map((c) => c.text).join('\n'),
}))
}

Expand All @@ -136,9 +141,7 @@ function sourceToUrl(filename: string): string {
// Example: "workers/configuration/index.md" -> "https://developers.cloudflare.com/workers/configuration/"
return (
'https://developers.cloudflare.com/' +
filename
.replace(/index\.mdx?$/, '')
.replace(/\.mdx?$/, '')
filename.replace(/index\.mdx?$/, '').replace(/\.mdx?$/, '')
)
}

Expand All @@ -147,16 +150,14 @@ function extractTitle(filename: string): string {
// Example: "workers/configuration/index.md" -> "Configuration"
const parts = filename.replace(/\.mdx?$/, '').split('/')
const lastPart = parts[parts.length - 1]

if (lastPart === 'index') {
// Use the parent directory name if filename is index
return parts[parts.length - 2] || 'Documentation'
}

// Convert kebab-case or snake_case to title case
return lastPart
.replace(/[-_]/g, ' ')
.replace(/\b\w/g, l => l.toUpperCase())
return lastPart.replace(/[-_]/g, ' ').replace(/\b\w/g, (l) => l.toUpperCase())
}

/**
Expand All @@ -167,20 +168,20 @@ function extractTitle(filename: string): string {
async function doWithRetries<T>(action: () => Promise<T>) {
const NUM_RETRIES = 5
const INIT_RETRY_MS = 100

for (let i = 0; i <= NUM_RETRIES; i++) {
try {
return await action()
} catch (e) {
// Check if error is retryable (system errors, not user errors)
const isRetryable = isRetryableError(e)

console.error(`AI Search attempt ${i + 1} failed:`, e)

if (!isRetryable || i === NUM_RETRIES) {
throw e
}

// Exponential backoff with jitter
const delay = Math.random() * INIT_RETRY_MS * Math.pow(2, i)
await scheduler.wait(delay)
Expand All @@ -200,7 +201,7 @@ function isRetryableError(error: unknown): boolean {
// Retry server errors (5xx) and rate limits (429), not client errors (4xx)
return status >= 500 || status === 429
}

// Handle network errors, timeouts, etc.
if (error instanceof Error) {
const errorMessage = error.message.toLowerCase()
Expand All @@ -211,7 +212,7 @@ function isRetryableError(error: unknown): boolean {
errorMessage.includes('fetch')
)
}

// Default to retryable for unknown errors (conservative approach)
return true
}
}
57 changes: 57 additions & 0 deletions pnpm-lock.yaml

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