transport.d.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { JSONRPCMessage, MessageExtraInfo, RequestId } from '../types.js';
  2. export type FetchLike = (url: string | URL, init?: RequestInit) => Promise<Response>;
  3. /**
  4. * Normalizes HeadersInit to a plain Record<string, string> for manipulation.
  5. * Handles Headers objects, arrays of tuples, and plain objects.
  6. */
  7. export declare function normalizeHeaders(headers: HeadersInit | undefined): Record<string, string>;
  8. /**
  9. * Creates a fetch function that includes base RequestInit options.
  10. * This ensures requests inherit settings like credentials, mode, headers, etc. from the base init.
  11. *
  12. * @param baseFetch - The base fetch function to wrap (defaults to global fetch)
  13. * @param baseInit - The base RequestInit to merge with each request
  14. * @returns A wrapped fetch function that merges base options with call-specific options
  15. */
  16. export declare function createFetchWithInit(baseFetch?: FetchLike, baseInit?: RequestInit): FetchLike;
  17. /**
  18. * Options for sending a JSON-RPC message.
  19. */
  20. export type TransportSendOptions = {
  21. /**
  22. * If present, `relatedRequestId` is used to indicate to the transport which incoming request to associate this outgoing message with.
  23. */
  24. relatedRequestId?: RequestId;
  25. /**
  26. * The resumption token used to continue long-running requests that were interrupted.
  27. *
  28. * This allows clients to reconnect and continue from where they left off, if supported by the transport.
  29. */
  30. resumptionToken?: string;
  31. /**
  32. * A callback that is invoked when the resumption token changes, if supported by the transport.
  33. *
  34. * This allows clients to persist the latest token for potential reconnection.
  35. */
  36. onresumptiontoken?: (token: string) => void;
  37. };
  38. /**
  39. * Describes the minimal contract for an MCP transport that a client or server can communicate over.
  40. */
  41. export interface Transport {
  42. /**
  43. * Starts processing messages on the transport, including any connection steps that might need to be taken.
  44. *
  45. * This method should only be called after callbacks are installed, or else messages may be lost.
  46. *
  47. * NOTE: This method should not be called explicitly when using Client, Server, or Protocol classes, as they will implicitly call start().
  48. */
  49. start(): Promise<void>;
  50. /**
  51. * Sends a JSON-RPC message (request or response).
  52. *
  53. * If present, `relatedRequestId` is used to indicate to the transport which incoming request to associate this outgoing message with.
  54. */
  55. send(message: JSONRPCMessage, options?: TransportSendOptions): Promise<void>;
  56. /**
  57. * Closes the connection.
  58. */
  59. close(): Promise<void>;
  60. /**
  61. * Callback for when the connection is closed for any reason.
  62. *
  63. * This should be invoked when close() is called as well.
  64. */
  65. onclose?: () => void;
  66. /**
  67. * Callback for when an error occurs.
  68. *
  69. * Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.
  70. */
  71. onerror?: (error: Error) => void;
  72. /**
  73. * Callback for when a message (request or response) is received over the connection.
  74. *
  75. * Includes the requestInfo and authInfo if the transport is authenticated.
  76. *
  77. * The requestInfo can be used to get the original request information (headers, etc.)
  78. */
  79. onmessage?: <T extends JSONRPCMessage>(message: T, extra?: MessageExtraInfo) => void;
  80. /**
  81. * The session ID generated for this connection.
  82. */
  83. sessionId?: string;
  84. /**
  85. * Sets the protocol version used for the connection (called when the initialize response is received).
  86. */
  87. setProtocolVersion?: (version: string) => void;
  88. }
  89. //# sourceMappingURL=transport.d.ts.map