PHP code example of friendsofhyperf / mcp-server

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/ */

    

friendsofhyperf / mcp-server example snippets



return [
    'servers' => [
        [
            'enabled' => true,
            'name' => 'My MCP Server',
            'version' => '1.0.0',
            'description' => 'A powerful MCP server implementation',
            'website_url' => 'https://example.com',
            'icons' => [
                [
                    'url' => 'https://example.com/icon.png',
                    'media_type' => 'image/png',
                    'width' => 64,
                    'height' => 64
                ]
            ],

            // 服务器能力配置
            'capabilities' => [
                'tools' => true,
                'resources' => true,
                'prompts' => true,
                'completions' => true,
            ],

            // 协议版本
            'protocol_version' => '2024-11-05',

            // 分页限制
            'pagination_limit' => 100,

            // 会话配置
            'session' => [
                'ttl' => 3600,
                'store' => \Mcp\Server\Session\SessionInterface::class,
                'factory' => \Mcp\Server\Session\SessionFactory::class,
            ],

            // 类发现配置
            'discovery' => [
                'base_path' => BASE_PATH,
                'scan_dirs' => ['app', 'src'],
                'exclude_dirs' => ['vendor', 'tests'],
                // 'cache' => \Psr\SimpleCache\CacheInterface::class,
            ],

            // 路由配置(HTTP 传输)
            'http' => [
                'path' => '/mcp',
                'options' => [
                    'middleware' => ['auth']
                ],
                // 'server' => 'http', // 指定服务器名称(可选)
            ],

            // 命令行配置(STDIO 传输)
            'stdio' => [
                'name' => 'mcp:server',
                'description' => 'Start MCP server via STDIO'
            ]
        ]
    ]
];



declare(strict_types=1);

namespace App\Tool;

use Mcp\Capability\Attribute\McpTool;

#[McpTool(
    name: 'calculator.add',
    description: 'Add two numbers together'
)]
class CalculatorTool
{
    public function handle(array $params): array
    {
        $result = $params['a'] + $params['b'];
        return [
            'content' => [
                [
                    'type' => 'text',
                    'text' => "The sum of {$params['a']} and {$params['b']} is {$result}"
                ]
            ]
        ];
    }
}



declare(strict_types=1);

namespace App\Resource;

use Mcp\Capability\Attribute\McpResource;

#[McpResource(
    uri: 'file:///var/log/app.log',
    name: 'Application Log',
    description: 'Current application log file',
    mimeType: 'text/plain'
)]
class LogResource
{
    public function handle(array $params): array
    {
        $content = file_get_contents('/var/log/app.log');
        return [
            'contents' => [
                [
                    'uri' => $params['uri'],
                    'mimeType' => 'text/plain',
                    'text' => $content
                ]
            ]
        ];
    }
}



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
    {
        // 自定义请求处理逻辑
    }
}
bash
php bin/hyperf.php vendor:publish friendsofhyperf/mcp-server
bash
# 启动 Hyperf 服务器,MCP 服务将自动注册并启动
php bin/hyperf.php start