Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `mcp/sdk` will be documented in this file.

0.9.0
-----

* Add `HttpTransport::getSessionId()` to read the server-minted `Mcp-Session-Id`: a request-scoped caller can persist it and pass it back through the constructor's `$headers` on a later transport. Always `null` on `2026-07-28`, which removed protocol-level sessions.

0.8.0
-----

Expand Down
16 changes: 16 additions & 0 deletions src/Client/Transport/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ public function onHeaders(callable $callback): void
$this->headerCallback = $callback;
}

/**
* The session ID minted by the server for this connection, if any.
*
* Captured from the Mcp-Session-Id response header of a handshake-era
* server; always null on 2026-07-28, which removed protocol-level sessions
* (SEP-2567). A caller that cannot keep the transport alive between
* interactions can persist it and pass it back through the constructor's
* $headers on a later transport, where it wins over the tracked value.
*
* @return string|null null until the server mints a session, and again after close()
*/
public function getSessionId(): ?string
{
return $this->sessionId;
}

public function send(string $data): void
{
$request = $this->requestFactory->createRequest('POST', $this->endpoint)
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Client/Transport/HttpTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,53 @@ public function sendRequest(RequestInterface $request): ResponseInterface
$this->assertSame('test-server', $client->getServerInfo()?->name);
}

#[TestDox('the server-minted session ID is exposed while connected and cleared on close')]
public function testSessionIdIsExposedAndClearedOnClose(): void
{
$httpClient = new class implements ClientInterface {
public function sendRequest(RequestInterface $request): ResponseInterface
{
$decoded = json_decode((string) $request->getBody(), true);

if ('initialize' !== ($decoded['method'] ?? null)) {
return new Response(202);
}

$payload = json_encode([
'jsonrpc' => '2.0',
'id' => $decoded['id'],
'result' => [
'protocolVersion' => '2025-11-25',
'capabilities' => ['tools' => ['listChanged' => false]],
'serverInfo' => ['name' => 'test-server', 'version' => '1.0.0'],
],
]);

return new Response(200, [
'Content-Type' => 'application/json',
'Mcp-Session-Id' => 'session-abc123',
], $payload);
}
};

$transport = new HttpTransport('http://localhost/mcp', [], $httpClient, $this->factory, $this->factory);

$this->assertNull($transport->getSessionId());

$client = Client::builder()
->setClientInfo('test-client', '1.0.0')
->setInitTimeout(1)
->build();

$client->connect($transport);

$this->assertSame('session-abc123', $transport->getSessionId());

$client->disconnect();

$this->assertNull($transport->getSessionId());
}

#[TestDox('SSE stream is aborted before the buffer can exceed the configured cap')]
public function testSseBufferIsBoundedByConfiguredCap(): void
{
Expand Down