@@ -12,27 +12,12 @@ export * from './types'
12
12
13
13
export function ViteMcp ( options : ViteMcpOptions = { } ) : Plugin {
14
14
const {
15
- updateCursorMcpJson = true ,
16
- updateVSCodeMcpJson = true ,
17
- updateWindsurfMcpJson = true ,
18
15
printUrl = true ,
19
16
mcpServer = ( vite : ViteDevServer ) => import ( './server' ) . then ( m => m . createMcpServerDefault ( options , vite ) ) ,
20
17
} = options
21
18
22
19
const mcpRoute = options . mcpRouteRoot ?? options . mcpPath ?? '/__mcp'
23
20
24
- const cursorMcpOptions = typeof updateCursorMcpJson == 'boolean'
25
- ? { enabled : updateCursorMcpJson }
26
- : updateCursorMcpJson
27
-
28
- const vscodeMcpOptions = typeof updateVSCodeMcpJson == 'boolean'
29
- ? { enabled : updateVSCodeMcpJson }
30
- : updateVSCodeMcpJson
31
-
32
- const windsurfMcpOptions = typeof updateWindsurfMcpJson === 'boolean'
33
- ? { enabled : updateWindsurfMcpJson }
34
- : updateWindsurfMcpJson
35
-
36
21
return {
37
22
name : 'vite-plugin-mcp' ,
38
23
async configureServer ( vite ) {
@@ -46,50 +31,7 @@ export function ViteMcp(options: ViteMcpOptions = {}): Plugin {
46
31
const protocol = vite . config . server . https ? 'https' : 'http'
47
32
const sseUrl = `${ protocol } ://${ options . host || 'localhost' } :${ options . port || port } ${ mcpRoute } /sse`
48
33
49
- if ( cursorMcpOptions . enabled ) {
50
- if ( existsSync ( join ( root , '.cursor' ) ) ) {
51
- const mcp = existsSync ( join ( root , '.cursor/mcp.json' ) )
52
- ? JSON . parse ( await fs . readFile ( join ( root , '.cursor/mcp.json' ) , 'utf-8' ) || '{}' )
53
- : { }
54
- mcp . mcpServers ||= { }
55
- mcp . mcpServers [ cursorMcpOptions . serverName || 'vite' ] = { url : sseUrl }
56
- await fs . writeFile ( join ( root , '.cursor/mcp.json' ) , `${ JSON . stringify ( mcp , null , 2 ) } \n` )
57
- }
58
- }
59
-
60
- if ( vscodeMcpOptions . enabled ) {
61
- const vscodeConfig = join ( root , '.vscode/settings.json' )
62
- if ( existsSync ( vscodeConfig ) ) {
63
- const mcp = existsSync ( join ( root , '.vscode/mcp.json' ) )
64
- ? JSON . parse ( await fs . readFile ( join ( root , '.vscode/mcp.json' ) , 'utf-8' ) || '{}' )
65
- : { }
66
- mcp . servers ||= { }
67
- mcp . servers [ vscodeMcpOptions . serverName || 'vite' ] = {
68
- type : 'sse' ,
69
- url : sseUrl ,
70
- }
71
- await fs . writeFile ( join ( root , '.vscode/mcp.json' ) , `${ JSON . stringify ( mcp , null , 2 ) } \n` )
72
- }
73
- }
74
-
75
- if ( windsurfMcpOptions . enabled ) {
76
- const windsurfDir = join ( homedir ( ) , '.codeium' , 'windsurf' )
77
- const windsurfConfigPath = join ( windsurfDir , 'mcp_config.json' )
78
- try {
79
- if ( ! existsSync ( windsurfDir ) ) {
80
- await fs . mkdir ( windsurfDir , { recursive : true } )
81
- }
82
- const config = existsSync ( windsurfConfigPath )
83
- ? JSON . parse ( await fs . readFile ( windsurfConfigPath , 'utf-8' ) . catch ( ( ) => '{}' ) || '{}' )
84
- : { }
85
- config . mcpServers ||= { }
86
- config . mcpServers [ windsurfMcpOptions . serverName || 'vite' ] = { url : sseUrl }
87
- await fs . writeFile ( windsurfConfigPath , `${ JSON . stringify ( config , null , 2 ) } \n` )
88
- }
89
- catch ( e ) {
90
- console . error ( `${ c . red . bold ( ' ➜ MCP (Windsurf): ' ) } Failed to update ${ windsurfConfigPath } ` , e )
91
- }
92
- }
34
+ await updateConfigs ( root , sseUrl , options )
93
35
94
36
if ( printUrl ) {
95
37
setTimeout ( ( ) => {
@@ -100,3 +42,66 @@ export function ViteMcp(options: ViteMcpOptions = {}): Plugin {
100
42
} ,
101
43
}
102
44
}
45
+
46
+ async function updateConfigs ( root : string , sseUrl : string , options : ViteMcpOptions ) : Promise < void > {
47
+ const {
48
+ updateConfig = 'auto' ,
49
+ updateConfigServerName = 'vite' ,
50
+ } = options
51
+
52
+ if ( updateConfig === false )
53
+ return
54
+
55
+ const configs = updateConfig === 'auto'
56
+ ? [
57
+ existsSync ( join ( root , '.cursor' ) ) ? 'cursor' as const : null ,
58
+ existsSync ( join ( root , '.vscode' ) ) ? 'vscode' as const : null ,
59
+ existsSync ( join ( homedir ( ) , '.codeium' , 'windsurf' ) ) ? 'windsurf' as const : null ,
60
+ ] . filter ( x => x !== null )
61
+ : Array . isArray ( updateConfig )
62
+ ? updateConfig
63
+ : [ ]
64
+
65
+ // Cursor
66
+ if ( configs . includes ( 'cursor' ) ) {
67
+ await fs . mkdir ( join ( root , '.cursor' ) , { recursive : true } )
68
+ const mcp = existsSync ( join ( root , '.cursor/mcp.json' ) )
69
+ ? JSON . parse ( await fs . readFile ( join ( root , '.cursor/mcp.json' ) , 'utf-8' ) || '{}' )
70
+ : { }
71
+ mcp . mcpServers ||= { }
72
+ mcp . mcpServers [ updateConfigServerName || 'vite' ] = { url : sseUrl }
73
+ await fs . writeFile ( join ( root , '.cursor/mcp.json' ) , `${ JSON . stringify ( mcp , null , 2 ) } \n` )
74
+ }
75
+
76
+ // VSCode
77
+ if ( configs . includes ( 'vscode' ) ) {
78
+ await fs . mkdir ( join ( root , '.vscode' ) , { recursive : true } )
79
+ const mcp = existsSync ( join ( root , '.vscode/mcp.json' ) )
80
+ ? JSON . parse ( await fs . readFile ( join ( root , '.vscode/mcp.json' ) , 'utf-8' ) || '{}' )
81
+ : { }
82
+ mcp . servers ||= { }
83
+ mcp . servers [ updateConfigServerName || 'vite' ] = {
84
+ type : 'sse' ,
85
+ url : sseUrl ,
86
+ }
87
+ await fs . writeFile ( join ( root , '.vscode/mcp.json' ) , `${ JSON . stringify ( mcp , null , 2 ) } \n` )
88
+ }
89
+
90
+ // Windsurf
91
+ if ( configs . includes ( 'windsurf' ) ) {
92
+ const windsurfDir = join ( homedir ( ) , '.codeium' , 'windsurf' )
93
+ const windsurfConfigPath = join ( windsurfDir , 'mcp_config.json' )
94
+ try {
95
+ await fs . mkdir ( windsurfDir , { recursive : true } )
96
+ const config = existsSync ( windsurfConfigPath )
97
+ ? JSON . parse ( await fs . readFile ( windsurfConfigPath , 'utf-8' ) . catch ( ( ) => '{}' ) || '{}' )
98
+ : { }
99
+ config . mcpServers ||= { }
100
+ config . mcpServers [ updateConfigServerName || 'vite' ] = { url : sseUrl }
101
+ await fs . writeFile ( windsurfConfigPath , `${ JSON . stringify ( config , null , 2 ) } \n` )
102
+ }
103
+ catch ( e ) {
104
+ console . error ( `${ c . red . bold ( ' ➜ MCP (Windsurf): ' ) } Failed to update ${ windsurfConfigPath } ` , e )
105
+ }
106
+ }
107
+ }
0 commit comments