1. Go to this page and download the library: Download friendsofhyperf/mcp-server 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/ */
declare(strict_types=1);
namespace App\Prompt;
use Mcp\Capability\Attribute\McpPrompt;
#[McpPrompt(
name: 'code-review',
description: 'Generate code review suggestions'
)]
class CodeReviewPrompt
{
public function handle(array $params): array
{
$prompt = "Please review the following code and provide suggestions for improvement:\n\n";
$prompt .= $params['code'] ?? '';
return [
'messages' => [
[
'role' => 'user',
'content' => [
'type' => 'text',
'text' => $prompt
]
]
]
];
}
}
use Mcp\Server\Session\SessionInterface;
class CustomSessionStore implements SessionInterface
{
public function get(string $sessionId): ?array
{
// 自定义会话获取逻辑
}
public function set(string $sessionId, array $data, int $ttl = null): void
{
// 自定义会话存储逻辑
}
public function delete(string $sessionId): void
{
// 自定义会话删除逻辑
}
}
use Mcp\Server\Handler\Request\RequestHandlerInterface;
use Mcp\Server\Transport\TransportInterface;
class CustomRequestHandler implements RequestHandlerInterface
{
public function canHandle(string $method): bool
{
return $method === 'custom/method';
}
public function handle(array $params, TransportInterface $transport): mixed
{
// 自定义请求处理逻辑
}
}