This is the documentation for v1.x — looking for the v2 documentation?
Skip to content

MCP TypeScript SDK / shared/transport

shared/transport ​

Interfaces ​

Transport ​

Defined in: src/shared/transport.ts:165

Describes the minimal contract for an MCP transport that a client or server can communicate over.

Properties ​

onclose? ​

optional onclose?: () => void

Defined in: src/shared/transport.ts:192

Callback for when the connection is closed for any reason.

This should be invoked when close() is called as well.

Returns ​

void

onerror? ​

optional onerror?: (error) => void

Defined in: src/shared/transport.ts:199

Callback for when an error occurs.

Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.

Parameters ​
error ​

Error

Returns ​

void

onmessage? ​

optional onmessage?: <T>(message, extra?) => void

Defined in: src/shared/transport.ts:208

Callback for when a message (request or response) is received over the connection.

Includes the requestInfo and authInfo if the transport is authenticated.

The requestInfo can be used to get the original request information (headers, etc.)

Type Parameters ​
T ​

T extends JSONRPCMessage

Parameters ​
message ​

T

extra? ​

MessageExtraInfo

Returns ​

void

sessionId? ​

optional sessionId?: string

Defined in: src/shared/transport.ts:213

The session ID generated for this connection.

setProtocolVersion? ​

optional setProtocolVersion?: (version) => void

Defined in: src/shared/transport.ts:218

Sets the protocol version used for the connection (called when the initialize response is received).

Parameters ​
version ​

string

Returns ​

void

Methods ​

close() ​

close(): Promise<void>

Defined in: src/shared/transport.ts:185

Closes the connection.

Returns ​

Promise<void>

send() ​

send(message, options?): Promise<void>

Defined in: src/shared/transport.ts:180

Sends a JSON-RPC message (request or response).

If present, relatedRequestId is used to indicate to the transport which incoming request to associate this outgoing message with.

Parameters ​
message ​

JSONRPCMessage

options? ​

TransportSendOptions

Returns ​

Promise<void>

start() ​

start(): Promise<void>

Defined in: src/shared/transport.ts:173

Starts processing messages on the transport, including any connection steps that might need to be taken.

This method should only be called after callbacks are installed, or else messages may be lost.

NOTE: This method should not be called explicitly when using Client, Server, or Protocol classes, as they will implicitly call start().

Returns ​

Promise<void>

Type Aliases ​

FetchLike ​

FetchLike = (url, init?) => Promise<Response>

Defined in: src/shared/transport.ts:3

Parameters ​

url ​

string | URL

init? ​

RequestInit

Returns ​

Promise<Response>


TransportSendOptions ​

TransportSendOptions = object

Defined in: src/shared/transport.ts:142

Options for sending a JSON-RPC message.

Properties ​

onresumptiontoken? ​

optional onresumptiontoken?: (token) => void

Defined in: src/shared/transport.ts:160

A callback that is invoked when the resumption token changes, if supported by the transport.

This allows clients to persist the latest token for potential reconnection.

Parameters ​
token ​

string

Returns ​

void

relatedRequestId? ​

optional relatedRequestId?: RequestId

Defined in: src/shared/transport.ts:146

If present, relatedRequestId is used to indicate to the transport which incoming request to associate this outgoing message with.

resumptionToken? ​

optional resumptionToken?: string

Defined in: src/shared/transport.ts:153

The resumption token used to continue long-running requests that were interrupted.

This allows clients to reconnect and continue from where they left off, if supported by the transport.

Functions ​

createFetchWithInit() ​

createFetchWithInit(baseFetch?, baseInit?): FetchLike

Defined in: src/shared/transport.ts:31

Creates a fetch function that includes base RequestInit options. This ensures requests inherit settings like credentials, mode, headers, etc. from the base init.

Parameters ​

baseFetch? ​

FetchLike = fetch

The base fetch function to wrap (defaults to global fetch)

baseInit? ​

RequestInit

The base RequestInit to merge with each request

Returns ​

FetchLike

A wrapped fetch function that merges base options with call-specific options


fetchLeavingRedirects() ​

fetchLeavingRedirects(baseFetch?): FetchLike

Defined in: src/shared/transport.ts:97

Internal

Wraps a fetch for redirectPolicy: 'follow': fetchWithinOrigin returns the result as it is, so redirects are left to the fetch.

Parameters ​

baseFetch? ​

FetchLike

Returns ​

FetchLike


fetchWithinOrigin() ​

fetchWithinOrigin(baseFetch?): FetchLike

Defined in: src/shared/transport.ts:104

Internal

Wraps a fetch so it follows a redirect only within the request's origin; any other redirect response is returned as is.

Parameters ​

baseFetch? ​

FetchLike = fetch

Returns ​

FetchLike


followWithinOrigin() ​

followWithinOrigin(baseFetch, url, init, response, followed?): Promise<Response> | undefined

Defined in: src/shared/transport.ts:69

Internal

Follows the redirect in response while it stays within the origin of url; undefined when response is to be used as is.

Parameters ​

baseFetch ​

FetchLike

url ​

string | URL

init ​

RequestInit | undefined

response ​

Response

followed? ​

number = 0

Returns ​

Promise<Response> | undefined


isWithinOrigin() ​

isWithinOrigin(from, to): boolean

Defined in: src/shared/transport.ts:52

Internal

Whether to has the scheme, host and port of from, or is its https form with both on default ports.

Parameters ​

from ​

URL

to ​

URL

Returns ​

boolean


normalizeHeaders() ​

normalizeHeaders(headers): Record<string, string>

Defined in: src/shared/transport.ts:9

Normalizes HeadersInit to a plain Record<string, string> for manipulation. Handles Headers objects, arrays of tuples, and plain objects.

Parameters ​

headers ​

HeadersInit | undefined

Returns ​

Record<string, string>


unfollowedRedirect() ​

unfollowedRedirect(response, requestUrl): string | undefined

Defined in: src/shared/transport.ts:120

Internal

Describes a redirect that fetchWithinOrigin returned unfollowed, naming its target without userinfo, query or fragment.

Parameters ​

response ​

Response

requestUrl ​

string | URL

Returns ​

string | undefined