Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/php/io/modelcontextprotocol/McpClient.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ public function __construct($endpoint, string $version= '2025-03-26') {
$this->capabilities= Capabilities::client();
}

/** Suspends this MCP client for later continuation */
public function suspend(): array {
return [
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a check if the transport is resumeable

'transport' => ['impl' => get_class($this->transport), 'suspended' => $this->transport->suspend()],
'version' => $this->version,
'server' => $this->server,
];
}

/** Resumes a previously suspended transport */
public static function resume(array $suspended): self {
$self= new self(
$suspended['transport']['impl']::resume($suspended['transport']['suspended']),
$suspended['version']
);
$self->server= $suspended['server'];
return $self;
}


/** @return io.modelcontextprotocol.Transport */
public function transport() { return $this->transport; }

Expand Down
15 changes: 14 additions & 1 deletion src/main/php/io/modelcontextprotocol/StreamableHttp.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class StreamableHttp extends Transport {
const SESSION = 'Mcp-Session-Id';

private $endpoint;
private $terminate= false;

/** @param string|util.URI|webservices.rest.Endpoint $endpoint */
public function __construct($endpoint) {
Expand All @@ -27,6 +28,17 @@ public function setTrace($cat) {
$this->endpoint->setTrace($cat);
}

/** Suspends this transport for later continuation */
public function suspend(): array {
$this->terminate= false;
return ['uri' => (string)$this->endpoint->base(), 'headers' => $this->endpoint->headers()];
}

/** Resumes a previously suspended transport */
public static function resume(array $suspended): self {
return new self((new Endpoint($suspended['uri']))->with($suspended['headers']));
}

/**
* Sends a notification
*
Expand Down Expand Up @@ -59,6 +71,7 @@ public function call($method, $params= null) {
// If a session header is returned, remember it
if ($session= $response->header(self::SESSION)) {
$this->endpoint->with(self::SESSION, $session);
$this->terminate= true;
}

// Separate content-type value from optional parameters, e.g. "charset"
Expand All @@ -85,7 +98,7 @@ public function call($method, $params= null) {

/** @return void */
public function close() {
if (!isset($this->endpoint->headers()[self::SESSION])) return;
if (!$this->terminate) return;

// Clients that no longer need a particular session SHOULD send an HTTP DELETE to the
// MCP endpoint with the Mcp-Session-Id header, to explicitly terminate the session
Expand Down
Loading