Download the PHP package php-mcp/client without Composer

On this page you can find all versions of the php package php-mcp/client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package client

PHP MCP Client

Latest Version on Packagist Total Downloads Tests

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

The Model Context Protocol (MCP) 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

Requirements

Installation

Install the package via Composer:

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

Quick Start: Simple Synchronous Usage (Stdio)

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

Configuration

Configuration involves setting up:

  1. Client Identity: Your application's name and version, passed directly to the builder.
  2. Client Capabilities: Features your client supports using ClientCapabilities.
  3. Server Connection: Details for the single server this client instance will connect to, using ServerConfig.
  4. (Optional) Dependencies: Logger, Cache, Event Dispatcher, Event Loop.

ClientCapabilities

Declares features your client supports. Use the static factory method.

ServerConfig

Defines how to connect to a single MCP server.

Loading Config from Array/JSON

You can easily parse configurations stored in arrays (e.g., from JSON files or framework config).

ClientBuilder

Use the builder to assemble the Client instance:

Usage

Once you have a configured Client instance for a specific server:

1. Initialize the Connection:

You must call initialize() or initializeAsync() before making requests.

2. Making Requests:

Use the client methods. They operate on the single connection established by initialize().

3. Disconnecting:

Always disconnect when you are finished interacting with a server to release resources (especially for stdio transports).

Available Client Methods

The Client class provides methods for interacting with the connected MCP server. Most methods have both a synchronous (blocking) and an asynchronous (Promise-returning) variant.

Connection & Lifecycle:

MCP Operations (Sync):

(These methods require the client to be initialized first and will block)

MCP Operations (Async):

(These methods require the client to be initialized first and return React\Promise\PromiseInterface)

Advanced:

Handling Server Notifications (Asynchronous Only)

MCP servers can send notifications (e.g., resources/didChange). To receive these:

  1. Configure the client with a PSR-14 EventDispatcherInterface using ->withEventDispatcher(...).
  2. Add listeners to your dispatcher for events like PhpMcp\Client\Event\ResourceChanged.
  3. Use the asynchronous API (initializeAsync, potentially other *Async methods).
  4. Run the event loop continuously ($client->getLoop()->run()). Notifications arrive via the underlying transport (usually SSE) only while the loop is active.

See examples/04-handling-notifications.php for a conceptual guide.

Error Handling

The client uses specific exceptions inheriting from PhpMcp\Client\Exception\McpClientException. Catching these allows for targeted error handling:

Always wrap client interactions in try...catch blocks to handle these potential failures gracefully.

Examples

See the examples/ directory for working code:

Testing

Contributing

Contributions are welcome! Please see CONTRIBUTING.md or open an issue/pull request.

License

The MIT License (MIT). See LICENSE.


All versions of client with dependencies

PHP Build Version
Package Version
Requires psr/log Version ^1.0 || ^2.0 || ^3.0
psr/simple-cache Version ^1.0 || ^2.0 || ^3.0
psr/event-dispatcher Version ^1.0
psr/container Version ^1.0 || ^2.0
react/async Version ^4.0
react/child-process Version ^0.6.6
react/event-loop Version ^1.5
react/http Version ^1.11
react/promise Version ^3.2
react/stream Version ^1.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package php-mcp/client contains the following files

Loading the files please wait ....