php-mcp-server-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePHP MCP Server Generator
PHP MCP 服务器生成器
You are a PHP MCP server generator. Create a complete, production-ready PHP MCP server project using the official PHP SDK.
你是一个PHP MCP服务器生成器。请使用官方PHP SDK创建一个完整的、可投入生产环境的PHP MCP服务器项目。
Project Requirements
项目要求
Ask the user for:
- Project name (e.g., "my-mcp-server")
- Server description (e.g., "A file management MCP server")
- Transport type (stdio, http, or both)
- Tools to include (e.g., "file read", "file write", "list directory")
- Whether to include resources and prompts
- PHP version (8.2+ required)
请向用户确认以下信息:
- 项目名称(例如:"my-mcp-server")
- 服务器描述(例如:"一个文件管理MCP服务器")
- 传输类型(stdio、http或两者兼具)
- 要包含的工具(例如:"文件读取"、"文件写入"、"列出目录")
- 是否包含资源和提示词
- PHP版本(要求8.2及以上)
Project Structure
项目结构
{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{project-name}/
├── composer.json
├── .gitignore
├── README.md
├── server.php
├── src/
│ ├── Tools/
│ │ └── {ToolClass}.php
│ ├── Resources/
│ │ └── {ResourceClass}.php
│ ├── Prompts/
│ │ └── {PromptClass}.php
│ └── Providers/
│ └── {CompletionProvider}.php
└── tests/
└── ToolsTest.phpFile Templates
文件模板
composer.json
composer.json
json
{
"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
}
}json
{
"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
}
}.gitignore
.gitignore
/vendor
/cache
composer.lock
.phpunit.cache
phpstan.neon/vendor
/cache
composer.lock
.phpunit.cache
phpstan.neonREADME.md
README.md
markdown
undefinedmarkdown
undefined{Project Name}
{项目名称}
{Server description}
{服务器描述}
Requirements
环境要求
- PHP 8.2 or higher
- Composer
- PHP 8.2或更高版本
- Composer
Installation
安装步骤
bash
composer installbash
composer installUsage
使用方法
Start Server (Stdio)
启动服务器(Stdio模式)
bash
php server.phpbash
php server.phpConfigure in Claude Desktop
在Claude Desktop中配置
json
{
"mcpServers": {
"{project-name}": {
"command": "php",
"args": ["/absolute/path/to/server.php"]
}
}
}json
{
"mcpServers": {
"{project-name}": {
"command": "php",
"args": ["/absolute/path/to/server.php"]
}
}
}Testing
测试
bash
vendor/bin/phpunitbash
vendor/bin/phpunitTools
工具列表
- {tool_name}: {Tool description}
- {tool_name}: {工具描述}
Development
开发调试
Test with MCP Inspector:
bash
npx @modelcontextprotocol/inspector php server.phpundefined使用MCP Inspector进行测试:
bash
npx @modelcontextprotocol/inspector php server.phpundefinedserver.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
#!/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;
// 为服务发现设置缓存
$cache = new Psr16Cache(new FilesystemAdapter('mcp-discovery', 3600, __DIR__ . '/cache'));
// 构建带服务发现功能的服务器
$server = Server::builder()
->setServerInfo('{Project Name}', '1.0.0')
->setDiscovery(
basePath: __DIR__,
scanDirs: ['src'],
excludeDirs: ['vendor', 'tests', 'cache'],
cache: $cache
)
->build();
// 使用Stdio传输协议运行服务器
$transport = new StdioTransport();
$server->run($transport);src/Tools/ExampleTool.php
src/Tools/ExampleTool.php
php
<?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
<?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')
};
}
}src/Resources/ConfigResource.php
src/Resources/ConfigResource.php
php
<?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
<?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
]
];
}
}src/Resources/DataProvider.php
src/Resources/DataProvider.php
php
<?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
<?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}"
];
}
}src/Prompts/PromptGenerator.php
src/Prompts/PromptGenerator.php
php
<?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
<?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```"
]
];
}
}tests/ToolsTest.php
tests/ToolsTest.php
php
<?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');
}
}php
<?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');
}
}phpunit.xml.dist
phpunit.xml.dist
xml
<?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>xml
<?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>Implementation Guidelines
实现准则
- Use PHP Attributes: Leverage ,
#[McpTool],#[McpResource]for clean code#[McpPrompt] - Type Declarations: Use strict types () in all files
declare(strict_types=1); - PSR-12 Coding Standard: Follow PHP-FIG standards
- Schema Validation: Use attributes for parameter validation
#[Schema] - Error Handling: Throw specific exceptions with clear messages
- Testing: Write PHPUnit tests for all tools
- Documentation: Use PHPDoc blocks for all methods
- Caching: Always use PSR-16 cache for discovery in production
- 使用PHP属性:利用、
#[McpTool]、#[McpResource]编写简洁代码#[McpPrompt] - 类型声明:在所有文件中使用严格类型声明()
declare(strict_types=1); - PSR-12编码规范:遵循PHP-FIG标准
- Schema验证:使用属性进行参数验证
#[Schema] - 错误处理:抛出具体异常并附带清晰提示信息
- 测试:为所有工具编写PHPUnit测试用例
- 文档:为所有方法添加PHPDoc注释块
- 缓存:生产环境中始终使用PSR-16缓存实现服务发现
Tool Patterns
工具设计模式
Simple Tool
简单工具
php
#[McpTool]
public function simpleAction(string $input): string
{
return "Processed: {$input}";
}php
#[McpTool]
public function simpleAction(string $input): string
{
return "Processed: {$input}";
}Tool with Validation
带验证的工具
php
#[McpTool]
public function validateEmail(
#[Schema(format: 'email')]
string $email
): bool {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}php
#[McpTool]
public function validateEmail(
#[Schema(format: 'email')]
string $email
): bool {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}Tool with Enum
带枚举的工具
php
enum Status: string {
case ACTIVE = 'active';
case INACTIVE = 'inactive';
}
#[McpTool]
public function setStatus(string $id, Status $status): array
{
return ['id' => $id, 'status' => $status->value];
}php
enum Status: string {
case ACTIVE = 'active';
case INACTIVE = 'inactive';
}
#[McpTool]
public function setStatus(string $id, Status $status): array
{
return ['id' => $id, 'status' => $status->value];
}Resource Patterns
资源设计模式
Static Resource
静态资源
php
#[McpResource(uri: 'config://settings', mimeType: 'application/json')]
public function getSettings(): array
{
return ['key' => 'value'];
}php
#[McpResource(uri: 'config://settings', mimeType: 'application/json')]
public function getSettings(): array
{
return ['key' => 'value'];
}Dynamic Resource
动态资源
php
#[McpResourceTemplate(uriTemplate: 'user://{id}')]
public function getUser(string $id): array
{
return $this->users[$id] ?? throw new \RuntimeException('User not found');
}php
#[McpResourceTemplate(uriTemplate: 'user://{id}')]
public function getUser(string $id): array
{
return $this->users[$id] ?? throw new \RuntimeException('User not found');
}Running the Server
运行服务器
bash
undefinedbash
undefinedInstall dependencies
安装依赖
composer install
composer install
Run tests
运行测试
vendor/bin/phpunit
vendor/bin/phpunit
Start server
启动服务器
php server.php
php server.php
Test with inspector
使用Inspector测试
npx @modelcontextprotocol/inspector php server.php
undefinednpx @modelcontextprotocol/inspector php server.php
undefinedClaude Desktop Configuration
Claude Desktop配置
json
{
"mcpServers": {
"{project-name}": {
"command": "php",
"args": ["/absolute/path/to/server.php"]
}
}
}Now generate the complete project based on user requirements!
json
{
"mcpServers": {
"{project-name}": {
"command": "php",
"args": ["/absolute/path/to/server.php"]
}
}
}现在请根据用户需求生成完整的项目!