Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .claude-plugin/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -22,8 +28,7 @@
"command": "php",
"args": [
"${CLAUDE_PLUGIN_ROOT}/scripts/server.php",
"mcp",
"${CLAUDE_PROJECT_DIR}"
"mcp"
]
}
},
Expand All @@ -32,8 +37,7 @@
"command": "php",
"args": [
"${CLAUDE_PLUGIN_ROOT}/scripts/server.php",
"lsp",
"${CLAUDE_PROJECT_DIR}"
"lsp"
],
"extensionToLanguage": {
".php": "php"
Expand Down
28 changes: 28 additions & 0 deletions .cursor-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -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}"
]
}
}
}
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,13 +22,32 @@ 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;
- `PSALM_PROJECT_DIR`: the project, if several directories have a `psalm.xml`.

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.
13 changes: 11 additions & 2 deletions scripts/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 44 additions & 3 deletions tests/LauncherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -220,14 +255,20 @@ private function writeProject(string $path, string $line, string $vendor = 'vend
/**
* @param array<string, string> $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' => '',
Expand Down
10 changes: 8 additions & 2 deletions tests/ManifestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ 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
{
$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);
Expand All @@ -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'),
Expand Down
Loading