Skip to content

Update mcp server examples #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions fern/sdk/mcp-server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,37 @@ async function main() {
});

// Create SSE transport for connection to remote Vapi MCP server
const transport = new SSEClientTransport({
url: 'https://mcp.vapi.ai/sse',
headers: {
'Authorization': `Bearer ${process.env.VAPI_TOKEN}`
}
});
const serverUrl = 'https://mcp.vapi.ai/sse';
const headers = {
Authorization: `Bearer ${process.env.VAPI_TOKEN}`,
};
const options = {
eventSourceInit: {
fetch: (url, init) => fetch(url, { ...init, headers }),
},
requestInit: {
headers,
},
};
const transport = new SSEClientTransport(new URL(serverUrl), options);

console.log('Connecting to Vapi MCP server via SSE...');
await mcpClient.connect(transport);
console.log('Connected successfully');

// Helper function to parse tool responses
function parseToolResponse(response) {
if (!response?.content) return response;
const textItem = response.content.find(item => item.type === 'text');
if (textItem?.text) {
try {
return JSON.parse(textItem.text);
} catch {
return textItem.text;
}
}
return response;
}

try {
// List available tools
Expand All @@ -189,7 +210,7 @@ async function main() {
arguments: {},
});

const assistants = assistantsResponse.content;
const assistants = parseToolResponse(assistantsResponse);
if (!(Array.isArray(assistants) && assistants.length > 0)) {
console.log('No assistants found. Please create an assistant in the Vapi dashboard first.');
return;
Expand All @@ -207,7 +228,7 @@ async function main() {
arguments: {},
});

const phoneNumbers = phoneNumbersResponse.content;
const phoneNumbers = parseToolResponse(phoneNumbersResponse);
if (!(Array.isArray(phoneNumbers) && phoneNumbers.length > 0)) {
console.log('No phone numbers found. Please add a phone number in the Vapi dashboard first.');
return;
Expand Down Expand Up @@ -236,7 +257,8 @@ async function main() {
},
});

console.log('Call created:', JSON.stringify(createCallResponse.content, null, 2));
const createdCall = parseToolResponse(createCallResponse);
console.log('Call created:', JSON.stringify(createdCall, null, 2));
} finally {
console.log('\nDisconnecting from server...');
await mcpClient.close();
Expand Down
Loading