@anthropic-ai/claude-code 1.0.58 → 1.0.59

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthropic-ai/claude-code",
3
- "version": "1.0.58",
3
+ "version": "1.0.59",
4
4
  "main": "sdk.mjs",
5
5
  "types": "sdk.d.ts",
6
6
  "bin": {
package/sdk.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // (c) Anthropic PBC. All rights reserved. Use is subject to Anthropic's Commercial Terms of Service (https://www.anthropic.com/legal/commercial-terms).
2
2
 
3
- // Version: 1.0.58
3
+ // Version: 1.0.59
4
4
 
5
5
  // src/entrypoints/sdk.ts
6
6
  import { spawn } from "child_process";
@@ -109,16 +109,21 @@ function query({
109
109
  pathToClaudeCodeExecutable,
110
110
  permissionMode = "default",
111
111
  permissionPromptToolName,
112
+ canUseTool,
112
113
  continue: continueConversation,
113
114
  resume,
114
115
  model,
115
116
  fallbackModel,
116
117
  strictMcpConfig,
117
- stderr
118
+ stderr,
119
+ env
118
120
  } = {}
119
121
  }) {
120
- if (!process.env.CLAUDE_CODE_ENTRYPOINT) {
121
- process.env.CLAUDE_CODE_ENTRYPOINT = "sdk-ts";
122
+ if (!env) {
123
+ env = { ...process.env };
124
+ }
125
+ if (!env.CLAUDE_CODE_ENTRYPOINT) {
126
+ env.CLAUDE_CODE_ENTRYPOINT = "sdk-ts";
122
127
  }
123
128
  if (pathToClaudeCodeExecutable === undefined) {
124
129
  const filename = fileURLToPath(import.meta.url);
@@ -134,8 +139,18 @@ function query({
134
139
  args.push("--max-turns", maxTurns.toString());
135
140
  if (model)
136
141
  args.push("--model", model);
137
- if (permissionPromptToolName)
142
+ if (canUseTool) {
143
+ if (typeof prompt === "string") {
144
+ throw new Error("canUseTool callback requires --input-format stream-json. Please set prompt as an AsyncIterable.");
145
+ }
146
+ if (permissionPromptToolName) {
147
+ throw new Error("canUseTool callback cannot be used with permissionPromptToolName. Please use one or the other.");
148
+ }
149
+ permissionPromptToolName = "stdio";
150
+ }
151
+ if (permissionPromptToolName) {
138
152
  args.push("--permission-prompt-tool", permissionPromptToolName);
153
+ }
139
154
  if (continueConversation)
140
155
  args.push("--continue");
141
156
  if (resume)
@@ -174,9 +189,7 @@ function query({
174
189
  cwd,
175
190
  stdio: ["pipe", "pipe", "pipe"],
176
191
  signal: abortController.signal,
177
- env: {
178
- ...process.env
179
- }
192
+ env
180
193
  });
181
194
  let childStdin;
182
195
  if (typeof prompt === "string") {
@@ -185,9 +198,9 @@ function query({
185
198
  streamToStdin(prompt, child.stdin, abortController);
186
199
  childStdin = child.stdin;
187
200
  }
188
- if (process.env.DEBUG || stderr) {
201
+ if (env.DEBUG || stderr) {
189
202
  child.stderr.on("data", (data) => {
190
- if (process.env.DEBUG) {
203
+ if (env.DEBUG) {
191
204
  console.error("Claude Code stderr:", data.toString());
192
205
  }
193
206
  if (stderr) {
@@ -214,7 +227,7 @@ function query({
214
227
  }
215
228
  });
216
229
  });
217
- const query2 = new Query(childStdin, child.stdout, processExitPromise);
230
+ const query2 = new Query(childStdin, child.stdout, processExitPromise, canUseTool);
218
231
  child.on("error", (error) => {
219
232
  if (abortController.signal.aborted) {
220
233
  query2.setError(new AbortError("Claude Code process aborted by user"));
@@ -225,9 +238,6 @@ function query({
225
238
  processExitPromise.finally(() => {
226
239
  cleanup();
227
240
  abortController.signal.removeEventListener("abort", cleanup);
228
- if (process.env.CLAUDE_SDK_MCP_SERVERS) {
229
- delete process.env.CLAUDE_SDK_MCP_SERVERS;
230
- }
231
241
  });
232
242
  return query2;
233
243
  }
@@ -236,13 +246,15 @@ class Query {
236
246
  childStdin;
237
247
  childStdout;
238
248
  processExitPromise;
249
+ canUseTool;
239
250
  pendingControlResponses = new Map;
240
251
  sdkMessages;
241
252
  inputStream = new Stream;
242
- constructor(childStdin, childStdout, processExitPromise) {
253
+ constructor(childStdin, childStdout, processExitPromise, canUseTool) {
243
254
  this.childStdin = childStdin;
244
255
  this.childStdout = childStdout;
245
256
  this.processExitPromise = processExitPromise;
257
+ this.canUseTool = canUseTool;
246
258
  this.readMessages();
247
259
  this.sdkMessages = this.readSdkMessages();
248
260
  }
@@ -276,6 +288,9 @@ class Query {
276
288
  handler(message.response);
277
289
  }
278
290
  continue;
291
+ } else if (message.type === "control_request") {
292
+ this.handleControlRequest(message);
293
+ continue;
279
294
  }
280
295
  this.inputStream.enqueue(message);
281
296
  }
@@ -288,6 +303,41 @@ class Query {
288
303
  rl.close();
289
304
  }
290
305
  }
306
+ async handleControlRequest(request) {
307
+ try {
308
+ const response = await this.processControlRequest(request);
309
+ const controlResponse = {
310
+ type: "control_response",
311
+ response: {
312
+ subtype: "success",
313
+ request_id: request.request_id,
314
+ response
315
+ }
316
+ };
317
+ this.childStdin?.write(JSON.stringify(controlResponse) + `
318
+ `);
319
+ } catch (error) {
320
+ const controlErrorResponse = {
321
+ type: "control_response",
322
+ response: {
323
+ subtype: "error",
324
+ request_id: request.request_id,
325
+ error: error.message || String(error)
326
+ }
327
+ };
328
+ this.childStdin?.write(JSON.stringify(controlErrorResponse) + `
329
+ `);
330
+ }
331
+ }
332
+ async processControlRequest(request) {
333
+ if (request.request.subtype === "can_use_tool") {
334
+ if (!this.canUseTool) {
335
+ throw new Error("canUseTool callback is not provided.");
336
+ }
337
+ return this.canUseTool(request.request.tool_name, request.request.input);
338
+ }
339
+ throw new Error("Unsupported control request subtype: " + request.request.subtype);
340
+ }
291
341
  async* readSdkMessages() {
292
342
  for await (const message of this.inputStream) {
293
343
  yield message;
Binary file