@@ -18,6 +18,8 @@ import {
1818 strings ,
1919 url ,
2020} from '@angular-devkit/schematics' ;
21+ import { parse } from 'jsonc-parser' ;
22+ import { JSONFile } from '../utility/json-file' ;
2123import { FileConfigurationHandlerOptions } from './types' ;
2224
2325const TOML_MCP_SERVERS_PROP = '[mcp_servers.angular-cli]' ;
@@ -26,7 +28,7 @@ const TOML_MCP_SERVERS_PROP = '[mcp_servers.angular-cli]';
2628 * Create or update a JSON MCP configuration file to include the Angular MCP server.
2729 */
2830export function addJsonMcpConfig (
29- { tree, context , fileInfo, tool } : FileConfigurationHandlerOptions ,
31+ { tree, fileInfo } : FileConfigurationHandlerOptions ,
3032 mcpServersProperty : string ,
3133) : Rule {
3234 const { name, directory } = fileInfo ;
@@ -45,43 +47,18 @@ export function addJsonMcpConfig(
4547 return file ;
4648 }
4749
48- const existingFileBuffer = tree . read ( file . path ) ;
49-
5050 // If we have an existing file, update the server property with
5151 // Angular MCP server configuration.
52- if ( existingFileBuffer ) {
53- // The JSON config file should be record-like.
54- let existing : Record < string , unknown > ;
55- try {
56- existing = JSON . parse ( existingFileBuffer . toString ( ) ) ;
57- } catch {
58- const path = `${ directory } /${ name } ` ;
59- const toolName = strings . classify ( tool ) ;
60- context . logger . warn (
61- `Skipping Angular MCP server configuration for '${ toolName } '.\n` +
62- `Unable to modify '${ path } '. ` +
63- 'Make sure that the file has a valid JSON syntax.\n' ,
64- ) ;
52+ const existingConfig = new JSONFile ( tree , file . path ) ;
53+ const existingMcpServers = existingConfig . get ( [ mcpServersProperty ] ) ?? { } ;
54+ const templateServersProp = parse ( file . content . toString ( ) ) [ mcpServersProperty ] ;
6555
66- return null ;
67- }
68- const existingServersProp = existing [ mcpServersProperty ] ;
69- const templateServersProp = JSON . parse ( file . content . toString ( ) ) [ mcpServersProperty ] ;
70-
71- // Note: If the Angular MCP server config already exists, we'll overwrite it.
72- existing [ mcpServersProperty ] = existingServersProp
73- ? {
74- ...existingServersProp ,
75- ...templateServersProp ,
76- }
77- : templateServersProp ;
78-
79- tree . overwrite ( file . path , JSON . stringify ( existing , null , 2 ) ) ;
56+ existingConfig . modify ( [ mcpServersProperty ] , {
57+ ...existingMcpServers ,
58+ ...templateServersProp ,
59+ } ) ;
8060
81- return null ;
82- }
83-
84- return file ;
61+ return null ;
8562 } ) ,
8663 ] ) ,
8764 ) ;
0 commit comments