Model Context Protocol PHP SDK [WIP] - MCP服务,MCP服务器,MCP教程

基本能力

产品定位

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:
bash
composer require php-llm/mcp-sdk

调试方式

  1. STDIO服务器调试: 使用Symfony控制台命令启动服务器。
    ```php
    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控制器启动服务器。
    ```php
    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();
}

}
```

点击访问

搜索工具

Swift MCP GUI Server - MCP服务,MCP服务器,MCP教程

2025-4-29 16:03:40

搜索工具

PowerPoint Automation MCP Server for Claude Desktop - MCP服务,MCP服务器,MCP教程

2025-4-29 16:03:48

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索