PHP code example of x2nx / webman-mcp
1. Go to this page and download the library: Download x2nx/webman-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/ */
x2nx / webman-mcp example snippets
// 处理 MCP 消息
$message = '{"jsonrpc":"2.0","id":"1","method":"tools/list"}';
$sessionId = '550e8400-e29b-41d4-a716-446655440000';
$result = mcp_server_handle_message($message, $sessionId);
// 返回格式化的响应消息数组
if ($result) {
foreach ($result as $response) {
echo "Session ID: " . $response['session_id'] . "\n";
echo "Message: " . $response['mcp_message'] . "\n";
}
}
function mcp_server_handle_message(string $message = '', string $sessionId = ''): mixed
// 获取 MCP 服务器实例
$server = mcp_server();
// 或者设置自定义服务器实例
$customServer = McpServer::builder()->build();
$server = mcp_server($customServer);
function mcp_server(?McpServer $server = null): McpServer
use X2nx\WebmanMcp\Process\Server;
// 使用单例实例
$server = Server::instance();
$result = $server->handleMessage($message, $sessionId);
// 或创建新实例
$server = new Server();
$result = $server->handleMessage($message, $sessionId);
Route::post('/api/mcp', function (Request $request) {
$message = $request->post('message');
$sessionId = $request->post('session_id', '');
return json(mcp_server_handle_message($message, $sessionId));
});
class ProcessMcpMessageJob {
public function handle($message, $sessionId) {
return mcp_server_handle_message($message, $sessionId);
}
}
php artisan mcp:process "{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"method\":\"tools/list\"}"
public function testMcpMessage() {
$result = mcp_server_handle_message($testMessage);
$this->assertIsArray($result);
}
use Mcp\Schema\Enum\ProtocolVersion;
return [
// 服务器配置
'server' => [
'name' => 'MCP Server',
'version' => '1.0.0',
'description' => 'MCP Server with Multi-Transport Support for Webman',
// 协议版本
'protocol_version' => ProtocolVersion::V2025_06_18,
// 分页配置
'pagination' => 50,
// 服务器指令描述
'instructions' => '',
// 服务器能力配置
'capabilities' => [],
// 组件发现配置
'discover' => [
'base_path' => base_path(),
'scan_dirs' => [
'app/mcp', // 扫描 MCP 组件目录
],
'exclude_dirs' => [
'vendor', // 排除第三方包
'runtime', // 排除运行时文件
'database', // 排除数据库文件
'docker', // 排除 Docker 文件
'public', // 排除公共文件
'config', // 排除配置文件
'support', // 排除支持文件
],
'cache' => [
'enable' => false, // 是否启用发现缓存
'ttl' => 3600, // 缓存过期时间(秒)
'store' => '', // 缓存存储名称,空则使用默认
],
],
// 传输协议配置
'transport' => [
'sse' => [
'enable' => true, // 启用 SSE 传输
'route' => [
'/sse', // SSE 连接端点
'/message' // SSE 消息发送端点
],
],
'stream' => [
'enable' => true, // 启用流式传输
'route' => [
'/mcp', // 流式传输端点
],
],
],
// 会话缓存配置
'session' => [
'ttl' => 3600, // 会话过期时间(秒)
'store' => '', // 会话存储名称,空则使用默认
]
]
];
'discover' => [
'cache' => [
'enable' => true,
'ttl' => 3600,
],
],
'session' => [
'store' => 'redis', // 使用 Redis 存储
],
// config/process.php
'mcp' => [
'handler' => ...,
'count' => 4, // 根据 CPU 核心数调整
],
return [
'http' => [
'listen' => 'http://0.0.0.0:7190', // 修改端口
'context' => [],
],
];
'discover' => [
'cache' => [
'enable' => true, // 启用缓存
'ttl' => 3600, // 缓存时间(秒)
],
],
// 方式 1: 使用全局辅助函数(推荐)
$result = mcp_server_handle_message($message, $sessionId);
// 方式 2: 使用 Server 类
$server = Server::instance();
$result = $server->handleMessage($message, $sessionId);
[
[
'session_id' => '550e8400-e29b-41d4-a716-446655440000',
'mcp_message' => '{"jsonrpc":"2.0","id":"1","result":{...}}'
],
// ... 更多响应消息
]
bash
# 开发模式 - stdio
php mcp-stdio.php
# 生产模式 - HTTP/SSE
php webman start
bash
# 创建用户管理工具
php webman make:mcp-tool UserManager \
--name=get_user_info \
--description="获取用户详细信息"
# 创建文件操作工具
php webman make:mcp-tool FileManager \
--name=read_file \
--description="读取文件内容"
bash
# 创建代码审查提示
php webman make:mcp-prompt CodeReview \
--name=code_review \
--description="代码质量审查和优化建议"
# 创建文档生成提示
php webman make:mcp-prompt DocGenerator \
--name=generate_docs \
--description="自动生成API文档"
bash
# 创建配置资源
php webman make:mcp-resource ConfigResource \
--name=get_config \
--description="获取系统配置信息"
# 创建数据库资源
php webman make:mcp-resource DatabaseResource \
--name=query_db \
--description="执行数据库查询"
bash
# 创建配置模板
php webman make:mcp-template ConfigResourceTemplate \
--name=get_config \
--description="获取配置信息模板"
# 创建API模板
php webman make:mcp-template ApiResourceTemplate \
--name=api_docs \
--description="API文档模板"
bash
# 启动生产服务(后台运行)
php webman start -d
# 查看服务状态
php webman status
# 停止服务
php webman stop
# 重启服务
php webman restart
# 查看日志
tail -f runtime/logs/webman.log
bash
# 创建示例工具
php webman make:mcp-tool ExampleTool --name=example --description="示例工具"
# 重启服务
php webman restart