PHPackages                             php-mcp/client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. php-mcp/client

ActiveLibrary

php-mcp/client
==============

PHP Client for the Model Context Protocol (MCP)

1.0.1(1y ago)5513.4k↓21.7%12[1 issues](https://github.com/php-mcp/client/issues)[1 PRs](https://github.com/php-mcp/client/pulls)MITPHPCI passing

Since May 6Pushed 1y ago3 watchersCompare

[ Source](https://github.com/php-mcp/client)[ Packagist](https://packagist.org/packages/php-mcp/client)[ RSS](/packages/php-mcp-client/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (14)Versions (3)Used By (0)

PHP MCP Client
==============

[](#php-mcp-client)

[![Latest Version on Packagist](https://camo.githubusercontent.com/13c067a50d736a60623b1f7bbbf7bc9094d0007762d4316486ea21b779aedcd9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068702d6d63702f636c69656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/php-mcp/client)[![Total Downloads](https://camo.githubusercontent.com/d196519910531ca70630f7eb664452317a6b39b21e9b4a23d714b3fcc559b48f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7068702d6d63702f636c69656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/php-mcp/client)[![Tests](https://camo.githubusercontent.com/8201e5095aa857674985113558b3e3e034d6779da2c936700d131b8157f8e42e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7068702d6d63702f636c69656e742f74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/php-mcp/client/actions/workflows/tests.yml)[![License](https://camo.githubusercontent.com/44b1814810ac8991334b19688cff68a79d850fcf20086d4008915d841f621914/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7068702d6d63702f636c69656e742e7376673f7374796c653d666c61742d737175617265)](LICENSE)

**PHP MCP Client is a PHP library for interacting with servers that implement the Model Context Protocol (MCP).**

It provides a developer-friendly interface to connect to individual MCP servers using different transports (`stdio`, `http+sse`), manage the connection lifecycle, discover server capabilities (Tools, Resources, Prompts), and execute requests like calling tools or reading resources.

While utilizing asynchronous I/O internally via ReactPHP for robustness and handling features like server-sent events, the library offers **both** a straightforward **synchronous (blocking) API** for common use cases and an **asynchronous (Promise-based) API** for advanced control and concurrency.

This library aligns with the MCP specification's model where one client instance manages a stateful connection to one server.

Introduction to MCP
-------------------

[](#introduction-to-mcp)

The [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) is an open standard designed to standardize how AI assistants and applications connect to external data sources, APIs, and tools (like codebases, databases, web browsers). It acts as a communication layer, enabling AI models (like Claude, or models integrated via frameworks like OpenAI's) to securely access and interact with context provided by different servers.

This client library allows your PHP application (acting as the "Host" in MCP terminology) to *consume* capabilities offered by one or more MCP servers.

Features
--------

[](#features)

- **Client-per-Server Model:** Each `Client` instance manages a stateful connection to a single configured MCP server, aligning with the MCP specification.
- **Fluent Configuration:** Easy setup for each client instance using a `Client::make()->with...()` builder pattern.
- **Dual API:**
    - **Synchronous Facade:** Interact with the server using straightforward, blocking methods (e.g., `$client->listTools()`, `$client->callTool(...)`) for simple integration.
    - **Asynchronous API:** Access underlying Promise-based methods (e.g., `$client->listToolsAsync()`, `$client->callToolAsync(...)`) for concurrency and integration with async PHP applications.
- **Multiple Transports:** Built-in support for:
    - `stdio`: Communicating with server processes via Standard Input/Output.
    - `http`: Communicating with servers via HTTP POST and Server-Sent Events (SSE).
- **Explicit Connection Lifecycle:** Requires `->initialize()` or `->initializeAsync()` to connect and perform the handshake before making requests. Provides `disconnect()` / `disconnectAsync()`.
- **Tool/Resource/Prompt Interaction:** Provides comprehensive methods (sync &amp; async) to list available elements and execute requests like `tools/call`, `resources/read`, `prompts/get`.
- **PSR Compliance:** Integrates with standard PHP interfaces:
    - `PSR-3` (LoggerInterface): Integrate your application's logger.
    - `PSR-16` (SimpleCacheInterface): Optional caching for server definitions.
    - `PSR-14` (EventDispatcherInterface): Optional handling of server-sent notifications via events (requires async handling).
- **Robust Error Handling:** Specific exceptions for different failure modes.
- **Asynchronous Core:** Utilizes ReactPHP internally for non-blocking I/O.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Composer
- *(For Stdio Transport)*: Ability to execute the server command.
- *(For Http Transport)*: Network access to the MCP server URL.

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require php-mcp/client
```

The necessary ReactPHP dependencies (`event-loop`, `promise`, `stream`, `child-process`, `http`) should be installed automatically.

Quick Start: Simple Synchronous Usage (Stdio)
---------------------------------------------

[](#quick-start-simple-synchronous-usage-stdio)

This example connects to a local filesystem server running via `npx`.

```
