setConfig
Update configuration files by setting values at specific key paths to manage settings in JSON, YAML, TOML, or Markdown formats.
Instructions
Update a configuration file by setting a value at a specific key path
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | Path to the configuration file | |
| key | Yes | Dot-separated key path to update (e.g., 'database.host') | |
| value | Yes | JSON-encoded value to set |
Implementation Reference
- src/index.ts:88-108 (handler)The main handler function for the setConfig tool. It parses the value, reads the config file, updates the nested object at the specified key path, writes back the file, and returns success or error message.async ({ file, key, value }: { file: string; key: string; value: string }) => { try { const json = JSON.parse(value); const data = await readConfigFile(file); const path = key.split("."); let curr: any = data; for (let i = 0; i < path.length - 1; i++) { const p = path[i]; if (typeof curr[p] !== "object" || curr[p] === null) curr[p] = {}; curr = curr[p]; } curr[path[path.length - 1]] = json; await writeConfigFile(file, data); return { content: [{ type: "text", text: `Successfully updated ${key} in ${file}` }] }; } catch (error) { return { content: [{ type: "text", text: `Error updating config: ${error}` }], isError: true, }; } }
- src/index.ts:83-87 (schema)Input schema using Zod for validating parameters: file path, key path, and JSON-encoded value.{ file: z.string().describe("Path to the configuration file"), key: z.string().describe("Dot-separated key path to update (e.g., 'database.host')"), value: z.string().describe("JSON-encoded value to set"), },
- src/index.ts:80-109 (registration)Registration of the setConfig tool using mcp.tool(), including name, description, schema, and handler.mcp.tool( "setConfig", "Update a configuration file by setting a value at a specific key path", { file: z.string().describe("Path to the configuration file"), key: z.string().describe("Dot-separated key path to update (e.g., 'database.host')"), value: z.string().describe("JSON-encoded value to set"), }, async ({ file, key, value }: { file: string; key: string; value: string }) => { try { const json = JSON.parse(value); const data = await readConfigFile(file); const path = key.split("."); let curr: any = data; for (let i = 0; i < path.length - 1; i++) { const p = path[i]; if (typeof curr[p] !== "object" || curr[p] === null) curr[p] = {}; curr = curr[p]; } curr[path[path.length - 1]] = json; await writeConfigFile(file, data); return { content: [{ type: "text", text: `Successfully updated ${key} in ${file}` }] }; } catch (error) { return { content: [{ type: "text", text: `Error updating config: ${error}` }], isError: true, }; } } );
- src/lib/files.ts:19-34 (helper)Helper function to read configuration files in various formats (JSON, JSONC, YAML, TOML). Used in setConfig to load the existing config.export async function readConfigFile(file: string): Promise<any> { const fmt = detectFormat(file); const content = await fs.readFile(file, "utf8"); switch (fmt) { case "yaml": case "yml": return parseYaml(content); case "toml": return TOML.parse(content); case "jsonc": return parseCommentJson(content); case "json": default: return JSON.parse(content); } }
- src/lib/files.ts:36-55 (helper)Helper function to write configuration files in various formats. Used in setConfig to save the updated config.export async function writeConfigFile(file: string, data: any): Promise<void> { const fmt = detectFormat(file); let content: string; switch (fmt) { case "yaml": case "yml": content = dumpYaml(data); break; case "toml": content = TOML.stringify(data as any); break; case "jsonc": content = stringifyCommentJson(data, null, 2); break; case "json": default: content = JSON.stringify(data, null, 2); } await fs.writeFile(file, content, "utf8"); }