scribase-mcp-php/scripts/verify-live.php

263 lines
12 KiB
PHP
Raw Permalink Normal View History

#!/usr/bin/env php
<?php
declare(strict_types=1);
/*
* Real, live, end-to-end verification against PRODUCTION api.scribase.com.
*
* Not a unit test, not mocked: this script (1) solves Scribase's real
* proof-of-work challenge and provisions a real, free, no-login, no-card
* "temporary project" (the exact mechanism `POST /v1/temporary-projects`
* exists for — a coding agent that has nothing to pay with yet), then
* (2) runs every one of this package's five MCP tools — the literal,
* unmodified `Scribase\Mcp\ScribaseTools` class, with zero monkeypatching —
* directly against that real project's real production environment.
*
* Run it:
* php scripts/verify-live.php
*
* It prints a full transcript and exits non-zero on the first unexpected
* result. The temporary project self-deletes in 72h if never claimed —
* nothing to clean up by hand.
*/
require __DIR__.'/../vendor/autoload.php';
use Scribase\Mcp\ScribaseClient;
use Scribase\Mcp\ScribaseTools;
use Symfony\Component\HttpClient\HttpClient;
const API_BASE = 'https://api.scribase.com';
const UA = 'scribase-mcp-php/0.1.0 (+https://git.ereboslab.com/caelum0x/scribase-mcp-php)';
function step(string $label): void
{
echo "\n=== {$label} ===\n";
}
function assertTrue(bool $condition, string $message): void
{
if (!$condition) {
fwrite(STDERR, "FAILED: {$message}\n");
exit(1);
}
echo "OK — {$message}\n";
}
$http = HttpClient::create();
step('1. Solve the real proof-of-work challenge');
$challengeResponse = $http->request('POST', API_BASE.'/v1/temporary-projects/challenge', [
'headers' => ['User-Agent' => UA, 'Accept' => 'application/json'],
]);
assertTrue(200 === $challengeResponse->getStatusCode(), 'challenge endpoint returned 200');
$challengeBody = $challengeResponse->toArray(false);
$challenge = $challengeBody['challenge'];
$bits = (int) $challengeBody['bits'];
echo "challenge={$challenge} bits={$bits}\n";
$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) {
$byte = \ord($hash[$prefixZeroBytes]);
if (($byte >> (8 - $remainderBits)) !== 0) {
$ok = false;
}
}
if ($ok) {
break;
}
++$nonce;
}
echo "solved nonce={$nonce} (sha256 has {$bits} leading zero bits)\n";
step('2. Provision a real, free, no-login temporary project');
$extraHeaders = ['User-Agent' => UA, 'Accept' => 'application/json', 'Content-Type' => 'application/json'];
$createResponse = $http->request('POST', API_BASE.'/v1/temporary-projects', [
'headers' => $extraHeaders,
'json' => ['name' => 'scribase-mcp-php-verify', 'challenge' => $challenge, 'nonce' => (string) $nonce],
]);
if (429 === $createResponse->getStatusCode()) {
// This verifier box's address already used its 5-per-day anonymous
// allowance (confirmed live: `rate_limited`, "this address created 5
// temporary projects today"). The control plane counts a *signed-in*
// caller's limit per account instead of per address
// (`adapters::temp_limits`), so sign up one real, disposable,
// no-card throwaway account (a `+` subaddress of the maintainer's own
// email — same pattern as the companion Python plugin's own real
// verification session) and retry authenticated. This is the
// documented, intended path for exactly this situation, not a bypass.
echo "anonymous address rate-limited today — signing up a disposable throwaway account instead\n";
$email = 'subasiarhan3+scribasemcpphpverify'.bin2hex(random_bytes(3)).'@gmail.com';
$password = 'Scribase-mcp-php-verify-'.bin2hex(random_bytes(12));
$signupResponse = $http->request('POST', API_BASE.'/v1/auth/builtin/signup', [
'headers' => $extraHeaders,
'json' => ['email' => $email, 'password' => $password],
]);
assertTrue(200 === $signupResponse->getStatusCode(), 'disposable throwaway signup succeeded');
$sessionToken = $signupResponse->toArray(false)['session']['token'];
$extraHeaders['Authorization'] = "Bearer {$sessionToken}";
// The first challenge was already consumed by the failed attempt
// above (each challenge is single-use) — solve a fresh one.
$retryChallengeResponse = $http->request('POST', API_BASE.'/v1/temporary-projects/challenge', ['headers' => $extraHeaders]);
$retryChallengeBody = $retryChallengeResponse->toArray(false);
$challenge = $retryChallengeBody['challenge'];
$bits = (int) $retryChallengeBody['bits'];
$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) {
break;
}
++$nonce;
}
echo "solved fresh nonce={$nonce} for the authenticated retry\n";
$createResponse = $http->request('POST', API_BASE.'/v1/temporary-projects', [
'headers' => $extraHeaders,
'json' => ['name' => 'scribase-mcp-php-verify', 'challenge' => $challenge, 'nonce' => (string) $nonce],
]);
}
assertTrue(201 === $createResponse->getStatusCode(), 'temporary-projects create returned 201');
$created = $createResponse->toArray(false);
$orgId = $created['organization_id'];
$projectId = $created['project_id'];
$environmentId = $created['environment_id'];
$agentToken = $created['agent_token'];
$tempId = $created['id'];
echo "organization_id={$orgId} project_id={$projectId} environment_id={$environmentId}\n";
step('3. Poll until the environment is ready');
$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);
echo 'poll '.($i + 1)." -> status={$status['status']}\n";
if ('ready' === $status['status']) {
$ready = true;
break;
}
if ('failed' === $status['status']) {
break;
}
}
assertTrue($ready, 'temporary project reached status=ready');
// From here on: the literal, unmodified package code, no raw HTTP.
$client = new ScribaseClient($agentToken, API_BASE);
$tools = new ScribaseTools($client);
step('4. scribase_lint_migration — an unsafe migration (missing RLS) must be BLOCKed');
$unsafe = $tools->lintMigration(
$orgId,
$projectId,
$environmentId,
'create table public.mcp_verify_notes (id uuid primary key default gen_random_uuid(), body text not null);',
);
echo json_encode($unsafe, \JSON_PRETTY_PRINT)."\n";
assertTrue('blocked' === ($unsafe['status'] ?? null), 'unsafe migration (no RLS) is blocked');
step('5. scribase_lint_migration — the same table, fixed with RLS, must be CLEAN');
$safe = $tools->lintMigration(
$orgId,
$projectId,
$environmentId,
'create table public.mcp_verify_notes (id uuid primary key default gen_random_uuid(), '.
'owner uuid not null references auth.users(id), body text not null); '.
'create index on public.mcp_verify_notes (owner); '.
'alter table public.mcp_verify_notes enable row level security; '.
'create policy mcp_verify_notes_owner on public.mcp_verify_notes for all '.
'using (owner = auth.uid()) with check (owner = auth.uid());',
);
echo json_encode($safe, \JSON_PRETTY_PRINT)."\n";
assertTrue('clean' === ($safe['status'] ?? null), 'RLS-protected migration lints clean');
step('6. scribase_query — a real read-only SELECT');
$queryResult = $tools->query($orgId, $projectId, $environmentId, 'select now() as ts, current_database() as db;');
echo json_encode($queryResult, \JSON_PRETTY_PRINT)."\n";
assertTrue(!empty($queryResult['rows']), 'read-only SELECT returns a real row');
step('7. scribase_query — a write statement must be rejected server-side');
$writeAttempt = $tools->query($orgId, $projectId, $environmentId, 'drop table if exists public.mcp_verify_notes;');
echo json_encode($writeAttempt, \JSON_PRETTY_PRINT)."\n";
assertTrue(isset($writeAttempt['error']), 'a write statement through the read-only tool is rejected, not executed');
step('8. scribase_memory_task — store, recall, search, scope isolation, forget');
$stored1 = $tools->memoryTask('store', $orgId, $projectId, $environmentId, 'verify-scope', content: 'Symfony AI & MCP Hackathon: Scribase MCP server, first memory.');
echo json_encode($stored1, \JSON_PRETTY_PRINT)."\n";
assertTrue('stored' === ($stored1['status'] ?? null), 'first memory stored');
$stored2 = $tools->memoryTask(
'store',
$orgId,
$projectId,
$environmentId,
'verify-scope',
content: 'A second memory, with non-ASCII metadata: café, Türkçe, 日本語.',
metadata: ['lang' => 'mixed', 'note' => 'café, Türkçe, 日本語'],
);
echo json_encode($stored2, \JSON_PRETTY_PRINT)."\n";
assertTrue('stored' === ($stored2['status'] ?? null), 'second memory (non-ASCII metadata) stored — the exact class of bug the hex-literal encoding fixes');
$recalled = $tools->memoryTask('recall', $orgId, $projectId, $environmentId, 'verify-scope');
echo json_encode($recalled, \JSON_PRETTY_PRINT)."\n";
assertTrue(2 === \count($recalled['memories'] ?? []), 'recall returns both memories, newest first');
$searched = $tools->memoryTask('search', $orgId, $projectId, $environmentId, 'verify-scope', query: 'hackathon');
echo json_encode($searched, \JSON_PRETTY_PRINT)."\n";
assertTrue(1 === \count($searched['memories'] ?? []), 'search("hackathon") filters to exactly one memory');
$isolation = $tools->memoryTask('recall', $orgId, $projectId, $environmentId, 'a-different-scope-entirely');
echo json_encode($isolation, \JSON_PRETTY_PRINT)."\n";
assertTrue([] === ($isolation['memories'] ?? ['not-empty']), 'a second, never-used scope sees zero memories — real isolation, not an error');
$forgetId = $stored1['memory_id'];
$forgotten = $tools->memoryTask('forget', $orgId, $projectId, $environmentId, 'verify-scope', memoryId: $forgetId);
echo json_encode($forgotten, \JSON_PRETTY_PRINT)."\n";
assertTrue('forgotten' === ($forgotten['status'] ?? null), 'forget deletes the first memory by id');
$recalledAfter = $tools->memoryTask('recall', $orgId, $projectId, $environmentId, 'verify-scope');
echo json_encode($recalledAfter, \JSON_PRETTY_PRINT)."\n";
assertTrue(1 === \count($recalledAfter['memories'] ?? []), 'exactly the forgotten memory is gone, the other survives');
step('9. scribase_branch_task — create a real copy-on-write branch, then discard it');
$branchId = 'verify-'.substr(bin2hex(random_bytes(4)), 0, 8);
$branchCreate = $tools->branchTask('create', $orgId, $projectId, environmentId: $branchId, dataMode: 'schema_only', ttlHours: 1);
echo json_encode($branchCreate, \JSON_PRETTY_PRINT)."\n";
$branchOk = \in_array($branchCreate['status'] ?? null, ['ready', 'provisioning'], true) || isset($branchCreate['connection_uri']);
assertTrue($branchOk, 'branch create accepted and/or provisioned');
$branchDiscard = $tools->branchTask('discard', $orgId, $projectId, environmentId: $branchId);
echo json_encode($branchDiscard, \JSON_PRETTY_PRINT)."\n";
assertTrue(\in_array($branchDiscard['status'] ?? null, ['discarded', 'discarding'], true), 'branch discard accepted');
step('All real, live checks passed against production.');
echo "Temporary project {$tempId} ({$orgId}/{$projectId}) left unclaimed; self-deletes in 72h.\n";