Skip to content

Commit 28e320b

Browse files
authored
Handle empty client config files (#1095)
If the config file for an MCP server was empty during an insert or delete, thv would error out. Change the behaviour to: 1. When inserting, check if the file is empty, and set its contents to an empty JSON object. 2. When removing, check if the file is empty and do nothing - if the file is empty, then there's no point in changing it. Fixes: #1094
1 parent 1fc3e31 commit 28e320b

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pkg/client/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ func validateConfigFileFormat(cf *ConfigFile) error {
411411
return fmt.Errorf("failed to read file %s: %w", cf.Path, err)
412412
}
413413

414+
if len(data) == 0 {
415+
data = []byte("{}") // Default to an empty JSON object if the file is empty
416+
}
417+
414418
// Default to JSON
415419
// we don't care about the contents of the file, we just want to validate that it's valid JSON
416420
_, err = hujson.Parse(data)

pkg/client/config_editor.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func (jcu *JSONConfigUpdater) Upsert(serverName string, data MCPServer) error {
5858
logger.Errorf("Failed to read file: %v", err)
5959
}
6060

61+
if len(content) == 0 {
62+
// If the file is empty, we need to initialize it with an empty JSON object
63+
content = []byte("{}")
64+
}
65+
6166
content = ensurePathExists(content, jcu.MCPServersPathPrefix)
6267

6368
v, _ := hujson.Parse(content)
@@ -112,6 +117,11 @@ func (jcu *JSONConfigUpdater) Remove(serverName string) error {
112117
logger.Errorf("Failed to read file: %v", err)
113118
}
114119

120+
if len(content) == 0 {
121+
// If the file is empty, there is nothing to remove.
122+
return nil
123+
}
124+
115125
v, _ := hujson.Parse(content)
116126

117127
patch := fmt.Sprintf(`[{ "op": "remove", "path": "%s/%s" } ]`, jcu.MCPServersPathPrefix, serverName)

0 commit comments

Comments
 (0)