diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ad8b3e..c47d2ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,24 @@ All notable changes to `php-mcp/client` will be documented in this file. +## v1.0.1 - 2025-05-06 + +This patch release introduces an API improvement to the `ClientBuilder` for a more streamlined configuration experience. + +### ✨ Enhancements + +* **Simplified Client Identity Configuration:** The `ClientBuilder` now features a new method `withClientInfo(string $name, string $version)`. This single method replaces the previous separate `withName(string $name)` and `withVersion(string $version)` methods for setting your client application's identity. This makes the builder API slightly more concise. + +### ⚠️ Deprecations + +* The methods `ClientBuilder::withName(string $name)` and `ClientBuilder::withVersion(string $version)` are now **deprecated**. +* Please update your code to use the new `ClientBuilder::withClientInfo(string $name, string $version)` method. +* The deprecated methods will be removed in a future `v2.0.0` release. They will continue to function as before in the `v1.x.x` series to maintain backward compatibility. + +### 📝 Documentation + +* The `README.md` and examples have been updated to reflect the new `withClientInfo()` method and the removal of the `ClientInfo` object from direct user configuration. + ## v1.0.0 - 2025-05-06 ### v1.0.0 - Initial Release @@ -15,81 +33,93 @@ It offers a robust, flexible, and developer-friendly solution designed specifica #### ✨ Key Features * **Client-per-Server Architecture:** Aligns with the MCP specification's core model where each `Client` instance manages a dedicated, stateful connection to a single configured MCP server. + * **Fluent Configuration Builder:** Set up client instances easily using the `Client::make()->with...()->build()` pattern, configuring client identity, capabilities, and the specific server connection details (`ServerConfig`). + * **Dual API for Flexibility:** - * **Synchronous Facade:** Interact with MCP servers using straightforward, blocking methods (e.g., `$client->initialize()`, `$client->listTools()`, `$client->callTool(...)`) for simple integration into traditional PHP scripts and frameworks. The underlying asynchronous complexity is handled internally. - * **Asynchronous API:** Access Promise-based methods (e.g., `$client->initializeAsync()`, `$client->listToolsAsync()`, `$client->callToolAsync(...)`) for advanced use cases, concurrent operations using `React\Promise\all`, or integration into asynchronous PHP applications (ReactPHP, Amp, Swoole). - + + * **Synchronous Facade:** Interact with MCP servers using straightforward, blocking methods (e.g., `$client->initialize()`, `$client->listTools()`, `$client->callTool(...)`) for simple integration into traditional PHP scripts and frameworks. The underlying asynchronous complexity is handled internally. + * **Asynchronous API:** Access Promise-based methods (e.g., `$client->initializeAsync()`, `$client->listToolsAsync()`, `$client->callToolAsync(...)`) for advanced use cases, concurrent operations using `React\Promise\all`, or integration into asynchronous PHP applications (ReactPHP, Amp, Swoole). + * **Multiple Transport Support:** - * **`stdio`:** Seamlessly connect to and manage local MCP server processes via standard input/output, ideal for command-line tools or embedded servers. Uses `react/child-process` internally. - * **`http`:** Connect to remote or local MCP servers over HTTP, handling POST requests for client messages and Server-Sent Events (SSE) for server messages and notifications. Uses `react/http` internally. - + + * **`stdio`:** Seamlessly connect to and manage local MCP server processes via standard input/output, ideal for command-line tools or embedded servers. Uses `react/child-process` internally. + * **`http`:** Connect to remote or local MCP servers over HTTP, handling POST requests for client messages and Server-Sent Events (SSE) for server messages and notifications. Uses `react/http` internally. + * **Explicit Connection Lifecycle:** Clear methods (`initialize`/`initializeAsync`, `disconnect`/`disconnectAsync`) to manage the stateful connection and MCP handshake process. + * **Full MCP Core Feature Support:** Implements core MCP operations: - * Capability Negotiation (`initialize`) - * Listing Tools, Resources, Prompts, Resource Templates - * Calling Tools (`tools/call`) - * Reading Resources (`resources/read`) - * Getting Prompts (`prompts/get`) - * Resource Subscriptions (`resources/subscribe`, `resources/unsubscribe`) *(Requires server capability)* - * Server Log Level Control (`logging/setLevel`) *(Requires server capability)* - * Connectivity Check (`ping`) - + + * Capability Negotiation (`initialize`) + * Listing Tools, Resources, Prompts, Resource Templates + * Calling Tools (`tools/call`) + * Reading Resources (`resources/read`) + * Getting Prompts (`prompts/get`) + * Resource Subscriptions (`resources/subscribe`, `resources/unsubscribe`) *(Requires server capability)* + * Server Log Level Control (`logging/setLevel`) *(Requires server capability)* + * Connectivity Check (`ping`) + * **PSR Compliance:** - * Integrates with `PSR-3 (LoggerInterface)` for flexible logging. - * Supports optional `PSR-16 (SimpleCacheInterface)` for caching server definitions (tools, resources, etc.) via `DefinitionCache`. - * Supports optional `PSR-14 (EventDispatcherInterface)` for handling server-sent notifications (e.g., `ResourceChanged`, `ToolsListChanged`) asynchronously. - + + * Integrates with `PSR-3 (LoggerInterface)` for flexible logging. + * Supports optional `PSR-16 (SimpleCacheInterface)` for caching server definitions (tools, resources, etc.) via `DefinitionCache`. + * Supports optional `PSR-14 (EventDispatcherInterface)` for handling server-sent notifications (e.g., `ResourceChanged`, `ToolsListChanged`) asynchronously. + * **Robust Error Handling:** Provides a hierarchy of specific exceptions (`ConfigurationException`, `ConnectionException`, `HandshakeException`, `RequestException`, `TimeoutException`, `TransportException`, `UnsupportedCapabilityException`, etc.) for predictable error management. + * **Asynchronous Core:** Built on ReactPHP's event loop and promises for efficient, non-blocking I/O handling crucial for `stdio` and `http+sse` transports. + #### 🚀 Getting Started 1. **Install:** - + ```bash composer require php-mcp/client - + + ``` 2. **Configure:** Define your server connection using `ServerConfig` and build a client instance. - + ```php use PhpMcp\Client\Client; - use PhpMcp\Client\Enum\TransportType; - use PhpMcp\Client\Model\ClientInfo; - use PhpMcp\Client\ServerConfig; - - $serverConfig = new ServerConfig( - name: 'my_stdio_server', - transport: TransportType::Stdio, - command: ['php', '/path/to/your/mcp_server.php'], - timeout: 15 - ); - - $clientInfo = new ClientInfo('MyPHPApp', '1.0'); - - $client = Client::make() - ->withClientName($clientInfo->name) // Pass name/version directly - ->withClientVersion($clientInfo->version) - ->withServerConfig($serverConfig) - // ->withLogger(new MyLogger()) // Optional - ->build(); - + use PhpMcp\Client\Enum\TransportType; + use PhpMcp\Client\Model\ClientInfo; + use PhpMcp\Client\ServerConfig; + + $serverConfig = new ServerConfig( + name: 'my_stdio_server', + transport: TransportType::Stdio, + command: ['php', '/path/to/your/mcp_server.php'], + timeout: 15 + ); + + $clientInfo = new ClientInfo('MyPHPApp', '1.0'); + + $client = Client::make() + ->withClientName($clientInfo->name) // Pass name/version directly + ->withClientVersion($clientInfo->version) + ->withServerConfig($serverConfig) + // ->withLogger(new MyLogger()) // Optional + ->build(); + + ``` 3. **Initialize & Interact (Sync Example):** - + ```php try { - $client->initialize(); // Connect & Handshake (blocks) - $tools = $client->listTools(); // Make request (blocks) - print_r($tools); - // ... other interactions ... - } catch (\Throwable $e) { - echo "Error: " . $e->getMessage(); - } finally { - $client->disconnect(); // Disconnect (blocks) - } - + $client->initialize(); // Connect & Handshake (blocks) + $tools = $client->listTools(); // Make request (blocks) + print_r($tools); + // ... other interactions ... + } catch (\Throwable $e) { + echo "Error: " . $e->getMessage(); + } finally { + $client->disconnect(); // Disconnect (blocks) + } + + ``` #### Documentation & Examples diff --git a/README.md b/README.md index e6d5a54..28dadfd 100644 --- a/README.md +++ b/README.md @@ -171,11 +171,10 @@ $httpConfig = new ServerConfig( name: 'remote_web_agent', // Required: Unique ID transport: TransportType::Http, // Required: Transport type timeout: 45.0, // Optional: Request timeout - url: 'http://localhost:8080/', // Required for Http: Base URL (NO /sse) + url: 'http://localhost:8080/sse',// Required for Http: SSE URL headers: [ // Optional for Http: Auth/Custom headers 'Authorization' => 'Bearer xyz789' ], - // sessionId: 'sess_abcdef' // Optional for Http: External session ID ); ``` @@ -195,7 +194,7 @@ $jsonConfig = '{ "timeout": 10 }, "http_api": { - "url": "https://api.example.com/mcp", + "url": "https://api.example.com/mcp/sse", "transport": "http", "headers": {"X-API-Key": "secret"} }