UNPKG

uploadthing-mcp

Version:
208 lines (202 loc) 7.66 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var mcp_exports = {}; __export(mcp_exports, { createMCPUploadThing: () => createMCPUploadThing }); module.exports = __toCommonJS(mcp_exports); var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js"); var import_server = require("uploadthing/server"); var fs = __toESM(require("node:fs/promises"), 1); var import_zod = require("zod"); function createMCPUploadThing({ token }) { if (!token) { throw new Error("UploadThing Token is required"); } const mcpServer = new import_mcp.McpServer({ name: "mcp-uploadthing", version: "0.0.2" }); const utapi = new import_server.UTApi({ token }); mcpServer.tool( "upload-files", "Upload files to UploadThing. Given a list of file with their paths, names, and types, it will upload the files to UploadThing and return the URLs of the uploaded files.", { files: import_zod.z.array( import_zod.z.object({ filePath: import_zod.z.string().describe("Path to file to upload"), fileName: import_zod.z.string().describe("Name of the file to upload"), fileType: import_zod.z.string().describe( "MIME type of the file (e.g., 'image/jpeg', 'application/pdf')" ) }) ).describe( "Array of files to upload to UploadThing. This includes the file path, file name, and file type." ) }, async (params) => { try { const filesPromises = params.files.map(async (file) => { const buffer = await fs.readFile(file.filePath); const fileObj = new File([buffer], file.fileName, { type: file.fileType }); return fileObj; }); const files = await Promise.allSettled(filesPromises); const failedFiles = files.map((file, idx) => ({ ...file, path: params.files[idx].filePath })).filter((file) => file.status === "rejected"); if (failedFiles.length > 0) { throw new Error( `Failed to read files given the following paths: ${failedFiles.map((file) => `${file.path}: ${JSON.stringify(file.reason)}`).join(", ")}` ); } const succesfulFiles = files.filter((file) => file.status === "fulfilled").map((file) => file.value); if (succesfulFiles.length === 0) { throw new Error("No files could be read to be uploaded"); } try { const results = await utapi.uploadFiles(succesfulFiles); const failedUploads = results.map((result, idx) => ({ error: result.error, file: succesfulFiles[idx] })).filter((result) => result.error); const successfulUploads = results.map((result, idx) => ({ data: result.data, file: succesfulFiles[idx] })).filter((result) => result.data); if (failedUploads.length > 0) { throw new Error( ` Successfully uploaded files: ${successfulUploads.map( (upload) => `${upload.file.name}: ${JSON.stringify(upload.data)}` ).join(", ")} Failed to upload files: ${failedUploads.map( (upload) => `${upload.file.name}: ${JSON.stringify(upload.error)}` ).join(", ")} ` ); } return { content: [ { type: "text", text: ` Successfully uploaded files: ${successfulUploads.map( (upload) => `${upload.file.name}: ${JSON.stringify(upload.data)}` ).join(", ")} ` } ] }; } catch (uploadError) { console.error("UploadThing API error:", uploadError); throw uploadError; } } catch (error) { console.error("File upload error:", error); return { content: [ { type: "text", text: `Files upload failed: ${error instanceof Error ? error.message : "Unknown error"}` } ], isError: true }; } } ); mcpServer.tool( "upload-files-from-urls", "Upload files by URL to UploadThing. Given a list of URLs, it will upload the files by URL to UploadThing and return the URLs of the uploaded files.", { filesByURL: import_zod.z.array(import_zod.z.string().url()).describe("Array of URLs of files to upload to UploadThing.") }, async (params) => { try { const urls = params.filesByURL; const results = await utapi.uploadFilesFromUrl(urls); const failedUploads = results.map((result, idx) => ({ error: result.error, url: urls[idx] })).filter((result) => result.error); const successfulUploads = results.map((result, idx) => ({ data: result.data, url: urls[idx] })).filter((result) => result.data); if (failedUploads.length > 0) { throw new Error( ` Successfully uploaded files by URL: ${successfulUploads.map( (upload) => `${upload.url}: ${JSON.stringify(upload.data)}` ).join(", ")} Failed to upload files by URL: ${failedUploads.map( (upload) => `${upload.url}: ${JSON.stringify(upload.error)}` ).join(", ")} ` ); } return { content: [ { type: "text", text: ` Successfully uploaded files by URL: ${successfulUploads.map( (upload) => `${upload.url}: ${JSON.stringify(upload.data)}` ).join(", ")} ` } ] }; } catch (error) { console.error("File upload error:", error); return { content: [ { type: "text", text: `Files by URL upload failed: ${error instanceof Error ? error.message : "Unknown error"}` } ], isError: true }; } } ); return mcpServer; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createMCPUploadThing }); //# sourceMappingURL=mcp.cjs.map