scribase-mcp-php/scripts/toolbox-check.php
caelum0x 819d99cec3 fix: scribase_memory_task was fully broken — lint-invisible policy, permissive-policy anti-pattern, silently-ignored destructive-confirm gate
Three real bugs found live against production api.scribase.com (`scripts/verify-live.php`), all blocking `scribase_memory_task` end-to-end:

1. `ensureMemoryTable()` hid `CREATE POLICY` inside a `DO $$ ... $$` block for idempotency; Scribase's own migration linter only scans top-level statements for `ENABLE ROW LEVEL SECURITY` + `CREATE POLICY`, so every bootstrap tripped `missing_rls` and `store` failed with `sql_blocked_by_lint` before a single memory was ever written.
2. Making the policy literal then tripped the linter's separate `permissive_policy` rule on `USING (true) WITH CHECK (true)` — correctly, since that predicate would authorise Supabase's `anon`/`authenticated` roles too if this table were ever exposed via PostgREST. Fixed by denying those two roles by name instead.
3. A `DROP POLICY IF EXISTS` (added for idempotency) and `memoryForget()`'s plain `DELETE` are both classified destructive by `database/execute` and silently no-op with `{"status":"confirmation_required"}`, HTTP 200, no `error` key — this client checked only the HTTP status, so `forget` always reported `not_found` while deleting nothing. Fixed: `ensureMemoryTable()` now does a cheap read-only existence check and only ever runs its one-time bootstrap script once (no `DROP` needed at all); `memoryForget()` now sends `confirm: true` (safe — its `DELETE` is already scoped to one caller-supplied id + the current scope, exactly the explicit single-purpose action `confirm` exists for). Both paths also now check for `confirmation_required` explicitly instead of inferring success from HTTP status alone.

Full 9-step `scripts/verify-live.php` suite (every tool, store/recall/search/isolation/forget, a real copy-on-write branch) now passes end-to-end against a fresh production temporary project — re-confirmed on two separate runs.

Also added `scripts/toolbox-check.php`: a live, zero-setup verification that the official Symfony AI MCP bridge (`ClientToolset`+`McpToolbox`) really spawns this package's MCP server and lists its real tools — closing half of the previous `class_exists()`-only verification gap on `examples/symfony-ai-agent-demo.php`. It also surfaces a real, confirmed-live naming quirk: `McpToolbox`'s `prefix: ''` is its own default sentinel for `<toolset-name>_`, not a "no prefix" override, so tools list as `scribase_scribase_<tool>`. The other half — the Agent loop actually calling a tool via a live LLM — still needs an owner-gated `OPENAI_API_KEY` not available in this environment; every constructor/method call in that demo was instead checked against the real installed symfony/ai-* source (exact signature matches, not just class_exists()), and this is disclosed plainly in the script and README rather than faked.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-17 15:46:40 +03:00

131 lines
5.1 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
declare(strict_types=1);
/*
* Real, live verification of the *other* half of
* `examples/symfony-ai-agent-demo.php` — the half that needs no paid LLM
* key to prove: `Symfony\AI\Agent\Bridge\Mcp\{ClientToolset,McpToolbox}`,
* the official `symfony/ai-mcp-tool` bridge, spawning this package's own
* MCP stdio server as a real child process and listing its real tools
* over real MCP JSON-RPC — not `class_exists()`, an actual run.
*
* It also surfaces a real, confirmed-live naming quirk: `McpToolbox`'s
* `$prefix` parameter has no "disable prefixing" value — passing `''`
* (its own default) is the *sentinel* for "use `<toolset-name>_`", not an
* override — so every tool below is listed as `scribase_scribase_<tool>`,
* doubled, since this package's own MCP tool names already start with
* `scribase_`. Harmless (the agent calls whatever name is actually
* listed), but real, hence checked here rather than assumed.
*
* Run it:
* php scripts/toolbox-check.php
* SCRIBASE_API_KEY=sbk_... php scripts/toolbox-check.php # skip provisioning
*/
require __DIR__.'/../vendor/autoload.php';
use Mcp\Client;
use Mcp\Client\Transport\StdioTransport;
use Symfony\AI\Agent\Bridge\Mcp\ClientToolset;
use Symfony\AI\Agent\Bridge\Mcp\McpToolbox;
use Symfony\Component\HttpClient\HttpClient;
const API_BASE = 'https://api.scribase.com';
const UA = 'scribase-mcp-php/0.1.0 (+https://github.com/caelum0x/scribase-mcp-php)';
function solvePow(string $challenge, int $bits): int
{
$nonce = 0;
$prefixZeroBytes = intdiv($bits, 8);
$remainderBits = $bits % 8;
while (true) {
$hash = hash('sha256', "{$challenge}:{$nonce}", true);
$ok = true;
for ($i = 0; $i < $prefixZeroBytes; ++$i) {
if ("\x00" !== $hash[$i]) {
$ok = false;
break;
}
}
if ($ok && $remainderBits > 0 && (\ord($hash[$prefixZeroBytes]) >> (8 - $remainderBits)) !== 0) {
$ok = false;
}
if ($ok) {
return $nonce;
}
++$nonce;
}
}
$scribaseKey = getenv('SCRIBASE_API_KEY') ?: null;
if (null === $scribaseKey) {
echo "No SCRIBASE_API_KEY set — provisioning a real, free, no-login temporary project.\n";
$http = HttpClient::create();
$challengeResponse = $http->request('POST', API_BASE.'/v1/temporary-projects/challenge', [
'headers' => ['User-Agent' => UA, 'Accept' => 'application/json'],
]);
$challengeBody = $challengeResponse->toArray(false);
$nonce = solvePow($challengeBody['challenge'], (int) $challengeBody['bits']);
$createResponse = $http->request('POST', API_BASE.'/v1/temporary-projects', [
'headers' => ['User-Agent' => UA, 'Accept' => 'application/json', 'Content-Type' => 'application/json'],
'json' => ['name' => 'scribase-mcp-php-toolbox-check', 'challenge' => $challengeBody['challenge'], 'nonce' => (string) $nonce],
]);
if (201 !== $createResponse->getStatusCode()) {
fwrite(STDERR, "Provisioning failed (HTTP {$createResponse->getStatusCode()}): {$createResponse->getContent(false)}\n");
fwrite(STDERR, "If this is a rate limit, run `php scripts/verify-live.php` instead (it has the throwaway-account fallback), then re-run this script with its SCRIBASE_API_KEY.\n");
exit(1);
}
$created = $createResponse->toArray(false);
$tempId = $created['id'];
$agentToken = $created['agent_token'];
echo "Polling until the environment is ready...\n";
$ready = false;
for ($i = 0; $i < 40; ++$i) {
sleep(5);
$statusResponse = $http->request('GET', API_BASE."/v1/temporary-projects/{$tempId}", [
'headers' => ['User-Agent' => UA, 'Accept' => 'application/json', 'Authorization' => "Bearer {$agentToken}"],
]);
$status = $statusResponse->toArray(false)['status'] ?? null;
if ('ready' === $status) {
$ready = true;
break;
}
if ('failed' === $status) {
break;
}
}
if (!$ready) {
fwrite(STDERR, "Temporary project never reached status=ready.\n");
exit(1);
}
$scribaseKey = $agentToken;
echo "Provisioned. Temporary project self-deletes in 72h if never claimed.\n";
}
echo "\n--- spawning the real MCP stdio server and listing its real tools, via the official Symfony AI MCP bridge ---\n";
$toolset = new ClientToolset(
'scribase',
Client::builder()->setClientInfo('scribase-mcp-php-toolbox-check', '0.1.0')->build(),
new StdioTransport(
command: \PHP_BINARY,
args: [__DIR__.'/../bin/scribase-mcp-server'],
env: ['SCRIBASE_API_KEY' => $scribaseKey],
),
);
$toolbox = new McpToolbox($toolset);
$tools = $toolbox->getTools();
if ([] === $tools) {
fwrite(STDERR, "McpToolbox listed zero tools — the stdio server did not start or did not respond.\n");
exit(1);
}
foreach ($tools as $tool) {
echo $tool->getName(), "\n ", str_replace("\n", "\n ", trim($tool->getDescription())), "\n\n";
}
echo \count($tools)." real tool(s) listed — confirms the bridge, the subprocess, and this package's MCP server all speak to each other correctly.\n";