PHP code example of hyperf / mcp-incubator

1. Go to this page and download the library: Download hyperf/mcp-incubator 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/ */

    

hyperf / mcp-incubator example snippets




use Hyperf\Mcp\Annotation\Tool;
use Hyperf\Mcp\Server\Annotation\Server;

#[Server(name: 'stdio', signature: 'mcp:command', description: '这是一个测试命令')]
class Foo
{
    #[Tool(name: 'sum', description: '计算两个数的和', serverName: 'stdio')]
    public function sum(#[Description('这是A参数')] int $a, #[Description('这是B参数')] int $b = 0): int
    {
        return $a + $b;
    }
}



use Hyperf\Server\Server;
use Hyperf\Server\Event;
use Hyperf\Mcp\Server\McpServer;

return [
    'type' => Hyperf\Server\CoroutineServer::class, # 建议协程风格
    'servers' => [
        'mcp-sse' => [
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 3000,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [McpServer::class, 'onRequest'],
                Event::ON_CLOSE => [McpServer::class, 'onClose'],
            ],
            'options' => [
                'mcp_path' => '/sse',
            ],
        ],
    ],
];



use Hyperf\Mcp\Annotation\Tool;
use Hyperf\Mcp\Server\Annotation\Server;

#[Server(name: 'mcp-sse', description: '这是一个测试命令')]
class Foo
{
    #[Tool(name: 'sum', description: '计算两个数的和', serverName: 'mcp-sse')]
    public function sum(#[Description('这是A参数')] int $a, #[Description('这是B参数')] int $b = 0): int
    {
        return $a + $b;
    }
}