streamableHttp.d.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Node.js HTTP Streamable HTTP Server Transport
  3. *
  4. * This is a thin wrapper around `WebStandardStreamableHTTPServerTransport` that provides
  5. * compatibility with Node.js HTTP server (IncomingMessage/ServerResponse).
  6. *
  7. * For web-standard environments (Cloudflare Workers, Deno, Bun), use `WebStandardStreamableHTTPServerTransport` directly.
  8. */
  9. import { IncomingMessage, ServerResponse } from 'node:http';
  10. import { Transport } from '../shared/transport.js';
  11. import { AuthInfo } from './auth/types.js';
  12. import { MessageExtraInfo, JSONRPCMessage, RequestId } from '../types.js';
  13. import { WebStandardStreamableHTTPServerTransportOptions, EventStore, StreamId, EventId } from './webStandardStreamableHttp.js';
  14. export type { EventStore, StreamId, EventId };
  15. /**
  16. * Configuration options for StreamableHTTPServerTransport
  17. *
  18. * This is an alias for WebStandardStreamableHTTPServerTransportOptions for backward compatibility.
  19. */
  20. export type StreamableHTTPServerTransportOptions = WebStandardStreamableHTTPServerTransportOptions;
  21. /**
  22. * Server transport for Streamable HTTP: this implements the MCP Streamable HTTP transport specification.
  23. * It supports both SSE streaming and direct HTTP responses.
  24. *
  25. * This is a wrapper around `WebStandardStreamableHTTPServerTransport` that provides Node.js HTTP compatibility.
  26. * It uses the `@hono/node-server` library to convert between Node.js HTTP and Web Standard APIs.
  27. *
  28. * Usage example:
  29. *
  30. * ```typescript
  31. * // Stateful mode - server sets the session ID
  32. * const statefulTransport = new StreamableHTTPServerTransport({
  33. * sessionIdGenerator: () => randomUUID(),
  34. * });
  35. *
  36. * // Stateless mode - explicitly set session ID to undefined
  37. * const statelessTransport = new StreamableHTTPServerTransport({
  38. * sessionIdGenerator: undefined,
  39. * });
  40. *
  41. * // Using with pre-parsed request body
  42. * app.post('/mcp', (req, res) => {
  43. * transport.handleRequest(req, res, req.body);
  44. * });
  45. * ```
  46. *
  47. * In stateful mode:
  48. * - Session ID is generated and included in response headers
  49. * - Session ID is always included in initialization responses
  50. * - Requests with invalid session IDs are rejected with 404 Not Found
  51. * - Non-initialization requests without a session ID are rejected with 400 Bad Request
  52. * - State is maintained in-memory (connections, message history)
  53. *
  54. * In stateless mode:
  55. * - No Session ID is included in any responses
  56. * - No session validation is performed
  57. */
  58. export declare class StreamableHTTPServerTransport implements Transport {
  59. private _webStandardTransport;
  60. private _requestListener;
  61. private _requestContext;
  62. constructor(options?: StreamableHTTPServerTransportOptions);
  63. /**
  64. * Gets the session ID for this transport instance.
  65. */
  66. get sessionId(): string | undefined;
  67. /**
  68. * Sets callback for when the transport is closed.
  69. */
  70. set onclose(handler: (() => void) | undefined);
  71. get onclose(): (() => void) | undefined;
  72. /**
  73. * Sets callback for transport errors.
  74. */
  75. set onerror(handler: ((error: Error) => void) | undefined);
  76. get onerror(): ((error: Error) => void) | undefined;
  77. /**
  78. * Sets callback for incoming messages.
  79. */
  80. set onmessage(handler: ((message: JSONRPCMessage, extra?: MessageExtraInfo) => void) | undefined);
  81. get onmessage(): ((message: JSONRPCMessage, extra?: MessageExtraInfo) => void) | undefined;
  82. /**
  83. * Starts the transport. This is required by the Transport interface but is a no-op
  84. * for the Streamable HTTP transport as connections are managed per-request.
  85. */
  86. start(): Promise<void>;
  87. /**
  88. * Closes the transport and all active connections.
  89. */
  90. close(): Promise<void>;
  91. /**
  92. * Sends a JSON-RPC message through the transport.
  93. */
  94. send(message: JSONRPCMessage, options?: {
  95. relatedRequestId?: RequestId;
  96. }): Promise<void>;
  97. /**
  98. * Handles an incoming HTTP request, whether GET or POST.
  99. *
  100. * This method converts Node.js HTTP objects to Web Standard Request/Response
  101. * and delegates to the underlying WebStandardStreamableHTTPServerTransport.
  102. *
  103. * @param req - Node.js IncomingMessage, optionally with auth property from middleware
  104. * @param res - Node.js ServerResponse
  105. * @param parsedBody - Optional pre-parsed body from body-parser middleware
  106. */
  107. handleRequest(req: IncomingMessage & {
  108. auth?: AuthInfo;
  109. }, res: ServerResponse, parsedBody?: unknown): Promise<void>;
  110. /**
  111. * Close an SSE stream for a specific request, triggering client reconnection.
  112. * Use this to implement polling behavior during long-running operations -
  113. * client will reconnect after the retry interval specified in the priming event.
  114. */
  115. closeSSEStream(requestId: RequestId): void;
  116. /**
  117. * Close the standalone GET SSE stream, triggering client reconnection.
  118. * Use this to implement polling behavior for server-initiated notifications.
  119. */
  120. closeStandaloneSSEStream(): void;
  121. }
  122. //# sourceMappingURL=streamableHttp.d.ts.map