add_server_configuration
Add or update server configurations in client configuration files for AI assistants like Cline, Roo Code, WindSurf, and Claude, using JSON format with optional override settings.
Instructions
Add or update a server configuration in a client configuration
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| allow_override | No | Whether to allow overriding an existing server configuration with the same name (default: false) | |
| client | Yes | Client name (cline, roo_code, windsurf, claude) | |
| json_config | Yes | Server configuration in JSON format | |
| server_name | Yes | Name of the server to add or update |
Implementation Reference
- src/index.ts:343-401 (handler)The main handler logic for the 'add_server_configuration' tool. It validates the input parameters (client, server_name, json_config, allow_override), reads the client's configuration file (creating if not exists), checks for existing server and override permission, adds or updates the server config in mcpServers object, writes back to file, and returns success message.case 'add_server_configuration': { const client = validateClient(args.client); const serverName = args.server_name; const jsonConfig = args.json_config; const allowOverride = args.allow_override === true; // Default to false if not provided if (typeof serverName !== 'string') { throw new McpError(ErrorCode.InvalidParams, 'server_name must be a string'); } if (typeof jsonConfig !== 'object' || jsonConfig === null) { throw new McpError(ErrorCode.InvalidParams, 'json_config must be a valid JSON object'); } const configPath = getConfigPath(client); let config; try { config = await readConfigFile(configPath); } catch (error) { if (error instanceof McpError && error.code === ErrorCode.InternalError && error.message.includes('not found')) { // Create a new configuration if it doesn't exist config = { mcpServers: {} }; } else { throw error; } } // Ensure mcpServers object exists if (!config.mcpServers) { config.mcpServers = {}; } // Check if server with the same name already exists const serverExists = config.mcpServers.hasOwnProperty(serverName); if (serverExists && !allowOverride) { throw new McpError( ErrorCode.InvalidParams, `Server '${serverName}' already exists in ${client} configuration. Set allow_override to true to update it.` ); } // Add or update the server configuration config.mcpServers[serverName] = jsonConfig; // Write the updated configuration await writeConfigFile(configPath, config); const action = serverExists ? 'updated' : 'added'; return { content: [ { type: 'text', text: `Server '${serverName}' configuration ${action} in ${client} configuration`, }, ], }; }
- src/index.ts:190-212 (schema)Input schema definition for the 'add_server_configuration' tool, specifying properties and requirements for client, server_name, json_config, and optional allow_override.inputSchema: { type: 'object', properties: { client: { type: 'string', description: 'Client name (cline, roo_code, windsurf, claude)', }, server_name: { type: 'string', description: 'Name of the server to add or update', }, json_config: { type: 'object', description: 'Server configuration in JSON format', }, allow_override: { type: 'boolean', description: 'Whether to allow overriding an existing server configuration with the same name (default: false)', default: false, }, }, required: ['client', 'server_name', 'json_config'], },
- src/index.ts:187-213 (registration)Tool registration in the ListTools response, defining name, description, and inputSchema for 'add_server_configuration'.{ name: 'add_server_configuration', description: 'Add or update a server configuration in a client configuration', inputSchema: { type: 'object', properties: { client: { type: 'string', description: 'Client name (cline, roo_code, windsurf, claude)', }, server_name: { type: 'string', description: 'Name of the server to add or update', }, json_config: { type: 'object', description: 'Server configuration in JSON format', }, allow_override: { type: 'boolean', description: 'Whether to allow overriding an existing server configuration with the same name (default: false)', default: false, }, }, required: ['client', 'server_name', 'json_config'], }, },
- src/index.ts:84-96 (helper)Helper function used by the handler to write the updated configuration file atomically with directory creation.const writeConfigFile = async (configPath: string, config: any): Promise<void> => { try { // Ensure the directory exists const dirPath = path.dirname(configPath); await fs.mkdir(dirPath, { recursive: true }); // Write the file with pretty formatting await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf8'); } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); throw new McpError(ErrorCode.InternalError, `Error writing configuration file: ${errorMessage}`); } };
- src/index.ts:70-81 (helper)Helper function used by the handler to read the existing client configuration file, throwing specific errors if not found.const readConfigFile = async (configPath: string): Promise<any> => { try { const data = await fs.readFile(configPath, 'utf8'); return JSON.parse(data); } catch (error: unknown) { if (error instanceof Error && 'code' in error && error.code === 'ENOENT') { throw new McpError(ErrorCode.InternalError, `Configuration file not found: ${configPath}`); } const errorMessage = error instanceof Error ? error.message : String(error); throw new McpError(ErrorCode.InternalError, `Error reading configuration file: ${errorMessage}`); } };