MCP PHP SDK

Protocol

Tags
final
phpstan-import-type

McpFiber from TransportInterface

phpstan-import-type

FiberSuspend from TransportInterface

author

Christopher Hertel mail@christopher-hertel.de

author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Constants

SESSION_ACTIVE_REQUEST_META  = '_mcp.active_request_meta'
Session key for active request meta
SESSION_LOGGING_LEVEL  = '_mcp.logging_level'
SESSION_OUTGOING_QUEUE  = '_mcp.outgoing_queue'
Session key for outgoing message queue
SESSION_PENDING_REQUESTS  = '_mcp.pending_requests'
Session key for pending outgoing requests
SESSION_REQUEST_ID_COUNTER  = '_mcp.request_id_counter'
Session key for request ID counter
SESSION_RESPONSES  = '_mcp.responses'
Session key for incoming client responses

Properties

$eventDispatcher  : EventDispatcherInterface|null
$logger  : LoggerInterface
$messageFactory  : MessageFactory
$notificationHandlers  : array<string|int, mixed>
$requestHandlers  : array<string|int, mixed>
$sessionFactory  : SessionFactoryInterface
$sessionStore  : SessionStoreInterface

Methods

__construct()  : mixed
checkResponse()  : Response<string|int, array<string, mixed>>|Error|null
connect()  : void
Connect this protocol to transport.
consumeOutgoingMessages()  : array<int, array{message: string, context: array}>
Consume (get and clear) all outgoing messages for a session.
destroySession()  : void
Destroy a specific session.
getPendingRequests()  : array<int, mixed>
Get pending requests for a session.
handleFiberYield()  : void
Handle values yielded by Fibers during transport-managed resumes.
processInput()  : void
Handle an incoming message from the transport.
sendNotification()  : void
Queues a notification for later delivery.
sendRequest()  : int
Sends a request to the client and returns the request ID.
dispatchEvent()  : T
Dispatches an event through the event dispatcher if available.
gcSessions()  : void
Run garbage collection on expired sessions.
handleInvalidMessage()  : void
Handle an invalid message from the transport.
handleNotification()  : void
handleRequest()  : void
Handle a request from the transport.
handleResponse()  : void
hasInitializeRequest()  : bool
queueOutgoing()  : void
Helper to queue outgoing messages in session.
resolveSession()  : SessionInterface|null
Resolves and validates the session based on the request context.
sendResponse()  : void
Sends a response either immediately or queued for later delivery.

Constants

SESSION_ACTIVE_REQUEST_META

Session key for active request meta

public mixed SESSION_ACTIVE_REQUEST_META = '_mcp.active_request_meta'

SESSION_LOGGING_LEVEL

public mixed SESSION_LOGGING_LEVEL = '_mcp.logging_level'

SESSION_OUTGOING_QUEUE

Session key for outgoing message queue

private mixed SESSION_OUTGOING_QUEUE = '_mcp.outgoing_queue'

SESSION_PENDING_REQUESTS

Session key for pending outgoing requests

private mixed SESSION_PENDING_REQUESTS = '_mcp.pending_requests'

SESSION_REQUEST_ID_COUNTER

Session key for request ID counter

private mixed SESSION_REQUEST_ID_COUNTER = '_mcp.request_id_counter'

SESSION_RESPONSES

Session key for incoming client responses

private mixed SESSION_RESPONSES = '_mcp.responses'

Properties

$eventDispatcher read-only

private EventDispatcherInterface|null $eventDispatcher = null

$logger read-only

private LoggerInterface $logger = new NullLogger()

$notificationHandlers read-only

private array<string|int, mixed> $notificationHandlers

$requestHandlers read-only

private array<string|int, mixed> $requestHandlers

Methods

__construct()

public __construct(array<int, RequestHandlerInterface<string|int, ResultInterface|array<string, mixed>>> $requestHandlers, array<int, NotificationHandlerInterface$notificationHandlers, MessageFactory $messageFactory, SessionFactoryInterface $sessionFactory, SessionStoreInterface $sessionStore[, LoggerInterface $logger = new NullLogger() ][, EventDispatcherInterface|null $eventDispatcher = null ]) : mixed
Parameters
$requestHandlers : array<int, RequestHandlerInterface<string|int, ResultInterface|array<string, mixed>>>
$notificationHandlers : array<int, NotificationHandlerInterface>
$messageFactory : MessageFactory
$sessionFactory : SessionFactoryInterface
$sessionStore : SessionStoreInterface
$logger : LoggerInterface = new NullLogger()
$eventDispatcher : EventDispatcherInterface|null = null

checkResponse()

public checkResponse(int $requestId, Uuid $sessionId) : Response<string|int, array<string, mixed>>|Error|null
Parameters
$requestId : int
$sessionId : Uuid
Return values
Response<string|int, array<string, mixed>>|Error|null

connect()

Connect this protocol to transport.

public connect(TransportInterface<string|int, mixed> $transport) : void

The protocol takes ownership of the transport and sets up all callbacks.

Parameters
$transport : TransportInterface<string|int, mixed>

consumeOutgoingMessages()

Consume (get and clear) all outgoing messages for a session.

public consumeOutgoingMessages(Uuid $sessionId) : array<int, array{message: string, context: array}>
Parameters
$sessionId : Uuid
Return values
array<int, array{message: string, context: array}>

destroySession()

Destroy a specific session.

public destroySession(Uuid $sessionId) : void
Parameters
$sessionId : Uuid

getPendingRequests()

Get pending requests for a session.

public getPendingRequests(Uuid $sessionId) : array<int, mixed>
Parameters
$sessionId : Uuid
Return values
array<int, mixed>

The pending requests

handleFiberYield()

Handle values yielded by Fibers during transport-managed resumes.

public handleFiberYield(FiberSuspend|null $yieldedValue, Uuid|null $sessionId) : void
Parameters
$yieldedValue : FiberSuspend|null
$sessionId : Uuid|null

processInput()

Handle an incoming message from the transport.

public processInput(TransportInterface<string|int, mixed> $transport, string $input, Uuid|null $sessionId) : void

This is called by the transport whenever ANY message arrives.

Parameters
$transport : TransportInterface<string|int, mixed>
$input : string
$sessionId : Uuid|null

dispatchEvent()

Dispatches an event through the event dispatcher if available.

private dispatchEvent(T $event) : T
Parameters
$event : T
Return values
T

gcSessions()

Run garbage collection on expired sessions.

private gcSessions() : void

Uses the session store's internal TTL configuration.

hasInitializeRequest()

private hasInitializeRequest(array<int, mixed> $messages) : bool
Parameters
$messages : array<int, mixed>
Return values
bool

resolveSession()

Resolves and validates the session based on the request context.

private resolveSession(TransportInterface<string|int, mixed> $transport, Uuid|null $sessionId, array<int, mixed> $messages) : SessionInterface|null
Parameters
$transport : TransportInterface<string|int, mixed>
$sessionId : Uuid|null

The session ID from the transport

$messages : array<int, mixed>

The parsed messages

Return values
SessionInterface|null

sendResponse()

Sends a response either immediately or queued for later delivery.

private sendResponse(TransportInterface<string|int, mixed> $transport, Response<string|int, ResultInterface|array<string, mixed>>|Error $response, SessionInterface|null $session[, array<string, mixed> $context = [] ]) : void
Parameters
$transport : TransportInterface<string|int, mixed>
$response : Response<string|int, ResultInterface|array<string, mixed>>|Error
$session : SessionInterface|null
$context : array<string, mixed> = []

        
On this page

Search results