#!/usr/bin/env node /** * mcp-config-tools — MCP server providing configuration file tools for AI agents. * * Tools: * 1. json_config — Read, validate, merge, and diff JSON/JSONC config files * 2. yaml_config — Read, validate, convert YAML files; schema validation * 3. toml_parser — Parse TOML files, extract sections, list dependencies * 4. ini_parser — Parse INI/properties files, convert to/from JSON * 5. config_diff — Compare two config files in any format */ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; import { parseJsonc, validateJson, deepMerge, findDifferences } from "./tools/json-config.js"; import { parseYaml, toYaml, yamlToJson, jsonToYaml, validateYaml, validateSchema, } from "./tools/yaml-config.js"; import { parseToml, extractSection, listDependencies, compareVersions } from "./tools/toml-parser.js"; import { parseIni, getSections, getValue, iniToJson, jsonToIni } from "./tools/ini-parser.js"; import { diffConfigs, formatDiff } from "./tools/config-diff.js"; import type { ConfigFormat } from "./tools/config-diff.js"; const server = new McpServer({ name: "mcp-config-tools", version: "1.0.0", }); // --------------------------------------------------------------------------- // Tool 1: json_config // --------------------------------------------------------------------------- server.tool( "json_config", "Read, validate, deep merge, and diff JSON/JSONC configuration files. Supports comments and trailing commas.", { action: z.enum(["parse", "validate", "merge", "diff"]).describe( "Action to perform: parse (parse JSONC), validate (check validity), merge (deep merge two configs), diff (find differences)" ), content: z.string().describe("Primary JSON/JSONC content"), content2: z.string().optional().describe("Second JSON/JSONC content (for merge/diff actions)"), }, async ({ action, content, content2 }) => { try { switch (action) { case "parse": { const data = parseJsonc(content); return { content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }] }; } case "validate": { const result = validateJson(content); return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }], }; } case "merge": { if (!content2) { return { content: [{ type: "text" as const, text: "Error: 'content2' is required for merge action." }] }; } const base = parseJsonc(content); const overlay = parseJsonc(content2); const merged = deepMerge(base, overlay); return { content: [{ type: "text" as const, text: JSON.stringify(merged, null, 2) }] }; } case "diff": { if (!content2) { return { content: [{ type: "text" as const, text: "Error: 'content2' is required for diff action." }] }; } const a = parseJsonc(content); const b = parseJsonc(content2); const diffs = findDifferences(a, b); return { content: [{ type: "text" as const, text: JSON.stringify(diffs, null, 2) }] }; } } } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err); return { content: [{ type: "text" as const, text: `Error: ${msg}` }] }; } } ); // --------------------------------------------------------------------------- // Tool 2: yaml_config // --------------------------------------------------------------------------- server.tool( "yaml_config", "Read, validate, and convert YAML configuration files. Validate against docker-compose, GitHub Actions, or Kubernetes schemas.", { action: z.enum(["parse", "validate", "to_json", "from_json", "validate_schema"]).describe( "Action: parse, validate, to_json (YAML->JSON), from_json (JSON->YAML), validate_schema" ), content: z.string().describe("YAML or JSON content to process"), schema_type: z .enum(["docker-compose", "github-actions", "kubernetes"]) .optional() .describe("Schema type for validate_schema action"), }, async ({ action, content, schema_type }) => { try { switch (action) { case "parse": { const data = parseYaml(content); return { content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }] }; } case "validate": { const result = validateYaml(content); return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] }; } case "to_json": { const json = yamlToJson(content); return { content: [{ type: "text" as const, text: json }] }; } case "from_json": { const yaml = jsonToYaml(content); return { content: [{ type: "text" as const, text: yaml }] }; } case "validate_schema": { if (!schema_type) { return { content: [{ type: "text" as const, text: "Error: 'schema_type' is required for validate_schema action." }], }; } const data = parseYaml(content); const result = validateSchema(data, schema_type); return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] }; } } } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err); return { content: [{ type: "text" as const, text: `Error: ${msg}` }] }; } } ); // --------------------------------------------------------------------------- // Tool 3: toml_parser // --------------------------------------------------------------------------- server.tool( "toml_parser", "Parse TOML configuration files (Cargo.toml, pyproject.toml, etc.). Extract sections, list dependencies, compare versions.", { action: z.enum(["parse", "extract_section", "list_deps", "compare_versions"]).describe( "Action: parse, extract_section, list_deps, compare_versions" ), content: z.string().optional().describe("TOML content to parse (for parse/extract_section/list_deps)"), section: z.string().optional().describe("Dot-separated section path (for extract_section)"), version_a: z.string().optional().describe("First version string (for compare_versions)"), version_b: z.string().optional().describe("Second version string (for compare_versions)"), }, async ({ action, content, section, version_a, version_b }) => { try { switch (action) { case "parse": { if (!content) return { content: [{ type: "text" as const, text: "Error: 'content' is required." }] }; const data = parseToml(content); return { content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }] }; } case "extract_section": { if (!content) return { content: [{ type: "text" as const, text: "Error: 'content' is required." }] }; if (!section) return { content: [{ type: "text" as const, text: "Error: 'section' is required." }] }; const data = parseToml(content); const result = extractSection(data, section); if (result === undefined) { return { content: [{ type: "text" as const, text: `Section '${section}' not found.` }] }; } return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] }; } case "list_deps": { if (!content) return { content: [{ type: "text" as const, text: "Error: 'content' is required." }] }; const data = parseToml(content); const deps = listDependencies(data); return { content: [{ type: "text" as const, text: JSON.stringify(deps, null, 2) }] }; } case "compare_versions": { if (!version_a || !version_b) { return { content: [{ type: "text" as const, text: "Error: 'version_a' and 'version_b' are required." }], }; } const cmp = compareVersions(version_a, version_b); const label = cmp < 0 ? "older" : cmp > 0 ? "newer" : "equal"; return { content: [{ type: "text" as const, text: `${version_a} is ${label} compared to ${version_b} (result: ${cmp})` }], }; } } } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err); return { content: [{ type: "text" as const, text: `Error: ${msg}` }] }; } } ); // --------------------------------------------------------------------------- // Tool 4: ini_parser // --------------------------------------------------------------------------- server.tool( "ini_parser", "Parse INI and properties configuration files. Read sections and keys, convert between INI and JSON formats.", { action: z.enum(["parse", "get_sections", "get_value", "to_json", "from_json"]).describe( "Action: parse, get_sections, get_value, to_json (INI->JSON), from_json (JSON->INI)" ), content: z.string().describe("INI or JSON content"), section: z.string().optional().describe("Section name (for get_value)"), key: z.string().optional().describe("Key name (for get_value)"), }, async ({ action, content, section, key }) => { try { switch (action) { case "parse": { const doc = parseIni(content); return { content: [{ type: "text" as const, text: JSON.stringify(doc, null, 2) }] }; } case "get_sections": { const doc = parseIni(content); const sections = getSections(doc); return { content: [{ type: "text" as const, text: JSON.stringify(sections, null, 2) }] }; } case "get_value": { if (!key) return { content: [{ type: "text" as const, text: "Error: 'key' is required for get_value." }] }; const doc = parseIni(content); const val = getValue(doc, key, section); if (val === undefined) { return { content: [{ type: "text" as const, text: `Key '${key}' not found${section ? ` in section '${section}'` : ""}.` }] }; } return { content: [{ type: "text" as const, text: val }] }; } case "to_json": { const doc = parseIni(content); const json = iniToJson(doc); return { content: [{ type: "text" as const, text: JSON.stringify(json, null, 2) }] }; } case "from_json": { const data = JSON.parse(content); const ini = jsonToIni(data); return { content: [{ type: "text" as const, text: ini }] }; } } } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err); return { content: [{ type: "text" as const, text: `Error: ${msg}` }] }; } } ); // --------------------------------------------------------------------------- // Tool 5: config_diff // --------------------------------------------------------------------------- server.tool( "config_diff", "Compare two configuration files in any supported format (JSON, YAML, TOML, INI). Shows additions, removals, and changes with nested path information.", { content_a: z.string().describe("First config file content"), content_b: z.string().describe("Second config file content"), format_a: z .enum(["json", "yaml", "toml", "ini", "auto"]) .default("auto") .describe("Format of first file (default: auto-detect)"), format_b: z .enum(["json", "yaml", "toml", "ini", "auto"]) .default("auto") .describe("Format of second file (default: auto-detect)"), }, async ({ content_a, content_b, format_a, format_b }) => { try { const result = diffConfigs(content_a, content_b, format_a as ConfigFormat, format_b as ConfigFormat); const formatted = formatDiff(result); const output = { summary: { totalChanges: result.totalChanges, additions: result.additions, removals: result.removals, modifications: result.modifications, }, formatted, entries: result.entries, }; return { content: [{ type: "text" as const, text: JSON.stringify(output, null, 2) }] }; } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err); return { content: [{ type: "text" as const, text: `Error: ${msg}` }] }; } } ); // --------------------------------------------------------------------------- // Start server // --------------------------------------------------------------------------- async function main(): Promise { const transport = new StdioServerTransport(); await server.connect(transport); console.error("mcp-config-tools server running on stdio"); } main().catch((err) => { console.error("Fatal error:", err); process.exit(1); });