返回市场
MCP软件开发工具包

MCP软件开发工具包

作者:php-llm35 星标更新:2025-07-17

项目介绍

模型上下文协议 PHP SDK [正在进行中]

[!重要]
PHP 大型语言模型变为 Symfony AI - 该项目已移至 github.com/symfony/ai。 请使用新的仓库进行所有未来的开发、问题报告和贡献。 感谢您的贡献 - 希望在 Symfony AI 中见到您!

适用于客户端和服务器应用程序的模型上下文协议 SDK,目前仅支持通过服务器发送事件(SSE)和标准输入输出(STDIO)作为服务器的工具调用。

查看 演示应用 获取一个工作示例,并查看 MCP 组件 以了解 Symfony 集成。

安装

composer require php-llm/mcp-sdk

使用 Symfony

服务器集成点针对 Symfony 控制台和 HttpFoundation(兼容 Laravel)进行了定制。

控制台命令用于 STDIO 服务器

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', '启动 MCP 服务器')]
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;
    }
}

控制器用于服务器发送事件服务器

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();
    }
}

暴露工具

SDK 在内部使用了 LLM ChainToolBox 来注册、分析和执行工具。结合其 Symfony 组件,您可以使用 #[AsTool] 属性来暴露工具。

use PhpLlm\LlmChain\ToolBox\Attribute\AsTool;

#[AsTool('company_name', '提供您的公司名称')]
final class CompanyName
{
    public function __invoke(): string
    {
        return 'ACME Corp.';
    }
}

参见 LLM Chain 文档 获取更多信息。