refactor: extract MCP config handling to use temp files on Windows

This commit is contained in:
weishu
2025-12-28 11:36:56 +08:00
parent 7922356669
commit 2ef7e0ec5f
3 changed files with 70 additions and 6 deletions
+6 -3
View File
@@ -27,6 +27,7 @@ import { withBunRuntimeEnv } from '@/utils/bunRuntime'
import { killProcessByChildProcess } from '@/utils/process'
import type { Writable } from 'node:stream'
import { logger } from '@/ui/logger'
import { appendMcpConfigArg } from '../utils/mcpConfig'
/**
* Query class manages Claude Code process interaction
@@ -285,6 +286,7 @@ export function query(config: {
// Build command arguments
const args = ['--output-format', 'stream-json', '--verbose']
let cleanupMcpConfig: (() => void) | null = null
if (customSystemPrompt) args.push('--system-prompt', customSystemPrompt)
if (appendSystemPrompt) args.push('--append-system-prompt', appendSystemPrompt)
@@ -301,9 +303,6 @@ export function query(config: {
if (settingsPath) args.push('--settings', settingsPath)
if (allowedTools.length > 0) args.push('--allowedTools', allowedTools.join(','))
if (disallowedTools.length > 0) args.push('--disallowedTools', disallowedTools.join(','))
if (mcpServers && Object.keys(mcpServers).length > 0) {
args.push('--mcp-config', JSON.stringify({ mcpServers }))
}
if (strictMcpConfig) args.push('--strict-mcp-config')
if (permissionMode) args.push('--permission-mode', permissionMode)
@@ -334,6 +333,8 @@ export function query(config: {
const spawnCommand = pathToClaudeCodeExecutable
const spawnArgs = args
cleanupMcpConfig = appendMcpConfigArg(spawnArgs, mcpServers)
// Spawn Claude Code process
// Use clean env for global claude to avoid local node_modules/.bin taking precedence
const baseEnv = isCommandOnly ? getCleanEnv() : process.env
@@ -394,6 +395,7 @@ export function query(config: {
// Handle process errors
child.on('error', (error) => {
cleanupMcpConfig?.()
if (config.options?.abort?.aborted) {
query.setError(new AbortError('Claude Code process aborted by user'))
} else {
@@ -408,6 +410,7 @@ export function query(config: {
if (process.env.CLAUDE_SDK_MCP_SERVERS) {
delete process.env.CLAUDE_SDK_MCP_SERVERS
}
cleanupMcpConfig?.()
})
return query