PHP code example of friendsofhyperf / mcp
1. Go to this page and download the library: Download friendsofhyperf/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/ */
friendsofhyperf / mcp example snippets
return [
'servers' => [
[
'name' => 'demo',
'version' => '1.0.0',
'description' => 'This is a demo mcp server.',
// SSE 服务器配置选项
'sse' => [
'server' => 'http',
'endpoint' => '/sse',
'middlewares' => [],
],
// 其他配置选项
'options' => [
'logger' => null,
'enforceStrictCapabilities' => false,
],
],
],
];
namespace App\Controller;
use FriendsOfHyperf\MCP\Annotation\Tool;
class FileController
{
#[Tool(name: 'read_file', description: '读取文件内容', server: 'demo')]
public function readFile(string $path): string
{
return ['toolResult' => file_get_contents($path)];
}
#[Tool(name: 'write_file', description: '写入文件内容', server: 'demo')]
public function writeFile(string $path, string $content): bool
{
return (bool) file_put_contents($path, $content);
}
}
namespace App\Controller;
use FriendsOfHyperf\MCP\Annotation\Resource;
class FileController
{
#[Resource(scheme: 'file', server: 'demo')]
public function getResource(string $path): string
{
return file_get_contents($path);
}
}
namespace App\Controller;
use FriendsOfHyperf\MCP\Annotation\Prompt;
class ChatController
{
#[Prompt(name: 'chat', description: '聊天功能', server: 'demo')]
public function chat(string $message): string
{
return "您发送的消息是:{$message}";
}
}
bash
php bin/hyperf.php vendor:publish friendsofhyperf/mcp
bash
php bin/hyperf.php start
bash
php bin/hyperf.php mcp:run --name=demo
bash
composer analyse