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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
],
```

Expand Down
2 changes: 1 addition & 1 deletion src/Relay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions src/Transport/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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<string, mixed>
*/
protected function getHeaders(): array
{
return $this->config['headers'] ?? [];
}

protected function getServerUrl(): string
{
return $this->config['url'];
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/Transport/HttpTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
Loading