|
| 1 | +import type { ProviderRuntimeHandler } from "../provider-runtime.ts"; |
| 2 | +import type { CloudflareDocsActionName } from "./actions.ts"; |
| 3 | + |
| 4 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 5 | +import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; |
| 6 | +import { optionalString } from "../../core/cast.ts"; |
| 7 | +import { ProviderRequestError } from "../provider-runtime.ts"; |
| 8 | + |
| 9 | +const cloudflareDocsMcpUrl = "https://docs.mcp.cloudflare.com/mcp"; |
| 10 | + |
| 11 | +export interface CloudflareDocsActionContext { |
| 12 | + fetcher?: typeof fetch; |
| 13 | + signal?: AbortSignal; |
| 14 | +} |
| 15 | + |
| 16 | +export const cloudflareDocsActionHandlers: Record< |
| 17 | + CloudflareDocsActionName, |
| 18 | + ProviderRuntimeHandler<CloudflareDocsActionContext> |
| 19 | +> = { |
| 20 | + search_cloudflare_documentation(input, context) { |
| 21 | + return searchCloudflareDocumentation(input, context); |
| 22 | + }, |
| 23 | + get_pages_to_workers_migration_guide(input, context) { |
| 24 | + return getPagesToWorkersMigrationGuide(input, context); |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +export async function searchCloudflareDocumentation( |
| 29 | + input: Record<string, unknown>, |
| 30 | + context: CloudflareDocsActionContext, |
| 31 | +): Promise<Record<string, unknown>> { |
| 32 | + const query = optionalString(input.query)?.trim(); |
| 33 | + if (!query) { |
| 34 | + throw new ProviderRequestError(400, "query parameter is required"); |
| 35 | + } |
| 36 | + |
| 37 | + return callCloudflareDocsTool("search_cloudflare_documentation", { query }, context); |
| 38 | +} |
| 39 | + |
| 40 | +export async function getPagesToWorkersMigrationGuide( |
| 41 | + input: Record<string, unknown>, |
| 42 | + context: CloudflareDocsActionContext, |
| 43 | +): Promise<Record<string, unknown>> { |
| 44 | + return callCloudflareDocsTool("migrate_pages_to_workers_guide", input, context); |
| 45 | +} |
| 46 | + |
| 47 | +async function callCloudflareDocsTool( |
| 48 | + toolName: string, |
| 49 | + args: Record<string, unknown>, |
| 50 | + context: CloudflareDocsActionContext, |
| 51 | +): Promise<Record<string, unknown>> { |
| 52 | + try { |
| 53 | + const transportOptions: Record<string, unknown> = {}; |
| 54 | + if (context.fetcher) { |
| 55 | + transportOptions.fetch = context.fetcher; |
| 56 | + } |
| 57 | + const transport = new StreamableHTTPClientTransport(new URL(cloudflareDocsMcpUrl), transportOptions); |
| 58 | + const client = new Client({ name: "open-connector", version: "1.0.0" }, { capabilities: {} }); |
| 59 | + await client.connect(transport); |
| 60 | + |
| 61 | + try { |
| 62 | + const result = await client.callTool({ |
| 63 | + name: toolName, |
| 64 | + arguments: args, |
| 65 | + }); |
| 66 | + if (result.isError) { |
| 67 | + const content = result.content as Array<{ type?: string; text?: string }> | undefined; |
| 68 | + const text = content?.find((c) => c.type === "text")?.text; |
| 69 | + throw new ProviderRequestError(502, text ?? "Cloudflare Docs MCP tool returned an unknown error."); |
| 70 | + } |
| 71 | + return result as Record<string, unknown>; |
| 72 | + } finally { |
| 73 | + await client.close().catch(() => {}); |
| 74 | + } |
| 75 | + } catch (error) { |
| 76 | + if (error instanceof ProviderRequestError) throw error; |
| 77 | + throw new ProviderRequestError( |
| 78 | + 502, |
| 79 | + `Cloudflare Docs MCP request failed: ${error instanceof Error ? error.message : String(error)}`, |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
0 commit comments