Sponsored by Deepsite.site

PHP MCP Client

Created By
SWIS6 months ago
Model Context Protocol client implementation for PHP
Overview

Model Context Protocol client implementation for PHP

PHP from Packagist Latest Version on Packagist Software License Buy us a tree Build Status Made by SWIS

A PHP client library for interacting with Model Context Protocol (MCP) servers.

Installation

You can install the package via composer:

composer require swisnl/mcp-client

Requirements

Features

  • Multiple transport mechanisms:
    • SSE (Server-Sent Events)
    • Stdio (Standard input/output)
    • Process (External process communication)
    • StreamableHttp (HTTP with session management)
  • Promise-based API with ReactPHP
  • PSR-3 Logger interface support
  • Most of MCP protocol support (2025-03-26)
  • Tool annotation support

Basic Usage

SSE Transport

use Swis\McpClient\Client;

// Create client with SSE transporter
$endpoint = 'https://your-mcp-server.com/sse';
$client = Client::withSse($endpoint);

// Connect to the server
$client->connect(function($initResponse) {
    echo "Connected to server: " . json_encode($initResponse['serverInfo']) . "\n";
});

// List available tools
$tools = $client->listTools();
foreach ($tools->getTools() as $tool) {
    echo "- {$tool->getName()}: {$tool->getDescription()}\n";
    
    // Access tool annotations if available
    if ($annotations = $tool->getAnnotations()) {
        echo "  * Read-only: " . ($annotations->getReadOnlyHint() ? 'Yes' : 'No') . "\n";
        echo "  * Title: " . ($annotations->getTitle() ?? 'N/A') . "\n";
    }
}

// Call a tool
$result = $client->callTool('echo', ['message' => 'Hello World!']);
echo $result->getResult() . "\n";

Process Transport

use Swis\McpClient\Client;

// Create client with a process transporter
[$client, $process] = Client::withProcess('/path/to/mcp-server/binary');

// Connect to the server
$client->connect();

// Use the client...

// Disconnect when done
$client->disconnect();

StreamableHttp Transport

use Swis\McpClient\Client;

// Create client with StreamableHttp transporter
$endpoint = 'https://your-mcp-server.com/';
$client = Client::withStreamableHttp($endpoint);

// Connect to the server
$client->connect();

// The transporter will automatically manage session IDs from the Mcp-Session-Id header

// Use the client...

// Disconnect when done
$client->disconnect();

Use in combination with Agents SDK

First, install Agents SDK

composer require swisnl/agents-sdk
use Swis\Agents\Agent;
use Swis\Agents\Mcp\McpConnection;
use Swis\McpClient\Client;
use Swis\Agents\Orchestrator;

$agent = new Agent(
    name: 'Calculator Agent',
    description: 'This Agent can perform arithmetic operations.',
    mcpConnections: [
        new MathMcpConnection(),
    ]
);

$orchestrator = new Orchestrator($agent);
echo $orchestrator
        ->withUserInstruction('What\'s 5 + 5?')
        ->run($agent)

class MathMcpConnection extends McpConnection
{
    public function __construct()
    {
        [$client, $process] = Client::withProcess(
            command: 'node ' . realpath(__DIR__ . '/node_modules/math-mcp/build/index.js'),
        );

        parent::__construct(
            client: $client,
            name: 'Math MCP',
        );
    }
}

Advanced Usage

Custom Transporter

You can implement your own transporter by implementing the TransporterInterface:

use Swis\McpClient\TransporterInterface;
use Swis\McpClient\EventDispatcher;

class CustomTransporter implements TransporterInterface
{
    // Implement required methods
}

// Create a client with your custom transporter
$transporter = new CustomTransporter();
$eventDispatcher = new EventDispatcher();
$client = new Client($transporter, $eventDispatcher);

Async Operations

The client supports async operations using ReactPHP promises:

$client->sendRequest(new ListToolsRequest())->then(...);

License

This package is open-sourced software licensed under the MIT license.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Recommend Clients
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Cherry Studio🍒 Cherry Studio is a desktop client that supports for multiple LLM providers.
ChatWiseThe second fastest AI chatbot™
y-cli 🚀A Tiny Terminal Chat App for AI Models with MCP Client Support
HyperChatHyperChat is a Chat client that strives for openness, utilizing APIs from various LLMs to achieve the best Chat experience, as well as implementing productivity tools through the MCP protocol.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
LutraLutra is the first MCP compatible client built for everyone
MCP PlaygroundCall MCP Server Tools Online
Cline – #1 on OpenRouterAutonomous coding agent right in your IDE, capable of creating/editing files, executing commands, using the browser, and more with your permission every step of the way.
Refact.aiOpen-source AI Agent for VS Code and JetBrains that autonomously solves coding tasks end-to-end.
CursorThe AI Code Editor
Continue⏩ Create, share, and use custom AI code assistants with our open-source IDE extensions and hub of models, rules, prompts, docs, and other building blocks
MCP ConnectEnables cloud-based AI services to access local Stdio based MCP servers via HTTP requests
DeepChatYour AI Partner on Desktop
A Sleek AI Assistant & MCP Client5ire is a cross-platform desktop AI assistant, MCP client. It compatible with major service providers, supports local knowledge base and tools via model context protocol servers .
Roo Code (prev. Roo Cline)Roo Code (prev. Roo Cline) gives you a whole dev team of AI agents in your code editor.
chatmcpChatMCP is an AI chat client implementing the Model Context Protocol (MCP).
Y GuiA web-based graphical interface for AI chat interactions with support for multiple AI models and MCP (Model Context Protocol) servers.
BACHAI-TWITTER-API45Twitter的一些api mcp
ZedCode at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
WindsurfThe new purpose-built IDE to harness magic