stdio.d.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { IOType } from 'node:child_process';
  2. import { Stream } from 'node:stream';
  3. import { Transport } from '../shared/transport.js';
  4. import { JSONRPCMessage } from '../types.js';
  5. export type StdioServerParameters = {
  6. /**
  7. * The executable to run to start the server.
  8. */
  9. command: string;
  10. /**
  11. * Command line arguments to pass to the executable.
  12. */
  13. args?: string[];
  14. /**
  15. * The environment to use when spawning the process.
  16. *
  17. * If not specified, the result of getDefaultEnvironment() will be used.
  18. */
  19. env?: Record<string, string>;
  20. /**
  21. * How to handle stderr of the child process. This matches the semantics of Node's `child_process.spawn`.
  22. *
  23. * The default is "inherit", meaning messages to stderr will be printed to the parent process's stderr.
  24. */
  25. stderr?: IOType | Stream | number;
  26. /**
  27. * The working directory to use when spawning the process.
  28. *
  29. * If not specified, the current working directory will be inherited.
  30. */
  31. cwd?: string;
  32. };
  33. /**
  34. * Environment variables to inherit by default, if an environment is not explicitly given.
  35. */
  36. export declare const DEFAULT_INHERITED_ENV_VARS: string[];
  37. /**
  38. * Returns a default environment object including only environment variables deemed safe to inherit.
  39. */
  40. export declare function getDefaultEnvironment(): Record<string, string>;
  41. /**
  42. * Client transport for stdio: this will connect to a server by spawning a process and communicating with it over stdin/stdout.
  43. *
  44. * This transport is only available in Node.js environments.
  45. */
  46. export declare class StdioClientTransport implements Transport {
  47. private _process?;
  48. private _readBuffer;
  49. private _serverParams;
  50. private _stderrStream;
  51. onclose?: () => void;
  52. onerror?: (error: Error) => void;
  53. onmessage?: (message: JSONRPCMessage) => void;
  54. constructor(server: StdioServerParameters);
  55. /**
  56. * Starts the server process and prepares to communicate with it.
  57. */
  58. start(): Promise<void>;
  59. /**
  60. * The stderr stream of the child process, if `StdioServerParameters.stderr` was set to "pipe" or "overlapped".
  61. *
  62. * If stderr piping was requested, a PassThrough stream is returned _immediately_, allowing callers to
  63. * attach listeners before the start method is invoked. This prevents loss of any early
  64. * error output emitted by the child process.
  65. */
  66. get stderr(): Stream | null;
  67. /**
  68. * The child process pid spawned by this transport.
  69. *
  70. * This is only available after the transport has been started.
  71. */
  72. get pid(): number | null;
  73. private processReadBuffer;
  74. close(): Promise<void>;
  75. send(message: JSONRPCMessage): Promise<void>;
  76. }
  77. //# sourceMappingURL=stdio.d.ts.map