diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ffd0de..74f514f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,3 +30,30 @@ jobs: steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - run: npx --yes @anthropic-ai/claude-code plugin validate --profile directory . + + registry: + # The MCP bundle and the image that registry.yml publishes, with the phar of Psalm's master branch + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + - uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # v2 + with: + php-version: '8.5' + coverage: none + - name: Build and check the MCP bundle + run: | + mkdir -p build/mcpb/scripts + curl --fail --location --silent --show-error --output build/mcpb/psalm.phar \ + https://github.com/psalm/phar/raw/master/psalm.phar + cp scripts/server.php build/mcpb/scripts/ + cp LICENSE README.md build/mcpb/ + cp .claude-plugin/icon.png build/mcpb/icon.png + jq '.version = "0.0.1"' registry/manifest.json > build/mcpb/manifest.json + php registry/smoke.php php "$PWD/build/mcpb/scripts/server.php" mcp '{project}' + npx --yes @anthropic-ai/mcpb@2.1.2 validate build/mcpb/manifest.json + npx --yes @anthropic-ai/mcpb@2.1.2 pack build/mcpb build/psalm.mcpb + - name: Build and check the image + run: | + docker build --tag psalm-mcp:smoke --file registry/Dockerfile build/mcpb + php registry/smoke.php docker run -i --rm --mount 'type=bind,src={project},dst={project}' \ + --workdir '{project}' psalm-mcp:smoke diff --git a/.github/workflows/npm.yml b/.github/workflows/npm.yml new file mode 100644 index 0000000..5c5c5ad --- /dev/null +++ b/.github/workflows/npm.yml @@ -0,0 +1,30 @@ +name: npm + +# Publishes the OpenCode plugin (package.json) for each release tag, through npm trusted publishing + +on: + push: + tags: + - 'v*' + +permissions: + contents: read + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - uses: actions/setup-node@8fccf151e3ba8038c300f83b40c8fd1e94f9c5ce # v7 + with: + node-version: '24' + registry-url: https://registry.npmjs.org + + - name: Check the version + run: test "v$(jq -r .version package.json)" = "$GITHUB_REF_NAME" + + - run: npm publish --provenance --access public diff --git a/.github/workflows/registry.yml b/.github/workflows/registry.yml new file mode 100644 index 0000000..780de2a --- /dev/null +++ b/.github/workflows/registry.yml @@ -0,0 +1,159 @@ +name: MCP registry + +# Publishes the MCP server of each Psalm release to the MCP registry, as an MCPB bundle (a release asset of this +# repository) and as a container image, both with the launcher and the release's psalm.phar. + +on: + schedule: + - cron: '17 */6 * * *' + workflow_dispatch: + inputs: + psalm: + description: The Psalm release to publish (default the latest) + required: false + +permissions: + contents: read + +concurrency: + group: registry + +env: + SERVER_NAME: io.github.psalm/psalm + IMAGE: ghcr.io/psalm/psalm-mcp + +jobs: + check: + runs-on: ubuntu-latest + outputs: + version: ${{ steps.release.outputs.version }} + publish: ${{ steps.release.outputs.publish }} + steps: + - id: release + env: + GH_TOKEN: ${{ github.token }} + INPUT_VERSION: ${{ inputs.psalm }} + run: | + version="${INPUT_VERSION:-$(gh release view -R vimeo/psalm --json tagName --jq .tagName)}" + [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] + echo "version=$version" >> "$GITHUB_OUTPUT" + if ! gh api "repos/vimeo/psalm/contents/src/Psalm/Internal/Cli/Mcp.php?ref=$version" --silent 2>/dev/null; then + echo "::notice::Psalm $version has no MCP server" + echo "publish=false" >> "$GITHUB_OUTPUT" + elif curl --silent --fail --output /dev/null \ + "https://registry.modelcontextprotocol.io/v0/servers/${SERVER_NAME/\//%2F}/versions/$version"; then + echo "publish=false" >> "$GITHUB_OUTPUT" + else + echo "publish=true" >> "$GITHUB_OUTPUT" + fi + + publish: + needs: check + if: needs.check.outputs.publish == 'true' + runs-on: ubuntu-latest + permissions: + contents: write + packages: write + id-token: write + env: + GH_TOKEN: ${{ github.token }} + VERSION: ${{ needs.check.outputs.version }} + MCPB_VERSION: 2.1.2 + MCP_PUBLISHER_VERSION: v1.8.21 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # v2 + with: + php-version: '8.5' + coverage: none + + - name: Download the phar + run: | + mkdir -p build/mcpb/scripts + gh release download "$VERSION" -R vimeo/psalm -p psalm.phar -D build/mcpb + expected=$(gh release view "$VERSION" -R vimeo/psalm --json assets \ + --jq '.assets[] | select(.name == "psalm.phar") | .digest') + test "sha256:$(sha256sum build/mcpb/psalm.phar | cut -d' ' -f1)" = "$expected" + + - name: Build the MCP bundle + run: | + cp scripts/server.php build/mcpb/scripts/ + cp LICENSE README.md build/mcpb/ + cp .claude-plugin/icon.png build/mcpb/icon.png + jq --arg v "$VERSION" '.version = $v' registry/manifest.json > build/mcpb/manifest.json + php registry/smoke.php php "$PWD/build/mcpb/scripts/server.php" mcp '{project}' + npx --yes "@anthropic-ai/mcpb@$MCPB_VERSION" validate build/mcpb/manifest.json + npx --yes "@anthropic-ai/mcpb@$MCPB_VERSION" pack build/mcpb "build/psalm-$VERSION.mcpb" + + - uses: docker/setup-qemu-action@99012661954931238ded8c8b007157a8430204e1 # v4 + - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4 + + - name: Build the image + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7 + with: + context: build/mcpb + file: registry/Dockerfile + load: true + tags: psalm-mcp:smoke + + - name: Check the image + run: | + php registry/smoke.php docker run -i --rm --mount 'type=bind,src={project},dst={project}' \ + --workdir '{project}' psalm-mcp:smoke + + - uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ github.token }} + + - name: Push the image + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7 + with: + context: build/mcpb + file: registry/Dockerfile + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ env.IMAGE }}:${{ env.VERSION }},${{ env.IMAGE }}:latest + + - name: Attach the bundle to a release + run: | + if gh release view "mcp-$VERSION" > /dev/null 2>&1; then + gh release upload "mcp-$VERSION" "build/psalm-$VERSION.mcpb" --clobber + else + gh release create "mcp-$VERSION" "build/psalm-$VERSION.mcpb" --target "$GITHUB_SHA" --latest=false \ + --title "Psalm $VERSION MCP server" \ + --notes "The MCP server of [Psalm $VERSION](https://github.com/vimeo/psalm/releases/tag/$VERSION) as an MCP bundle, and as the image \`$IMAGE:$VERSION\`." + fi + + - name: Write server.json + run: | + jq --arg v "$VERSION" \ + --arg bundle "https://github.com/${{ github.repository }}/releases/download/mcp-$VERSION/psalm-$VERSION.mcpb" \ + --arg sha "$(sha256sum "build/psalm-$VERSION.mcpb" | cut -d' ' -f1)" \ + --arg image "$IMAGE:$VERSION" \ + '.version = $v + | .packages[0].identifier = $bundle | .packages[0].fileSha256 = $sha + | .packages[1].identifier = $image' \ + registry/server.json > server.json + cat server.json + + - uses: sigstore/cosign-installer@881c63bd87e38389154debd8331404ce7ca8a3ab # v4 + + - name: Download mcp-publisher + run: | + archive=mcp-publisher_linux_amd64.tar.gz + base_url="https://github.com/modelcontextprotocol/registry/releases/download/$MCP_PUBLISHER_VERSION" + curl --fail --location --silent --show-error --output "$archive" "$base_url/$archive" + curl --fail --location --silent --show-error --output "$archive.sigstore.json" "$base_url/$archive.sigstore.json" + cosign verify-blob --bundle "$archive.sigstore.json" \ + --certificate-identity "https://github.com/modelcontextprotocol/registry/.github/workflows/release.yml@refs/tags/$MCP_PUBLISHER_VERSION" \ + --certificate-oidc-issuer https://token.actions.githubusercontent.com \ + "$archive" + tar xzf "$archive" mcp-publisher + + - name: Publish to the MCP registry + run: | + ./mcp-publisher login github-oidc + ./mcp-publisher publish diff --git a/.gitignore b/.gitignore index 08b4f39..c70eac1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /vendor/ composer.lock .phpunit.cache/ +/psalm.phar +/build/ diff --git a/README.md b/README.md index 01f500f..abed254 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,19 @@ Two environment variables change what the plugin runs: Other agents: [Using Psalm with AI agents](https://psalm.dev/docs/running_psalm/ai_agents/). +### MCP registry + +The [MCP registry](https://registry.modelcontextprotocol.io) lists the MCP server as `io.github.psalm/psalm`, in two +packages that bring a `psalm.phar` for projects without Psalm: + +- an MCP bundle (`psalm-VERSION.mcpb`, in this repository's releases) for Claude Desktop and other clients that install + bundles. It needs `php`; +- the image `ghcr.io/psalm/psalm-mcp`, which needs Docker. It runs with the project mounted at its own path: + + ```sh + docker run -i --rm --mount type=bind,src="$PWD",dst="$PWD" --workdir "$PWD" ghcr.io/psalm/psalm-mcp + ``` + ## What the plugin runs and sends - `php scripts/server.php` starts when the agent does. It looks for the Psalm of the project (in `vendor/`, @@ -42,6 +55,7 @@ Other agents: [Using Psalm with AI agents](https://psalm.dev/docs/running_psalm/ - Psalm loads the project's Composer autoloader and the Psalm plugins in its `psalm.xml`, so use the plugin in projects you trust. `psalm_check` without paths runs the project's `psalm` command. - The skill runs the scripts of the skill that the project's Psalm ships, in `vendor/vimeo/psalm/skills/psalm`. +- The MCP bundle and the image also run the `psalm.phar` they ship when the project has no Psalm. - Nothing is sent anywhere by default. The skill can send a self-contained snippet to `https://psalm.dev/check`, to check a bug report against the latest Psalm, only after you agree, and it reads the documentation on psalm.dev. Psalm sends its results to `https://shepherd.dev` only when it runs with `--shepherd`. The plugin downloads nothing @@ -50,4 +64,6 @@ Other agents: [Using Psalm with AI agents](https://psalm.dev/docs/running_psalm/ ## Releasing Set the same version in `.claude-plugin/plugin.json`, `.cursor-plugin/plugin.json`, `gemini-extension.json` and -`package.json`, and tag it. +`package.json`, and tag it. The `v*` tag publishes the OpenCode package to npm (`npm.yml`). + +`registry.yml` publishes the MCP bundle, the image and the registry entry of each Psalm release, with its version. diff --git a/registry/Dockerfile b/registry/Dockerfile new file mode 100644 index 0000000..3f52098 --- /dev/null +++ b/registry/Dockerfile @@ -0,0 +1,18 @@ +# The MCP server of the Psalm of a project, or of the psalm.phar beside the launcher when the project has none. Run it +# with the project mounted at its own path, so that the paths the agent passes exist in the container: +# docker run -i --rm --mount type=bind,src=$PWD,dst=$PWD --workdir $PWD ghcr.io/psalm/psalm-mcp +FROM php:8.5-cli-alpine + +RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS linux-headers \ + && docker-php-ext-install pcntl \ + && apk del .build-deps + +COPY scripts/server.php /opt/psalm/scripts/server.php +COPY psalm.phar /opt/psalm/psalm.phar + +LABEL io.modelcontextprotocol.server.name="io.github.psalm/psalm" +LABEL org.opencontainers.image.source="https://github.com/psalm/agent-plugins" +LABEL org.opencontainers.image.description="Psalm static analysis for PHP as an MCP server" +LABEL org.opencontainers.image.licenses="MIT" + +ENTRYPOINT ["php", "/opt/psalm/scripts/server.php", "mcp"] diff --git a/registry/manifest.json b/registry/manifest.json new file mode 100644 index 0000000..38377c8 --- /dev/null +++ b/registry/manifest.json @@ -0,0 +1,45 @@ +{ + "manifest_version": "0.3", + "name": "psalm", + "display_name": "Psalm", + "version": "0.0.0", + "description": "Psalm static analysis for PHP: check code, inspect inferred types, find references and explain issues.", + "author": { + "name": "Psalm", + "url": "https://psalm.dev" + }, + "homepage": "https://psalm.dev/docs/running_psalm/ai_agents/", + "repository": { + "type": "git", + "url": "https://github.com/psalm/agent-plugins" + }, + "license": "MIT", + "icon": "icon.png", + "server": { + "type": "binary", + "entry_point": "scripts/server.php", + "mcp_config": { + "command": "php", + "args": [ + "${__dirname}/scripts/server.php", + "mcp", + "${user_config.project}" + ] + } + }, + "user_config": { + "project": { + "type": "directory", + "title": "PHP project", + "description": "The directory of the PHP project that Psalm analyzes", + "required": true + } + }, + "compatibility": { + "platforms": [ + "darwin", + "linux", + "win32" + ] + } +} diff --git a/registry/server.json b/registry/server.json new file mode 100644 index 0000000..ef21ccb --- /dev/null +++ b/registry/server.json @@ -0,0 +1,59 @@ +{ + "$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json", + "name": "io.github.psalm/psalm", + "title": "Psalm", + "description": "Psalm static analysis for PHP: check code, inspect types, find references, explain issues", + "version": "0.0.0", + "websiteUrl": "https://psalm.dev/docs/running_psalm/ai_agents/", + "repository": { + "url": "https://github.com/psalm/agent-plugins", + "source": "github" + }, + "packages": [ + { + "registryType": "mcpb", + "identifier": "", + "fileSha256": "", + "transport": { + "type": "stdio" + } + }, + { + "registryType": "oci", + "identifier": "", + "transport": { + "type": "stdio" + }, + "runtimeArguments": [ + { + "type": "named", + "name": "--mount", + "description": "The PHP project, mounted at its own path", + "value": "type=bind,src={project},dst={project}", + "isRequired": true, + "variables": { + "project": { + "description": "The directory of the PHP project that Psalm analyzes", + "format": "filepath", + "isRequired": true + } + } + }, + { + "type": "named", + "name": "--workdir", + "description": "The PHP project", + "value": "{project}", + "isRequired": true, + "variables": { + "project": { + "description": "The directory of the PHP project that Psalm analyzes", + "format": "filepath", + "isRequired": true + } + } + } + ] + } + ] +} diff --git a/registry/smoke.php b/registry/smoke.php new file mode 100644 index 0000000..2736ab4 --- /dev/null +++ b/registry/smoke.php @@ -0,0 +1,72 @@ + $argv */ +if (count($argv) < 2) { + fwrite(STDERR, USAGE); + exit(1); +} + +$project = sys_get_temp_dir() . '/psalm-smoke-' . bin2hex(random_bytes(4)); +mkdir($project . '/src', 0777, true); +$project = (string) realpath($project); +file_put_contents($project . '/psalm.xml', <<<'XML' + + + + + + +XML); +file_put_contents($project . '/src/a.php', " str_replace('{project}', $project, $arg), array_slice($argv, 1)); +$process = proc_open($command, [['pipe', 'r'], ['pipe', 'w'], STDERR], $pipes, $project); +if (!is_resource($process)) { + fwrite(STDERR, "Could not start the server\n"); + exit(1); +} + +foreach ([ + ['jsonrpc' => '2.0', 'id' => 1, 'method' => 'initialize', 'params' => [ + 'protocolVersion' => '2025-06-18', + 'capabilities' => new stdClass(), + 'clientInfo' => ['name' => 'smoke', 'version' => '1'], + ]], + ['jsonrpc' => '2.0', 'method' => 'notifications/initialized'], + ['jsonrpc' => '2.0', 'id' => 2, 'method' => 'tools/call', 'params' => [ + 'name' => 'psalm_check', + 'arguments' => ['paths' => ['src/a.php']], + ]], +] as $message) { + fwrite($pipes[0], json_encode($message) . "\n"); +} + +while (($line = fgets($pipes[1])) !== false) { + $message = json_decode($line, true); + if (!is_array($message) || ($message['id'] ?? null) !== 2) { + continue; + } + + fclose($pipes[0]); + proc_terminate($process); + if (isset($message['error']) || ($message['result']['isError'] ?? false)) { + fwrite(STDERR, $line); + exit(1); + } + echo "The MCP server checked src/a.php\n"; + exit(0); +} + +fwrite(STDERR, "The MCP server exited without answering\n"); +exit(1); diff --git a/scripts/server.php b/scripts/server.php index da812a3..f6b59fe 100644 --- a/scripts/server.php +++ b/scripts/server.php @@ -7,8 +7,8 @@ Starts the Psalm of a PHP project as an MCP server (mcp) or a language server (lsp), on stdin and stdout. It looks for Psalm in the project (vendor/vimeo/psalm, psalm/phar, vendor-bin/*, tools/*), in the global -Composer packages, then on the PATH. PROJECT_DIR defaults to $CLAUDE_PROJECT_DIR, $COPILOT_SESSION_CWD, the current -directory, then $PWD, leaving out the directory of the plugin. +Composer packages, on the PATH, then for a psalm.phar in the directory above this script. PROJECT_DIR defaults to +$CLAUDE_PROJECT_DIR, $COPILOT_SESSION_CWD, the current directory, then $PWD, leaving out the directory of the plugin. Arguments after PROJECT_DIR are passed on to the server. Environment: @@ -205,6 +205,12 @@ function findServer(string $mode, string $root, ?string &$problem): ?array } } + // the PHAR that an MCP bundle or the container image ships beside the launcher + $bundled = dirname(__DIR__) . '/psalm.phar'; + if (is_file($bundled)) { + return [$php, $bundled, $mode === 'lsp' ? '--language-server' : '--mcp']; + } + $problem = 'Psalm is not installed in ' . $root . '. Install it with `composer require --dev vimeo/psalm`, then' . ' create its config with `vendor/bin/psalm --init`, and restart the agent.'; return null; diff --git a/tests/LauncherTest.php b/tests/LauncherTest.php index 3aa8fc6..f417491 100644 --- a/tests/LauncherTest.php +++ b/tests/LauncherTest.php @@ -226,6 +226,29 @@ public function testRunsPsalmWithTheChosenPhp(): void ); } + public function testRunsTheBundledPharWithoutAPsalmInTheProject(): void + { + $bundle = $this->dir . '/bundle'; + mkdir($bundle . '/scripts', 0777, true); + copy(__DIR__ . '/../scripts/server.php', $bundle . '/scripts/server.php'); + file_put_contents($bundle . '/psalm.phar', 'dir . '/project'); + file_put_contents($this->dir . '/project/psalm.xml', ''); + + self::assertSame('bundled --mcp', $this->launch($this->dir . '/project', launcher: $bundle)); + } + + public function testPrefersThePsalmOfTheProjectToTheBundledPhar(): void + { + $bundle = $this->dir . '/bundle'; + mkdir($bundle . '/scripts', 0777, true); + copy(__DIR__ . '/../scripts/server.php', $bundle . '/scripts/server.php'); + file_put_contents($bundle . '/psalm.phar', 'writeProject('project', 'project'); + + self::assertSame("project\n", $this->launch($this->dir . '/project', launcher: $bundle)); + } + public function testSaysWhenPsalmIsNotInstalled(): void { $output = $this->launch($this->dir);