@anthropic-ai/claude-code 1.0.120 → 1.0.123

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/cli.js +1280 -1256
  2. package/package.json +1 -1
  3. package/sdk.d.ts +10 -1
  4. package/sdk.mjs +73 -40
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthropic-ai/claude-code",
3
- "version": "1.0.120",
3
+ "version": "1.0.123",
4
4
  "main": "sdk.mjs",
5
5
  "types": "sdk.d.ts",
6
6
  "bin": {
package/sdk.d.ts CHANGED
@@ -246,7 +246,7 @@ export type Options = {
246
246
  export type PermissionMode = 'default' | 'acceptEdits' | 'bypassPermissions' | 'plan';
247
247
  export type SlashCommand = {
248
248
  name: string;
249
- desciption: string;
249
+ description: string;
250
250
  argumentHint: string;
251
251
  };
252
252
  export type ModelInfo = {
@@ -254,6 +254,14 @@ export type ModelInfo = {
254
254
  displayName: string;
255
255
  description: string;
256
256
  };
257
+ export type McpServerStatus = {
258
+ name: string;
259
+ status: 'connected' | 'failed' | 'needs-auth' | 'pending';
260
+ serverInfo?: {
261
+ name: string;
262
+ version: string;
263
+ };
264
+ };
257
265
  export type SDKMessageBase = {
258
266
  uuid: UUID;
259
267
  session_id: string;
@@ -346,6 +354,7 @@ export interface Query extends AsyncGenerator<SDKMessage, void> {
346
354
  setModel(model?: string): Promise<void>;
347
355
  supportedCommands(): Promise<SlashCommand[]>;
348
356
  supportedModels(): Promise<ModelInfo[]>;
357
+ mcpServerStatus(): Promise<McpServerStatus[]>;
349
358
  }
350
359
  /**
351
360
  * Query Claude Code
package/sdk.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // (c) Anthropic PBC. All rights reserved. Use is subject to Anthropic's Commercial Terms of Service (https://www.anthropic.com/legal/commercial-terms).
4
4
 
5
- // Version: 1.0.120
5
+ // Version: 1.0.123
6
6
 
7
7
  // Want to see the unminified source? We're hiring!
8
8
  // https://job-boards.greenhouse.io/anthropic/jobs/4816199008
@@ -6364,7 +6364,6 @@ class ProcessTransport {
6364
6364
  initialize() {
6365
6365
  try {
6366
6366
  const {
6367
- prompt,
6368
6367
  additionalDirectories = [],
6369
6368
  cwd,
6370
6369
  executable = isRunningWithBun() ? "bun" : "node",
@@ -6389,7 +6388,13 @@ class ProcessTransport {
6389
6388
  canUseTool,
6390
6389
  includePartialMessages
6391
6390
  } = this.options;
6392
- const args = ["--output-format", "stream-json", "--verbose"];
6391
+ const args = [
6392
+ "--output-format",
6393
+ "stream-json",
6394
+ "--verbose",
6395
+ "--input-format",
6396
+ "stream-json"
6397
+ ];
6393
6398
  if (customSystemPrompt)
6394
6399
  args.push("--system-prompt", customSystemPrompt);
6395
6400
  if (appendSystemPrompt)
@@ -6401,9 +6406,6 @@ class ProcessTransport {
6401
6406
  if (env.DEBUG)
6402
6407
  args.push("--debug-to-stderr");
6403
6408
  if (canUseTool) {
6404
- if (typeof prompt === "string") {
6405
- throw new Error("canUseTool callback requires --input-format stream-json. Please set prompt as an AsyncIterable.");
6406
- }
6407
6409
  if (permissionPromptToolName) {
6408
6410
  throw new Error("canUseTool callback cannot be used with permissionPromptToolName. Please use one or the other.");
6409
6411
  }
@@ -6439,12 +6441,6 @@ class ProcessTransport {
6439
6441
  if (includePartialMessages) {
6440
6442
  args.push("--include-partial-messages");
6441
6443
  }
6442
- if (typeof prompt === "string") {
6443
- args.push("--print");
6444
- args.push("--", prompt.trim());
6445
- } else {
6446
- args.push("--input-format", "stream-json");
6447
- }
6448
6444
  for (const dir of additionalDirectories) {
6449
6445
  args.push("--add-dir", dir);
6450
6446
  }
@@ -6466,7 +6462,7 @@ class ProcessTransport {
6466
6462
  const isNative = isNativeBinary(pathToClaudeCodeExecutable);
6467
6463
  const spawnCommand = isNative ? pathToClaudeCodeExecutable : executable;
6468
6464
  const spawnArgs = isNative ? args : [...executableArgs, pathToClaudeCodeExecutable, ...args];
6469
- this.logDebug(isNative ? `Spawning Claude Code native binary: ${pathToClaudeCodeExecutable} ${args.join(" ")}` : `Spawning Claude Code process: ${executable} ${[...executableArgs, pathToClaudeCodeExecutable, ...args].join(" ")}`);
6465
+ this.logForDebugging(isNative ? `Spawning Claude Code native binary: ${pathToClaudeCodeExecutable} ${args.join(" ")}` : `Spawning Claude Code process: ${executable} ${[...executableArgs, pathToClaudeCodeExecutable, ...args].join(" ")}`);
6470
6466
  const stderrMode = env.DEBUG || stderr ? "pipe" : "ignore";
6471
6467
  this.child = spawn(spawnCommand, spawnArgs, {
6472
6468
  cwd,
@@ -6476,13 +6472,9 @@ class ProcessTransport {
6476
6472
  });
6477
6473
  this.childStdin = this.child.stdin;
6478
6474
  this.childStdout = this.child.stdout;
6479
- if (typeof prompt === "string") {
6480
- this.childStdin.end();
6481
- this.childStdin = undefined;
6482
- }
6483
6475
  if (env.DEBUG || stderr) {
6484
6476
  this.child.stderr.on("data", (data) => {
6485
- this.logDebug(`Claude Code stderr: ${data.toString()}`);
6477
+ this.logForDebugging(`Claude Code stderr: ${data.toString()}`);
6486
6478
  if (stderr) {
6487
6479
  stderr(data.toString());
6488
6480
  }
@@ -6503,7 +6495,7 @@ class ProcessTransport {
6503
6495
  this.exitError = new AbortError("Claude Code process aborted by user");
6504
6496
  } else {
6505
6497
  this.exitError = new Error(`Failed to spawn Claude Code process: ${error.message}`);
6506
- this.logDebug(this.exitError.message);
6498
+ this.logForDebugging(this.exitError.message);
6507
6499
  }
6508
6500
  });
6509
6501
  this.child.on("close", (code, signal) => {
@@ -6514,7 +6506,7 @@ class ProcessTransport {
6514
6506
  const error = this.getProcessExitError(code, signal);
6515
6507
  if (error) {
6516
6508
  this.exitError = error;
6517
- this.logDebug(error.message);
6509
+ this.logForDebugging(error.message);
6518
6510
  }
6519
6511
  }
6520
6512
  });
@@ -6532,7 +6524,7 @@ class ProcessTransport {
6532
6524
  }
6533
6525
  return;
6534
6526
  }
6535
- logDebug(message) {
6527
+ logForDebugging(message) {
6536
6528
  if (process.env.DEBUG) {
6537
6529
  process.stderr.write(`${message}
6538
6530
  `);
@@ -6790,7 +6782,7 @@ class SdkControlServerTransport {
6790
6782
  // src/core/Query.ts
6791
6783
  class Query {
6792
6784
  transport;
6793
- isStreamingMode;
6785
+ isSingleUserTurn;
6794
6786
  canUseTool;
6795
6787
  hooks;
6796
6788
  abortController;
@@ -6804,9 +6796,11 @@ class Query {
6804
6796
  nextCallbackId = 0;
6805
6797
  sdkMcpTransports = new Map;
6806
6798
  pendingMcpResponses = new Map;
6807
- constructor(transport, isStreamingMode, canUseTool, hooks, abortController, sdkMcpServers = new Map) {
6799
+ firstResultReceivedPromise;
6800
+ firstResultReceivedResolve;
6801
+ constructor(transport, isSingleUserTurn, canUseTool, hooks, abortController, sdkMcpServers = new Map) {
6808
6802
  this.transport = transport;
6809
- this.isStreamingMode = isStreamingMode;
6803
+ this.isSingleUserTurn = isSingleUserTurn;
6810
6804
  this.canUseTool = canUseTool;
6811
6805
  this.hooks = hooks;
6812
6806
  this.abortController = abortController;
@@ -6816,10 +6810,12 @@ class Query {
6816
6810
  server.connect(sdkTransport);
6817
6811
  }
6818
6812
  this.sdkMessages = this.readSdkMessages();
6813
+ this.firstResultReceivedPromise = new Promise((resolve) => {
6814
+ this.firstResultReceivedResolve = resolve;
6815
+ });
6819
6816
  this.readMessages();
6820
- if (this.isStreamingMode) {
6821
- this.initialization = this.initialize();
6822
- }
6817
+ this.initialization = this.initialize();
6818
+ this.initialization.catch(() => {});
6823
6819
  }
6824
6820
  setError(error) {
6825
6821
  this.inputStream.error(error);
@@ -6869,6 +6865,16 @@ class Query {
6869
6865
  } else if (message.type === "control_cancel_request") {
6870
6866
  this.handleControlCancelRequest(message);
6871
6867
  continue;
6868
+ } else if (message.type === "keep_alive") {
6869
+ continue;
6870
+ }
6871
+ if (message.type === "result") {
6872
+ if (this.firstResultReceivedResolve) {
6873
+ this.firstResultReceivedResolve();
6874
+ }
6875
+ if (this.isSingleUserTurn) {
6876
+ this.transport.endInput();
6877
+ }
6872
6878
  }
6873
6879
  this.inputStream.enqueue(message);
6874
6880
  }
@@ -6999,9 +7005,6 @@ class Query {
6999
7005
  });
7000
7006
  }
7001
7007
  request(request) {
7002
- if (!this.isStreamingMode) {
7003
- throw new Error(`${request.subtype} requires --input-format stream-json`);
7004
- }
7005
7008
  const requestId = Math.random().toString(36).substring(2, 15);
7006
7009
  const sdkRequest = {
7007
7010
  request_id: requestId,
@@ -7021,17 +7024,18 @@ class Query {
7021
7024
  });
7022
7025
  }
7023
7026
  async supportedCommands() {
7024
- if (!this.isStreamingMode || !this.initialization) {
7025
- throw new Error("supportedCommands is only supported in streaming mode");
7026
- }
7027
7027
  return (await this.initialization).commands;
7028
7028
  }
7029
7029
  async supportedModels() {
7030
- if (!this.isStreamingMode || !this.initialization) {
7031
- throw new Error("supportedModels is only supported in streaming mode");
7032
- }
7033
7030
  return (await this.initialization).models;
7034
7031
  }
7032
+ async mcpServerStatus() {
7033
+ const response = await this.request({
7034
+ subtype: "mcp_status"
7035
+ });
7036
+ const mcpStatusResponse = response.response;
7037
+ return mcpStatusResponse.mcpServers;
7038
+ }
7035
7039
  async streamInput(stream) {
7036
7040
  try {
7037
7041
  for await (const message of stream) {
@@ -7040,6 +7044,25 @@ class Query {
7040
7044
  await Promise.resolve(this.transport.write(JSON.stringify(message) + `
7041
7045
  `));
7042
7046
  }
7047
+ if (this.sdkMcpTransports.size > 0 && this.firstResultReceivedPromise) {
7048
+ const STREAM_CLOSE_TIMEOUT = 1e4;
7049
+ let timeoutId;
7050
+ await Promise.race([
7051
+ this.firstResultReceivedPromise.then(() => {
7052
+ if (timeoutId) {
7053
+ clearTimeout(timeoutId);
7054
+ }
7055
+ }),
7056
+ new Promise((resolve) => {
7057
+ timeoutId = setTimeout(() => {
7058
+ resolve();
7059
+ }, STREAM_CLOSE_TIMEOUT);
7060
+ })
7061
+ ]);
7062
+ if (timeoutId) {
7063
+ clearTimeout(timeoutId);
7064
+ }
7065
+ }
7043
7066
  this.transport.endInput();
7044
7067
  } catch (error) {
7045
7068
  if (!(error instanceof AbortError)) {
@@ -14045,9 +14068,8 @@ function query({
14045
14068
  }
14046
14069
  }
14047
14070
  }
14048
- const isStreamingMode = typeof prompt !== "string";
14071
+ const isSingleUserTurn = typeof prompt === "string";
14049
14072
  const transport = new ProcessTransport({
14050
- prompt,
14051
14073
  abortController,
14052
14074
  additionalDirectories,
14053
14075
  cwd,
@@ -14074,8 +14096,19 @@ function query({
14074
14096
  hooks: !!hooks,
14075
14097
  includePartialMessages
14076
14098
  });
14077
- const query2 = new Query(transport, isStreamingMode, canUseTool, hooks, abortController, sdkMcpServers);
14078
- if (typeof prompt !== "string") {
14099
+ const query2 = new Query(transport, isSingleUserTurn, canUseTool, hooks, abortController, sdkMcpServers);
14100
+ if (typeof prompt === "string") {
14101
+ transport.write(JSON.stringify({
14102
+ type: "user",
14103
+ session_id: "",
14104
+ message: {
14105
+ role: "user",
14106
+ content: [{ type: "text", text: prompt }]
14107
+ },
14108
+ parent_tool_use_id: null
14109
+ }) + `
14110
+ `);
14111
+ } else {
14079
14112
  query2.streamInput(prompt);
14080
14113
  }
14081
14114
  return query2;