diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json index 780b7da..ee100ee 100644 --- a/.claude-plugin/plugin.json +++ b/.claude-plugin/plugin.json @@ -10,6 +10,12 @@ "homepage": "https://psalm.dev/docs/running_psalm/ai_agents/", "repository": "https://github.com/psalm/agent-plugins", "license": "MIT", + "network": { + "hosts": [ + "psalm.dev", + "shepherd.dev" + ] + }, "keywords": [ "php", "psalm", @@ -22,8 +28,7 @@ "command": "php", "args": [ "${CLAUDE_PLUGIN_ROOT}/scripts/server.php", - "mcp", - "${CLAUDE_PROJECT_DIR}" + "mcp" ] } }, @@ -32,8 +37,7 @@ "command": "php", "args": [ "${CLAUDE_PLUGIN_ROOT}/scripts/server.php", - "lsp", - "${CLAUDE_PROJECT_DIR}" + "lsp" ], "extensionToLanguage": { ".php": "php" diff --git a/.cursor-plugin/plugin.json b/.cursor-plugin/plugin.json new file mode 100644 index 0000000..989a027 --- /dev/null +++ b/.cursor-plugin/plugin.json @@ -0,0 +1,28 @@ +{ + "name": "psalm", + "version": "1.0.0", + "description": "Psalm static analysis for PHP: MCP tools to check code, inspect inferred types, find references and explain issues, and the Psalm skill.", + "author": { + "name": "Psalm" + }, + "homepage": "https://psalm.dev/docs/running_psalm/ai_agents/", + "repository": "https://github.com/psalm/agent-plugins", + "license": "MIT", + "keywords": [ + "php", + "psalm", + "static-analysis", + "mcp" + ], + "logo": ".claude-plugin/icon.png", + "mcpServers": { + "psalm": { + "command": "php", + "args": [ + "${CURSOR_PLUGIN_ROOT}/scripts/server.php", + "mcp", + "${workspaceFolder}" + ] + } + } +} diff --git a/README.md b/README.md index f6eebf0..01f500f 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,6 @@ gives an agent: - a skill to run Psalm, fix its issues and annotate code, which is the one the Psalm of your project ships; - in Claude Code, the language server, which shows Psalm's errors after each edit. -The plugin runs the Psalm of your project on your machine, with `php`. Psalm loads the project's autoloader and Psalm -plugins, so use it in projects you trust. Code leaves your machine only if you agree to upload a snippet to psalm.dev -for a bug report. - ## Setup Install Psalm in the project with `composer require --dev vimeo/psalm`, and create its config with @@ -26,6 +22,10 @@ Gemini CLI: `gemini extensions install https://github.com/psalm/agent-plugins` OpenCode: `opencode plugin add github:psalm/agent-plugins` +Cursor: `agent plugin install https://github.com/psalm/agent-plugins` + +GitHub Copilot CLI: `copilot plugin marketplace add psalm/agent-plugins`, then `copilot plugin install psalm@psalm` + Two environment variables change what the plugin runs: - `PSALM_PHP`: the PHP binary that runs Psalm; @@ -33,6 +33,21 @@ Two environment variables change what the plugin runs: Other agents: [Using Psalm with AI agents](https://psalm.dev/docs/running_psalm/ai_agents/). +## 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/`, + `vendor-bin/*/vendor/`, `tools/`, the global Composer packages, then the `PATH`) and runs its MCP server + (`psalm-mcp`) or, in Claude Code, its language server (`psalm-language-server`), in the project directory. If there + is no Psalm, the MCP server only says how to install it. +- 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`. +- 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 + and collects no data. + ## Releasing -Set the same version in `.claude-plugin/plugin.json`, `gemini-extension.json` and `package.json`, and tag it. +Set the same version in `.claude-plugin/plugin.json`, `.cursor-plugin/plugin.json`, `gemini-extension.json` and +`package.json`, and tag it. diff --git a/scripts/server.php b/scripts/server.php index 3521dd3..da812a3 100644 --- a/scripts/server.php +++ b/scripts/server.php @@ -7,7 +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, then to the current directory. +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. Arguments after PROJECT_DIR are passed on to the server. Environment: @@ -254,7 +255,15 @@ function serveProblem(string $problem): never // agents that don't know a placeholder of a plugin pass it as it is $start = getenv('PSALM_PROJECT_DIR') ?: $argv[2] ?? ''; if ($start === '' || str_starts_with($start, '${')) { - $start = getenv('CLAUDE_PROJECT_DIR') ?: getcwd(); + // the directory of the agent's session: Copilot CLI starts servers in the directory of the plugin + $start = false; + $plugin = (string) realpath(dirname(__DIR__)); + foreach ([getenv('CLAUDE_PROJECT_DIR'), getenv('COPILOT_SESSION_CWD'), getcwd(), getenv('PWD')] as $dir) { + if (is_string($dir) && $dir !== '' && is_dir($dir) && !isInside($dir, $plugin)) { + $start = $dir; + break; + } + } } $start = is_string($start) ? realpath($start) : false; if ($start === false) { diff --git a/tests/LauncherTest.php b/tests/LauncherTest.php index 5923d0c..3aa8fc6 100644 --- a/tests/LauncherTest.php +++ b/tests/LauncherTest.php @@ -97,6 +97,41 @@ public function testIgnoresAPlaceholderTheAgentDoesNotKnow(): void self::assertSame("project\n", $this->launch('${CLAUDE_PROJECT_DIR}')); } + public function testRunsTheProjectOfTheAgent(): void + { + $this->writeProject('project', 'project'); + + self::assertSame("project\n", $this->launch(null, ['CLAUDE_PROJECT_DIR' => $this->dir . '/project'])); + } + + public function testRunsTheProjectOfTheWorkingDirectory(): void + { + $this->writeProject('', 'project'); + + self::assertSame("project\n", $this->launch(null)); + } + + public function testRunsTheProjectOfTheCopilotSession(): void + { + $this->writeProject('project', 'project'); + + self::assertSame("project\n", $this->launch(null, ['COPILOT_SESSION_CWD' => $this->dir . '/project'])); + } + + public function testDoesNotTakeTheDirectoryOfThePluginForTheProject(): void + { + $plugin = $this->dir . '/plugin'; + mkdir($plugin . '/scripts', 0777, true); + copy(__DIR__ . '/../scripts/server.php', $plugin . '/scripts/server.php'); + file_put_contents($plugin . '/composer.json', '{}'); + $this->writeProject('project', 'project'); + + self::assertSame( + "project\n", + $this->launch(null, ['PWD' => $this->dir . '/project'], launcher: $plugin, cwd: $plugin), + ); + } + public function testFindsTheOnlyProjectOfTheRepository(): void { mkdir($this->dir . '/.git'); @@ -220,14 +255,20 @@ private function writeProject(string $path, string $line, string $vendor = 'vend /** * @param array $env */ - private function launch(string $project, array $env = [], string $mode = 'mcp'): string + private function launch( + ?string $project, + array $env = [], + string $mode = 'mcp', + string $launcher = __DIR__ . '/..', + ?string $cwd = null, + ): string { $nowhere = $this->dir . '/nowhere'; $process = proc_open( - [PHP_BINARY, __DIR__ . '/../scripts/server.php', $mode, $project], + [PHP_BINARY, $launcher . '/scripts/server.php', $mode, ...($project === null ? [] : [$project])], [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes, - $this->dir, + $cwd ?? $this->dir, // no global Psalm $env + [ 'PATH' => '', diff --git a/tests/ManifestTest.php b/tests/ManifestTest.php index bff0104..7a812cb 100644 --- a/tests/ManifestTest.php +++ b/tests/ManifestTest.php @@ -17,6 +17,7 @@ public function testTheManifestsHaveTheSameVersion(): void self::assertIsString($version); self::assertSame($version, self::readJson('gemini-extension.json')['version'] ?? null); self::assertSame($version, self::readJson('package.json')['version'] ?? null); + self::assertSame($version, self::readJson('.cursor-plugin/plugin.json')['version'] ?? null); } public function testThePluginsStartTheLauncher(): void @@ -24,11 +25,11 @@ public function testThePluginsStartTheLauncher(): void $plugin = self::readJson('.claude-plugin/plugin.json'); self::assertSame(['psalm' => [ 'command' => 'php', - 'args' => ['${CLAUDE_PLUGIN_ROOT}/scripts/server.php', 'mcp', '${CLAUDE_PROJECT_DIR}'], + 'args' => ['${CLAUDE_PLUGIN_ROOT}/scripts/server.php', 'mcp'], ]], $plugin['mcpServers'] ?? null); self::assertSame(['psalm' => [ 'command' => 'php', - 'args' => ['${CLAUDE_PLUGIN_ROOT}/scripts/server.php', 'lsp', '${CLAUDE_PROJECT_DIR}'], + 'args' => ['${CLAUDE_PLUGIN_ROOT}/scripts/server.php', 'lsp'], 'extensionToLanguage' => ['.php' => 'php'], 'startupTimeout' => 300_000, ]], $plugin['lspServers'] ?? null); @@ -38,6 +39,11 @@ public function testThePluginsStartTheLauncher(): void 'args' => ['${extensionPath}/scripts/server.php', 'mcp', '${workspacePath}'], ]], self::readJson('gemini-extension.json')['mcpServers'] ?? null); + self::assertSame(['psalm' => [ + 'command' => 'php', + 'args' => ['${CURSOR_PLUGIN_ROOT}/scripts/server.php', 'mcp', '${workspaceFolder}'], + ]], self::readJson('.cursor-plugin/plugin.json')['mcpServers'] ?? null); + self::assertStringContainsString( 'join(root, "scripts", "server.php")', (string) file_get_contents(self::ROOT . '/opencode/index.js'),