@@ -18,6 +18,7 @@ import {
1818 strings ,
1919 url ,
2020} from '@angular-devkit/schematics' ;
21+ import { JSONFile } from '../utility/json-file' ;
2122import { FileConfigurationHandlerOptions } from './types' ;
2223
2324const TOML_MCP_SERVERS_PROP = '[mcp_servers.angular-cli]' ;
@@ -26,7 +27,7 @@ const TOML_MCP_SERVERS_PROP = '[mcp_servers.angular-cli]';
2627 * Create or update a JSON MCP configuration file to include the Angular MCP server.
2728 */
2829export function addJsonMcpConfig (
29- { tree, context , fileInfo, tool } : FileConfigurationHandlerOptions ,
30+ { tree, fileInfo } : FileConfigurationHandlerOptions ,
3031 mcpServersProperty : string ,
3132) : Rule {
3233 const { name, directory } = fileInfo ;
@@ -45,43 +46,18 @@ export function addJsonMcpConfig(
4546 return file ;
4647 }
4748
48- const existingFileBuffer = tree . read ( file . path ) ;
49-
5049 // If we have an existing file, update the server property with
5150 // 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- ) ;
51+ const existingConfig = new JSONFile ( tree , file . path ) ;
52+ const existingMcpServers = existingConfig . get ( [ mcpServersProperty ] ) ?? { } ;
53+ const templateServersProp = JSON . parse ( file . content . toString ( ) ) [ mcpServersProperty ] ;
6554
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 ) ) ;
55+ existingConfig . modify ( [ mcpServersProperty ] , {
56+ ...existingMcpServers ,
57+ ...templateServersProp ,
58+ } ) ;
8059
81- return null ;
82- }
83-
84- return file ;
60+ return null ;
8561 } ) ,
8662 ] ) ,
8763 ) ;
0 commit comments