Skip to content

SseMcpTransport

Namespace: Vercel.AI.Mcp

public sealed class SseMcpTransport : IMcpTransport, IDisposable

MCP over the legacy HTTP+SSE transport. StartAsync(CancellationToken) opens a GET event stream, waits for the endpoint event, then CancellationToken) posts JSON-RPC messages to that endpoint. Responses arrive on the stream.

Inheritance: object

Implements: IMcpTransport, IDisposable

View source

SseMcpTransport(HttpClient, Uri, IReadOnlyDictionary<string, string>?)

Section titled “SseMcpTransport(HttpClient, Uri, IReadOnlyDictionary<string, string>?)”
public SseMcpTransport(HttpClient httpClient, Uri url, IReadOnlyDictionary<string, string>? headers = null)

Creates a transport for the SSE endpoint at <code class=“paramref”>url</code>.

Parameters

Name Type Description
httpClient HttpClient
url Uri
headers IReadOnlyDictionary<string, string>

View source

public const string LatestLegacyProtocolVersion = "2024-11-05"

Protocol version sent in mcp-protocol-version until ProtocolVersion is set.

Value: string

View source

public Action? OnClose { get; set; }

Called when the stream closes after CloseAsync().

Value: Action

View source

public Action<Exception>? OnError { get; set; }

Called with each transport error, such as a failed connection or an unparsable message.

Value: Action<Exception>

View source

public Action<JsonElement>? OnMessage { get; set; }

Called with each JSON-RPC message the server sends.

Value: Action<JsonElement>

View source

public string? ProtocolVersion { get; set; }

Negotiated protocol version. Defaults to LatestLegacyProtocolVersion when unset.

Value: string

View source

CallAsync(string, JsonElement, CancellationToken)

Section titled “CallAsync(string, JsonElement, CancellationToken)”
public Task<JsonElement> CallAsync(string method, JsonElement parameters, CancellationToken cancellationToken)

Calls <code class=“paramref”>method</code> and returns the JSON-RPC result.

Parameters

Name Type Description
method string
parameters JsonElement
cancellationToken CancellationToken

Returns: Task<JsonElement>

View source

public Task CloseAsync()

Closes the event stream and fails pending calls.

Returns: Task

View source

public static JsonElement DeserializeMessage(string line)

Parses one JSON-RPC message. Prototype-pollution keys are rejected.

Parameters

Name Type Description
line string

Returns: JsonElement

View source

public void Dispose()

Closes the transport.

View source

public Task SendAsync(JsonElement message, CancellationToken cancellationToken = default)

Posts one JSON-RPC message to the endpoint the server announced.

Parameters

Name Type Description
message JsonElement
cancellationToken CancellationToken

Returns: Task

View source

public Task StartAsync(CancellationToken cancellationToken = default)

Opens the event stream and completes once the server has sent its endpoint event.

Parameters

Name Type Description
cancellationToken CancellationToken

Returns: Task

View source

Apache-2.0 · Copyright 2023 Vercel, Inc. · unofficial community port, not an official Vercel product