Skip to content

Commit 3668ba6

Browse files
committed
run shell path async to avoid blocking config load
1 parent 0938308 commit 3668ba6

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

core/context/mcp/MCPConnection.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class MCPConnection {
4747
};
4848

4949
constructor(public options: MCPOptions) {
50-
this.transport = this.constructTransport(options);
50+
// Don't construct transport in constructor to avoid blocking
51+
this.transport = {} as Transport; // Will be set in connectClient
5152

5253
this.client = new Client(
5354
{
@@ -132,7 +133,8 @@ class MCPConnection {
132133
});
133134
}),
134135
(async () => {
135-
this.transport = this.constructTransport(this.options);
136+
this.transport = await this.constructTransportAsync(this.options);
137+
136138
try {
137139
await this.client.connect(this.transport);
138140
} catch (error) {
@@ -295,7 +297,9 @@ class MCPConnection {
295297
};
296298
}
297299

298-
private constructTransport(options: MCPOptions): Transport {
300+
private async constructTransportAsync(
301+
options: MCPOptions,
302+
): Promise<Transport> {
299303
switch (options.transport.type) {
300304
case "stdio":
301305
const env: Record<string, string> = options.transport.env
@@ -309,7 +313,7 @@ class MCPConnection {
309313
// For non-Windows platforms, try to get the PATH from user shell
310314
if (process.platform !== "win32") {
311315
try {
312-
const shellEnvPath = getEnvPathFromUserShell();
316+
const shellEnvPath = await getEnvPathFromUserShell();
313317
if (shellEnvPath && shellEnvPath !== process.env.PATH) {
314318
env.PATH = shellEnvPath;
315319
}

core/util/shellPath.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { execSync } from "child_process";
2-
import { homedir } from "os";
1+
import { exec } from "child_process";
2+
import { promisify } from "util";
33

4-
export function getEnvPathFromUserShell(): string | undefined {
4+
const execAsync = promisify(exec);
5+
export async function getEnvPathFromUserShell(): Promise<string | undefined> {
56
if (process.platform === "win32") {
67
console.warn(`${getEnvPathFromUserShell.name} not implemented for Windows`);
78
return undefined;
@@ -15,7 +16,7 @@ export function getEnvPathFromUserShell(): string | undefined {
1516
// Source common profile files
1617
const command = `${process.env.SHELL} -l -c 'for f in ~/.zprofile ~/.zshrc ~/.bash_profile ~/.bashrc; do [ -f "$f" ] && source "$f" 2>/dev/null; done; echo $PATH'`;
1718

18-
const stdout = execSync(command, {
19+
const { stdout } = await execAsync(command, {
1920
encoding: "utf8",
2021
});
2122

0 commit comments

Comments
 (0)