MCP PHP SDK

Client

Main MCP Client facade.

Provides a synchronous API for communicating with MCP servers. All blocking operations are delegated to the transport.

Tags
author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Constants

RETRY_BASE_DELAY_MS  : mixed = 100

Properties

$config  : Configuration
$logger  : LoggerInterface
$protocol  : Protocol
$transport  : TransportInterface|null

Methods

__construct()  : mixed
builder()  : Builder
Create a new client builder for fluent configuration.
callTool()  : CallToolResult
Call a tool on the server.
complete()  : CompletionCompleteResult
Request completion suggestions for a prompt or resource argument.
connect()  : void
Connect to an MCP server using the provided transport.
disconnect()  : void
Disconnect from the server.
getInstructions()  : string|null
Get server instructions.
getPrompt()  : GetPromptResult
Get a prompt from the server.
getProtocolVersion()  : ProtocolVersion|null
Protocol revision negotiated during the handshake.
getServerInfo()  : Implementation|null
Get server information from initialization.
isConnected()  : bool
Check if connected and initialized.
listPrompts()  : ListPromptsResult
List available prompts from the server.
listResources()  : ListResourcesResult
List available resources from the server.
listResourceTemplates()  : ListResourceTemplatesResult
List available resource templates from the server.
listTools()  : ListToolsResult
List available tools from the server.
ping()  : void
Send a ping request to the server.
readResource()  : ReadResourceResult
Read a resource by URI.
sendRootsListChanged()  : void
Notify the server that the client's list of roots has changed.
setLoggingLevel()  : void
Set the minimum logging level for server log messages.
sendRequest()  : Response<string|int, mixed>
Send a request to the server and wait for response.

Constants

RETRY_BASE_DELAY_MS

private mixed RETRY_BASE_DELAY_MS = 100

Properties

$logger read-only

private LoggerInterface $logger = new NullLogger()

Methods

builder()

Create a new client builder for fluent configuration.

public static builder() : Builder
Return values
Builder

callTool()

Call a tool on the server.

public callTool(string $name[, array<string, mixed> $arguments = [] ][, callable(float $progress, Array $total, Array $message): void|null $onProgress = null ]) : CallToolResult
Parameters
$name : string

Tool name

$arguments : array<string, mixed> = []

Tool arguments

$onProgress : callable(float $progress, Array $total, Array $message): void|null = null

Optional callback for progress updates

Return values
CallToolResult

disconnect()

Disconnect from the server.

public disconnect() : void

getInstructions()

Get server instructions.

public getInstructions() : string|null
Return values
string|null

getPrompt()

Get a prompt from the server.

public getPrompt(string $name[, array<string, string> $arguments = [] ][, callable(float $progress, Array $total, Array $message): void|null $onProgress = null ]) : GetPromptResult
Parameters
$name : string

Prompt name

$arguments : array<string, string> = []

Prompt arguments

$onProgress : callable(float $progress, Array $total, Array $message): void|null = null

Optional callback for progress updates

Return values
GetPromptResult

getProtocolVersion()

Protocol revision negotiated during the handshake.

public getProtocolVersion() : ProtocolVersion|null

This is the version the server answered with, which is not necessarily the one configured on the builder: a server that cannot speak the requested revision counter-offers one it supports. Null until the handshake completed.

Return values
ProtocolVersion|null

isConnected()

Check if connected and initialized.

public isConnected() : bool
Return values
bool

listTools()

List available tools from the server.

public listTools([string|null $cursor = null ]) : ListToolsResult
Parameters
$cursor : string|null = null
Return values
ListToolsResult

ping()

Send a ping request to the server.

public ping() : void

readResource()

Read a resource by URI.

public readResource(string $uri[, callable(float $progress, Array $total, Array $message): void|null $onProgress = null ]) : ReadResourceResult
Parameters
$uri : string

The resource URI

$onProgress : callable(float $progress, Array $total, Array $message): void|null = null

Optional callback for progress updates

Return values
ReadResourceResult

sendRootsListChanged()

Notify the server that the client's list of roots has changed.

public sendRootsListChanged() : void

The server should react by requesting an updated list via roots/list.

Tags
throws
RuntimeException

if the client did not advertise the roots.listChanged capability

ConnectionException

if the client is not connected

setLoggingLevel()

Set the minimum logging level for server log messages.

public setLoggingLevel(LoggingLevel $level) : void
Parameters
$level : LoggingLevel

sendRequest()

Send a request to the server and wait for response.

private sendRequest(Request $request[, callable(float $progress, Array $total, Array $message): void|null $onProgress = null ]) : Response<string|int, mixed>
Parameters
$request : Request
$onProgress : callable(float $progress, Array $total, Array $message): void|null = null
Tags
throws
RequestException|ConnectionException
Return values
Response<string|int, mixed>
On this page

Search results