Files
goodbuddy/src/main/capabilities/mcp-client-transport.ts
T
mesalogo be39665ca9 feat: add managed local tool environments
GoodBuddy can now provide managed Node.js and on-demand Python to local Skills and stdio MCP servers, or validate an explicitly selected custom interpreter. The Capabilities and tools settings page exposes source selection, diagnostics, installation progress, cancellation, and removal while new processes receive immutable PATH snapshots.

Application settings migrate to v10. Local terminal and remote Host environments remain unchanged. Windows x64 native-source Node and Python paths are validated; OSS mirror publication, redistributed license resources, and the remaining native platform probes are recorded as release blockers.

Release note: 新增工具执行环境,可为本机 Skills 和 stdio MCP 使用 GoodBuddy 托管的 Node.js、按需安装的 Python,或经过验证的自定义解释器。
2026-09-02 10:36:56 +08:00

91 lines
2.7 KiB
TypeScript

import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
import type { ResolvedMcpServer } from './capability-service'
import {
isCuratedMcpLaunchDescriptor,
type CuratedMcpLaunchDescriptor
} from './curated-mcp-launch'
import {
applyLaunchEnvironmentPath,
buildCredentialFilteredUserEnvironment
} from '../agent/process-environment'
import type { LaunchEnvironmentProvider } from '../local-tool-environment/launch-environment-provider'
export function createMcpTransport(
server: ResolvedMcpServer | CuratedMcpLaunchDescriptor,
launchEnvironmentProvider?: LaunchEnvironmentProvider
): Transport {
if (isCuratedMcpLaunchDescriptor(server)) {
const environment = Object.fromEntries(
Object.entries(
applyLaunchEnvironmentPath(
{ ...server.env },
launchEnvironmentProvider
)
).filter(
(entry): entry is [string, string] =>
entry[1] !== undefined
)
)
return new StdioClientTransport({
command: server.command,
args: [...server.args],
cwd: server.cwd,
env: environment,
stderr: 'ignore',
maxBufferSize: 2 * 1024 * 1024
})
}
if ((server as { transport?: string }).transport === 'curated-stdio') {
throw new Error('无效的精选 MCP 启动描述')
}
if (server.transport === 'stdio') {
const env = launchEnvironmentProvider
? Object.fromEntries(
Object.entries(
applyLaunchEnvironmentPath(
buildCredentialFilteredUserEnvironment(),
launchEnvironmentProvider
)
).filter(
(entry): entry is [string, string] =>
entry[1] !== undefined
)
)
: undefined
return new StdioClientTransport({
command: server.command,
args: server.args,
...(env ? { env } : {}),
stderr: 'ignore',
maxBufferSize: 2 * 1024 * 1024
})
}
const url = new URL(server.url)
const requestInit: RequestInit | undefined = server.secret
? {
headers: {
Authorization: `Bearer ${server.secret}`
}
}
: undefined
return server.transport === 'http'
? new StreamableHTTPClientTransport(url, {
requestInit,
reconnectionOptions: {
initialReconnectionDelay: 500,
maxReconnectionDelay: 2_000,
reconnectionDelayGrowFactor: 1.5,
maxRetries: 0
}
})
: new SSEClientTransport(url, {
requestInit
})
}