diff --git a/README.md b/README.md index 856216d..a0bb783 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,9 @@ For MCP servers that communicate over HTTP: 'api_key' => env('RELAY_GITHUB_SERVER_API_KEY'), 'timeout' => 30, 'transport' => Transport::Http, + 'headers' => [ + 'User-Agent' => 'prism-php-relay/1.0', + ] ], ``` diff --git a/src/Relay.php b/src/Relay.php index cca3b53..9cdd7dc 100644 --- a/src/Relay.php +++ b/src/Relay.php @@ -340,7 +340,7 @@ protected function getSchemeParameters(array $properties, string $definitionName * * @throws RelayException */ - protected function getSchemeParameter(string $name, array $property, string $definitionName): Schema|null + protected function getSchemeParameter(string $name, array $property, string $definitionName): ?Schema { if ($property === []) { return null; diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index d09015d..c8e1ec8 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -78,6 +78,10 @@ protected function sendHttpRequest(array $payload): Response $this->hasApiKey(), fn ($http) => $http->withToken($this->getApiKey()) ) + ->when( + $this->hasHeaders(), + fn ($http) => $http->withHeaders($this->getHeaders()) + ) ->post($this->getServerUrl(), $payload); } @@ -91,11 +95,26 @@ protected function hasApiKey(): bool return isset($this->config['api_key']) && $this->config['api_key'] !== null; } + protected function hasHeaders(): bool + { + return isset($this->config['headers']) + && is_array($this->config['headers']) + && ! empty($this->config['headers']); + } + protected function getApiKey(): string { return (string) ($this->config['api_key'] ?? ''); } + /** + * @return array + */ + protected function getHeaders(): array + { + return $this->config['headers'] ?? []; + } + protected function getServerUrl(): string { return $this->config['url']; diff --git a/tests/Unit/Transport/HttpTransportTest.php b/tests/Unit/Transport/HttpTransportTest.php index 889e4b5..0a71f0f 100644 --- a/tests/Unit/Transport/HttpTransportTest.php +++ b/tests/Unit/Transport/HttpTransportTest.php @@ -202,3 +202,25 @@ Http::assertSent(fn ($request) => $request->hasHeader('Accept', 'application/json')); }); + +it('supports passing arbitrary request headers', function (): void { + $transport = new HttpTransport([ + 'url' => 'http://example.com/api', + 'timeout' => 30, + 'headers' => [ + 'User-Agent' => 'prism-php-relay/1.0', + ], + ]); + + Http::fake([ + 'http://example.com/api' => Http::response([ + 'jsonrpc' => '2.0', + 'id' => '1', + 'result' => ['status' => 'success'], + ]), + ]); + + $transport->sendRequest('test/method'); + + Http::assertSent(fn ($request) => $request->hasHeader('User-Agent', 'prism-php-relay/1.0')); +});