返回源码地图

packages/mcp/mcp-client/src/transport.ts

main snapshot · da00f7f5358f · 正文引用章节 12;完整原文可核对,不声称全文件人工逐行审计

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1/**
2 * Transport factory: creates the appropriate MCP transport based on the
3 * plugin's resolved config. Stdio spawns a child process (with credential
4 * scrubbing); Streamable HTTP connects to a URL.
5 *
6 * @module
7 */
8
9import type { Transport } from '@modelcontextprotocol/client'
10import { StdioClientTransport } from '@modelcontextprotocol/client/stdio'
11import { StreamableHTTPClientTransport } from '@modelcontextprotocol/client'
12import { scrubbedParentEnv } from '@deepseek-ai/dsh-subprocess'
13import type { Config } from './index.ts'
14
15/**
16 * The subprocess seam's scrubbed parent env (credential-shaped and stale
17 * `DSH_*` names dropped), plus the spec's explicit env. The MCP SDK owns the
18 * actual spawn, so this transport shares the scrub definition rather than the
19 * spawn path.
20 */
21function buildChildEnv(extra: Record<string, string>): Record<string, string> {
22 return { ...scrubbedParentEnv(), ...extra }
23}
24
25/**
26 * Create an MCP transport from the resolved plugin config.
27 *
28 * @param config - Resolved plugin config discriminated on `transport`.
29 * @returns A connected-ready MCP Transport (stdio or Streamable HTTP).
30 */
31export function createTransport(config: Config): Transport {
32 switch (config.transport) {
33 case 'stdio':
34 return new StdioClientTransport({
35 command: config.command,
36 args: config.args,
37 env: buildChildEnv(config.env),
38 cwd: config.cwd,
39 })
40 case 'streamable-http':
41 return new StreamableHTTPClientTransport(
42 new URL(config.url),
43 { requestInit: { headers: config.headers } },
44 )
45 }
46}