Module: PostHog::MCP::TransportExtension Private

Defined in:
lib/posthog/mcp/server_extension.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Prepended onto MCP::Server::Transports::StreamableHTTPTransport once. Publishes the HTTP headers of the in-flight request to RequestScope and adds the Mcp-Session-Id token minted by Instrumentation on stateless initialize responses.

Instance Method Summary collapse

Instance Method Details

#handle_request(request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/posthog/mcp/server_extension.rb', line 36

def handle_request(request)
  server = instance_variable_defined?(:@server) ? @server : nil
  return super unless server && PostHog::MCP.tracking_data(server)

  env = request.respond_to?(:env) ? request.env : {}

  RequestScope.with(headers: RequestScope.headers_from_env(env)) do |scope|
    response = super
    mint = scope[:mint]
    if mint && response.is_a?(Array) && response[1].is_a?(Hash) &&
       response[1].keys.none? { |key| key.to_s.casecmp?(MCP_SESSION_HEADER) }
      response[1][MCP_SESSION_HEADER] = mint
    end
    response
  end
end