PHP应用框架

MCP PHP应用是一个基于Model Context Protocol的PHP应用框架,用于快速开发和集成工具。

  • by james2037
  • PHP
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 james2037/mcp-php-application

使用说明

MCP PHP应用程序

这 MCP PHP应用程序 是一个存根应用程序,旨在与 MCP PHP服务器框架它为用户实施 模型上下文协议 (MCP)在他们的项目中。

先决条件

在使用此应用程序之前,请确保已安装以下内容:

  1. PHP程序:跑步 php --version 应报告8.1或更高版本。
  2. 作曲家PHP的依赖管理器。你可以找到它 这里.

安装

要设置应用程序,请执行以下步骤:

  1. 克隆此存储库:

    git clone https://github.com/james2037/mcp-php-application.git
    
  2. 导航到项目目录:

    cd mcp-php-application
    
  3. 使用Composer安装依赖项:

    composer install
    

创建工具

要为应用程序创建工具,请执行以下步骤:

  1. 在中创建一个新类 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', 'A calculator tool')]
    class CalculatorTool extends Tool
    {
        protected function doExecute(
            #[ParameterAttribute('operation', type: 'string', description: 'Operation to perform (add/subtract)')]
            #[ParameterAttribute('a', type: 'number', description: 'First number')]
            #[ParameterAttribute('b', type: 'number', description: 'Second number')]
            array $arguments
        ): array {
            $result = match ($arguments['operation']) {
                'add' => $arguments['a'] + $arguments['b'],
                'subtract' => $arguments['a'] - $arguments['b'],
                default => throw new \InvalidArgumentException('Invalid operation')
            };
    
            return $this->text((string)$result);
        }
    }
    
  2. 这 #[ToolAttribute] 注释定义了工具的名称和描述。

  3. 工具的参数是通过以下方式定义的 #[ParameterAttribute] 注释。

  4. 实施 doExecute 定义工具功能的方法。

运行服务器

要启动MCP PHP服务器,请运行以下命令:

php path/to/mcp_server.php

这 mcp_server.php 文件位于此存储库的根目录中。

服务器将启动并监听基于模型上下文协议的请求。

特性

目前,你可以编写工具。服务器仅支持STDIO传输。新的能力和运输即将到来。

贡献

欢迎对MCP PHP应用程序的贡献!请随时提交问题或拉取请求以改进功能、文档或示例。

许可证

该项目根据 MIT许可证.