From d1c1add170e3accfa57e14bb90e1f618e3f92e8d Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Tue, 20 Jan 2026 18:22:19 +0100 Subject: [PATCH] Make it possible to supply custom headers along with StreamableHttp --- .../StreamableHttp.class.php | 15 +++++++++++ .../unittest/StreamableHttpTest.class.php | 26 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/main/php/io/modelcontextprotocol/StreamableHttp.class.php b/src/main/php/io/modelcontextprotocol/StreamableHttp.class.php index ecfa7a2..b7edc0a 100755 --- a/src/main/php/io/modelcontextprotocol/StreamableHttp.class.php +++ b/src/main/php/io/modelcontextprotocol/StreamableHttp.class.php @@ -45,6 +45,21 @@ public function version($version) { $this->endpoint->with(['MCP-Protocol-Version' => $version]); } + /** @return [:string] */ + public function headers() { return $this->endpoint->headers(); } + + /** + * Adds headers to be sent with every request + * + * @param string|[:string] $arg + * @param ?string $value + * @return self + */ + public function with($arg, $value= null) { + $this->endpoint->with($arg, $value); + return $this; + } + /** * Sends a notification * diff --git a/src/test/php/io/modelcontextprotocol/unittest/StreamableHttpTest.class.php b/src/test/php/io/modelcontextprotocol/unittest/StreamableHttpTest.class.php index 4222104..d302f36 100755 --- a/src/test/php/io/modelcontextprotocol/unittest/StreamableHttpTest.class.php +++ b/src/test/php/io/modelcontextprotocol/unittest/StreamableHttpTest.class.php @@ -46,6 +46,32 @@ private function newFixture(array &$sessions, string $base= '/mcp'): StreamableH return new StreamableHttp(new TestEndpoint($routes, $base)); } + #[Test] + public function accepts_json_and_event_stream_by_default() { + Assert::equals( + 'text/event-stream, application/json', + (new StreamableHttp('https://example.com/'))->headers()['Accept'] + ); + } + + #[Test] + public function pass_header() { + $token= 'Token test'; + Assert::equals($token, (new StreamableHttp('https://example.com/')) + ->with('Authorization', $token) + ->headers()['Authorization'] + ); + } + + #[Test] + public function pass_headers() { + $token= 'Token test'; + Assert::equals($token, (new StreamableHttp('https://example.com/')) + ->with(['Authorization' => $token]) + ->headers()['Authorization'] + ); + } + #[Test, Values(['application/json', 'application/json; charset=utf-8'])] public function json($type) { $value= ['name' => 'test', 'version' => '1.0.0'];