MCP TypeScript SDK / shared/transport
shared/transport
Interfaces
Transport
Defined in: src/shared/transport.ts:74
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:101
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:108
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:117
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:122
The session ID generated for this connection.
setProtocolVersion?
optionalsetProtocolVersion?: (version) =>void
Defined in: src/shared/transport.ts:127
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:94
Closes the connection.
Returns
Promise<void>
send()
send(
message,options?):Promise<void>
Defined in: src/shared/transport.ts:89
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:82
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:51
Options for sending a JSON-RPC message.
Properties
onresumptiontoken?
optionalonresumptiontoken?: (token) =>void
Defined in: src/shared/transport.ts:69
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:55
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:62
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
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>