Skip to content

Commit 000f443

Browse files
feat: add update_tool
1 parent 77cbf12 commit 000f443

3 files changed

Lines changed: 86 additions & 7 deletions

File tree

src/schemas/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,8 @@ const BackoffPlanSchema = z.object({
363363
baseDelaySeconds: z.number().default(1).describe('Base delay between retries in seconds'),
364364
});
365365

366-
export const CreateToolInputSchema = z.object({
367-
type: z.enum(['sms', 'transferCall', 'function', 'apiRequest'])
368-
.describe('Type of the tool to create'),
369-
366+
// Base tool configuration schema (reusable for both create and update)
367+
const BaseToolConfigSchema = z.object({
370368
// Common fields for all tools
371369
name: z.string().optional().describe('Name of the function/tool'),
372370
description: z.string().optional().describe('Description of what the function/tool does'),
@@ -400,10 +398,21 @@ export const CreateToolInputSchema = z.object({
400398
}).optional().describe('API Request tool configuration - for HTTP API integration'),
401399
});
402400

401+
export const CreateToolInputSchema = BaseToolConfigSchema.extend({
402+
type: z.enum(['sms', 'transferCall', 'function', 'apiRequest'])
403+
.describe('Type of the tool to create'),
404+
});
405+
406+
export const UpdateToolInputSchema = BaseToolConfigSchema.extend({
407+
toolId: z.string().describe('ID of the tool to update'),
408+
});
409+
403410
export const ToolOutputSchema = BaseResponseSchema.extend({
404411
type: z
405412
.string()
406413
.describe('Type of the tool (dtmf, function, mcp, query, etc.)'),
407414
name: z.string().describe('Name of the tool'),
408415
description: z.string().describe('Description of the tool'),
416+
parameters: z.record(z.any()).describe('Parameters of the tool'),
417+
server: ServerSchema.describe('Server of the tool'),
409418
});

src/tools/tool.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
22
import { VapiClient, Vapi } from '@vapi-ai/server-sdk';
33

4-
import { GetToolInputSchema, CreateToolInputSchema } from '../schemas/index.js';
5-
import { transformToolInput, transformToolOutput } from '../transformers/index.js';
4+
import { GetToolInputSchema, CreateToolInputSchema, UpdateToolInputSchema } from '../schemas/index.js';
5+
import { transformToolInput, transformUpdateToolInput, transformToolOutput } from '../transformers/index.js';
66
import { createToolHandler } from './utils.js';
77

88
export const registerToolTools = (
@@ -39,4 +39,15 @@ export const registerToolTools = (
3939
return transformToolOutput(tool);
4040
})
4141
);
42+
43+
server.tool(
44+
'update_tool',
45+
'Updates an existing Vapi tool',
46+
UpdateToolInputSchema.shape,
47+
createToolHandler(async (data) => {
48+
const updateToolDto = transformUpdateToolInput(data);
49+
const tool = await vapiClient.tools.update(data.toolId, updateToolDto);
50+
return transformToolOutput(tool);
51+
})
52+
);
4253
};

src/transformers/index.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ToolOutputSchema,
1010
UpdateAssistantInputSchema,
1111
CreateToolInputSchema,
12+
UpdateToolInputSchema,
1213
} from '../schemas/index.js';
1314

1415
// ===== Assistant Transformers =====
@@ -310,8 +311,61 @@ export function transformToolInput(
310311
return toolDto;
311312
}
312313

314+
export function transformUpdateToolInput(
315+
input: z.infer<typeof UpdateToolInputSchema>
316+
): any {
317+
let updateDto: any = {};
318+
319+
// Add function definition if name and description are provided
320+
if (input.name || input.description) {
321+
updateDto.function = {
322+
...(input.name && { name: input.name }),
323+
...(input.description && { description: input.description }),
324+
};
325+
}
326+
327+
// Handle SMS tool configuration
328+
if (input.sms?.metadata) {
329+
updateDto.metadata = input.sms.metadata;
330+
}
331+
332+
// Handle Transfer call tool configuration
333+
if (input.transferCall?.destinations) {
334+
updateDto.destinations = input.transferCall.destinations;
335+
}
336+
337+
// Handle Function tool configuration
338+
if (input.function?.parameters && input.function?.server) {
339+
// For function tools, add parameters to the existing function object
340+
if (updateDto.function) {
341+
updateDto.function.parameters = input.function.parameters;
342+
} else {
343+
updateDto.function = {
344+
parameters: input.function.parameters,
345+
};
346+
}
347+
348+
updateDto.server = {
349+
url: input.function.server.url,
350+
...(input.function.server.headers && { headers: input.function.server.headers }),
351+
};
352+
}
353+
354+
// Handle API Request tool configuration
355+
if (input.apiRequest) {
356+
if (input.apiRequest.url) updateDto.url = input.apiRequest.url;
357+
if (input.apiRequest.method) updateDto.method = input.apiRequest.method;
358+
if (input.apiRequest.headers) updateDto.headers = input.apiRequest.headers;
359+
if (input.apiRequest.body) updateDto.body = input.apiRequest.body;
360+
if (input.apiRequest.backoffPlan) updateDto.backoffPlan = input.apiRequest.backoffPlan;
361+
if (input.apiRequest.timeoutSeconds) updateDto.timeoutSeconds = input.apiRequest.timeoutSeconds;
362+
}
363+
364+
return updateDto;
365+
}
366+
313367
export function transformToolOutput(
314-
tool: any
368+
tool: Vapi.ToolsGetResponse
315369
): z.infer<typeof ToolOutputSchema> {
316370
return {
317371
id: tool.id,
@@ -320,5 +374,10 @@ export function transformToolOutput(
320374
type: tool.type || '',
321375
name: tool.function?.name || '',
322376
description: tool.function?.description || '',
377+
parameters: tool.function?.parameters || {},
378+
server: {
379+
url: tool.server?.url || '',
380+
headers: tool.server?.headers as Record<string, string> || {},
381+
}
323382
};
324383
}

0 commit comments

Comments
 (0)