@@ -24,6 +24,10 @@ let config: Record<string, { api_url: string, api_key: string }>;
2424try {
2525 const configContent = readFileSync ( CONFIG_PATH , 'utf-8' ) ;
2626 config = JSON . parse ( configContent ) ;
27+
28+ if ( Object . keys ( config ) . length === 0 ) {
29+ throw new Error ( 'Config file exists but is empty' ) ;
30+ }
2731} catch ( error ) {
2832 console . error ( 'Error reading config file:' , error ) ;
2933 config = { } ;
@@ -45,9 +49,44 @@ const server = new Server(
4549
4650// Helper function to get server config
4751function getServerConfig ( serverName : string ) : { API_URL: string , JWT : string } {
52+ if ( Object . keys ( config ) . length === 0 ) {
53+ const exampleConfig = {
54+ "myserver" : {
55+ "api_url" : "http://localhost:1337" ,
56+ "api_key" : "your-jwt-token-from-strapi-admin"
57+ }
58+ } ;
59+
60+ throw new Error (
61+ `No server configuration found!\n\n` +
62+ `Please create a configuration file at:\n` +
63+ `${ CONFIG_PATH } \n\n` +
64+ `Example configuration:\n` +
65+ `${ JSON . stringify ( exampleConfig , null , 2 ) } \n\n` +
66+ `Steps to set up:\n` +
67+ `1. Create the .mcp directory: mkdir -p ~/.mcp\n` +
68+ `2. Create the config file: touch ~/.mcp/strapi-mcp-server.config.json\n` +
69+ `3. Add your server configuration using the example above\n` +
70+ `4. Get your JWT token from Strapi Admin Panel > Settings > API Tokens\n` +
71+ `5. Make sure the file permissions are secure: chmod 600 ~/.mcp/strapi-mcp-server.config.json`
72+ ) ;
73+ }
74+
4875 const serverConfig = config [ serverName ] ;
4976 if ( ! serverConfig ) {
50- throw new Error ( `Server "${ serverName } " not found in config. Available servers: ${ Object . keys ( config ) . join ( ', ' ) } ` ) ;
77+ throw new Error (
78+ `Server "${ serverName } " not found in config.\n\n` +
79+ `Available servers: ${ Object . keys ( config ) . join ( ', ' ) } \n\n` +
80+ `To add a new server, edit:\n` +
81+ `${ CONFIG_PATH } \n\n` +
82+ `Example configuration:\n` +
83+ `{\n` +
84+ ` "${ serverName } ": {\n` +
85+ ` "api_url": "http://localhost:1337",\n` +
86+ ` "api_key": "your-jwt-token-from-strapi-admin"\n` +
87+ ` }\n` +
88+ `}`
89+ ) ;
5190 }
5291 return {
5392 API_URL : serverConfig . api_url ,
@@ -615,6 +654,38 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
615654
616655 try {
617656 if (name === "strapi_list_servers") {
657+ if (Object.keys(config).length === 0) {
658+ const exampleConfig = {
659+ "myserver": {
660+ "api_url": "http://localhost:1337",
661+ "api_key": "your-jwt-token-from-strapi-admin"
662+ }
663+ };
664+
665+ return {
666+ content: [
667+ {
668+ type: "text",
669+ text: JSON.stringify({
670+ error: "No servers configured",
671+ help: {
672+ message: "No server configuration found. Please create a configuration file.",
673+ config_path: CONFIG_PATH,
674+ example_config: exampleConfig,
675+ setup_steps: [
676+ "Create the .mcp directory: mkdir -p ~/.mcp",
677+ "Create the config file: touch ~/.mcp/strapi-mcp-server.config.json",
678+ "Add your server configuration using the example above",
679+ "Get your JWT token from Strapi Admin Panel > Settings > API Tokens",
680+ "Make sure the file permissions are secure: chmod 600 ~/.mcp/strapi-mcp-server.config.json"
681+ ]
682+ }
683+ }, null, 2),
684+ },
685+ ],
686+ };
687+ }
688+
618689 const servers = Object.keys(config).map(serverName => ({
619690 name: serverName,
620691 api_url: config[serverName].api_url
@@ -624,7 +695,11 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
624695 content: [
625696 {
626697 type: "text",
627- text: JSON.stringify({ servers }, null, 2),
698+ text: JSON.stringify({
699+ servers,
700+ config_path: CONFIG_PATH,
701+ help: "To add more servers, edit the configuration file at the path shown above."
702+ }, null, 2),
628703 },
629704 ],
630705 };
0 commit comments