From e63dacb8daa482ffb16e0e129eb659fd4dcf1507 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Wed, 26 Mar 2025 00:45:01 +0000 Subject: [PATCH] Add additional Dumpling AI API endpoints --- README.md | 16 ++ src/index.ts | 572 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 588 insertions(+) diff --git a/README.md b/README.md index b41b8bc..408cf39 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,14 @@ This MCP server provides the following tools: - **search-places**: Perform Google Places searches - **search-news**: Perform Google News searches - **get-google-reviews**: Fetch Google reviews for a place +- **scrape**: Extract data from a specified URL +- **crawl**: Crawl a website and return structured content +- **extract-document**: Extract structured data from document files +- **extract-image**: Extract structured data from image files +- **doc-to-text**: Convert PDF or DOCX documents into plain text +- **screenshot**: Capture a screenshot of a specified URL +- **generate-ai-image**: Generate high-quality AI images +- **extract-video**: Extract structured data from video files ## Prerequisites 📋 @@ -106,6 +114,14 @@ Search for recent developments in quantum computing and summarize the key findin What are the most popular places to visit in New York according to Google Maps? ``` +``` +Extract the text from this PDF document: https://example.com/document.pdf +``` + +``` +Generate an AI image of a futuristic cityscape with flying cars. +``` + ## Troubleshooting 🔧 ### Common Issues diff --git a/src/index.ts b/src/index.ts index 1b297a8..e4aa683 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,14 @@ * - Added search-places tool to perform Google Places searches * - Added search-news tool to perform Google News searches * - Added get-google-reviews tool to fetch Google reviews for a place + * - Added scrape tool to extract data from a specified URL + * - Added crawl tool to crawl a website and return structured content + * - Added extract-document tool to extract structured data from document files + * - Added extract-image tool to extract structured data from image files + * - Added doc-to-text tool to convert PDF or DOCX documents into plain text + * - Added screenshot tool to capture a screenshot of a specified URL + * - Added generate-ai-image tool to generate high-quality AI images + * - Added extract-video tool to extract structured data from video files */ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; @@ -542,6 +550,570 @@ server.tool( } ); +// Tool to scrape data from a specified URL +server.tool( + "scrape", + { + url: z.string().url(), + format: z.enum(["markdown", "html", "screenshot"]).optional(), + cleaned: z.boolean().optional(), + renderJs: z.boolean().optional(), + }, + async ({ url, format, cleaned, renderJs }) => { + // Get API key from environment variable + const apiKey = process.env.DUMPLING_API_KEY; + if (!apiKey) { + throw new Error("DUMPLING_API_KEY environment variable not set"); + } + + try { + const response = await fetch(`${NWS_API_BASE}/api/v1/scrape`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify({ + url, + format, + cleaned, + renderJs, + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `Failed to scrape URL: ${response.status} ${errorText}` + ); + } + + const data = await response.json(); + + return { + content: [ + { + type: "text", + text: JSON.stringify( + { + title: data.title, + url: data.url, + content: data.content, + metadata: data.metadata, + }, + null, + 2 + ), + }, + ], + }; + } catch (error) { + console.error("Error scraping URL:", error); + throw error; + } + } +); + +// Tool to crawl a website and return structured content +server.tool( + "crawl", + { + url: z.string().url(), + limit: z.number().optional(), + depth: z.number().optional(), + format: z.enum(["markdown", "html"]).optional(), + cleaned: z.boolean().optional(), + renderJs: z.boolean().optional(), + }, + async ({ url, limit, depth, format, cleaned, renderJs }) => { + // Get API key from environment variable + const apiKey = process.env.DUMPLING_API_KEY; + if (!apiKey) { + throw new Error("DUMPLING_API_KEY environment variable not set"); + } + + try { + const response = await fetch(`${NWS_API_BASE}/api/v1/crawl`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify({ + url, + limit, + depth, + format, + cleaned, + renderJs, + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `Failed to crawl website: ${response.status} ${errorText}` + ); + } + + const data = await response.json(); + + return { + content: [ + { + type: "text", + text: JSON.stringify(data, null, 2), + }, + ], + }; + } catch (error) { + console.error("Error crawling website:", error); + throw error; + } + } +); + +// Tool to extract structured data from document files +server.tool( + "extract-document", + { + url: z.string().url().optional(), + base64: z.string().optional(), + prompt: z.string(), + jsonMode: z.boolean().optional(), + model: z.string().optional(), + }, + async ({ url, base64, prompt, jsonMode, model }) => { + // Ensure either url or base64 is provided + if (!url && !base64) { + throw new Error("Either url or base64 is required"); + } + + // Get API key from environment variable + const apiKey = process.env.DUMPLING_API_KEY; + if (!apiKey) { + throw new Error("DUMPLING_API_KEY environment variable not set"); + } + + try { + const response = await fetch(`${NWS_API_BASE}/api/v1/extract-document`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify({ + url, + base64, + prompt, + jsonMode, + model, + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `Failed to extract document data: ${response.status} ${errorText}` + ); + } + + const data = await response.json(); + + return { + content: [ + { + type: "text", + text: JSON.stringify( + { + output: data.output, + creditUsage: data.creditUsage, + }, + null, + 2 + ), + }, + ], + }; + } catch (error) { + console.error("Error extracting document data:", error); + throw error; + } + } +); + +// Tool to extract structured data from image files +server.tool( + "extract-image", + { + images: z.array( + z.object({ + url: z.string().url().optional(), + base64: z.string().optional(), + }) + ), + prompt: z.string(), + jsonMode: z.boolean().optional(), + model: z.string().optional(), + }, + async ({ images, prompt, jsonMode, model }) => { + // Validate that each image has either url or base64 + for (const image of images) { + if (!image.url && !image.base64) { + throw new Error("Each image must have either url or base64"); + } + } + + // Get API key from environment variable + const apiKey = process.env.DUMPLING_API_KEY; + if (!apiKey) { + throw new Error("DUMPLING_API_KEY environment variable not set"); + } + + try { + const response = await fetch(`${NWS_API_BASE}/api/v1/extract-image`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify({ + images, + prompt, + jsonMode, + model, + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `Failed to extract image data: ${response.status} ${errorText}` + ); + } + + const data = await response.json(); + + return { + content: [ + { + type: "text", + text: JSON.stringify( + { + output: data.output, + creditUsage: data.creditUsage, + }, + null, + 2 + ), + }, + ], + }; + } catch (error) { + console.error("Error extracting image data:", error); + throw error; + } + } +); + +// Tool to convert PDF or DOCX documents into plain text +server.tool( + "doc-to-text", + { + url: z.string().url().optional(), + base64: z.string().optional(), + pages: z.string().optional(), + }, + async ({ url, base64, pages }) => { + // Ensure either url or base64 is provided + if (!url && !base64) { + throw new Error("Either url or base64 is required"); + } + + // Get API key from environment variable + const apiKey = process.env.DUMPLING_API_KEY; + if (!apiKey) { + throw new Error("DUMPLING_API_KEY environment variable not set"); + } + + try { + const response = await fetch(`${NWS_API_BASE}/api/v1/doc-to-text`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify({ + url, + base64, + pages, + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `Failed to convert document to text: ${response.status} ${errorText}` + ); + } + + const data = await response.json(); + + return { + content: [ + { + type: "text", + text: JSON.stringify( + { + text: data.text, + pages: data.pages, + creditUsage: data.creditUsage, + }, + null, + 2 + ), + }, + ], + }; + } catch (error) { + console.error("Error converting document to text:", error); + throw error; + } + } +); + +// Tool to capture a screenshot of a specified URL +server.tool( + "screenshot", + { + url: z.string().url(), + viewportWidth: z.number().optional(), + viewportHeight: z.number().optional(), + fullPage: z.boolean().optional(), + blockCookieBanners: z.boolean().optional(), + waitTime: z.number().optional(), + autoScroll: z.boolean().optional(), + renderJs: z.boolean().optional(), + }, + async ({ + url, + viewportWidth, + viewportHeight, + fullPage, + blockCookieBanners, + waitTime, + autoScroll, + renderJs, + }) => { + // Get API key from environment variable + const apiKey = process.env.DUMPLING_API_KEY; + if (!apiKey) { + throw new Error("DUMPLING_API_KEY environment variable not set"); + } + + try { + const response = await fetch(`${NWS_API_BASE}/api/v1/screenshot`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify({ + url, + viewportWidth, + viewportHeight, + fullPage, + blockCookieBanners, + waitTime, + autoScroll, + renderJs, + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `Failed to capture screenshot: ${response.status} ${errorText}` + ); + } + + const data = await response.json(); + + return { + content: [ + { + type: "text", + text: JSON.stringify( + { + screenshotUrl: data.screenshotUrl, + base64: data.base64, + creditUsage: data.creditUsage, + }, + null, + 2 + ), + }, + ], + }; + } catch (error) { + console.error("Error capturing screenshot:", error); + throw error; + } + } +); + +// Tool to generate high-quality AI images +server.tool( + "generate-ai-image", + { + prompt: z.string(), + model: z.string().optional(), + size: z.string().optional(), + style: z.string().optional(), + negativePrompt: z.string().optional(), + seed: z.number().optional(), + steps: z.number().optional(), + cfgScale: z.number().optional(), + sampler: z.string().optional(), + }, + async ({ + prompt, + model, + size, + style, + negativePrompt, + seed, + steps, + cfgScale, + sampler, + }) => { + // Get API key from environment variable + const apiKey = process.env.DUMPLING_API_KEY; + if (!apiKey) { + throw new Error("DUMPLING_API_KEY environment variable not set"); + } + + try { + const response = await fetch(`${NWS_API_BASE}/api/v1/generate-ai-image`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify({ + prompt, + model, + size, + style, + negativePrompt, + seed, + steps, + cfgScale, + sampler, + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `Failed to generate AI image: ${response.status} ${errorText}` + ); + } + + const data = await response.json(); + + return { + content: [ + { + type: "text", + text: JSON.stringify( + { + imageUrl: data.imageUrl, + base64: data.base64, + creditUsage: data.creditUsage, + }, + null, + 2 + ), + }, + ], + }; + } catch (error) { + console.error("Error generating AI image:", error); + throw error; + } + } +); + +// Tool to extract structured data from video files +server.tool( + "extract-video", + { + url: z.string().url().optional(), + base64: z.string().optional(), + prompt: z.string(), + jsonMode: z.boolean().optional(), + model: z.string().optional(), + }, + async ({ url, base64, prompt, jsonMode, model }) => { + // Ensure either url or base64 is provided + if (!url && !base64) { + throw new Error("Either url or base64 is required"); + } + + // Get API key from environment variable + const apiKey = process.env.DUMPLING_API_KEY; + if (!apiKey) { + throw new Error("DUMPLING_API_KEY environment variable not set"); + } + + try { + const response = await fetch(`${NWS_API_BASE}/api/v1/extract-video`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify({ + url, + base64, + prompt, + jsonMode, + model, + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `Failed to extract video data: ${response.status} ${errorText}` + ); + } + + const data = await response.json(); + + return { + content: [ + { + type: "text", + text: JSON.stringify( + { + output: data.output, + creditUsage: data.creditUsage, + }, + null, + 2 + ), + }, + ], + }; + } catch (error) { + console.error("Error extracting video data:", error); + throw error; + } + } +); + async function main() { const transport = new StdioServerTransport(); await server.connect(transport);