@@ -10,40 +10,62 @@ import ejs from "ejs";
1010import os from "os" ;
1111
1212const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
13-
14- async function updateClaudeConfig ( directory : string , name : string ) {
15- let configDir ;
13+ function getClaudeConfigDir ( ) : string {
1614 switch ( os . platform ( ) ) {
1715 case "darwin" :
18- configDir = path . join (
16+ return path . join (
1917 os . homedir ( ) ,
2018 "Library" ,
2119 "Application Support" ,
2220 "Claude" ,
2321 ) ;
24- break ;
2522 case "win32" :
26- configDir = path . join ( process . env . APPDATA || "" , "Claude" ) ;
27- break ;
23+ if ( ! process . env . APPDATA ) {
24+ throw new Error ( "APPDATA environment variable is not set" ) ;
25+ }
26+ return path . join ( process . env . APPDATA , "Claude" ) ;
2827 default :
29- return ;
28+ throw new Error (
29+ `Unsupported operating system for Claude configuration: ${ os . platform ( ) } ` ,
30+ ) ;
3031 }
32+ }
3133
32- const configFile = path . join ( configDir , "claude_desktop_config.json" ) ;
33-
34+ async function updateClaudeConfig ( name : string , directory : string ) {
3435 try {
36+ const configFile = path . join (
37+ getClaudeConfigDir ( ) ,
38+ "claude_desktop_config.json" ,
39+ ) ;
40+
3541 const config = JSON . parse ( await fs . readFile ( configFile , "utf-8" ) ) ;
3642
3743 if ( ! config . mcpServers ) {
3844 config . mcpServers = { } ;
3945 }
4046
41- if ( ! config . mcpServers [ name ] ) {
42- config . mcpServers [ name ] = {
43- command : "node" ,
44- args : [ "index.js" ] ,
45- } ;
47+ if ( config . mcpServers [ name ] ) {
48+ const { replace } = await inquirer . prompt ( [
49+ {
50+ type : "confirm" ,
51+ name : "replace" ,
52+ message : `An MCP server named "${ name } " is already configured for Claude.app. Do you want to replace it?` ,
53+ default : false ,
54+ } ,
55+ ] ) ;
56+ if ( ! replace ) {
57+ console . log (
58+ chalk . yellow (
59+ `Skipped replacing Claude.app config for existing MCP server "${ name } "` ,
60+ ) ,
61+ ) ;
62+ return ;
63+ }
4664 }
65+ config . mcpServers [ name ] = {
66+ command : "node" ,
67+ args : [ path . resolve ( directory , "build" , "index.js" ) ] ,
68+ } ;
4769
4870 await fs . writeFile ( configFile , JSON . stringify ( config , null , 2 ) ) ;
4971 console . log (
@@ -134,7 +156,7 @@ async function createServer(directory: string, options: any = {}) {
134156 spinner . succeed ( chalk . green ( "MCP server created successfully!" ) ) ;
135157
136158 if ( answers . installForClaude ) {
137- await updateClaudeConfig ( directory , config . name ) ;
159+ await updateClaudeConfig ( config . name , directory ) ;
138160 }
139161
140162 // Print next steps
0 commit comments