diff --git a/.github/workflows/extra-integration.yml b/.github/workflows/extra-integration.yml new file mode 100644 index 0000000..5ba486e --- /dev/null +++ b/.github/workflows/extra-integration.yml @@ -0,0 +1,197 @@ +name: Extra MCP integration + +on: + workflow_call: + inputs: + evo_ref: + type: string + required: false + default: 3.5.x + emcp_ref: + type: string + required: false + default: main + php_version: + type: string + required: false + default: '8.4' + server_handle: + type: string + required: false + default: content + expected_tools: + description: Tool names (space or newline separated) the Extra contributes; each must be listed by tools/list + type: string + required: false + default: '' + +permissions: + contents: read + +jobs: + integration: + runs-on: ubuntu-latest + steps: + - name: Checkout calling Extra + uses: actions/checkout@v4 + with: + path: extra + + - name: Checkout eMCP + uses: actions/checkout@v4 + with: + repository: evolution-cms/evo-mcp + ref: ${{ inputs.emcp_ref }} + path: emcp + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ inputs.php_version }} + extensions: sqlite3, pdo_sqlite, mbstring, xml, curl, gd, zip, intl + tools: composer:v2 + coverage: none + + # A CMS build pulls a few hundred dist archives from api.github.com, which + # rate-limits anonymous callers; the run's own token lifts that. + - name: Authenticate Composer against GitHub + env: + GH_TOKEN: ${{ github.token }} + run: composer config --global --auth github-oauth.github.com "$GH_TOKEN" + + - name: Download Evolution CMS + env: + EVO_REF: ${{ inputs.evo_ref }} + run: git clone --depth 1 --branch "$EVO_REF" https://github.com/evolution-cms/evolution.git evo + + - name: Install Evolution CMS dependencies + working-directory: evo/core + env: + COMPOSER_ALLOW_SUPERUSER: '1' + run: composer install --no-dev --no-interaction --prefer-dist --no-progress + + # A clean CMS first, then the packages, as on a real site: with eMCP (and its sTask) + # already in vendor, the CLI installer runs their migrations without recording them, + # and the later migrate stops at "table already exists". + - name: Install test Evolution CMS using SQLite + working-directory: evo/install + run: | + php cli-install.php \ + --typeInstall=1 \ + --databaseType=sqlite \ + --database=evolution \ + --tablePrefix=evo_ \ + --cmsAdmin=admin \ + --cmsAdminEmail=admin@example.com \ + --cmsPassword=IntegrationOnly123 \ + --language=en \ + --removeInstall=y \ + --skipComposer=y < /dev/null + + - name: Add local Extra and eMCP as Composer path repositories + env: + EXTRA_PATH: ${{ github.workspace }}/extra + EMCP_PATH: ${{ github.workspace }}/emcp + EVO_PATH: ${{ github.workspace }}/evo + run: | + php <<'PHP' + 'evolutioncms/custom', + 'minimum-stability' => 'dev', + 'prefer-stable' => true, + 'repositories' => [ + ['type' => 'path', 'url' => $relative($extraPath), 'options' => ['symlink' => true]], + ['type' => 'path', 'url' => $relative($emcpPath), 'options' => ['symlink' => true]], + ], + 'require' => [ + $package => '*@dev', + 'evolution-cms/emcp' => '*@dev', + ], + 'autoload' => ['psr-4' => new stdClass()], + ]; + $destination = $evoPath . '/core/custom/composer.json'; + if (!is_dir(dirname($destination))) { + mkdir(dirname($destination), 0775, true); + } + file_put_contents($destination, json_encode($custom, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) . "\n"); + file_put_contents(getenv('GITHUB_ENV'), 'EXTRA_PACKAGE=' . $package . "\n", FILE_APPEND); + PHP + + - name: Install Extra and eMCP dependencies + working-directory: evo/core + env: + COMPOSER_ALLOW_SUPERUSER: '1' + COMPOSER_MEMORY_LIMIT: '-1' + run: composer update --no-dev --no-interaction --prefer-dist --no-progress --with-all-dependencies "$EXTRA_PACKAGE" evolution-cms/emcp + + - name: Register packages and run migrations + working-directory: evo/core + shell: bash + run: | + rm -f storage/cache/env.php + php artisan package:discover + php artisan vendor:publish --provider='EvolutionCMS\eMCP\eMCPServiceProvider' --tag=emcp-config --force --no-interaction + php artisan vendor:publish --provider='EvolutionCMS\eMCP\eMCPServiceProvider' --tag=emcp-mcp-config --force --no-interaction + php artisan migrate --force --no-interaction + # artisan migrate exits 0 even when a migration fails, so check what is left. + php artisan migrate:status --no-interaction | tee "$RUNNER_TEMP/migrate-status.txt" + if grep -q 'Pending' "$RUNNER_TEMP/migrate-status.txt"; then + echo '::error::Migrations are still pending after migrate' + exit 1 + fi + + - name: Verify eMCP and Extra installation + working-directory: evo/core + run: | + composer show evolution-cms/emcp + composer show "$EXTRA_PACKAGE" + php artisan emcp:list-servers + + - name: Verify MCP initialize and tools/list + working-directory: evo/core + env: + SERVER_HANDLE: ${{ inputs.server_handle }} + EXPECTED_TOOLS: ${{ inputs.expected_tools }} + EVO_REF: ${{ inputs.evo_ref }} + EMCP_REF: ${{ inputs.emcp_ref }} + PHP_VERSION: ${{ inputs.php_version }} + # bash with pipefail, so a failing emcp:test is not hidden by tee. + shell: bash + run: | + args=(--server="$SERVER_HANDLE") + for tool in $EXPECTED_TOOLS; do + args+=(--expect-tool="$tool") + done + php artisan emcp:test "${args[@]}" | tee "$RUNNER_TEMP/emcp-test.log" + { + echo "### eMCP integration: $EXTRA_PACKAGE" + echo + echo "- Evolution CMS: \`$EVO_REF\`, eMCP: \`$EMCP_REF\`, PHP $PHP_VERSION" + echo "- Expected tools: ${EXPECTED_TOOLS:-none (protocol checks only)}" + echo + echo '```' + cat "$RUNNER_TEMP/emcp-test.log" + echo '```' + } >> "$GITHUB_STEP_SUMMARY" diff --git a/EXTRA_CI.md b/EXTRA_CI.md new file mode 100644 index 0000000..126e684 --- /dev/null +++ b/EXTRA_CI.md @@ -0,0 +1,29 @@ +# Reusable CI for Evolution CMS Extras + +The workflow `.github/workflows/extra-integration.yml` is reusable from third-party Extra repositories. It checks out the calling repository and eMCP, installs both as local Composer path repositories in a fresh Evolution CMS, runs migrations, and verifies MCP `initialize` and `tools/list` using `emcp:test`. + +Add a job to an Extra repository's GitHub Actions workflow: + +```yaml +jobs: + emcp: + uses: evolution-cms/evo-mcp/.github/workflows/extra-integration.yml@main + with: + evo_ref: 3.5.x + php_version: '8.4' + server_handle: content + # Optional: tools your Extra contributes; each must appear in tools/list. + expected_tools: >- + vendor.tool_one + vendor.tool_two +``` + +Inputs (all optional): `evo_ref` (Evolution CMS branch or tag, default `3.5.x`), `emcp_ref` (eMCP ref, default `main`), `php_version` (default `8.4`), `server_handle` (default `content`), `expected_tools` (space- or newline-separated tool names, default empty). + +The job, in order: installs a clean Evolution CMS on SQLite, adds the Extra and eMCP from the checked-out sources (as a site owner adding them later would), runs `package:discover`, publishes the eMCP config, runs all migrations and fails if any is still pending (`artisan migrate` itself exits 0 when one fails), shows both packages and the configured servers, then runs `emcp:test`. The run summary contains the `emcp:test` output, including every tool name `tools/list` returned (all pages). + +The caller must have a valid root `composer.json` declaring the Extra's Composer package name. The workflow tests the calling commit rather than a published Packagist version. It does not require a Claude Code plugin, and it does not install optional API credentials. + +`emcp:test` verifies MCP `initialize` and `tools/list` for the selected server, and with `expected_tools` it fails when any named tool is missing (`php artisan emcp:test --server=content --expect-tool=vendor.tool_one`). It does not call the tools: invoking them needs a manager identity and data the Extra must set up, so `tools/call` coverage remains the Extra's own test suite. Without `expected_tools` the workflow does not assert that the Extra contributes any tool. + +Pin the reusable workflow to a release tag or commit SHA for reproducible CI once a stable version is available. diff --git a/README.md b/README.md index ce36cc4..1d04c3e 100644 --- a/README.md +++ b/README.md @@ -307,6 +307,8 @@ toolset is package-owned and the registry will reject that external server. `php artisan emcp:test --server=` runs generic `initialize` and `tools/list` checks for third-party servers. When the selected class is the canonical eMCP `ContentServer`, it also verifies the required Evolution toolset. +Add `--expect-tool=` (repeatable) to fail when a tool an Extra contributes +is missing; [EXTRA_CI.md](EXTRA_CI.md) wires this into a reusable workflow. Use `php artisan emcp:list-servers` to see both accepted servers and concrete reasons why configured entries were rejected. diff --git a/src/Console/Commands/eMcpTestCommand.php b/src/Console/Commands/eMcpTestCommand.php index b5eff56..859e715 100644 --- a/src/Console/Commands/eMcpTestCommand.php +++ b/src/Console/Commands/eMcpTestCommand.php @@ -20,10 +20,15 @@ */ class eMcpTestCommand extends Command { - protected $signature = 'emcp:test {--server= : Test a specific server handle}'; + protected $signature = 'emcp:test + {--server= : Test a specific server handle} + {--expect-tool=* : Tool name that must be listed by tools/list (repeatable)}'; protected $description = 'Run Gate A smoke checks for initialize/tools:list contract.'; + /** Guards against a server that never stops returning nextCursor. */ + private const MAX_TOOL_PAGES = 100; + /** * Execute initialize and tools/list against the selected server handle. * @@ -108,56 +113,63 @@ public function handle(ServerRegistry $registry): int $this->info('initialize: OK'); - $toolsListPayload = [ - 'jsonrpc' => '2.0', - 'id' => 'tools-1', - 'method' => 'tools/list', - 'params' => (object)[], - ]; - - $toolsList = $this->dispatchJsonRpc($serverHandle, $toolsListPayload, $sessionId); - if (isset($toolsList['error'])) { - $this->error($toolsList['error']); - return self::FAILURE; - } - - $toolsListResponse = $toolsList['response'] ?? null; - if (!$toolsListResponse instanceof HttpResponse) { - $this->error('tools/list returned unexpected response type.'); - return self::FAILURE; - } + // tools/list is paginated (Laravel MCP sends 15 per page), so follow nextCursor. + $toolNames = []; + $cursor = null; + $page = 0; + do { + if (++$page > self::MAX_TOOL_PAGES) { + $this->error('tools/list kept returning nextCursor after ' . self::MAX_TOOL_PAGES . ' pages.'); + return self::FAILURE; + } - if ($toolsListResponse->getStatusCode() !== 200) { - $this->error('tools/list failed with HTTP ' . $toolsListResponse->getStatusCode()); - $this->line((string)$toolsListResponse->getContent()); - return self::FAILURE; - } + $toolsListPayload = [ + 'jsonrpc' => '2.0', + 'id' => 'tools-' . $page, + 'method' => 'tools/list', + 'params' => $cursor === null ? (object)[] : ['cursor' => $cursor], + ]; + + $toolsList = $this->dispatchJsonRpc($serverHandle, $toolsListPayload, $sessionId); + if (isset($toolsList['error'])) { + $this->error($toolsList['error']); + return self::FAILURE; + } - $toolsListJson = $this->decodeJson((string)$toolsListResponse->getContent()); - if ($toolsListJson === null) { - $this->error('tools/list returned invalid JSON payload.'); - return self::FAILURE; - } + $toolsListResponse = $toolsList['response'] ?? null; + if (!$toolsListResponse instanceof HttpResponse) { + $this->error('tools/list returned unexpected response type.'); + return self::FAILURE; + } - if (!isset($toolsListJson['result']['tools']) || !is_array($toolsListJson['result']['tools'])) { - $this->error('tools/list response does not contain result.tools array.'); - $this->line((string)$toolsListResponse->getContent()); - return self::FAILURE; - } + if ($toolsListResponse->getStatusCode() !== 200) { + $this->error('tools/list failed with HTTP ' . $toolsListResponse->getStatusCode()); + $this->line((string)$toolsListResponse->getContent()); + return self::FAILURE; + } - $toolNames = []; - foreach ($toolsListJson['result']['tools'] as $tool) { - if (!is_array($tool)) { - continue; + $toolsListJson = $this->decodeJson((string)$toolsListResponse->getContent()); + if ($toolsListJson === null) { + $this->error('tools/list returned invalid JSON payload.'); + return self::FAILURE; } - $name = trim((string)($tool['name'] ?? '')); - if ($name !== '') { - $toolNames[] = $name; + if (!isset($toolsListJson['result']['tools']) || !is_array($toolsListJson['result']['tools'])) { + $this->error('tools/list response does not contain result.tools array.'); + $this->line((string)$toolsListResponse->getContent()); + return self::FAILURE; } - } - $requiredTools = $this->requiredToolsForServer($serverClass); + $toolNames = array_merge($toolNames, self::toolNamesFromPage($toolsListJson['result']['tools'])); + $cursor = self::nextCursor($toolsListJson['result']); + } while ($cursor !== null); + + $toolNames = array_values(array_unique($toolNames)); + + sort($toolNames); + $this->line('tools/list returned ' . count($toolNames) . ' tools: ' . implode(', ', $toolNames)); + + $requiredTools = $this->requiredToolsForServer($serverClass, (array)$this->option('expect-tool')); $missingTools = array_values(array_diff($requiredTools, $toolNames)); if ($missingTools !== []) { @@ -171,23 +183,66 @@ public function handle(ServerRegistry $registry): int return self::SUCCESS; } + /** + * Tool names of one tools/list page; entries without a name are skipped. + * + * @param array $tools + * @return array + */ + private static function toolNamesFromPage(array $tools): array + { + $names = []; + foreach ($tools as $tool) { + if (!is_array($tool)) { + continue; + } + + $name = trim((string)($tool['name'] ?? '')); + if ($name !== '') { + $names[] = $name; + } + } + + return $names; + } + + /** + * @param array $result + */ + private static function nextCursor(array $result): ?string + { + $cursor = $result['nextCursor'] ?? null; + + return is_string($cursor) && $cursor !== '' ? $cursor : null; + } + /** * Return package-owned tools that must be present for a canonical server. * - * Third-party servers intentionally receive an empty requirement list so - * emcp:test remains a protocol smoke test for arbitrary extensions. + * Third-party servers intentionally receive no built-in requirements so + * emcp:test remains a protocol smoke test for arbitrary extensions; an + * Extra names its own tools through --expect-tool. * + * @param array $expected * @return array * * @since 1.1.0 */ - private function requiredToolsForServer(string $serverClass): array + private function requiredToolsForServer(string $serverClass, array $expected = []): array { + $extra = []; + foreach ($expected as $name) { + $name = is_string($name) ? trim($name) : ''; + if ($name !== '') { + $extra[] = $name; + } + } + if ($serverClass !== ContentServer::class) { - return []; + return array_values(array_unique($extra)); } - return [ + return array_values(array_unique(array_merge([ 'evo.content.search', 'evo.content.get', 'evo.content.root_tree', @@ -197,7 +252,7 @@ private function requiredToolsForServer(string $serverClass): array 'evo.content.siblings', 'evo.model.list', 'evo.model.get', - ]; + ], $extra))); } /** diff --git a/tests/Unit/eMcpTestCommandRequirementsTest.php b/tests/Unit/eMcpTestCommandRequirementsTest.php index f6f4caa..ef7ddf8 100644 --- a/tests/Unit/eMcpTestCommandRequirementsTest.php +++ b/tests/Unit/eMcpTestCommandRequirementsTest.php @@ -52,5 +52,42 @@ function assertRequirement(bool $condition, string $message): void 'Third-party servers must only receive generic initialize/tools:list checks' ); + /** @var array $expectedExtension */ + $expectedExtension = $method->invoke($command, 'Vendor\Project\PingServer', ['amount.mounts', ' ', 'amount.mounts', 7]); + assertRequirement( + $expectedExtension === ['amount.mounts'], + 'Third-party servers must require exactly the tools named by --expect-tool' + ); + + /** @var array $expectedCore */ + $expectedCore = $method->invoke($command, ContentServer::class, ['amount.mounts', 'evo.content.get']); + assertRequirement( + in_array('amount.mounts', $expectedCore, true) && in_array('evo.content.search', $expectedCore, true), + 'ContentServer must require both its own toolset and the tools named by --expect-tool' + ); + assertRequirement( + count($expectedCore) === count(array_unique($expectedCore)), + 'Required tools must not repeat' + ); + + // tools/list pages: every named tool is kept, the cursor ends the loop when absent or empty. + $names = (new ReflectionMethod($command, 'toolNamesFromPage'))->invoke(null, [ + ['name' => 'amount.mounts'], ['name' => ' '], 'junk', ['title' => 'no name'], ['name' => 'evo.model.get'], + ]); + assertRequirement($names === ['amount.mounts', 'evo.model.get'], 'A page must yield exactly its named tools'); + + $next = new ReflectionMethod($command, 'nextCursor'); + assertRequirement($next->invoke(null, ['nextCursor' => 'abc']) === 'abc', 'nextCursor must be followed'); + assertRequirement($next->invoke(null, ['nextCursor' => '']) === null, 'An empty nextCursor ends the listing'); + assertRequirement($next->invoke(null, ['tools' => []]) === null, 'A missing nextCursor ends the listing'); + assertRequirement($next->invoke(null, ['nextCursor' => 5]) === null, 'A non-string nextCursor ends the listing'); + + // extra-integration.yml passes these options; renaming one breaks every Extra's CI. + $signature = (string)(new ReflectionProperty($command, 'signature'))->getValue($command); + assertRequirement( + str_contains($signature, '{--expect-tool=*'), + 'emcp:test must keep the --expect-tool option used by the reusable Extra workflow' + ); + echo "eMCP test command requirement checks passed.\n"; }