trypost/app/Mcp/Tools/Post/ListPostsTool.php
Jamie Ontiveros 6e10e394a9
Use whereLike for search so MySQL works alongside PostgreSQL (#302)
Seven search call sites used the `ilike` operator, which only PostgreSQL
understands. On MySQL they raise a syntax error, so post, asset, label,
signature and workspace-member search — plus the MCP list-posts tool —
were unusable on an engine `config/database.php` has always supported and
the docs advertise.

Replace them with `whereLike($column, $value)`, which the query grammars
translate per driver: PostgresGrammar emits `ilike` and MySqlGrammar emits
`like`. The generated SQL on PostgreSQL is therefore unchanged.

Verified by running the full suite on both engines:

  PostgreSQL 16    3888 passed, 0 failed
  MySQL 8.0.46     one pre-existing failure fixed, none introduced

Also adds case-insensitivity assertions to the five affected suites that
lacked them, and search coverage for ListPostsTool, which had none.

Note for MySQL installs: `like` is case-insensitive by virtue of the
column collation, not the operator. Under the default `utf8mb4_unicode_ci`
it is also accent-insensitive, so a search for "cafe" matches a stored
"café" — PostgreSQL's `ilike` does not. That difference comes from the
collation rather than this change. A `_bin` or `_cs` collation would make
search case-sensitive on both.

Co-authored-by: Paulo Castellano <paulo@castellanos.llc>
2026-08-26 10:59:31 -03:00

70 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Mcp\Tools\Post;
use App\Enums\Post\Status;
use App\Http\Resources\Api\PostResource;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\Validation\Rule;
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('List posts for the current workspace, ordered by scheduled date (newest first). Optional filters: status (draft|scheduled|published) and search (matches against post content).')]
class ListPostsTool extends Tool
{
public function handle(Request $request): ResponseFactory
{
$validated = $request->validate([
'status' => ['sometimes', 'string', Rule::in([
Status::Draft->value,
Status::Scheduled->value,
Status::Published->value,
Status::Failed->value,
])],
'search' => ['sometimes', 'string', 'max:255'],
'limit' => ['sometimes', 'integer', 'min:1', 'max:100'],
]);
$query = $request->user()->currentWorkspace
->posts()
->with(['postPlatforms.socialAccount', 'labels']);
$query = match (data_get($validated, 'status')) {
Status::Draft->value => $query->draft(),
Status::Scheduled->value => $query->scheduled(),
Status::Published->value => $query->published(),
Status::Failed->value => $query->failed(),
default => $query,
};
if ($search = data_get($validated, 'search')) {
$query->whereLike('content', '%'.$search.'%');
}
$posts = $query->latest('scheduled_at')
->limit((int) data_get($validated, 'limit', 50))
->get();
return Response::structured([
'posts' => PostResource::collection($posts)->resolve(),
]);
}
public function schema(JsonSchema $schema): array
{
return [
'status' => $schema->string()
->enum(['draft', 'scheduled', 'published', 'failed'])
->description('Filter by status. "published" includes partially-published posts.'),
'search' => $schema->string()->description('Case-insensitive substring match against the post content.'),
'limit' => $schema->integer()->description('Max results (1-100, default 50).'),
];
}
}