MCP PHP 应用程序 是一个设计用于与 MCP PHP 服务器框架 一起工作的样板应用程序。它作为用户在其项目中实现 模型上下文协议 (MCP) 的起点。
在使用此应用程序之前,请确保已安装以下内容:
php --version 应该报告版本为 8.1 或更高。要设置应用程序,请按照以下步骤操作:
克隆此仓库:
git clone https://github.com/james2037/mcp-php-application.git
导航到项目目录:
cd mcp-php-application
使用 Composer 安装依赖项:
composer install
要为此应用程序创建工具,请按照以下步骤操作:
在 tools/ 目录下创建一个新的类。例如:
<?php
namespace App\Tools;
use MCP\Server\Tool\Tool;
use MCP\Server\Tool\Attribute\Tool as ToolAttribute;
use MCP\Server\Tool\Attribute\Parameter as ParameterAttribute;
#[ToolAttribute('calculator', '一个计算器工具')]
class CalculatorTool extends Tool
{
protected function doExecute(
#[ParameterAttribute('operation', type: 'string', description: '要执行的操作(加/减)')]
#[ParameterAttribute('a', type: 'number', description: '第一个数字')]
#[ParameterAttribute('b', type: 'number', description: '第二个数字')]
array $arguments
): array {
$result = match ($arguments['operation']) {
'add' => $arguments['a'] + $arguments['b'],
'subtract' => $arguments['a'] - $arguments['b'],
default => throw new \InvalidArgumentException('无效的操作')
};
return $this->text((string)$result);
}
}
#[ToolAttribute] 注解定义了工具的名称和描述。
工具的参数使用 #[ParameterAttribute] 注解进行定义。
实现 doExecute 方法来定义工具的功能。
要启动 MCP PHP 服务器,请运行以下命令:
php 路径/到/mcp_server.php
mcp_server.php 文件位于此仓库的根目录下。
服务器将启动并根据模型上下文协议监听请求。
目前,您可以编写工具。服务器仅支持 STDIO 传输。新的功能和传输方式即将推出。
欢迎对 MCP PHP 应用程序做出贡献!请随意提交问题或拉取请求以改进功能、文档或示例。
本项目采用 MIT 许可证。