Skip to content

Conversation

@lvluoyue
Copy link
Contributor

@lvluoyue lvluoyue commented Nov 10, 2025

hi, I am integrating this SDK with webman framework. When using ClientGateway to send logs, I noticed that the messages are being output to the console, because webman framework starts multiple processes through the console and creates httpserver via socket, so it outputs to the console. Now I need to extend StreamableHttpTransport to send the results to the stream instead.
image

Breaking Changes

No breaking changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

@lvluoyue
Copy link
Contributor Author

lvluoyue commented Nov 10, 2025

<?php

use \Mcp\Server\Transport\StreamableHttpTransport as BaseStreamableHttpTransport;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Uid\Uuid;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\ServerSentEvents;

class StreamableHttpTransport extends BaseStreamableHttpTransport
{

    private readonly TcpConnection $connection;

    /**
     * @param array<string, string> $corsHeaders
     */
    public function __construct(
        private readonly ServerRequestInterface $request,
        ?ResponseFactoryInterface $responseFactory = null,
        ?StreamFactoryInterface $streamFactory = null,
        array $corsHeaders = [],
        LoggerInterface $logger = new NullLogger(),
    ) {
        $this->connection = $request->getAttribute(TcpConnection::class);
        parent::__construct($request, $responseFactory, $streamFactory, $corsHeaders, $logger);
    }

    protected function handleFiberTermination(): void
    {
        $finalResult = $this->sessionFiber->getReturn();

        if (null !== $finalResult) {
            try {
                $encoded = json_encode($finalResult, \JSON_THROW_ON_ERROR);
                $this->connection->send(new ServerSentEvents([
                    'event' => 'message',
                    'data' => $encoded,
                ]));
            } catch (\JsonException $e) {
                $this->logger->error('SSE: Failed to encode final Fiber result.', ['exception' => $e]);
            }
        }

        $this->sessionFiber = null;
    }

    protected function flushOutgoingMessages(?Uuid $sessionId): void
    {
        $messages = $this->getOutgoingMessages($sessionId);

        foreach ($messages as $message) {
            $this->connection->send(new ServerSentEvents([
                'event' => 'message',
                'data' => $message['message'],
            ]));
        }
    }

}

Copy link
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lvluoyue

@chr-hertel chr-hertel merged commit 9827068 into modelcontextprotocol:main Nov 11, 2025
10 checks passed
@chr-hertel chr-hertel added the Server Issues & PRs related to the Server component label Nov 11, 2025
@chr-hertel chr-hertel changed the title Change visibility of StreamableHttpTransport methods to protected [Server] Change visibility of StreamableHttpTransport methods to protected Nov 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Server Issues & PRs related to the Server component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants