Skip to content

Beautiful Open-Source Form Builder

2.6 KB
Raw Blame
<?php

namespace App\Mcp\Tools;

use App\Service\Forms\AgentFormDraftService;
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\Attributes\Name;
use Laravel\Mcp\Server\Tools\Annotations\IsDestructive;
use Laravel\Mcp\Server\Tools\Annotations\IsOpenWorld;
use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly;

#[Name('open_form_draft_in_editor')]
#[Description('Create an OpnForm editor link only after the user chooses to continue editing the guest draft in OpnForm. Pass expected_version from the displayed preview to pin the link; it can be reused while that version remains current and the seven-day draft has not expired. Restore an old version before opening it.')]
#[IsReadOnly(false)]
#[IsDestructive(false)]
#[IsOpenWorld(false)]
class OpenFormDraftInEditorTool extends GuestDraftMcpTool
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        $tool = parent::toArray();

        return [
            ...$tool,
            '_meta' => [
                ...($tool['_meta'] ?? []),
                'ui' => ['visibility' => ['model', 'app']],
            ],
        ];
    }

    public function handle(Request $request, AgentFormDraftService $drafts): ResponseFactory
    {
        $validated = $request->validate([
            'draft_handle' => ['required', 'string', 'size:43'],
            'expected_version' => ['sometimes', 'integer', 'min:1'],
        ]);

        $handoff = $drafts->issueEditorHandoff($validated['draft_handle'], $validated['expected_version'] ?? null);

        return Response::structured([
            'editor_handoff_ready' => true,
            'expires_at' => $handoff['expires_at'],
        ])->withMeta('editor_url', $handoff['editor_url']);
    }

    public function schema(JsonSchema $schema): array
    {
        return [
            'expected_version' => $schema->integer()->min(1)->description('Version displayed in the preview. If it is no longer current, restore that version first instead of opening a different version.'),
            'draft_handle' => $schema->string()
                ->description('Opaque reference returned by create_form_draft. Pass it unchanged.')
                ->min(43)
                ->max(43)
                ->required(),
        ];
    }

    public function outputSchema(JsonSchema $schema): array
    {
        return [
            'editor_handoff_ready' => $schema->boolean()->required(),
            'expires_at' => $schema->string()->format('date-time')->required(),
        ];
    }
}