php-mcp-server-generator by github/awesome-copilot
npx skills add https://github.com/github/awesome-copilot --skill php-mcp-server-generator你是一个 PHP MCP 服务器生成器。使用官方的 PHP SDK 创建一个完整的、可用于生产环境的 PHP MCP 服务器项目。
向用户询问:
{project-name}/
├── composer.json
├── .gitignore
├── README.md
├── server.php
├── src/
│ ├── Tools/
│ │ └── {ToolClass}.php
│ ├── Resources/
│ │ └── {ResourceClass}.php
│ ├── Prompts/
│ │ └── {PromptClass}.php
│ └── Providers/
│ └── {CompletionProvider}.php
└── tests/
└── ToolsTest.php
{
"name": "your-org/{project-name}",
"description": "{Server description}",
"type": "project",
"require": {
"php": "^8.2",
"mcp/sdk": "^0.1"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
"symfony/cache": "^6.4"
},
"autoload": {
"psr-4": {
"App\\\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\\\": "tests/"
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
}
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
/vendor
/cache
composer.lock
.phpunit.cache
phpstan.neon
# {Project Name}
{Server description}
## 要求
- PHP 8.2 或更高版本
- Composer
## 安装
```bash
composer install
php server.php
{
"mcpServers": {
"{project-name}": {
"command": "php",
"args": ["/absolute/path/to/server.php"]
}
}
}
vendor/bin/phpunit
使用 MCP Inspector 进行测试:
npx @modelcontextprotocol/inspector php server.php
### server.php
```php
#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Mcp\Server;
use Mcp\Server\Transport\StdioTransport;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;
// Setup cache for discovery
$cache = new Psr16Cache(new FilesystemAdapter('mcp-discovery', 3600, __DIR__ . '/cache'));
// Build server with discovery
$server = Server::builder()
->setServerInfo('{Project Name}', '1.0.0')
->setDiscovery(
basePath: __DIR__,
scanDirs: ['src'],
excludeDirs: ['vendor', 'tests', 'cache'],
cache: $cache
)
->build();
// Run with stdio transport
$transport = new StdioTransport();
$server->run($transport);
<?php
declare(strict_types=1);
namespace App\Tools;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Capability\Attribute\Schema;
class ExampleTool
{
/**
* 使用提供的名称执行问候。
*
* @param string $name 要问候的名字
* @return string 问候消息
*/
#[McpTool]
public function greet(string $name): string
{
return "Hello, {$name}!";
}
/**
* 执行算术计算。
*/
#[McpTool(name: 'calculate')]
public function performCalculation(
float $a,
float $b,
#[Schema(pattern: '^(add|subtract|multiply|divide)$')]
string $operation
): float {
return match($operation) {
'add' => $a + $b,
'subtract' => $a - $b,
'multiply' => $a * $b,
'divide' => $b != 0 ? $a / $b :
throw new \InvalidArgumentException('Division by zero'),
default => throw new \InvalidArgumentException('Invalid operation')
};
}
}
<?php
declare(strict_types=1);
namespace App\Resources;
use Mcp\Capability\Attribute\McpResource;
class ConfigResource
{
/**
* 提供应用程序配置。
*/
#[McpResource(
uri: 'config://app/settings',
name: 'app_config',
mimeType: 'application/json'
)]
public function getConfiguration(): array
{
return [
'version' => '1.0.0',
'environment' => 'production',
'features' => [
'logging' => true,
'caching' => true
]
];
}
}
<?php
declare(strict_types=1);
namespace App\Resources;
use Mcp\Capability\Attribute\McpResourceTemplate;
class DataProvider
{
/**
* 按类别和 ID 提供数据。
*/
#[McpResourceTemplate(
uriTemplate: 'data://{category}/{id}',
name: 'data_resource',
mimeType: 'application/json'
)]
public function getData(string $category, string $id): array
{
// 示例数据检索
return [
'category' => $category,
'id' => $id,
'data' => "Sample data for {$category}/{$id}"
];
}
}
<?php
declare(strict_types=1);
namespace App\Prompts;
use Mcp\Capability\Attribute\McpPrompt;
use Mcp\Capability\Attribute\CompletionProvider;
class PromptGenerator
{
/**
* 生成代码审查提示词。
*/
#[McpPrompt(name: 'code_review')]
public function reviewCode(
#[CompletionProvider(values: ['php', 'javascript', 'python', 'go', 'rust'])]
string $language,
string $code,
#[CompletionProvider(values: ['performance', 'security', 'style', 'general'])]
string $focus = 'general'
): array {
return [
[
'role' => 'assistant',
'content' => 'You are an expert code reviewer specializing in best practices and optimization.'
],
[
'role' => 'user',
'content' => "Review this {$language} code with focus on {$focus}:\n\n```{$language}\n{$code}\n```"
]
];
}
/**
* 生成文档提示词。
*/
#[McpPrompt]
public function generateDocs(string $code, string $style = 'detailed'): array
{
return [
[
'role' => 'user',
'content' => "Generate {$style} documentation for:\n\n```\n{$code}\n```"
]
];
}
}
<?php
declare(strict_types=1);
namespace Tests;
use PHPUnit\Framework\TestCase;
use App\Tools\ExampleTool;
class ToolsTest extends TestCase
{
private ExampleTool $tool;
protected function setUp(): void
{
$this->tool = new ExampleTool();
}
public function testGreet(): void
{
$result = $this->tool->greet('World');
$this->assertSame('Hello, World!', $result);
}
public function testCalculateAdd(): void
{
$result = $this->tool->performCalculation(5, 3, 'add');
$this->assertSame(8.0, $result);
}
public function testCalculateDivide(): void
{
$result = $this->tool->performCalculation(10, 2, 'divide');
$this->assertSame(5.0, $result);
}
public function testCalculateDivideByZero(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Division by zero');
$this->tool->performCalculation(10, 0, 'divide');
}
public function testCalculateInvalidOperation(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid operation');
$this->tool->performCalculation(5, 3, 'modulo');
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
#[McpTool], #[McpResource], #[McpPrompt] 保持代码整洁declare(strict_types=1);)#[Schema] 属性进行参数验证#[McpTool]
public function simpleAction(string $input): string
{
return "Processed: {$input}";
}
#[McpTool]
public function validateEmail(
#[Schema(format: 'email')]
string $email
): bool {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
enum Status: string {
case ACTIVE = 'active';
case INACTIVE = 'inactive';
}
#[McpTool]
public function setStatus(string $id, Status $status): array
{
return ['id' => $id, 'status' => $status->value];
}
#[McpResource(uri: 'config://settings', mimeType: 'application/json')]
public function getSettings(): array
{
return ['key' => 'value'];
}
#[McpResourceTemplate(uriTemplate: 'user://{id}')]
public function getUser(string $id): array
{
return $this->users[$id] ?? throw new \RuntimeException('User not found');
}
# 安装依赖
composer install
# 运行测试
vendor/bin/phpunit
# 启动服务器
php server.php
# 使用 inspector 测试
npx @modelcontextprotocol/inspector php server.php
{
"mcpServers": {
"{project-name}": {
"command": "php",
"args": ["/absolute/path/to/server.php"]
}
}
}
现在根据用户需求生成完整的项目!
每周安装数
7.4K
代码仓库
GitHub 星标数
27.0K
首次出现
2026年2月25日
安全审计
安装于
codex7.3K
gemini-cli7.3K
opencode7.3K
cursor7.2K
github-copilot7.2K
kimi-cli7.2K
You are a PHP MCP server generator. Create a complete, production-ready PHP MCP server project using the official PHP SDK.
Ask the user for:
{project-name}/
├── composer.json
├── .gitignore
├── README.md
├── server.php
├── src/
│ ├── Tools/
│ │ └── {ToolClass}.php
│ ├── Resources/
│ │ └── {ResourceClass}.php
│ ├── Prompts/
│ │ └── {PromptClass}.php
│ └── Providers/
│ └── {CompletionProvider}.php
└── tests/
└── ToolsTest.php
{
"name": "your-org/{project-name}",
"description": "{Server description}",
"type": "project",
"require": {
"php": "^8.2",
"mcp/sdk": "^0.1"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
"symfony/cache": "^6.4"
},
"autoload": {
"psr-4": {
"App\\\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\\\": "tests/"
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
}
}
/vendor
/cache
composer.lock
.phpunit.cache
phpstan.neon
# {Project Name}
{Server description}
## Requirements
- PHP 8.2 or higher
- Composer
## Installation
```bash
composer install
php server.php
{
"mcpServers": {
"{project-name}": {
"command": "php",
"args": ["/absolute/path/to/server.php"]
}
}
}
vendor/bin/phpunit
Test with MCP Inspector:
npx @modelcontextprotocol/inspector php server.php
### server.php
```php
#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Mcp\Server;
use Mcp\Server\Transport\StdioTransport;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;
// Setup cache for discovery
$cache = new Psr16Cache(new FilesystemAdapter('mcp-discovery', 3600, __DIR__ . '/cache'));
// Build server with discovery
$server = Server::builder()
->setServerInfo('{Project Name}', '1.0.0')
->setDiscovery(
basePath: __DIR__,
scanDirs: ['src'],
excludeDirs: ['vendor', 'tests', 'cache'],
cache: $cache
)
->build();
// Run with stdio transport
$transport = new StdioTransport();
$server->run($transport);
<?php
declare(strict_types=1);
namespace App\Tools;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Capability\Attribute\Schema;
class ExampleTool
{
/**
* Performs a greeting with the provided name.
*
* @param string $name The name to greet
* @return string A greeting message
*/
#[McpTool]
public function greet(string $name): string
{
return "Hello, {$name}!";
}
/**
* Performs arithmetic calculations.
*/
#[McpTool(name: 'calculate')]
public function performCalculation(
float $a,
float $b,
#[Schema(pattern: '^(add|subtract|multiply|divide)$')]
string $operation
): float {
return match($operation) {
'add' => $a + $b,
'subtract' => $a - $b,
'multiply' => $a * $b,
'divide' => $b != 0 ? $a / $b :
throw new \InvalidArgumentException('Division by zero'),
default => throw new \InvalidArgumentException('Invalid operation')
};
}
}
<?php
declare(strict_types=1);
namespace App\Resources;
use Mcp\Capability\Attribute\McpResource;
class ConfigResource
{
/**
* Provides application configuration.
*/
#[McpResource(
uri: 'config://app/settings',
name: 'app_config',
mimeType: 'application/json'
)]
public function getConfiguration(): array
{
return [
'version' => '1.0.0',
'environment' => 'production',
'features' => [
'logging' => true,
'caching' => true
]
];
}
}
<?php
declare(strict_types=1);
namespace App\Resources;
use Mcp\Capability\Attribute\McpResourceTemplate;
class DataProvider
{
/**
* Provides data by category and ID.
*/
#[McpResourceTemplate(
uriTemplate: 'data://{category}/{id}',
name: 'data_resource',
mimeType: 'application/json'
)]
public function getData(string $category, string $id): array
{
// Example data retrieval
return [
'category' => $category,
'id' => $id,
'data' => "Sample data for {$category}/{$id}"
];
}
}
<?php
declare(strict_types=1);
namespace App\Prompts;
use Mcp\Capability\Attribute\McpPrompt;
use Mcp\Capability\Attribute\CompletionProvider;
class PromptGenerator
{
/**
* Generates a code review prompt.
*/
#[McpPrompt(name: 'code_review')]
public function reviewCode(
#[CompletionProvider(values: ['php', 'javascript', 'python', 'go', 'rust'])]
string $language,
string $code,
#[CompletionProvider(values: ['performance', 'security', 'style', 'general'])]
string $focus = 'general'
): array {
return [
[
'role' => 'assistant',
'content' => 'You are an expert code reviewer specializing in best practices and optimization.'
],
[
'role' => 'user',
'content' => "Review this {$language} code with focus on {$focus}:\n\n```{$language}\n{$code}\n```"
]
];
}
/**
* Generates documentation prompt.
*/
#[McpPrompt]
public function generateDocs(string $code, string $style = 'detailed'): array
{
return [
[
'role' => 'user',
'content' => "Generate {$style} documentation for:\n\n```\n{$code}\n```"
]
];
}
}
<?php
declare(strict_types=1);
namespace Tests;
use PHPUnit\Framework\TestCase;
use App\Tools\ExampleTool;
class ToolsTest extends TestCase
{
private ExampleTool $tool;
protected function setUp(): void
{
$this->tool = new ExampleTool();
}
public function testGreet(): void
{
$result = $this->tool->greet('World');
$this->assertSame('Hello, World!', $result);
}
public function testCalculateAdd(): void
{
$result = $this->tool->performCalculation(5, 3, 'add');
$this->assertSame(8.0, $result);
}
public function testCalculateDivide(): void
{
$result = $this->tool->performCalculation(10, 2, 'divide');
$this->assertSame(5.0, $result);
}
public function testCalculateDivideByZero(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Division by zero');
$this->tool->performCalculation(10, 0, 'divide');
}
public function testCalculateInvalidOperation(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid operation');
$this->tool->performCalculation(5, 3, 'modulo');
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
#[McpTool], #[McpResource], #[McpPrompt] for clean codedeclare(strict_types=1);) in all files#[Schema] attributes for parameter validation#[McpTool]
public function simpleAction(string $input): string
{
return "Processed: {$input}";
}
#[McpTool]
public function validateEmail(
#[Schema(format: 'email')]
string $email
): bool {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
enum Status: string {
case ACTIVE = 'active';
case INACTIVE = 'inactive';
}
#[McpTool]
public function setStatus(string $id, Status $status): array
{
return ['id' => $id, 'status' => $status->value];
}
#[McpResource(uri: 'config://settings', mimeType: 'application/json')]
public function getSettings(): array
{
return ['key' => 'value'];
}
#[McpResourceTemplate(uriTemplate: 'user://{id}')]
public function getUser(string $id): array
{
return $this->users[$id] ?? throw new \RuntimeException('User not found');
}
# Install dependencies
composer install
# Run tests
vendor/bin/phpunit
# Start server
php server.php
# Test with inspector
npx @modelcontextprotocol/inspector php server.php
{
"mcpServers": {
"{project-name}": {
"command": "php",
"args": ["/absolute/path/to/server.php"]
}
}
}
Now generate the complete project based on user requirements!
Weekly Installs
7.4K
Repository
GitHub Stars
27.0K
First Seen
Feb 25, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex7.3K
gemini-cli7.3K
opencode7.3K
cursor7.2K
github-copilot7.2K
kimi-cli7.2K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装