MCP ExplorerExplorer

Mcp Sdk

@php-llmon a year ago
31 MIT
FreeCommunity
AI Systems
# Model Context Protocol SDK for Client and Server Applications, Based on PHP

Overview

What is Mcp Sdk

The mcp-sdk is a Model Context Protocol Software Development Kit designed for building client and server applications in PHP. It facilitates the implementation of the Model Context Protocol, enabling seamless communication between various components of an application.

Use cases

The mcp-sdk can be used in various scenarios, including building real-time applications that require server-sent events, creating command-line tools that interact with server processes, and developing web applications that need efficient communication between the client and server.

How to use

To use the mcp-sdk, install it via Composer with the command ‘composer require php-llm/mcp-sdk’. For integration with Symfony, you can create console commands and controllers that utilize the SDK’s server functionalities, such as STDIO and Server-Sent Events.

Key features

Key features of the mcp-sdk include support for Symfony Console and HttpFoundation, easy integration with server-sent events, and the ability to expose tools using the LLM Chain’s ToolBox. It also provides a structured way to manage communication between client and server applications.

Where to use

undefined

Content

Model Context Protocol PHP SDK [WIP]

Model Context Protocol SDK for Client and Server applications in PHP.
Currently only support Tool Calls as Server via Server-Sent Events (SSE) and STDIO.

See Demo App for a working example and MCP Bundle for Symfony integration.

Installation

composer require php-llm/mcp-sdk

Usage with Symfony

Server integration points for are tailored to Symfony Console and HttpFoundation (Laravel compatible).

Console Command for STDIO Server

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

Controller for Server-Sent Events Server

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

Exposing Tools

Under the hood the SDK uses LLM Chain’s ToolBox to register, analyze and
execute tools. In combination with its Symfony Bundle you can expose
tools with #[AsTool] attribute.

use PhpLlm\LlmChain\ToolBox\Attribute\AsTool;

#[AsTool('company_name', 'Provides the name of your company')]
final class CompanyName
{
    public function __invoke(): string
    {
        return 'ACME Corp.'
    }
}

See LLM Chain Documentation for more information.

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers