@@ -163,16 +163,37 @@ async function main() {
163
163
});
164
164
165
165
// 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);
172
179
173
180
console .log (' Connecting to Vapi MCP server via SSE...' );
174
181
await mcpClient .connect (transport);
175
182
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
+ }
176
197
177
198
try {
178
199
// List available tools
@@ -189,7 +210,7 @@ async function main() {
189
210
arguments: {},
190
211
});
191
212
192
- const assistants = assistantsResponse . content ;
213
+ const assistants = parseToolResponse ( assistantsResponse) ;
193
214
if (! (Array .isArray (assistants) && assistants .length > 0 )) {
194
215
console .log (' No assistants found. Please create an assistant in the Vapi dashboard first.' );
195
216
return ;
@@ -207,7 +228,7 @@ async function main() {
207
228
arguments: {},
208
229
});
209
230
210
- const phoneNumbers = phoneNumbersResponse . content ;
231
+ const phoneNumbers = parseToolResponse ( phoneNumbersResponse) ;
211
232
if (! (Array .isArray (phoneNumbers) && phoneNumbers .length > 0 )) {
212
233
console .log (' No phone numbers found. Please add a phone number in the Vapi dashboard first.' );
213
234
return ;
@@ -236,7 +257,8 @@ async function main() {
236
257
},
237
258
});
238
259
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 ));
240
262
} finally {
241
263
console .log (' \n Disconnecting from server...' );
242
264
await mcpClient .close ();
0 commit comments