@@ -168,18 +168,38 @@ async function main() {
168168 Authorization: ` Bearer ${ process .env .VAPI_TOKEN } ` ,
169169 };
170170 const options = {
171+ requestInit: { headers: headers },
171172 eventSourceInit: {
172- fetch : (url , init ) => fetch (url, { ... init, headers }),
173- },
174- requestInit: {
175- headers,
173+ fetch : (url , init ) => {
174+ return fetch (url, {
175+ ... (init || {}),
176+ headers: {
177+ ... (init? .headers || {}),
178+ ... headers,
179+ },
180+ });
181+ },
176182 },
177183 };
178184 const transport = new SSEClientTransport (new URL (serverUrl), options);
179185
180186 console .log (' Connecting to Vapi MCP server via SSE...' );
181187 await mcpClient .connect (transport);
182188 console .log (' Connected successfully' );
189+
190+ // Helper function to parse tool responses
191+ function parseToolResponse (response ) {
192+ if (! response? .content ) return response;
193+ const textItem = response .content .find (item => item .type === ' text' );
194+ if (textItem? .text ) {
195+ try {
196+ return JSON .parse (textItem .text );
197+ } catch {
198+ return textItem .text ;
199+ }
200+ }
201+ return response;
202+ }
183203
184204 try {
185205 // List available tools
@@ -196,7 +216,7 @@ async function main() {
196216 arguments: {},
197217 });
198218
199- const assistants = assistantsResponse . content ;
219+ const assistants = parseToolResponse ( assistantsResponse) ;
200220 if (! (Array .isArray (assistants) && assistants .length > 0 )) {
201221 console .log (' No assistants found. Please create an assistant in the Vapi dashboard first.' );
202222 return ;
@@ -214,7 +234,7 @@ async function main() {
214234 arguments: {},
215235 });
216236
217- const phoneNumbers = phoneNumbersResponse . content ;
237+ const phoneNumbers = parseToolResponse ( phoneNumbersResponse) ;
218238 if (! (Array .isArray (phoneNumbers) && phoneNumbers .length > 0 )) {
219239 console .log (' No phone numbers found. Please add a phone number in the Vapi dashboard first.' );
220240 return ;
@@ -243,7 +263,8 @@ async function main() {
243263 },
244264 });
245265
246- console .log (' Call created:' , JSON .stringify (createCallResponse .content , null , 2 ));
266+ const createdCall = parseToolResponse (createCallResponse);
267+ console .log (' Call created:' , JSON .stringify (createdCall, null , 2 ));
247268 } finally {
248269 console .log (' \n Disconnecting from server...' );
249270 await mcpClient .close ();
0 commit comments