Skip to content

McpConfigWriter

Category: Config

Source: mcp_config_writer.dart

Mutates the mcp.servers.* block of config.yaml while preserving the user's comments, formatting, and key order.

Backed by package:yaml_edit, which performs surgical edits on the original YAML source. Every mutation writes the file atomically via tmp + rename, and re-parses the result through [parseMcpConfig] before committing — if the edit would produce an unloadable file, the original is restored and [McpConfigWriteError] is thrown.

Classes

McpConfigWriteError

Thrown when the writer refuses an operation (duplicate id, missing id, or post-mutation re-parse failure).

Constructor

dart
McpConfigWriteError(this.message)

Properties

PropertyTypeDescription
messageString

Methods

String toString()

McpConfigWriter

Constructor

dart
McpConfigWriter(this.configPath, {Environment? environment})

Properties

PropertyTypeDescription
configPathStringAbsolute path to the user's config.yaml.

Methods

bool hasServer(String id)

Returns true if mcp.servers.<id> exists in the on-disk YAML.

void addServer(McpServerSpec spec, {bool overwrite = false})

Writes a server entry under mcp.servers.<id>. Creates the mcp: and mcp.servers: blocks if missing. Throws if [spec.id] already exists and [overwrite] is false.

Every path is a scoped edit of the mcp.servers subtree only. We never re-render or replace the whole mcp: block, so sibling keys (e.g. mcp.tool_policy — a security deny-list) and user comments are preserved. See the C2 regression tests in mcp_config_writer_test.dart.

void removeServer(String id)

Removes mcp.servers.<id>. Throws if the id is not present.

void setEnabled(String id, bool enabled)

Sets mcp.servers.<id>.enabled. Throws if the id is not present.

`void updateAuth(
String id, {
required McpAuthSpec auth,
Uri? resourceMetadataUrl,
Uri? authorizationServer,

})`

Updates a server's auth, resource_metadata_url, and authorization_server keys atomically. Used after a successful OAuth flow to (a) write back auth: {kind: oauth} when previously none and (b) cache the discovered URLs to skip rediscovery next session.

Idempotent — re-running with the same arguments is a no-op modulo the file mtime.

Released under the MIT License.