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?
optionalonclose?: () =>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?
optionalonerror?: (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?
optionalonmessage?: <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?
Returns
void
sessionId?
optionalsessionId?:string
Defined in: src/shared/transport.ts:213
The session ID generated for this connection.
setProtocolVersion?
optionalsetProtocolVersion?: (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
options?
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?
optionalonresumptiontoken?: (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?
optionalrelatedRequestId?: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?
optionalresumptionToken?: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
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?
Returns
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
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
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