Skip to content

📨 The ultimate agentic social media scheduling tool 🤖

feat(mcp): upload widget for uploading local files from Claude and ChatGPT

Nevo David2c29ea3eeb66
diff --git a/apps/backend/src/api/api.module.ts b/apps/backend/src/api/api.module.ts
index ba1e687e7..52281a5fe 100644
--- a/apps/backend/src/api/api.module.ts
+++ b/apps/backend/src/api/api.module.ts
@@ -16,6 +16,8 @@ import { IntegrationManager } from '@gitroom/nestjs-libraries/integrations/integ
import { SettingsController } from '@gitroom/backend/api/routes/settings.controller';
import { PostsController } from '@gitroom/backend/api/routes/posts.controller';
import { MediaController } from '@gitroom/backend/api/routes/media.controller';
+import { MediaWidgetController } from '@gitroom/backend/api/routes/media.widget.controller';
+import { UploadWidgetAuthMiddleware } from '@gitroom/backend/services/auth/upload.widget.auth.middleware';
import { UploadModule } from '@gitroom/nestjs-libraries/upload/upload.module';
import { BillingController } from '@gitroom/backend/api/routes/billing.controller';
import { NotificationsController } from '@gitroom/backend/api/routes/notifications.controller';
@@ -76,7 +78,7 @@ const authenticatedController = [
@Module({
imports: [UploadModule],
controllers: process.env.MCP_ONLY
- ? [RootController, OAuthController]
+ ? [RootController, OAuthController, MediaWidgetController]
: [
RootController,
PaymentController,
@@ -87,6 +89,7 @@ const authenticatedController = [
EnterpriseController,
NoAuthIntegrationsController,
OAuthController,
+ MediaWidgetController,
...authenticatedController,
],
providers: [
@@ -98,6 +101,7 @@ const authenticatedController = [
OpenaiService,
ExtractContentService,
AuthMiddleware,
+ UploadWidgetAuthMiddleware,
PoliciesGuard,
PermissionsService,
CodesService,
@@ -119,5 +123,6 @@ const authenticatedController = [
export class ApiModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes(...authenticatedController);
+ consumer.apply(UploadWidgetAuthMiddleware).forRoutes(MediaWidgetController);
}
}
diff --git a/apps/backend/src/api/routes/media.widget.controller.ts b/apps/backend/src/api/routes/media.widget.controller.ts
new file mode 100644
index 000000000..c194d4d17
--- /dev/null
+++ b/apps/backend/src/api/routes/media.widget.controller.ts
@@ -0,0 +1,48 @@
+import {
+ BadRequestException,
+ Controller,
+ Get,
+ Post,
+ Req,
+ UploadedFile,
+ UseInterceptors,
+} from '@nestjs/common';
+import { Request } from 'express';
+import { ApiTags } from '@nestjs/swagger';
+import { Organization } from '@prisma/client';
+import { FileInterceptor } from '@nestjs/platform-express';
+import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request';
+import { MediaService } from '@gitroom/nestjs-libraries/database/prisma/media/media.service';
+import { streamUploadOptions } from '@gitroom/nestjs-libraries/upload/multer.stream.engine';
+
+@ApiTags('Media')
+@Controller('/media-widget')
+export class MediaWidgetController {
+ constructor(private _mediaService: MediaService) {}
+
+ @Post('/upload')
+ @UseInterceptors(FileInterceptor('file', streamUploadOptions()))
+ async upload(
+ @GetOrgFromRequest() org: Organization,
+ @Req() req: Request,
+ @UploadedFile() file: Express.Multer.File
+ ) {
+ if (!file) {
+ throw new BadRequestException('No file provided');
+ }
+ return this._mediaService.saveUploadSessionFile(
+ org.id,
+ // @ts-ignore
+ req.uploadSession,
+ file.filename,
+ file.path,
+ file.originalname
+ );
+ }
+
+ @Get('/status')
+ status(@GetOrgFromRequest() org: Organization, @Req() req: Request) {
+ // @ts-ignore
+ return this._mediaService.getUploadSession(org.id, req.uploadSession);
+ }
+}
diff --git a/apps/backend/src/services/auth/upload.widget.auth.middleware.ts b/apps/backend/src/services/auth/upload.widget.auth.middleware.ts
new file mode 100644
index 000000000..9529cd729
--- /dev/null
+++ b/apps/backend/src/services/auth/upload.widget.auth.middleware.ts
@@ -0,0 +1,34 @@
+import { HttpStatus, Injectable, NestMiddleware } from '@nestjs/common';
+import { Request, Response, NextFunction } from 'express';
+import { MediaService } from '@gitroom/nestjs-libraries/database/prisma/media/media.service';
+
+// The MCP upload widget runs in the host's sandboxed iframe (a foreign origin
+// without our cookies), so it authenticates with a short-lived ticket. It is
+// checked here, before the multipart interceptor streams anything to storage
+@Injectable()
+export class UploadWidgetAuthMiddleware implements NestMiddleware {
+ constructor(private _mediaService: MediaService) {}
+ async use(req: Request, res: Response, next: NextFunction) {
+ // Not part of the global cors() allowlist on purpose: that one allows
+ // credentials, and the sandbox origins are shared with every other connector.
+ // The global cors() answers every preflight itself, so the widget has to stay
+ // on "simple" requests (GET / multipart POST, no custom headers)
+ res.setHeader('Access-Control-Allow-Origin', '*');
+
+ const ticket =
+ typeof req.query.ticket === 'string' &&
+ (await this._mediaService.getUploadTicket(req.query.ticket));
+ if (!ticket) {
+ res
+ .status(HttpStatus.UNAUTHORIZED)
+ .json({ msg: 'Upload ticket not found or expired' });
+ return;
+ }
+
+ // @ts-ignore
+ req.org = { id: ticket.org };
+ // @ts-ignore
+ req.uploadSession = ticket.sessionId;
+ next();
+ }
+}
diff --git a/chatgpt-app-submission.json b/chatgpt-app-submission.json
index 82d417524..5a10433fe 100644
--- a/chatgpt-app-submission.json
+++ b/chatgpt-app-submission.json
@@ -163,6 +163,42 @@
"open_world_justification": "Reads from a public URL and stores the resulting media through external storage.",
"destructive_justification": "Does not delete or overwrite existing media, revoke access, or publish content."
}
+ },
+ "uploadWidgetTool": {
+ "annotations": {
+ "readOnlyHint": false,
+ "openWorldHint": false,
+ "destructiveHint": false
+ },
+ "justifications": {
+ "read_only_justification": "Opens an upload session and shows a widget the user uploads media to their own media library with.",
+ "open_world_justification": "Only creates an upload session inside the user's Postiz workspace; nothing is published or sent to a third party.",
+ "destructive_justification": "Does not delete or overwrite existing media, revoke access, or publish content."
+ }
+ },
+ "uploadWidgetTicketTool": {
+ "annotations": {
+ "readOnlyHint": false,
+ "openWorldHint": false,
+ "destructiveHint": false
+ },
+ "justifications": {
+ "read_only_justification": "Creates a short-lived upload ticket for the upload widget; it is only callable by the widget, not by the model.",
+ "open_world_justification": "The ticket only allows uploads into the user's own Postiz media library; no public or third-party state changes.",
+ "destructive_justification": "Does not delete or overwrite anything, revoke access, or publish content."
+ }
+ },
+ "uploadWidgetStatusTool": {
+ "annotations": {
+ "readOnlyHint": true,
+ "openWorldHint": false,
+ "destructiveHint": false
+ },
+ "justifications": {
+ "read_only_justification": "Only reads the media uploaded in an upload widget session.",
+ "open_world_justification": "Reads internal Postiz data for the user's workspace only.",
+ "destructive_justification": "Read-only; it cannot change or delete anything."
+ }
}
},
"test_cases": [
diff --git a/libraries/nestjs-libraries/src/chat/agent.tool.interface.ts b/libraries/nestjs-libraries/src/chat/agent.tool.interface.ts
index 42de60758..e3bea8b46 100644
--- a/libraries/nestjs-libraries/src/chat/agent.tool.interface.ts
+++ b/libraries/nestjs-libraries/src/chat/agent.tool.interface.ts
@@ -4,5 +4,7 @@ export type ToolReturn = ToolAction<any, any, any, any, any, any>;
export interface AgentToolInterface {
name: string;
+ // needs an MCP host (e.g. renders a ui:// widget), so the in-app agent doesn't get it
+ mcpOnly?: boolean;
run(): ToolReturn;
}
diff --git a/libraries/nestjs-libraries/src/chat/load.tools.service.ts b/libraries/nestjs-libraries/src/chat/load.tools.service.ts
index a0194b624..3b14f86fd 100644
--- a/libraries/nestjs-libraries/src/chat/load.tools.service.ts
+++ b/libraries/nestjs-libraries/src/chat/load.tools.service.ts
@@ -6,6 +6,7 @@ import { pStore } from '@gitroom/nestjs-libraries/chat/mastra.store';
import { array, object, string } from 'zod';
import { ModuleRef } from '@nestjs/core';
import { toolList } from '@gitroom/nestjs-libraries/chat/tools/tool.list';
+import { AgentToolInterface } from '@gitroom/nestjs-libraries/chat/agent.tool.interface';
import dayjs from 'dayjs';
export const AgentState = object({
@@ -21,11 +22,15 @@ const renderArray = (list: string[], show: boolean) => {
export class LoadToolsService {
constructor(private _moduleRef: ModuleRef) {}
- async loadTools() {
+ async loadTools(mcpOnly = false) {
return (
await Promise.all<{ name: string; tool: any }>(
toolList
- .map((p) => this._moduleRef.get(p, { strict: false }))
+ .map(
+ (p) =>
+ this._moduleRef.get(p, { strict: false }) as AgentToolInterface
+ )
+ .filter((p) => !!p.mcpOnly === mcpOnly)
.map(async (p) => ({
name: p.name as string,
tool: await p.run(),
diff --git a/libraries/nestjs-libraries/src/chat/start.mcp.ts b/libraries/nestjs-libraries/src/chat/start.mcp.ts
index 3c28f38c4..03ae71aee 100644
--- a/libraries/nestjs-libraries/src/chat/start.mcp.ts
+++ b/libraries/nestjs-libraries/src/chat/start.mcp.ts
@@ -1,11 +1,13 @@
import { INestApplication } from '@nestjs/common';
import { Request, Response } from 'express';
import { MastraService } from '@gitroom/nestjs-libraries/chat/mastra.service';
+import { LoadToolsService } from '@gitroom/nestjs-libraries/chat/load.tools.service';
import { MCPServer } from '@mastra/mcp';
import { OrganizationService } from '@gitroom/nestjs-libraries/database/prisma/organizations/organization.service';
import { OAuthService } from '@gitroom/nestjs-libraries/database/prisma/oauth/oauth.service';
import { runWithContext } from './async.storage';
import { createOAuthMiddleware } from './oauth-middleware';
+import { UPLOAD_WIDGET_URI, uploadWidgetHtml } from '@gitroom/nestjs-libraries/chat/ui/upload.widget';
const fixAcceptHeader = (req: Request) => {
const value = 'application/json, text/event-stream';
req.headers.accept = value;
@@ -29,6 +31,7 @@ export const startMcp = async (app: INestApplication) => {
const mastraService = app.get(MastraService, { strict: false });
const organizationService = app.get(OrganizationService, { strict: false });
const oauthService = app.get(OAuthService, { strict: false });
+ const loadToolsService = app.get(LoadToolsService, { strict: false });
const resolveAuth = async (token: string) => {
if (token.startsWith('pos_')) {
@@ -41,7 +44,11 @@ export const startMcp = async (app: INestApplication) => {
const mastra = await mastraService.mastra();
const agent = mastra.getAgent('postiz');
- const tools = await agent.listTools();
+ const tools = {
+ ...(await agent.listTools()),
+ // tools that only make sense inside an MCP host (ui:// widgets)
+ ...(await loadToolsService.loadTools(true)),
+ };
// The Claude connector directory does not accept AI media generation tools,
// so the directory-facing endpoint hides them. Direct connections
@@ -57,11 +64,28 @@ export const startMcp = async (app: INestApplication) => {
Object.entries(tools).filter(([name]) => !claudeHiddenTools.includes(name))
) as typeof tools;
+ const backendUrl = process.env.NEXT_PUBLIC_OVERRIDE_BACKEND_URL || process.env.NEXT_PUBLIC_BACKEND_URL;
+
+ // MCP Apps widgets (ui:// resources). They run in the host's sandboxed iframe,
+ // which can only reach the domains listed in the csp
+ const appResources = {
+ [UPLOAD_WIDGET_URI]: {
+ name: 'Upload Media',
+ description: 'Upload an image or video from the device to the media library',
+ html: uploadWidgetHtml(backendUrl!),
+ meta: {
+ csp: { connectDomains: [new URL(backendUrl!).origin] },
+ prefersBorder: true,
+ },
+ },
+ };
+
const serverConfig = {
name: 'Postiz MCP',
version: '1.0.0',
tools,
agents: { postiz: agent },
+ appResources,
};
const server = new MCPServer(serverConfig);
@@ -73,16 +97,16 @@ export const startMcp = async (app: INestApplication) => {
name: 'Postiz MCP',
version: '1.0.0',
tools,
+ appResources,
});
const claudeOauthServer = new MCPServer({
name: 'Postiz MCP',
version: '1.0.0',
tools: claudeTools,
+ appResources,
});
- const backendUrl = process.env.NEXT_PUBLIC_OVERRIDE_BACKEND_URL || process.env.NEXT_PUBLIC_BACKEND_URL;
-
// Two RFC 8414 path-based issuers backed by the same endpoints and code.
// /mcp-oauth-chatgpt is what the ChatGPT app submission points at: it does
// not advertise a registration_endpoint, so the OpenAI builder defaults to
diff --git a/libraries/nestjs-libraries/src/chat/tools/tool.list.ts b/libraries/nestjs-libraries/src/chat/tools/tool.list.ts
index dc5fc9893..c1c5a4bf7 100644
--- a/libraries/nestjs-libraries/src/chat/tools/tool.list.ts
+++ b/libraries/nestjs-libraries/src/chat/tools/tool.list.ts
@@ -11,6 +11,9 @@ import { GroupListTool } from '@gitroom/nestjs-libraries/chat/tools/group.list.t
import { UploadFromUrlTool } from '@gitroom/nestjs-libraries/chat/tools/upload.from.url.tool';
import { PostsListTool } from '@gitroom/nestjs-libraries/chat/tools/posts.list.tool';
import { PostSettingsTool } from '@gitroom/nestjs-libraries/chat/tools/post.settings.tool';
+import { UploadWidgetTool } from '@gitroom/nestjs-libraries/chat/tools/upload.widget.tool';
+import { UploadWidgetTicketTool } from '@gitroom/nestjs-libraries/chat/tools/upload.widget.ticket.tool';
+import { UploadWidgetStatusTool } from '@gitroom/nestjs-libraries/chat/tools/upload.widget.status.tool';
export const toolList = [
IntegrationListTool,
@@ -26,4 +29,7 @@ export const toolList = [
VideoStatusTool,
GenerateImageTool,
UploadFromUrlTool,
+ UploadWidgetTool,
+ UploadWidgetTicketTool,
+ UploadWidgetStatusTool,
];
diff --git a/libraries/nestjs-libraries/src/chat/tools/upload.widget.status.tool.ts b/libraries/nestjs-libraries/src/chat/tools/upload.widget.status.tool.ts
new file mode 100644
index 000000000..ce32d1259
--- /dev/null
+++ b/libraries/nestjs-libraries/src/chat/tools/upload.widget.status.tool.ts
@@ -0,0 +1,72 @@
+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 { checkAuth } from '@gitroom/nestjs-libraries/chat/auth.context';
+
+@Injectable()
+export class UploadWidgetStatusTool implements AgentToolInterface {
+ constructor(private _mediaService: MediaService) {}
+ name = 'uploadWidgetStatusTool';
+ mcpOnly = true;
+
+ run() {
+ return createTool({
+ id: 'uploadWidgetStatusTool',
+ description: `List the media the user uploaded with the upload widget, using the sessionId returned by uploadWidgetTool.
+An empty list means the user did not upload anything yet. A media with the status "processing" is still being prepared: wait about 10 seconds and call again.
+A media with the status "ready" can be used as a post attachment with its { id, path }.`,
+ mcp: {
+ annotations: {
+ title: 'Upload Widget Status',
+ readOnlyHint: true,
+ destructiveHint: false,
+ idempotentHint: true,
+ openWorldHint: false,
+ },
+ },
+ inputSchema: z.object({
+ sessionId: z
+ .string()
+ .describe('The sessionId returned by uploadWidgetTool'),
+ }),
+ outputSchema: z.object({
+ media: z
+ .array(
+ z.object({
+ id: z.string(),
+ path: z.string(),
+ status: z.string(),
+ error: z.string().optional(),
+ })
+ )
+ .optional(),
+ error: z.string().optional(),
+ }),
+ execute: async (inputData, context) => {
+ checkAuth(inputData, context);
+ try {
+ const org = JSON.parse(
+ (context?.requestContext as any)?.get('organization') as string
+ );
+ const media = await this._mediaService.getUploadSession(
+ org.id,
+ inputData.sessionId
+ );
+ return {
+ media: media.map((p) => ({
+ id: p.id,
+ path: p.path,
+ status: p.status,
+ ...(p.processingError ? { error: p.processingError } : {}),
+ })),
+ };
+ } catch (err) {
+ const message = err instanceof Error ? err.message : String(err);
+ return { error: `Upload session lookup failed: ${message}` };
+ }
+ },
+ });
+ }
+}
diff --git a/libraries/nestjs-libraries/src/chat/tools/upload.widget.ticket.tool.ts b/libraries/nestjs-libraries/src/chat/tools/upload.widget.ticket.tool.ts
new file mode 100644
index 000000000..f716a2126
--- /dev/null
+++ b/libraries/nestjs-libraries/src/chat/tools/upload.widget.ticket.tool.ts
@@ -0,0 +1,63 @@
+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 { checkAuth } from '@gitroom/nestjs-libraries/chat/auth.context';
+
+// Meant for the upload widget itself: visibility "app" asks the host to keep it
+// away from the model, so the upload credential stays out of the conversation.
+// It is a hint only - the ticket is still scoped to the caller's own organization
+@Injectable()
+export class UploadWidgetTicketTool implements AgentToolInterface {
+ constructor(private _mediaService: MediaService) {}
+ name = 'uploadWidgetTicketTool';
+ mcpOnly = true;
+
+ run() {
+ return createTool({
+ id: 'uploadWidgetTicketTool',
+ description: `Used by the upload widget to get a short-lived upload ticket for the sessionId returned by uploadWidgetTool.`,
+ mcp: {
+ annotations: {
+ title: 'Upload Widget Ticket',
+ readOnlyHint: false,
+ destructiveHint: false,
+ idempotentHint: false,
+ openWorldHint: false,
+ },
+ _meta: {
+ ui: {
+ visibility: ['app'],
+ },
+ },
+ },
+ inputSchema: z.object({
+ sessionId: z
+ .string()
+ .describe('The sessionId returned by uploadWidgetTool'),
+ }),
+ outputSchema: z.object({
+ ticket: z.string().optional(),
+ error: z.string().optional(),
+ }),
+ execute: async (inputData, context) => {
+ checkAuth(inputData, context);
+ try {
+ const org = JSON.parse(
+ (context?.requestContext as any)?.get('organization') as string
+ );
+ return {
+ ticket: await this._mediaService.createUploadTicket(
+ org.id,
+ inputData.sessionId
+ ),
+ };
+ } catch (err) {
+ const message = err instanceof Error ? err.message : String(err);
+ return { error: `Failed to create an upload ticket: ${message}` };
+ }
+ },
+ });
+ }
+}
diff --git a/libraries/nestjs-libraries/src/chat/tools/upload.widget.tool.ts b/libraries/nestjs-libraries/src/chat/tools/upload.widget.tool.ts
new file mode 100644
index 000000000..826b927ad
--- /dev/null
+++ b/libraries/nestjs-libraries/src/chat/tools/upload.widget.tool.ts
@@ -0,0 +1,57 @@
+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 { checkAuth } from '@gitroom/nestjs-libraries/chat/auth.context';
+import { UPLOAD_WIDGET_URI } from '@gitroom/nestjs-libraries/chat/ui/upload.widget';
+
+@Injectable()
+export class UploadWidgetTool implements AgentToolInterface {
+ constructor(private _mediaService: MediaService) {}
+ name = 'uploadWidgetTool';
+ mcpOnly = true;
+
+ run() {
+ return createTool({
+ id: 'uploadWidgetTool',
+ description: `Show the user an upload widget to add images or videos from their own device to the media library.
+Use this when the user wants to attach a local file to a post. When the media is already available on a public URL, use uploadFromUrlTool instead.
+The widget is only displayed by apps that support MCP Apps (interactive UI); in any other app no widget appears and uploadFromUrlTool with a public URL is the way to add media.
+Returns a sessionId: the files the user uploads are reported in the conversation, and can also be read with uploadWidgetStatusTool.`,
+ mcp: {
+ annotations: {
+ title: 'Upload Media From Device',
+ readOnlyHint: false,
+ destructiveHint: false,
+ idempotentHint: false,
+ openWorldHint: false,
+ },
+ _meta: {
+ ui: {
+ resourceUri: UPLOAD_WIDGET_URI,
+ },
+ },
+ },
+ inputSchema: z.object({}),
+ outputSchema: z.object({
+ sessionId: z.string().optional(),
+ error: z.string().optional(),
+ }),
+ execute: async (inputData, context) => {
+ checkAuth(inputData, context);
+ try {
+ const org = JSON.parse(
+ (context?.requestContext as any)?.get('organization') as string
+ );
+ return {
+ sessionId: await this._mediaService.createUploadSession(org.id),
+ };
+ } catch (err) {
+ const message = err instanceof Error ? err.message : String(err);
+ return { error: `Failed to open the upload widget: ${message}` };
+ }
+ },
+ });
+ }
+}
diff --git a/libraries/nestjs-libraries/src/chat/ui/upload.widget.ts b/libraries/nestjs-libraries/src/chat/ui/upload.widget.ts
new file mode 100644
index 000000000..362f98484
--- /dev/null
+++ b/libraries/nestjs-libraries/src/chat/ui/upload.widget.ts
@@ -0,0 +1,246 @@
+import { getMaxSize } from '@gitroom/nestjs-libraries/upload/custom.upload.validation';
+
+const megabytes = (mime: string) => Math.floor(getMaxSize(mime) / 1024 / 1024);
+
+export const UPLOAD_WIDGET_URI = 'ui://postiz/upload';
+
+// MCP Apps (SEP-1865) widget: a single self-contained HTML document rendered by
+// the host inside a sandboxed iframe. It can't load our bundles or cookies, so
+// it talks to the host over postMessage JSON-RPC and to the backend over fetch:
+// uploadWidgetTool result (sessionId) -> uploadWidgetTicketTool (ticket, through
+// the host) -> POST /media-widget/upload -> report the media back to the model.
+// The upload is a plain multipart fetch on purpose: no custom headers and no
+// XHR progress listeners, so the browser never sends a CORS preflight
+export const uploadWidgetHtml = (backendUrl: string) => `<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="utf-8" />
+<meta name="viewport" content="width=device-width, initial-scale=1" />
+<style>
+ :root { color-scheme: light dark; --bg: #ffffff; --fg: #0e0e0e; --muted: #6b6b6b; --border: #d9d9d9; --accent: #612bd3; --ok: #1a7f37; --bad: #cf222e; }
+ @media (prefers-color-scheme: dark) { :root { --bg: #1a1919; --fg: #ffffff; --muted: #9c9c9c; --border: #3a3a3a; --ok: #3fb950; --bad: #f85149; } }
+ * { box-sizing: border-box; }
+ body { margin: 0; padding: 16px; background: var(--bg); color: var(--fg); font: 14px/1.4 -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
+ #drop { display: block; border: 2px dashed var(--border); border-radius: 8px; padding: 24px 16px; text-align: center; cursor: pointer; }
+ #drop.over { border-color: var(--accent); }
+ #drop.disabled { opacity: 0.5; pointer-events: none; }
+ #drop small { display: block; color: var(--muted); margin-top: 4px; }
+ input[type=file] { display: none; }
+ ul { list-style: none; padding: 0; margin: 0; }
+ li { padding: 6px 0; display: flex; gap: 8px; align-items: baseline; }
+ li:first-child { margin-top: 12px; }
+ li .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
+ li .state { color: var(--muted); }
+ li.ok .state { color: var(--ok); }
+ li.bad .state { color: var(--bad); white-space: normal; }
+ #error { color: var(--bad); margin-top: 12px; }
+ #error:empty { display: none; }
+</style>
+</head>
+<body>
+ <label id="drop" class="disabled">
+ Choose files or drop them here
+ <small>Images up to ${megabytes('image/png')} MB and videos up to ${megabytes('video/mp4')} MB</small>
+ <input id="file" type="file" accept="image/*,video/mp4" multiple />
+ </label>
+ <ul id="files"></ul>
+ <div id="error"></div>
+<script>
+(function () {
+ var BACKEND = ${JSON.stringify(backendUrl).replace(/</g, '\\u003c')};
+ var nextId = 1;
+ var pending = {};
+ var sessionId = null;
+ var uploaded = [];
+ var busy = false;
+
+ function send(message) { window.parent.postMessage(Object.assign({ jsonrpc: '2.0' }, message), '*'); }
+ function request(method, params) {
+ return new Promise(function (resolve, reject) {
+ var id = nextId++;
+ pending[id] = { resolve: resolve, reject: reject };
+ send({ id: id, method: method, params: params });
+ });
+ }
+ function notify(method, params) { send({ method: method, params: params }); }
+ function resize() { notify('ui/notifications/size-changed', { height: document.body.offsetHeight }); }
+ function wait(ms) { return new Promise(function (resolve) { setTimeout(resolve, ms); }); }
+
+ var drop = document.getElementById('drop');
+ var list = document.getElementById('files');
+ var error = document.getElementById('error');
+ function fail(message) { error.textContent = message; resize(); }
+
+ function start(result) {
+ var content = (result && result.structuredContent) || {};
+ if (!content.sessionId) {
+ fail(content.error || 'The upload widget could not be opened.');
+ return;
+ }
+ sessionId = content.sessionId;
+ drop.className = '';
+ }
+
+ window.addEventListener('message', function (event) {
+ var data = event.data;
+ if (event.source !== window.parent || !data || data.jsonrpc !== '2.0') return;
+ if (data.id !== undefined && !data.method) {
+ var waiting = pending[data.id];
+ if (!waiting) return;
+ delete pending[data.id];
+ if (data.error) waiting.reject(new Error(data.error.message || 'The host rejected the request'));
+ else waiting.resolve(data.result);
+ return;
+ }
+ if (data.method === 'ui/notifications/tool-result') start(data.params);
+ // Requests from the host (ui/resource-teardown, ping) need an answer
+ if (data.id !== undefined && data.method) send({ id: data.id, result: {} });
+ });
+
+ function getTicket() {
+ return request('tools/call', { name: 'uploadWidgetTicketTool', arguments: { sessionId: sessionId } }).then(function (result) {
+ var content = (result && result.structuredContent) || {};
+ if (!content.ticket) throw new Error(content.error || 'Could not get an upload ticket');
+ return content.ticket;
+ });
+ }
+
+ function readError(res) {
+ return res.json().then(
+ function (body) { return new Error(body.message || body.msg || 'Upload failed (' + res.status + ')'); },
+ function () { return new Error('Upload failed (' + res.status + ')'); }
+ );
+ }
+
+ function uploadFile(file, ticket) {
+ var form = new FormData();
+ form.append('file', file);
+ return fetch(BACKEND + '/media-widget/upload?ticket=' + encodeURIComponent(ticket), { method: 'POST', body: form }).then(function (res) {
+ if (!res.ok) return readError(res).then(function (err) { throw err; });
+ return res.json();
+ });
+ }
+
+ // Videos and images may be normalized after the upload, poll until they are usable
+ function waitUntilReady(media, ticket, deadline) {
+ if (media.status !== 'processing') return Promise.resolve(media);
+ deadline = deadline || Date.now() + 15 * 60 * 1000;
+ if (Date.now() > deadline) return Promise.reject(new Error('Processing is taking too long, check the media library later'));
+ return wait(3000)
+ .then(function () { return fetch(BACKEND + '/media-widget/status?ticket=' + encodeURIComponent(ticket)).catch(function () { return null; }); })
+ .then(function (res) {
+ if (!res) return waitUntilReady(media, ticket, deadline);
+ // the ticket is short lived and a long video can outlive it
+ if (res.status === 401) return getTicket().then(function (fresh) { return waitUntilReady(media, fresh, deadline); });
+ if (!res.ok) return readError(res).then(function (err) { throw err; });
+ return res.json().then(function (all) {
+ var current = all.filter(function (p) { return p.id === media.id; })[0] || media;
+ return waitUntilReady(current, ticket, deadline);
+ });
+ });
+ }
+
+ function row(file) {
+ var li = document.createElement('li');
+ var name = document.createElement('span');
+ var state = document.createElement('span');
+ name.className = 'name';
+ name.textContent = file.name;
+ state.className = 'state';
+ li.appendChild(name);
+ li.appendChild(state);
+ list.appendChild(li);
+ return function (kind, text) {
+ li.className = kind;
+ state.textContent = text;
+ resize();
+ };
+ }
+
+ function report(done) {
+ var summary = done.map(function (p) { return p.name + ' (id: ' + p.id + ', path: ' + p.path + ')'; }).join(', ');
+ // Silent context first, so the model has the ids even if the host defers the message.
+ // A host that never answers it must not hold the message back
+ return Promise.race([
+ request('ui/update-model-context', {
+ structuredContent: { sessionId: sessionId, media: uploaded },
+ content: [{ type: 'text', text: 'Media uploaded with the upload widget: ' + summary }],
+ }),
+ wait(5000),
+ ])
+ .catch(function () {})
+ .then(function () {
+ return request('ui/message', {
+ role: 'user',
+ content: [{ type: 'text', text: 'I uploaded ' + summary + ' with the upload widget.' }],
+ });
+ })
+ .catch(function () {});
+ }
+
+ function handle(files) {
+ if (busy || !sessionId || !files.length) return;
+ busy = true;
+ drop.className = 'disabled';
+ fail('');
+ var done = [];
+ getTicket()
+ .then(function (ticket) {
+ return files.reduce(function (chain, file) {
+ var set = row(file);
+ return chain.then(function () {
+ set('', 'Uploading…');
+ return uploadFile(file, ticket)
+ .then(function (media) {
+ if (media.status === 'processing') set('', 'Processing…');
+ return waitUntilReady(media, ticket);
+ })
+ .then(function (media) {
+ if (media.status === 'failed') throw new Error(media.processingError || 'Processing failed');
+ var item = { id: media.id, path: media.path, name: media.originalName || file.name };
+ uploaded.push(item);
+ done.push(item);
+ set('ok', 'Uploaded');
+ })
+ .catch(function (err) { set('bad', err.message || 'Upload failed'); });
+ });
+ }, Promise.resolve());
+ })
+ .catch(function (err) { fail(err.message || 'Upload failed'); })
+ .then(function () {
+ busy = false;
+ drop.className = '';
+ if (done.length) report(done);
+ });
+ }
+
+ document.getElementById('file').addEventListener('change', function (e) {
+ handle(Array.prototype.slice.call(e.target.files || []));
+ e.target.value = '';
+ });
+ ['dragenter', 'dragover'].forEach(function (name) {
+ drop.addEventListener(name, function (e) { e.preventDefault(); if (!busy && sessionId) drop.className = 'over'; });
+ });
+ drop.addEventListener('dragleave', function (e) { e.preventDefault(); if (!busy && sessionId) drop.className = ''; });
+ drop.addEventListener('drop', function (e) {
+ e.preventDefault();
+ if (!busy && sessionId) drop.className = '';
+ handle(Array.prototype.slice.call((e.dataTransfer && e.dataTransfer.files) || []));
+ });
+
+ ['dragover', 'drop'].forEach(function (name) {
+ document.addEventListener(name, function (e) { e.preventDefault(); });
+ });
+
+ request('ui/initialize', {
+ appInfo: { name: 'postiz-upload', version: '1.0.0' },
+ appCapabilities: {},
+ protocolVersion: '2026-01-26',
+ }).then(
+ function () { notify('ui/notifications/initialized', {}); resize(); },
+ function () { fail('This app cannot display the upload widget.'); }
+ );
+})();
+</script>
+</body>
+</html>`;
diff --git a/libraries/nestjs-libraries/src/database/prisma/media/media.service.ts b/libraries/nestjs-libraries/src/database/prisma/media/media.service.ts
index 11d222c52..bdeaf1f8d 100644
--- a/libraries/nestjs-libraries/src/database/prisma/media/media.service.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/media/media.service.ts
@@ -19,6 +19,8 @@ import { organizationId } from '@gitroom/nestjs-libraries/temporal/temporal.sear
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import { MediaProcessorJob } from '@gitroom/nestjs-libraries/upload/media.processor.interface';
import { extname } from 'path';
+import { randomBytes } from 'crypto';
+import { ioRedis } from '@gitroom/nestjs-libraries/redis/redis.service';
import { ssrfSafeDispatcher } from '@gitroom/nestjs-libraries/dtos/webhooks/ssrf.safe.dispatcher';
import {
getMaxSize,
@@ -227,6 +229,78 @@ export class MediaService {
return this._mediaRepository.getMediaStatus(org, id);
}
+ // Upload widget (MCP Apps): the session id is what the model sees and polls,
+ // the ticket is the credential the widget uploads with. It is handed to the
+ // widget only, so it doesn't end up in the conversation
+ async createUploadSession(org: string) {
+ const sessionId = randomBytes(16).toString('hex');
+ await ioRedis.set(`uploadSession:${sessionId}`, org, 'EX', 3600);
+ return sessionId;
+ }
+
+ private async checkUploadSession(org: string, sessionId: string) {
+ if ((await ioRedis.get(`uploadSession:${sessionId}`)) !== org) {
+ throw new HttpException('Upload session not found or expired', 404);
+ }
+ }
+
+ async createUploadTicket(org: string, sessionId: string) {
+ await this.checkUploadSession(org, sessionId);
+ const ticket = randomBytes(32).toString('hex');
+ await ioRedis.set(
+ `uploadTicket:${ticket}`,
+ JSON.stringify({ org, sessionId }),
+ 'EX',
+ 600
+ );
+ return ticket;
+ }
+
+ // A ticket never outlives its session: the file is streamed to storage right
+ // after this check, so an expired session has to be refused here
+ async getUploadTicket(ticket: string) {
+ const found = JSON.parse(
+ (await ioRedis.get(`uploadTicket:${ticket}`)) || 'null'
+ ) as { org: string; sessionId: string } | null;
+ if (
+ !found ||
+ (await ioRedis.get(`uploadSession:${found.sessionId}`)) !== found.org
+ ) {
+ return null;
+ }
+ return found;
+ }
+
+ async saveUploadSessionFile(
+ org: string,
+ sessionId: string,
+ fileName: string,
+ filePath: string,
+ originalName?: string
+ ) {
+ await this.checkUploadSession(org, sessionId);
+ const media = await this.saveUploadedFile(
+ org,
+ fileName,
+ filePath,
+ originalName
+ );
+ // a list, so parallel uploads of the same session can't overwrite each other
+ await ioRedis.rpush(`uploadSessionMedia:${sessionId}`, media.id);
+ await ioRedis.expire(`uploadSessionMedia:${sessionId}`, 3600);
+ return media;
+ }
+
+ async getUploadSession(org: string, sessionId: string) {
+ await this.checkUploadSession(org, sessionId);
+ const list = await ioRedis.lrange(`uploadSessionMedia:${sessionId}`, 0, -1);
+ return (
+ await Promise.all(
+ list.map((id) => this._mediaRepository.getMediaStatus(org, id))
+ )
+ ).filter((f) => f);
+ }
+
async getMediaStatus(org: string, id: string) {
const media = await this._mediaRepository.getMediaStatus(org, id);
if (!media) {
diff --git a/libraries/nestjs-libraries/src/redis/redis.service.ts b/libraries/nestjs-libraries/src/redis/redis.service.ts
index 92a95a6a4..c54b142de 100644
--- a/libraries/nestjs-libraries/src/redis/redis.service.ts
+++ b/libraries/nestjs-libraries/src/redis/redis.service.ts
@@ -18,6 +18,20 @@ class MockRedis {
return 1;
}
+ async rpush(key: string, value: any) {
+ this.data.set(key, [...(this.data.get(key) || []), value]);
+ return this.data.get(key).length;
+ }
+
+ async lrange(key: string, start: number, end: number) {
+ const list = this.data.get(key) || [];
+ return list.slice(start, end === -1 ? undefined : end + 1);
+ }
+
+ async expire() {
+ return 1;
+ }
+
// Add other Redis methods as needed for your tests
}