PHP code example of pfinalclub / php-mcp
1. Go to this page and download the library: Download pfinalclub/php-mcp library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pfinalclub / php-mcp example snippets
declare(strict_types=1);
PFPMcp\Tools\Calculator;
// 创建服务器实例
$server = new Server();
// 注册工具
$server->registerTool(new Calculator());
// 启动服务器
$server->start();
declare(strict_types=1);
namespace PFPMcp\Tools;
use PhpMcp\Attributes\McpTool;
use PhpMcp\Attributes\Schema;
class CustomTool
{
#[McpTool(
name: 'custom_action',
description: '执行自定义操作'
)]
public function execute(
#[Schema(description: '输入参数')]
string $input
): array {
return [
'success' => true,
'result' => strtoupper($input),
'timestamp' => time()
];
}
}
declare(strict_types=1);
use PFPMcp\Server\Server;
use PFPMcp\Config\ServerConfig;
$config = new ServerConfig([
'transport' => 'http',
'host' => '0.0.0.0',
'port' => 8080,
'log_level' => 'info',
'max_connections' => 1000
]);
$server = new Server($config);
$server->start();
declare(strict_types=1);
return [
'transport' => $_ENV['MCP_TRANSPORT'] ?? 'stdio',
'host' => $_ENV['MCP_HOST'] ?? '0.0.0.0',
'port' => (int)($_ENV['MCP_PORT'] ?? 8080),
'log_level' => $_ENV['MCP_LOG_LEVEL'] ?? 'info',
'session' => [
'backend' => $_ENV['MCP_SESSION_BACKEND'] ?? 'memory',
'ttl' => (int)($_ENV['MCP_SESSION_TTL'] ?? 3600),
],
'security' => [
'rate_limit' => (int)($_ENV['MCP_RATE_LIMIT'] ?? 100),
'rate_window' => (int)($_ENV['MCP_RATE_WINDOW'] ?? 60),
],
'performance' => [
'max_connections' => (int)($_ENV['MCP_MAX_CONNECTIONS'] ?? 1000),
'timeout' => (int)($_ENV['MCP_TIMEOUT'] ?? 30),
],
];
bash
composer bash
# 克隆仓库
git clone https://github.com/pfinalclub/php-mcp.git
cd php-mcp
# 安装依赖
composer install
# 运行测试
composer test
# 代码格式化
composer fix