返回市场
MCP-PHP应用服务器

MCP-PHP应用服务器

作者:james20373 星标更新:2025-01-13

项目介绍

MCP PHP 应用程序

MCP PHP 应用程序 是一个设计用于与 MCP PHP 服务器框架 一起工作的样板应用程序。它作为用户在其项目中实现 模型上下文协议 (MCP) 的起点。

预备条件

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

  1. PHP:运行 php --version 应该报告版本为 8.1 或更高。
  2. Composer: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', '一个计算器工具')]
    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);
        }
    }
    
  2. #[ToolAttribute] 注解定义了工具的名称和描述。

  3. 工具的参数使用 #[ParameterAttribute] 注解进行定义。

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

运行服务器

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

php 路径/到/mcp_server.php

mcp_server.php 文件位于此仓库的根目录下。

服务器将启动并根据模型上下文协议监听请求。

功能

目前,您可以编写工具。服务器仅支持 STDIO 传输。新的功能和传输方式即将推出。

贡献

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

许可证

本项目采用 MIT 许可证