Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

3.5 KB
Raw Blame
<?php

declare(strict_types=1);

namespace App\Mcp\Tools\Post;

use App\Enums\Media\Type as MediaType;
use App\Mcp\Concerns\AuthorizesMcpTool;
use App\Models\Workspace;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\ResponseFactory;
use Laravel\Mcp\Server\Attributes\Description;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\Annotations\IsDestructive;
use Laravel\Mcp\Server\Tools\Annotations\IsOpenWorld;
use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly;

#[IsReadOnly(false)]
#[IsDestructive(false)]
#[IsOpenWorld(false)]
#[Description('Issue a one-shot signed POST URL that lets the user upload a local file (image, video, or PDF document) directly to this workspace. Accepted types: JPEG, PNG, GIF, WebP, MP4, MOV, PDF, plus HEIC/HEIF photos (converted to JPEG) when the server can convert them. Returns an upload_token, upload_url, max_bytes, and max_bytes_by_type — those are the server ceilings (max_bytes equals the video cap; an image is also refused above 67 megapixels by default); each network enforces its own, usually smaller, size / duration / format caps at schedule or publish time, so check list-content-types-tool for the post\'s enabled content_types first. Video duration is measured on the server after the upload. Upload the file with one multipart/form-data POST to upload_url, the file in the form field named by field_name (`media`), e.g. `curl -F media=@path/to/file "<upload_url>"`; hand that command to the user or run it from the MCP client. The URL works once, until expires_at; ask for a new one for each file. A refused file answers 422 with the reason. The upload is kept 24 hours (unused uploads are then deleted). After upload, pass the upload_token as a media item to create-post-tool, create-posts-tool or update-post-tool, or call attach-media-from-upload-tool(post_id, upload_token), before it expires. A token is spent by the first post that takes it; to put the same file on another post, pass that post\'s media id instead. A file already online can skip the upload: pass its url as a media item or call attach-media-from-url-tool.')]
class RequestMediaUploadTool extends Tool
{
    use AuthorizesMcpTool;

    public function handle(Request $request): Response|ResponseFactory
    {
        $workspace = $this->authorizeCurrentWorkspace($request, 'createPost');

        if (! $workspace instanceof Workspace) {
            return $workspace;
        }

        $workspaceId = $workspace->id;

        $ttlMinutes = (int) config('trypost.media.signed_upload_url_ttl_minutes');

        $token = (string) Str::uuid();
        $expiresAt = CarbonImmutable::now()->addMinutes($ttlMinutes);

        $uploadUrl = URL::temporarySignedRoute(
            'api.uploads.store',
            $expiresAt,
            ['token' => $token, 'workspace_id' => $workspaceId],
        );

        return Response::structured([
            'upload_token' => $token,
            'upload_url' => $uploadUrl,
            'expires_at' => $expiresAt->toIso8601String(),
            'max_bytes' => MediaType::Video->maxSizeInBytes(),
            'max_bytes_by_type' => [
                'image' => MediaType::Image->maxSizeInBytes(),
                'video' => MediaType::Video->maxSizeInBytes(),
                'document' => MediaType::Document->maxSizeInBytes(),
            ],
            'field_name' => 'media',
        ]);
    }

    public function schema(JsonSchema $schema): array
    {
        return [];
    }
}