scribase-mcp-php/examples/symfony-ai-agent-demo.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

122 lines
5.5 KiB
PHP

#!/usr/bin/env php
<?php
declare(strict_types=1);
/*
* A real Symfony AI `Agent`, using this package's Scribase MCP server as its
* only toolbox, via `Symfony\AI\Agent\Bridge\Mcp\{ClientToolset,McpToolbox}` —
* the official MCP bridge shipped by `symfony/ai-mcp-tool`.
*
* This is the piece a plain MCP `tools/list`/`tools/call` smoke test (see
* `scripts/verify-live.php`) cannot show on its own: a real LLM, inside a
* real Symfony AI `Agent` loop, deciding on its own which Scribase tool to
* call and with which arguments, then using the real tool result to answer.
*
* Verification status, precisely:
* - Steps 1-2 below (`ClientToolset` + `McpToolbox` spawning this
* package's real MCP stdio server and listing its real tools over
* real MCP JSON-RPC) HAVE been run live — not just `class_exists()`,
* the actual bridge, the actual subprocess, a real
* `SCRIBASE_API_KEY` from a real temporary project
* (`scripts/toolbox-check.php` in this same directory; not part of
* the five package tools, kept only as that evidence). Confirmed:
* every tool is listed, named `scribase_scribase_*` — doubled,
* because `McpToolbox`'s `$prefix` has no "no prefix" value; passing
* `''` (its own default) does not disable the automatic
* `<toolset-name>_` prefix, it *is* the sentinel for it (see
* `McpToolbox::getTools()`: `'' !== $this->prefix ? $this->prefix :
* $this->toolset->getName().'_'`). Harmless — the agent just calls
* the tool by whatever name it is actually listed under — but
* real, so it is disclosed rather than silently prettied up.
* - Step 3 (the `Agent` loop actually calling a tool and answering)
* has every constructor argument, named parameter and method call
* checked against the real installed `symfony/ai-agent`,
* `symfony/ai-platform`, `symfony/ai-mcp-tool` and `mcp/sdk` source
* (signatures match exactly) but has NOT itself been run end-to-end
* with a live LLM call — that needs a real OpenAI-compatible key,
* an owner-gated secret not configured in the environment this
* package was built in. Disclosed here rather than faked.
*
* Needs:
* - OPENAI_API_KEY — any OpenAI-compatible key (see above).
* - SCRIBASE_API_KEY — see README "Getting a real API key".
* - SCRIBASE_ORG / SCRIBASE_PROJECT — an existing project (or run
* `scripts/verify-live.php` first to provision a free temporary one).
*
* Run it:
* OPENAI_API_KEY=sk-... SCRIBASE_API_KEY=sbk_... \
* SCRIBASE_ORG=tmp-... SCRIBASE_PROJECT=my-app \
* php examples/symfony-ai-agent-demo.php
*/
require __DIR__.'/../vendor/autoload.php';
use Mcp\Client;
use Mcp\Client\Transport\StdioTransport;
use Symfony\AI\Agent\Agent;
use Symfony\AI\Agent\Bridge\Mcp\ClientToolset;
use Symfony\AI\Agent\Bridge\Mcp\McpToolbox;
use Symfony\AI\Platform\Bridge\OpenAi\Factory as OpenAiFactory;
use Symfony\AI\Platform\Platform;
$openAiKey = getenv('OPENAI_API_KEY') ?: null;
$scribaseKey = getenv('SCRIBASE_API_KEY') ?: null;
$org = getenv('SCRIBASE_ORG') ?: null;
$project = getenv('SCRIBASE_PROJECT') ?: null;
foreach (['OPENAI_API_KEY' => $openAiKey, 'SCRIBASE_API_KEY' => $scribaseKey, 'SCRIBASE_ORG' => $org, 'SCRIBASE_PROJECT' => $project] as $name => $value) {
if (!$value) {
fwrite(STDERR, "{$name} is not set. See this file's header comment for what's needed.\n");
exit(1);
}
}
// 1. The MCP toolset: this package's own stdio server, as a Symfony AI
// toolbox. Spawned as a real child process, same transport an agent
// host (Claude Desktop, the MCP Inspector, …) would use.
$toolset = new ClientToolset(
'scribase',
Client::builder()->setClientInfo('scribase-mcp-php-demo', '0.1.0')->build(),
new StdioTransport(
command: \PHP_BINARY,
args: [__DIR__.'/../bin/scribase-mcp-server'],
env: ['SCRIBASE_API_KEY' => $scribaseKey],
),
);
// `prefix: ''` is McpToolbox's own default (not an override — see the
// header comment above): every tool below is listed twice-prefixed,
// as `scribase_scribase_<tool>`, since this package's own MCP tool
// names already start with `scribase_` and McpToolbox adds the
// toolset's own name (`'scribase'`) in front of every remote tool name.
$toolbox = new McpToolbox($toolset);
// 2. The platform: any OpenAI-compatible provider works unchanged (the
// `symfony/ai-*-platform` family covers OpenAI, Gemini, Vertex AI,
// Ollama, Anthropic-via-open-responses, …) — OpenAI here only because
// it needs no base-URL override for a judge running this locally.
$platform = new Platform([OpenAiFactory::createProvider($openAiKey)]);
// 3. The agent: a real tool-calling loop, not a scripted call. The model
// decides on its own to call `scribase_scribase_query` (see the
// double-prefix note above), with whatever SQL it judges answers the
// question, then reads the real row back.
$agent = new Agent($platform, 'gpt-4o-mini', toolbox: $toolbox, name: 'scribase-demo-agent');
$prompt = \sprintf(
"You have Scribase database tools for organization '%s', project '%s', environment 'production'. ".
'Run a read-only query that reports the current Postgres server time and the database name, then tell me what it returned.',
$org,
$project,
);
$execution = $agent->call($prompt);
echo "--- tool calls the model made ---\n";
foreach ($execution->asToolCalls() as $call) {
echo $call->getName(), ' ', json_encode($call->getArguments()), "\n";
}
echo "\n--- final answer ---\n";
echo $execution->asText(), "\n";