trypost/app/Mcp/Tools/Asset/GetAssetTool.php
Paulo Castellano eb2b345163
feat: Asset Library list, preview, and attach via API and MCP (#282)
* feat: list, preview, and attach Asset Library media via API and MCP

Let API and MCP clients reuse workspace assets instead of re-uploading, sharing the same scoped query, signed preview, and idempotent attach path.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Align Asset Library API and MCP with main media patterns.

Drop the signed-preview stack, return Storage URLs and PostResource like existing attach flows, and query medias by morph owner instead of getMedia().

Co-authored-by: Cursor <cursoragent@cursor.com>

* Paginate workspace assets with the app default page size.

Keep list pagination in the action via config('app.pagination.default') instead of a hardcoded API page size.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Move asset API and MCP input rules into FormRequests.

Keep controllers and tools free of inline field validation; MCP tools reuse the request rule definitions.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Document asset MCP tools with explicit parameters and constraints.

Spell out workspace scope, return fields, sibling tools, and rejection cases so agents can call list/get/attach without guessing.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Harden asset attach against races and keep library metadata on the post.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Relock the library asset on attach so a deleted file cannot land on the post.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Document that omitting alt on attach keeps the library alt text.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-16 17:08:20 -03:00

55 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Mcp\Tools\Asset;
use App\Actions\Media\FindWorkspaceAsset;
use App\Http\Requests\Mcp\Asset\GetAssetRequest;
use App\Http\Resources\Api\AssetResource;
use App\Mcp\Concerns\AuthorizesMcpTool;
use App\Models\Workspace;
use Illuminate\Contracts\JsonSchema\JsonSchema;
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\IsReadOnly;
#[IsReadOnly]
#[Description('Get one Asset Library item by id from the current workspace (the workspace bound to this MCP token). The id must belong to the workspace "assets" collection — logos, avatars, and other workspaces return "Asset not found." Returns id, original_filename, type (image|video|document), mime_type, size, url, meta, and created_at. Does not include the storage path. url is the stored public file URL, not a short-lived signed preview. Requires permission to create posts (viewers cannot). Use list-assets-tool to discover ids. To attach this item to a draft or scheduled post, call attach-existing-asset-tool with the same asset_id.')]
class GetAssetTool extends Tool
{
use AuthorizesMcpTool;
public function handle(Request $request): Response|ResponseFactory
{
$workspace = $this->authorizeCurrentWorkspace(
$request,
'createPost',
'Not authorized to view assets.',
);
if (! $workspace instanceof Workspace) {
return $workspace;
}
$validated = $request->validate((new GetAssetRequest)->rules());
$asset = FindWorkspaceAsset::execute($workspace, data_get($validated, 'asset_id'));
if (! $asset) {
return Response::error('Asset not found.');
}
return Response::structured((new AssetResource($asset))->resolve());
}
public function schema(JsonSchema $schema): array
{
return [
'asset_id' => $schema->string()->required()->description('Required UUID of an Asset Library media item in the current workspace. Same id returned by list-assets-tool. Wrong workspace, missing id, or a non-library collection (logo/avatar) fails with "Asset not found."'),
];
}
}