This project provides a complete implementation of an MCP (Model Control Protocol) server framework in PHP. It allows developers to define MCP services elegantly using annotations and supports various handlers such as Tool, Prompt, and Resource. The framework also includes a comprehensive logging system and Docker support.
git clone https://github.com/he426100/php-mcp-server
cd php-mcp-server
composer install
php bin/console mcp:test-server
The framework provides three core annotations for defining MCP services:
Used to define tool handlers:
use Mcp\Annotation\Tool;
class MyService {
#[Tool(
name: 'calculate-sum',
description: '计算两个数的和',
parameters: [
'num1' => [
'type' => 'number',
'description' => '第一个数字',
'required' => true
],
'num2' => [
'type' => 'number',
'description' => '第二个数字',
'required' => true
]
]
)]
public function sum(int $num1, int $num2): int
{
return $num1 + $num2;
}
}
Used to define prompt template handlers:
use Mcp\Annotation\Prompt;
class MyService {
#[Prompt(
name: 'greeting',
description: '生成问候语',
arguments: [
'name' => [
'description' => '要问候的人名',
'required' => true
]
]
)]
public function greeting(string $name): string
{
return "Hello, {$name}!";
}
}
Used to define resource handlers:
use Mcp\Annotation\Resource;
class MyService {
#[Resource(
uri: 'example://greeting',
name: 'Greeting Text',
description: '问候语资源',
mimeType: 'text/plain'
)]
public function getGreeting(): string
{
return "Hello from MCP server!";
}
}
namespace Your\Namespace;
use Mcp\Annotation\Tool;
use Mcp\Annotation\Prompt;
use Mcp\Annotation\Resource;
class CustomService
{
#[Tool(name: 'custom-tool', description: '自定义工具')]
public function customTool(): string
{
return "Custom tool result";
}
}
namespace Your\Namespace\Command;
use He426100\McpServer\Command\AbstractMcpServerCommand;
use Your\Namespace\CustomService;
class CustomServerCommand extends AbstractMcpServerCommand
{
protected string $serverName = 'custom-server';
protected string $serviceClass = CustomService::class;
protected function configure(): void
{
parent::configure();
$this->setName('custom:server')
->setDescription('运行自定义 MCP 服务器');
}
}
In composer.json
, add:
{
"autoload": {
"psr-4": {
"Your\\Namespace\\": "src/"
}
}
}
Parameter | Type | Description | Required |
---|---|---|---|
name | string | Tool name | Yes |
description | string | Tool description | Yes |
parameters | array | Parameter definition | No |
Parameter | Type | Description | Required |
---|---|---|---|
name | string | Prompt template name | Yes |
description | string | Prompt description | Yes |
arguments | array | Argument definition | No |
Parameter | Type | Description | Required |
---|---|---|---|
uri | string | Resource URI | Yes |
name | string | Resource name | Yes |
description | string | Resource description | Yes |
mimeType | string | MIME type | No |
Server logs are saved by default in runtime/server_log.txt
. You can modify this by extending AbstractMcpServerCommand
:
protected string $logFilePath = '/custom/path/to/log.txt';
Build and run the container:
docker build -t php-mcp-server .
docker run -i --rm php-mcp-server
Issues and Pull Requests are welcome.