33import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' ;
44import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js' ;
55import { VapiClient } from '@vapi-ai/server-sdk' ;
6- import { hasValidToken , getToken , startAuthFlow , isAuthInProgress , getAuthUrl } from './auth.js' ;
6+ import { hasValidToken , getToken , startAuthFlow , isAuthInProgress , getAuthUrl , clearConfig } from './auth.js' ;
77import { registerAllTools } from './tools/index.js' ;
88
99import dotenv from 'dotenv' ;
@@ -37,16 +37,24 @@ function createMcpServer() {
3737 'Authenticate with Vapi. Call this first if other tools return authentication errors.' ,
3838 { } ,
3939 async ( ) => {
40- // Check if already authenticated
40+ // Check if we have a token and validate it
4141 if ( hasValidToken ( ) ) {
42- return {
43- content : [
44- {
45- type : 'text' as const ,
46- text : 'Already authenticated with Vapi! You can now use other Vapi tools.' ,
47- } ,
48- ] ,
49- } ;
42+ try {
43+ const client = getVapiClient ( ) ;
44+ await client . assistants . list ( { limit : 1 } ) ;
45+ return {
46+ content : [
47+ {
48+ type : 'text' as const ,
49+ text : 'Already authenticated with Vapi! You can now use other Vapi tools.' ,
50+ } ,
51+ ] ,
52+ } ;
53+ } catch {
54+ // Token is stale — clear it and restart auth
55+ clearConfig ( ) ;
56+ vapiClient = null ;
57+ }
5058 }
5159
5260 // Check if auth is already in progress
@@ -56,7 +64,7 @@ function createMcpServer() {
5664 content : [
5765 {
5866 type : 'text' as const ,
59- text : `Authentication in progress. Please complete sign-in at :\n\n${ url } \n\nAfter signing in, try your request again.` ,
67+ text : `Authentication in progress. Please complete sign-in:\n\n${ url } \n\nAfter signing in, try your request again.` ,
6068 } ,
6169 ] ,
6270 } ;
@@ -69,7 +77,7 @@ function createMcpServer() {
6977 content : [
7078 {
7179 type : 'text' as const ,
72- text : `Please sign in to Vapi by opening this URL :\n\n${ authUrl } \n\nAfter signing in, try your request again.` ,
80+ text : `Please sign in to Vapi:\n\n${ authUrl } \n\nAfter signing in, try your request again.` ,
7381 } ,
7482 ] ,
7583 } ;
@@ -87,40 +95,19 @@ function createMcpServer() {
8795 }
8896 ) ;
8997
90- // Register status tool
98+ // Register logout tool
9199 mcpServer . tool (
92- 'vapi_auth_status ' ,
93- 'Check Vapi authentication status ' ,
100+ 'vapi_logout ' ,
101+ 'Log out of Vapi and clear stored credentials. Use this if your auth token is stale or you want to switch accounts. ' ,
94102 { } ,
95103 async ( ) => {
96- if ( hasValidToken ( ) ) {
97- return {
98- content : [
99- {
100- type : 'text' as const ,
101- text : 'Authenticated with Vapi and ready to use.' ,
102- } ,
103- ] ,
104- } ;
105- }
106-
107- if ( isAuthInProgress ( ) ) {
108- const url = getAuthUrl ( ) ;
109- return {
110- content : [
111- {
112- type : 'text' as const ,
113- text : `Authentication in progress. Please complete sign-in at:\n\n${ url } ` ,
114- } ,
115- ] ,
116- } ;
117- }
118-
104+ clearConfig ( ) ;
105+ vapiClient = null ;
119106 return {
120107 content : [
121108 {
122109 type : 'text' as const ,
123- text : 'Not authenticated . Use the vapi_login tool to sign in.' ,
110+ text : 'Logged out of Vapi . Use vapi_login to sign in again .' ,
124111 } ,
125112 ] ,
126113 } ;
0 commit comments