Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ if (args.includes("--help") || args.includes("-h")) {
"deepcode - Deep Code CLI",
"",
"Usage:",
" deepcode Launch the interactive TUI in the current directory",
" deepcode --version Print the version",
" deepcode --help Show this help",
" deepcode Launch the interactive TUI in the current directory",
" deepcode -p <prompt> Launch with a pre-filled prompt",
" deepcode --prompt <prompt> Same as -p",
" deepcode --version Print the version",
" deepcode --help Show this help",
"",
"Configuration:",
" ~/.deepcode/settings.json User-level API key, model, base URL",
Expand Down Expand Up @@ -50,6 +52,15 @@ if (args.includes("--help") || args.includes("-h")) {
process.exit(0);
}

function extractInitialPrompt(args: string[]): string | undefined {
const promptIndex = args.findIndex((arg) => arg === "-p" || arg === "--prompt");
if (promptIndex !== -1 && promptIndex + 1 < args.length) {
return args[promptIndex + 1];
}
return undefined;
}

let initialPrompt = extractInitialPrompt(args);
const projectRoot = process.cwd();
configureWindowsShell();

Expand All @@ -67,8 +78,15 @@ async function main(): Promise<void> {

function startApp(): void {
let restarting = false;
const appInitialPrompt = initialPrompt;
initialPrompt = undefined;
const inkInstance = render(
<App projectRoot={projectRoot} version={packageInfo.version} onRestart={() => restartRef.current?.()} />,
<App
projectRoot={projectRoot}
version={packageInfo.version}
initialPrompt={appInitialPrompt}
onRestart={() => restartRef.current?.()}
/>,
{ exitOnCtrlC: false }
);

Expand Down
34 changes: 31 additions & 3 deletions src/mcp/mcp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,24 @@ export class McpClient {
>();
private stderrBuffer = "";
private notificationHandler: McpNotificationHandler | null = null;
private disconnectHandler: ((reason: string) => void) | null = null;
private intentionallyDisconnected = false;

constructor(
private readonly serverName: string,
private readonly command: string,
private readonly args: string[] = [],
private readonly env?: Record<string, string>,
onNotification?: McpNotificationHandler
onNotification?: McpNotificationHandler,
onDisconnect?: (reason: string) => void
) {
this.notificationHandler = onNotification ?? null;
this.disconnectHandler = onDisconnect ?? null;
}

async connect(timeoutMs: number): Promise<void> {
return new Promise((resolve, reject) => {
this.intentionallyDisconnected = false;
const childEnv = {
...process.env,
...this.env,
Expand All @@ -144,17 +149,35 @@ export class McpClient {
});
}

let resolved = false;
const safeReject = (err: Error) => {
if (!resolved) {
resolved = true;
reject(err);
}
};

this.process.on("error", (err) => {
reject(this.withStderr(`Failed to start MCP server "${this.serverName}" (${this.command}): ${err.message}`));
safeReject(
this.withStderr(`Failed to start MCP server "${this.serverName}" (${this.command}): ${err.message}`)
);
});

this.process.on("close", (code) => {
const error = this.withStderr(`MCP server "${this.serverName}" exited with code ${code}`);
const reason = `MCP server "${this.serverName}" exited with code ${code}`;
const error = this.withStderr(reason);
for (const [, pending] of this.pendingRequests) {
clearTimeout(pending.timer);
pending.reject(error);
}
this.pendingRequests.clear();
this.reader?.close();
this.reader = null;
this.process = null;
if (!this.intentionallyDisconnected && this.disconnectHandler) {
this.disconnectHandler(reason);
}
safeReject(error);
});

if (this.process.stderr) {
Expand Down Expand Up @@ -263,6 +286,7 @@ export class McpClient {
}

disconnect(): void {
this.intentionallyDisconnected = true;
if (this.reader) {
this.reader.close();
this.reader = null;
Expand All @@ -273,6 +297,10 @@ export class McpClient {
}
}

isConnected(): boolean {
return this.process !== null && this.process.exitCode === null;
}

private sendRequest(method: string, params: Record<string, unknown>, timeoutMs = 30_000): Promise<unknown> {
return new Promise((resolve, reject) => {
const id = this.nextId++;
Expand Down
Loading
Loading