Skip to content
Draft
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
23 changes: 23 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Response
use Conditionable;
use Macroable;

/**
* @var array<string, string>
*/
protected array $headers = [];

protected function __construct(
protected Content $content,
protected Role $role = Role::User,
Expand Down Expand Up @@ -109,6 +114,24 @@ public function withMeta(array|string $meta, mixed $value = null): static
return $this;
}

/**
* @param array<string, string> $headers
*/
public function withHeaders(array $headers): static
{
$this->headers = array_merge($this->headers, $headers);

return $this;
}

/**
* @return array<string, string>
*/
public function headers(): array
{
return $this->headers;
}

/**
* @throws NotImplementedException
*/
Expand Down
12 changes: 12 additions & 0 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ public function getStructuredContent(): ?array
{
return $this->structuredContent;
}

/**
* Get all HTTP headers from all responses.
*
* @return array<string, string>
*/
public function headers(): array
{
return $this->responses
->flatMap(fn (Response $response) => $response->headers())
->all();
}
}
30 changes: 30 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Laravel\Mcp\Server\Testing\PendingTestResponse;
use Laravel\Mcp\Server\Testing\TestResponse;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Transport\HttpTransport;
use Laravel\Mcp\Server\Transport\JsonRpcNotification;
use Laravel\Mcp\Server\Transport\JsonRpcRequest;
use Laravel\Mcp\Server\Transport\JsonRpcResponse;
Expand Down Expand Up @@ -233,6 +234,8 @@ protected function handleMessage(JsonRpcRequest $request, ServerContext $context
{
$response = $this->runMethodHandle($request, $context);

$this->applyResponseHeaders();

if (! is_iterable($response)) {
$this->transport->send($response->toJson());

Expand Down Expand Up @@ -283,6 +286,33 @@ protected function generateSessionId(): string
return Str::uuid()->toString();
}

protected function applyResponseHeaders(): void
{
$container = Container::getInstance();

if (! $container->bound('mcp.response.factory')) {
return;
}

$responseFactory = $container->make('mcp.response.factory');

if (! $responseFactory instanceof ResponseFactory) {
return;
}

$headers = $responseFactory->headers();

if ($headers === []) {
return;
}

if ($this->transport instanceof HttpTransport) {
$this->transport->withHeaders($headers);
}

$container->forgetInstance('mcp.response.factory');
}

/**
* @param array<array-key, mixed> $arguments
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Server/Methods/Concerns/InteractsWithResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ protected function toJsonRpcResponse(JsonRpcRequest $request, Response|ResponseF
}
});

\Illuminate\Container\Container::getInstance()->instance('mcp.response.factory', $responseFactory);

return JsonRpcResponse::result($request->id, $serializable($responseFactory));
}

Expand Down
17 changes: 16 additions & 1 deletion src/Server/Transport/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

class HttpTransport implements Transport
{
/**
* @var array<string, string>
*/
protected array $customHeaders = [];

/**
* @param (Closure(string): void)|null $handler
*/
Expand Down Expand Up @@ -70,6 +75,16 @@ public function stream(Closure $stream): void
$this->stream = $stream;
}

/**
* @param array<string, string> $headers
*/
public function withHeaders(array $headers): static
{
$this->customHeaders = array_merge($this->customHeaders, $headers);

return $this;
}

protected function sendStreamMessage(string $message): void
{
echo 'data: '.$message."\n\n";
Expand Down Expand Up @@ -98,6 +113,6 @@ protected function getHeaders(): array
$headers['X-Accel-Buffering'] = 'no';
}

return $headers;
return array_merge($headers, $this->customHeaders);
}
}
Loading