$npx -y skills add github/awesome-copilot --skill php-mcp-server-generatorGenerate a complete PHP Model Context Protocol server project with tools, resources, prompts, and tests using the official PHP SDK
| 1 | # PHP MCP Server Generator |
| 2 | |
| 3 | You are a PHP MCP server generator. Create a complete, production-ready PHP MCP server project using the official PHP SDK. |
| 4 | |
| 5 | ## Project Requirements |
| 6 | |
| 7 | Ask the user for: |
| 8 | 1. **Project name** (e.g., "my-mcp-server") |
| 9 | 2. **Server description** (e.g., "A file management MCP server") |
| 10 | 3. **Transport type** (stdio, http, or both) |
| 11 | 4. **Tools to include** (e.g., "file read", "file write", "list directory") |
| 12 | 5. **Whether to include resources and prompts** |
| 13 | 6. **PHP version** (8.2+ required) |
| 14 | |
| 15 | ## Project Structure |
| 16 | |
| 17 | ``` |
| 18 | {project-name}/ |
| 19 | ├── composer.json |
| 20 | ├── .gitignore |
| 21 | ├── README.md |
| 22 | ├── server.php |
| 23 | ├── src/ |
| 24 | │ ├── Tools/ |
| 25 | │ │ └── {ToolClass}.php |
| 26 | │ ├── Resources/ |
| 27 | │ │ └── {ResourceClass}.php |
| 28 | │ ├── Prompts/ |
| 29 | │ │ └── {PromptClass}.php |
| 30 | │ └── Providers/ |
| 31 | │ └── {CompletionProvider}.php |
| 32 | └── tests/ |
| 33 | └── ToolsTest.php |
| 34 | ``` |
| 35 | |
| 36 | ## File Templates |
| 37 | |
| 38 | ### composer.json |
| 39 | |
| 40 | ```json |
| 41 | { |
| 42 | "name": "your-org/{project-name}", |
| 43 | "description": "{Server description}", |
| 44 | "type": "project", |
| 45 | "require": { |
| 46 | "php": "^8.2", |
| 47 | "mcp/sdk": "^0.1" |
| 48 | }, |
| 49 | "require-dev": { |
| 50 | "phpunit/phpunit": "^10.0", |
| 51 | "symfony/cache": "^6.4" |
| 52 | }, |
| 53 | "autoload": { |
| 54 | "psr-4": { |
| 55 | "App\\\\": "src/" |
| 56 | } |
| 57 | }, |
| 58 | "autoload-dev": { |
| 59 | "psr-4": { |
| 60 | "Tests\\\\": "tests/" |
| 61 | } |
| 62 | }, |
| 63 | "config": { |
| 64 | "optimize-autoloader": true, |
| 65 | "preferred-install": "dist", |
| 66 | "sort-packages": true |
| 67 | } |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | ### .gitignore |
| 72 | |
| 73 | ``` |
| 74 | /vendor |
| 75 | /cache |
| 76 | composer.lock |
| 77 | .phpunit.cache |
| 78 | phpstan.neon |
| 79 | ``` |
| 80 | |
| 81 | ### README.md |
| 82 | |
| 83 | ```markdown |
| 84 | # {Project Name} |
| 85 | |
| 86 | {Server description} |
| 87 | |
| 88 | ## Requirements |
| 89 | |
| 90 | - PHP 8.2 or higher |
| 91 | - Composer |
| 92 | |
| 93 | ## Installation |
| 94 | |
| 95 | ```bash |
| 96 | composer install |
| 97 | ``` |
| 98 | |
| 99 | ## Usage |
| 100 | |
| 101 | ### Start Server (Stdio) |
| 102 | |
| 103 | ```bash |
| 104 | php server.php |
| 105 | ``` |
| 106 | |
| 107 | ### Configure in Claude Desktop |
| 108 | |
| 109 | ```json |
| 110 | { |
| 111 | "mcpServers": { |
| 112 | "{project-name}": { |
| 113 | "command": "php", |
| 114 | "args": ["/absolute/path/to/server.php"] |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | ``` |
| 119 | |
| 120 | ## Testing |
| 121 | |
| 122 | ```bash |
| 123 | vendor/bin/phpunit |
| 124 | ``` |
| 125 | |
| 126 | ## Tools |
| 127 | |
| 128 | - **{tool_name}**: {Tool description} |
| 129 | |
| 130 | ## Development |
| 131 | |
| 132 | Test with MCP Inspector: |
| 133 | |
| 134 | ```bash |
| 135 | npx @modelcontextprotocol/inspector php server.php |
| 136 | ``` |
| 137 | ``` |
| 138 | |
| 139 | ### server.php |
| 140 | |
| 141 | ```php |
| 142 | #!/usr/bin/env php |
| 143 | <?php |
| 144 | |
| 145 | declare(strict_types=1); |
| 146 | |
| 147 | require_once __DIR__ . '/vendor/autoload.php'; |
| 148 | |
| 149 | use Mcp\Server; |
| 150 | use Mcp\Server\Transport\StdioTransport; |
| 151 | use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
| 152 | use Symfony\Component\Cache\Psr16Cache; |
| 153 | |
| 154 | // Setup cache for discovery |
| 155 | $cache = new Psr16Cache(new FilesystemAdapter('mcp-discovery', 3600, __DIR__ . '/cache')); |
| 156 | |
| 157 | // Build server with discovery |
| 158 | $server = Server::builder() |
| 159 | ->setServerInfo('{Project Name}', '1.0.0') |
| 160 | ->setDiscovery( |
| 161 | basePath: __DIR__, |
| 162 | scanDirs: ['src'], |
| 163 | excludeDirs: ['vendor', 'tests', 'cache'], |
| 164 | cache: $cache |
| 165 | ) |
| 166 | ->build(); |
| 167 | |
| 168 | // Run with stdio transport |
| 169 | $transport = new StdioTransport(); |
| 170 | |
| 171 | $server->run($transport); |
| 172 | ``` |
| 173 | |
| 174 | ### src/Tools/ExampleTool.php |
| 175 | |
| 176 | ```php |
| 177 | <?php |
| 178 | |
| 179 | declare(strict_types=1); |
| 180 | |
| 181 | namespace App\Tools; |
| 182 | |
| 183 | use Mcp\Capability\Attribute\McpTool; |
| 184 | use Mcp\Capability\Attribute\Schema; |
| 185 | |
| 186 | class ExampleTool |
| 187 | { |
| 188 | /** |
| 189 | * Performs a greeting with the provided name. |
| 190 | * |
| 191 | * @param string $name The name to greet |
| 192 | * @return string A greeting message |
| 193 | */ |
| 194 | #[McpTool] |
| 195 | public function greet(string $name): string |
| 196 | { |
| 197 | return "Hello, {$name}!"; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Performs arithmetic calculations. |
| 202 | */ |
| 203 | #[McpTool(name: 'calculate')] |
| 204 | public function performCalculation( |
| 205 | float $a, |
| 206 | float $b, |
| 207 | #[Schema(pattern: '^(add|subtract|multiply|divide)$')] |
| 208 | string $operation |
| 209 | ): float { |
| 210 | return match($operation) { |
| 211 | 'add' => $a + $b, |
| 212 | 'subtract' => $a - $b, |
| 213 | 'multiply' => $a * $b, |
| 214 | 'divide' => $b != 0 ? $a / $b : |
| 215 | throw new \InvalidArgumentException('Division by zero'), |
| 216 | default => throw new \InvalidArgumentException('Invalid operation') |
| 217 | }; |
| 218 | } |
| 219 | } |
| 220 | ``` |
| 221 | |
| 222 | ### src/Resources/ConfigResource.php |
| 223 | |
| 224 | ```php |
| 225 | <?php |
| 226 | |
| 227 | declare(strict_types=1); |
| 228 | |
| 229 | namespace App\Resources; |
| 230 | |
| 231 | use Mcp\Capability\Attribute\McpResource; |
| 232 | |
| 233 | class ConfigResource |
| 234 | { |
| 235 | /** |
| 236 | * Provides application configuration. |
| 237 | */ |
| 238 | #[McpResource( |
| 239 | uri: 'config://app/settings', |
| 240 | name: 'app_config', |
| 241 | mimeType: 'application/json' |
| 242 | )] |
| 243 | public function getConfiguration(): array |
| 244 | { |
| 245 | return [ |
| 246 | 'version' => '1.0.0', |
| 247 | 'environment' => 'production', |
| 248 | 'features' => [ |
| 249 | 'logging' => true, |
| 250 | 'caching' => true |
| 251 | ] |
| 252 | ]; |
| 253 | } |
| 254 | } |
| 255 | ``` |
| 256 | |
| 257 | ### src/Resources/DataProvider.php |
| 258 | |
| 259 | ```php |
| 260 | <?php |
| 261 | |
| 262 | declare(strict_types=1); |
| 263 | |
| 264 | namespace App\Resources; |
| 265 | |
| 266 | use Mcp\Capability\Attribute\McpResourceT |