π¨ The ultimate agentic social media scheduling tool π€
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
feat: mcp upload
Nevo Davidd31226d889db
diff --git a/libraries/nestjs-libraries/src/chat/tools/integration.schedule.post.ts b/libraries/nestjs-libraries/src/chat/tools/integration.schedule.post.tsindex e6a657941..19433a7fa 100644--- a/libraries/nestjs-libraries/src/chat/tools/integration.schedule.post.ts+++ b/libraries/nestjs-libraries/src/chat/tools/integration.schedule.post.ts@@ -8,6 +8,24 @@ import { makeId } from '@gitroom/nestjs-libraries/services/make.is';import { AllProvidersSettings } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/all.providers.settings';import { Integration } from '@prisma/client';import { checkAuth } from '@gitroom/nestjs-libraries/chat/auth.context';+import {+ ValidUrlExtension,+ ValidUrlPath,+} from '@gitroom/helpers/utils/valid.url.path';++const validUrlExtension = new ValidUrlExtension();+const validUrlPath = new ValidUrlPath();++// Same URL validation as MediaDto (valid.url.path) - each attachment must+// point to an allowed upload domain and a supported file extension.+const attachmentUrl = z+ .string()+ .refine((url) => validUrlPath.validate(url, {} as any), {+ message: validUrlPath.defaultMessage({} as any),+ })+ .refine((url) => validUrlExtension.validate(url, {} as any), {+ message: validUrlExtension.defaultMessage({} as any),+ });@Injectable()export class IntegrationSchedulePostTool implements AgentToolInterface {@@ -75,7 +93,7 @@ If the tools return errors, you would need to rerun it with the right parameters"The content of the post, HTML, Each line must be wrapped in <p> here is the possible tags: h1, h2, h3, u, strong, li, ul, p (you can't have u and strong together)"),attachments: z- .array(z.string())+ .array(attachmentUrl).describe('The image of the post (URLS)'),}))diff --git a/libraries/nestjs-libraries/src/chat/tools/tool.list.ts b/libraries/nestjs-libraries/src/chat/tools/tool.list.tsindex beab9a755..d2be0fc24 100644--- a/libraries/nestjs-libraries/src/chat/tools/tool.list.ts+++ b/libraries/nestjs-libraries/src/chat/tools/tool.list.ts@@ -6,6 +6,7 @@ import { VideoFunctionTool } from '@gitroom/nestjs-libraries/chat/tools/video.fuimport { GenerateVideoTool } from '@gitroom/nestjs-libraries/chat/tools/generate.video.tool';import { GenerateImageTool } from '@gitroom/nestjs-libraries/chat/tools/generate.image.tool';import { IntegrationListTool } from '@gitroom/nestjs-libraries/chat/tools/integration.list.tool';+import { UploadFromUrlTool } from '@gitroom/nestjs-libraries/chat/tools/upload.from.url.tool';export const toolList = [IntegrationListTool,@@ -16,4 +17,5 @@ export const toolList = [VideoFunctionTool,GenerateVideoTool,GenerateImageTool,+ UploadFromUrlTool,];diff --git a/libraries/nestjs-libraries/src/chat/tools/upload.from.url.tool.ts b/libraries/nestjs-libraries/src/chat/tools/upload.from.url.tool.tsnew file mode 100644index 000000000..c8a8fc617--- /dev/null+++ b/libraries/nestjs-libraries/src/chat/tools/upload.from.url.tool.ts@@ -0,0 +1,101 @@+import { AgentToolInterface } from '@gitroom/nestjs-libraries/chat/agent.tool.interface';+import { createTool } from '@mastra/core/tools';+import { z } from 'zod';+import { Injectable } from '@nestjs/common';+import { MediaService } from '@gitroom/nestjs-libraries/database/prisma/media/media.service';+import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';+import { checkAuth } from '@gitroom/nestjs-libraries/chat/auth.context';+import { ssrfSafeDispatcher } from '@gitroom/nestjs-libraries/dtos/webhooks/ssrf.safe.dispatcher';+import { Readable } from 'stream';+// eslint-disable-next-line @typescript-eslint/no-var-requires+const { fromBuffer } = require('file-type');++// Same allow-list as the public API /upload-from-url route.+const ALLOWED_MIME = new Set<string>([+ 'image/jpeg',+ 'image/png',+ 'image/gif',+ 'image/webp',+ 'image/avif',+ 'image/bmp',+ 'image/tiff',+ 'video/mp4',+]);++@Injectable()+export class UploadFromUrlTool implements AgentToolInterface {+ private storage = UploadFactory.createStorage();++ constructor(private _mediaService: MediaService) {}+ name = 'uploadFromUrlTool';++ run() {+ return createTool({+ id: 'uploadFromUrlTool',+ description: `Upload a remote image or video into the media library from a public URL.+Use this before scheduling a post when the user provides an external media URL (not already hosted on our domain),+so the attachment passes the upload-domain validation. Returns the hosted media { id, path } to use as an attachment.`,+ mcp: {+ annotations: {+ title: 'Upload Media From URL',+ readOnlyHint: false,+ destructiveHint: false,+ idempotentHint: false,+ openWorldHint: true,+ },+ },+ inputSchema: z.object({+ url: z+ .string()+ .url()+ .describe('The public URL of the image or video to upload'),+ }),+ outputSchema: z+ .object({+ id: z.string(),+ path: z.string(),+ })+ .or(z.object({ errors: z.string() })),+ execute: async (inputData, context) => {+ checkAuth(inputData, context);+ const org = JSON.parse(+ (context?.requestContext as any)?.get('organization') as string+ );++ const response = await fetch(inputData.url, {+ // @ts-ignore β undici option, not in lib.dom fetch types+ dispatcher: ssrfSafeDispatcher,+ });++ if (!response.ok) {+ return { errors: 'Failed to fetch URL' };+ }++ const buffer = Buffer.from(await response.arrayBuffer());+ const detected = await fromBuffer(buffer);+ if (!detected || !ALLOWED_MIME.has(detected.mime)) {+ return { errors: 'Unsupported file type.' };+ }++ const getFile = await this.storage.uploadFile({+ buffer,+ mimetype: detected.mime,+ size: buffer.length,+ path: '',+ fieldname: '',+ destination: '',+ stream: new Readable(),+ filename: '',+ originalname: `upload.${detected.ext}`,+ encoding: '',+ });++ return this._mediaService.saveFile(+ org.id,+ getFile.originalname,+ getFile.path+ );+ },+ });+ }+}