@@ -163,16 +163,37 @@ async function main() {
163163 });
164164
165165 // Create SSE transport for connection to remote Vapi MCP server
166- const transport = new SSEClientTransport ({
167- url: ' https://mcp.vapi.ai/sse' ,
168- headers: {
169- ' Authorization' : ` Bearer ${ process .env .VAPI_TOKEN } `
170- }
171- });
166+ const serverUrl = ' https://mcp.vapi.ai/sse' ;
167+ const headers = {
168+ Authorization: ` Bearer ${ process .env .VAPI_TOKEN } ` ,
169+ };
170+ const options = {
171+ eventSourceInit: {
172+ fetch : (url , init ) => fetch (url, { ... init, headers }),
173+ },
174+ requestInit: {
175+ headers,
176+ },
177+ };
178+ const transport = new SSEClientTransport (new URL (serverUrl), options);
172179
173180 console .log (' Connecting to Vapi MCP server via SSE...' );
174181 await mcpClient .connect (transport);
175182 console .log (' Connected successfully' );
183+
184+ // Helper function to parse tool responses
185+ function parseToolResponse (response ) {
186+ if (! response? .content ) return response;
187+ const textItem = response .content .find (item => item .type === ' text' );
188+ if (textItem? .text ) {
189+ try {
190+ return JSON .parse (textItem .text );
191+ } catch {
192+ return textItem .text ;
193+ }
194+ }
195+ return response;
196+ }
176197
177198 try {
178199 // List available tools
@@ -189,7 +210,7 @@ async function main() {
189210 arguments: {},
190211 });
191212
192- const assistants = assistantsResponse . content ;
213+ const assistants = parseToolResponse ( assistantsResponse) ;
193214 if (! (Array .isArray (assistants) && assistants .length > 0 )) {
194215 console .log (' No assistants found. Please create an assistant in the Vapi dashboard first.' );
195216 return ;
@@ -207,7 +228,7 @@ async function main() {
207228 arguments: {},
208229 });
209230
210- const phoneNumbers = phoneNumbersResponse . content ;
231+ const phoneNumbers = parseToolResponse ( phoneNumbersResponse) ;
211232 if (! (Array .isArray (phoneNumbers) && phoneNumbers .length > 0 )) {
212233 console .log (' No phone numbers found. Please add a phone number in the Vapi dashboard first.' );
213234 return ;
@@ -236,7 +257,8 @@ async function main() {
236257 },
237258 });
238259
239- console .log (' Call created:' , JSON .stringify (createCallResponse .content , null , 2 ));
260+ const createdCall = parseToolResponse (createCallResponse);
261+ console .log (' Call created:' , JSON .stringify (createdCall, null , 2 ));
240262 } finally {
241263 console .log (' \n Disconnecting from server...' );
242264 await mcpClient .close ();
0 commit comments