Model Context Protocol PHP SDK [WIP]

Model Context Protocol PHP SDK [WIP]

site icon
2025.04.01 15
PHPPHP SDKLLM交互工具调用开发效率
Model Context Protocol PHP SDK 是一个用于PHP客户端和服务器应用程序的SDK,目前支持通过Server-Sent Events (SSE)和STDIO进行工具调用。它主要用于与LLM(大型语言模型)交互,提供工具注册、分析和执行功能。
View on GitHub

Overview

基本能力

产品定位

Model Context Protocol PHP SDK 是一个用于PHP客户端和服务器应用程序的SDK,专注于与大型语言模型(LLM)的交互,提供工具调用功能。

核心功能

  • 支持通过Server-Sent Events (SSE)和STDIO进行工具调用。
  • 提供工具注册、分析和执行功能。
  • 与Symfony框架集成,支持Symfony Console和HttpFoundation。

适用场景

  • 需要与LLM交互的PHP应用程序。
  • 使用Symfony或Laravel框架的项目。
  • 需要工具调用功能的开发场景。

工具列表

  • ToolBox: 用于注册、分析和执行工具。
  • SymfonyConsoleTransport: 用于STDIO服务器的Symfony控制台传输。
  • StreamTransport: 用于SSE服务器的流传输。
  • CachePoolStore: 用于SSE服务器的缓存存储。

常见问题解答

  • 目前仅支持工具调用作为服务器通过SSE和STDIO。
  • 更多信息可以参考LLM Chain Documentation

使用教程

使用依赖

安装前需要确保已安装PHP和Composer。

安装教程

运行以下命令安装SDK:

composer require php-llm/mcp-sdk

调试方式

  1. STDIO服务器调试: 使用Symfony控制台命令启动服务器。
namespace App\Command;

use PhpLlm\McpSdk\Server;
use PhpLlm\McpSdk\Server\Transport\Stdio\SymfonyConsoleTransport;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand('mcp', 'Starts an MCP server')]
final class McpCommand extends Command
{
    public function __construct(
        private readonly Server $server,
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->server->connect(
            new SymfonyConsoleTransport($input, $output)
        );

        return Command::SUCCESS;
    }
}
  1. SSE服务器调试: 使用Symfony控制器启动服务器。
namespace App\Controller;

use PhpLlm\McpSdk\Server;
use PhpLlm\McpSdk\Server\Transport\Sse\Store\CachePoolStore;
use PhpLlm\McpSdk\Server\Transport\Sse\StreamTransport;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Uid\Uuid;

#[AsController]
#[Route('/mcp', name: 'mcp_')]
final readonly class McpController
{
    public function __construct(
        private Server $server,
        private CachePoolStore $store,
        private UrlGeneratorInterface $urlGenerator,
    ) {
    }

    #[Route('/sse', name: 'sse', methods: ['GET'])]
    public function sse(): StreamedResponse
    {
        $id = Uuid::v4();
        $endpoint = $this->urlGenerator->generate('mcp_messages', ['id' => $id], UrlGeneratorInterface::ABSOLUTE_URL);
        $transport = new StreamTransport($endpoint, $this->store, $id);

        return new StreamedResponse(fn() => $this->server->connect($transport), headers: [
            'Content-Type' => 'text/event-stream',
            'Cache-Control' => 'no-cache',
            'X-Accel-Buffering' => 'no',
        ]);
    }

    #[Route('/messages/{id}', name: 'messages', methods: ['POST'])]
    public function messages(Request $request, Uuid $id): Response
    {
        $this->store->push($id, $request->getContent());

        return new Response();
    }
}

许可证

该项目遵循 MIT 开源许可条款,请参阅 MIT 了解完整条款。