MCPcopy Create free account
hub / github.com/claude-code-best/claude-code / handleInitializeRequest

Function handleInitializeRequest

src/cli/print.ts:4558–4747  ·  view source on GitHub ↗
(
  request: SDKControlInitializeRequest,
  requestId: string,
  initialized: boolean,
  output: Stream<StdoutMessage>,
  commands: Command[],
  modelInfos: ModelInfo[],
  structuredIO: StructuredIO,
  enableAuthStatus: boolean,
  options: {
    systemPrompt: string | undefined
    appendSystemPrompt: string | undefined
    agent?: string | undefined
    userSpecifiedModel?: string | undefined
    [key: string]: unknown
  },
  agents: AgentDefinition[],
  getAppState: () => AppState,
)

Source from the content-addressed store, hash-verified

4556}
4557
4558async function handleInitializeRequest(
4559 request: SDKControlInitializeRequest,
4560 requestId: string,
4561 initialized: boolean,
4562 output: Stream<StdoutMessage>,
4563 commands: Command[],
4564 modelInfos: ModelInfo[],
4565 structuredIO: StructuredIO,
4566 enableAuthStatus: boolean,
4567 options: {
4568 systemPrompt: string | undefined
4569 appendSystemPrompt: string | undefined
4570 agent?: string | undefined
4571 userSpecifiedModel?: string | undefined
4572 [key: string]: unknown
4573 },
4574 agents: AgentDefinition[],
4575 getAppState: () => AppState,
4576): Promise<void> {
4577 if (initialized) {
4578 output.enqueue({
4579 type: 'control_response',
4580 response: {
4581 subtype: 'error',
4582 error: 'Already initialized',
4583 request_id: requestId,
4584 pending_permission_requests:
4585 structuredIO.getPendingPermissionRequests(),
4586 },
4587 })
4588 return
4589 }
4590
4591 // Apply systemPrompt/appendSystemPrompt from stdin to avoid ARG_MAX limits
4592 if (request.systemPrompt !== undefined) {
4593 options.systemPrompt = request.systemPrompt
4594 }
4595 if (request.appendSystemPrompt !== undefined) {
4596 options.appendSystemPrompt = request.appendSystemPrompt
4597 }
4598 if (request.promptSuggestions !== undefined) {
4599 options.promptSuggestions = request.promptSuggestions
4600 }
4601
4602 // Merge agents from stdin to avoid ARG_MAX limits
4603 if (request.agents) {
4604 const stdinAgents = parseAgentsFromJson(request.agents, 'flagSettings')
4605 agents.push(...stdinAgents)
4606 }
4607
4608 // Re-evaluate main thread agent after SDK agents are merged
4609 // This allows --agent to reference agents defined via SDK
4610 if (options.agent) {
4611 // If main.tsx already found this agent (filesystem-defined), it already
4612 // applied systemPrompt/model/initialPrompt. Skip to avoid double-apply.
4613 const alreadyResolved = getMainThreadAgentType() === options.agent
4614 const mainThreadAgent = agents.find(a => a.agentType === options.agent)
4615 if (mainThreadAgent && !alreadyResolved) {

Callers 1

runHeadlessStreamingFunction · 0.85

Calls 15

parseAgentsFromJsonFunction · 0.85
getMainThreadAgentTypeFunction · 0.85
setMainThreadAgentTypeFunction · 0.85
isBuiltInAgentFunction · 0.85
parseUserSpecifiedModelFunction · 0.85
setMainLoopModelOverrideFunction · 0.85
getCwdFunction · 0.85
getAccountInformationFunction · 0.85
registerHookCallbacksFunction · 0.85
setInitJsonSchemaFunction · 0.85
getAPIProviderFunction · 0.85

Tested by

no test coverage detected