|
| 1 | +/** |
| 2 | + * Transformers for MCP server inputs and outputs. |
| 3 | + * |
| 4 | + * These functions transform data between the MCP tool interface |
| 5 | + * and the Vapi API format. |
| 6 | + */ |
| 7 | + |
| 8 | +/** Phone number providers that support outbound calling. */ |
| 9 | +const OUTBOUND_CAPABLE_PROVIDERS = ["twilio", "vonage", "telnyx", "byo-phone-number"] as const; |
| 10 | + |
| 11 | +/** Phone number providers that are inbound-only. */ |
| 12 | +const INBOUND_ONLY_PROVIDERS = ["vapi"] as const; |
| 13 | + |
| 14 | +export type OutboundCapableProvider = (typeof OUTBOUND_CAPABLE_PROVIDERS)[number]; |
| 15 | +export type InboundOnlyProvider = (typeof INBOUND_ONLY_PROVIDERS)[number]; |
| 16 | +export type PhoneNumberProvider = OutboundCapableProvider | InboundOnlyProvider; |
| 17 | + |
| 18 | +export interface PhoneNumberOutput { |
| 19 | + id: string; |
| 20 | + orgId: string; |
| 21 | + provider: PhoneNumberProvider; |
| 22 | + number?: string; |
| 23 | + name?: string; |
| 24 | + createdAt: string; |
| 25 | + updatedAt: string; |
| 26 | + assistantId?: string; |
| 27 | + workflowId?: string; |
| 28 | + squadId?: string; |
| 29 | +} |
| 30 | + |
| 31 | +export interface PhoneNumberApiResponse { |
| 32 | + id: string; |
| 33 | + orgId: string; |
| 34 | + provider: string; |
| 35 | + number?: string; |
| 36 | + name?: string; |
| 37 | + createdAt: string; |
| 38 | + updatedAt: string; |
| 39 | + assistantId?: string; |
| 40 | + workflowId?: string; |
| 41 | + squadId?: string; |
| 42 | + [key: string]: unknown; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Transforms a phone number API response into the MCP output format. |
| 47 | + * |
| 48 | + * Exposes the `provider` field so MCP users can identify which numbers |
| 49 | + * support outbound calling (twilio, vonage, telnyx, byo-phone-number) |
| 50 | + * versus inbound-only (vapi). |
| 51 | + */ |
| 52 | +export function transformPhoneNumberOutput(apiResponse: PhoneNumberApiResponse): PhoneNumberOutput { |
| 53 | + return { |
| 54 | + id: apiResponse.id, |
| 55 | + orgId: apiResponse.orgId, |
| 56 | + provider: apiResponse.provider as PhoneNumberProvider, |
| 57 | + number: apiResponse.number, |
| 58 | + name: apiResponse.name, |
| 59 | + createdAt: apiResponse.createdAt, |
| 60 | + updatedAt: apiResponse.updatedAt, |
| 61 | + assistantId: apiResponse.assistantId, |
| 62 | + workflowId: apiResponse.workflowId, |
| 63 | + squadId: apiResponse.squadId, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +export interface CallInput { |
| 68 | + phoneNumberId?: string; |
| 69 | + assistantId?: string; |
| 70 | + workflowId?: string; |
| 71 | + squadId?: string; |
| 72 | + customerId?: string; |
| 73 | + customer?: { |
| 74 | + number: string; |
| 75 | + [key: string]: unknown; |
| 76 | + }; |
| 77 | + [key: string]: unknown; |
| 78 | +} |
| 79 | + |
| 80 | +export interface CallApiPayload { |
| 81 | + phoneNumberId?: string; |
| 82 | + assistantId?: string; |
| 83 | + workflowId?: string; |
| 84 | + squadId?: string; |
| 85 | + customerId?: string; |
| 86 | + customer?: { |
| 87 | + number: string; |
| 88 | + [key: string]: unknown; |
| 89 | + }; |
| 90 | + [key: string]: unknown; |
| 91 | +} |
| 92 | + |
| 93 | +export class OutboundCallValidationError extends Error { |
| 94 | + constructor(message: string) { |
| 95 | + super(message); |
| 96 | + this.name = "OutboundCallValidationError"; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Validates whether a phone number can be used for outbound calling. |
| 102 | + * |
| 103 | + * Vapi-provisioned numbers are inbound-only and cannot dial outbound. |
| 104 | + * This pre-validates the phone number type before making the API call |
| 105 | + * to provide a clear, actionable error message instead of the cryptic |
| 106 | + * API error "Vapi Numbers Can't Dial Outbound Yet". |
| 107 | + * |
| 108 | + * @param phoneNumberId - The ID of the phone number to validate. |
| 109 | + * @param fetchPhoneNumber - A function that fetches the phone number details by ID. |
| 110 | + * @throws {OutboundCallValidationError} If the phone number is a Vapi-provisioned number. |
| 111 | + */ |
| 112 | +export async function validatePhoneNumberForOutbound( |
| 113 | + phoneNumberId: string, |
| 114 | + fetchPhoneNumber: (id: string) => Promise<PhoneNumberApiResponse>, |
| 115 | +): Promise<void> { |
| 116 | + const phoneNumber = await fetchPhoneNumber(phoneNumberId); |
| 117 | + |
| 118 | + if (phoneNumber.provider === "vapi") { |
| 119 | + throw new OutboundCallValidationError( |
| 120 | + `Phone number "${phoneNumberId}" is a Vapi-provisioned number (provider: "vapi") and cannot be used for outbound calls. ` + |
| 121 | + "Vapi-provisioned numbers are inbound-only. " + |
| 122 | + "To make outbound calls, use a Twilio or Vonage imported number instead. " + |
| 123 | + 'You can check your available numbers with the "list_phone_numbers" tool and look for numbers with provider "twilio" or "vonage".', |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Transforms call input from MCP format to the API payload format. |
| 130 | + * |
| 131 | + * If a `phoneNumberId` is provided, this function validates that the |
| 132 | + * phone number is capable of outbound calling before returning the |
| 133 | + * API payload. Vapi-provisioned numbers will be rejected with a |
| 134 | + * clear error message. |
| 135 | + * |
| 136 | + * @param input - The MCP call input. |
| 137 | + * @param fetchPhoneNumber - A function that fetches phone number details by ID. |
| 138 | + * Required when `phoneNumberId` is provided in the input. |
| 139 | + * @returns The API payload for creating an outbound call. |
| 140 | + * @throws {OutboundCallValidationError} If a Vapi-provisioned number is used. |
| 141 | + */ |
| 142 | +export async function transformCallInput( |
| 143 | + input: CallInput, |
| 144 | + fetchPhoneNumber?: (id: string) => Promise<PhoneNumberApiResponse>, |
| 145 | +): Promise<CallApiPayload> { |
| 146 | + if (input.phoneNumberId && fetchPhoneNumber) { |
| 147 | + await validatePhoneNumberForOutbound(input.phoneNumberId, fetchPhoneNumber); |
| 148 | + } |
| 149 | + |
| 150 | + const payload: CallApiPayload = {}; |
| 151 | + |
| 152 | + if (input.phoneNumberId !== undefined) { |
| 153 | + payload.phoneNumberId = input.phoneNumberId; |
| 154 | + } |
| 155 | + if (input.assistantId !== undefined) { |
| 156 | + payload.assistantId = input.assistantId; |
| 157 | + } |
| 158 | + if (input.workflowId !== undefined) { |
| 159 | + payload.workflowId = input.workflowId; |
| 160 | + } |
| 161 | + if (input.squadId !== undefined) { |
| 162 | + payload.squadId = input.squadId; |
| 163 | + } |
| 164 | + if (input.customerId !== undefined) { |
| 165 | + payload.customerId = input.customerId; |
| 166 | + } |
| 167 | + if (input.customer !== undefined) { |
| 168 | + payload.customer = input.customer; |
| 169 | + } |
| 170 | + |
| 171 | + return payload; |
| 172 | +} |
0 commit comments