@@ -240,45 +240,64 @@ function copilotHookDocument(runtimeFile) {
240240 }
241241}
242242
243- function buildMcpServers ( runtimeRoot ) {
243+ /**
244+ * Map global host id → memory/profile agent identity (VALID_AGENTS).
245+ * Claude host root is `.claude` but agent id is `claude-code`.
246+ */
247+ function hostToRuntimeAgent ( host ) {
248+ const normalized = String ( host || '' ) . trim ( ) . toLowerCase ( )
249+ if ( normalized === 'claude' ) return 'claude-code'
250+ if ( normalized === 'copilot' || normalized === 'codex' || normalized === 'grok' ) return normalized
251+ // gemini / unknown: leave unset so detectRuntimeAgent can still apply
252+ return ''
253+ }
254+
255+ function buildMcpServers ( runtimeRoot , options = { } ) {
256+ const agent = hostToRuntimeAgent ( options . agent || options . host )
257+ const env = agent ? { DEVCODEX_AGENT : agent } : undefined
258+ const base = {
259+ type : 'stdio' ,
260+ command : 'node'
261+ }
244262 return {
245263 'devcodex-memory' : {
246- type : 'stdio' ,
247- command : 'node' ,
248- args : [ portable ( path . join ( runtimeRoot , 'mcp' , 'memory-server.js' ) ) , '.' ]
264+ ... base ,
265+ args : [ portable ( path . join ( runtimeRoot , 'mcp' , 'memory-server.js' ) ) , '.' ] ,
266+ ... ( env ? { env } : { } )
249267 } ,
250268 'devcodex-profile' : {
251- type : 'stdio' ,
252- command : 'node' ,
253- args : [ portable ( path . join ( runtimeRoot , 'mcp' , 'profile-server.js' ) ) , '.' ]
269+ ... base ,
270+ args : [ portable ( path . join ( runtimeRoot , 'mcp' , 'profile-server.js' ) ) , '.' ] ,
271+ ... ( env ? { env } : { } )
254272 }
255273 }
256274}
257275
258- function buildCopilotMcpServers ( runtimeRoot ) {
276+ function buildCopilotMcpServers ( runtimeRoot , options = { } ) {
259277 return Object . fromEntries (
260- Object . entries ( buildMcpServers ( runtimeRoot ) ) . map ( ( [ name , server ] ) => [
278+ Object . entries ( buildMcpServers ( runtimeRoot , options ) ) . map ( ( [ name , server ] ) => [
261279 name ,
262280 {
263281 type : 'local' ,
264282 command : server . command ,
265283 args : server . args ,
266- env : { } ,
284+ env : server . env && typeof server . env === 'object' ? { ... server . env } : { } ,
267285 tools : [ '*' ]
268286 }
269287 ] )
270288 )
271289}
272290
273291/** VS Code user mcp.json "servers" entries (stdio). */
274- function buildVscodeMcpServers ( runtimeRoot ) {
292+ function buildVscodeMcpServers ( runtimeRoot , options = { } ) {
275293 return Object . fromEntries (
276- Object . entries ( buildMcpServers ( runtimeRoot ) ) . map ( ( [ name , server ] ) => [
294+ Object . entries ( buildMcpServers ( runtimeRoot , { agent : options . agent || options . host || 'copilot' } ) ) . map ( ( [ name , server ] ) => [
277295 name ,
278296 {
279297 type : 'stdio' ,
280298 command : server . command ,
281- args : server . args
299+ args : server . args ,
300+ ...( server . env ? { env : server . env } : { } )
282301 }
283302 ] )
284303 )
@@ -287,7 +306,7 @@ function buildVscodeMcpServers (runtimeRoot) {
287306/**
288307 * Merge DevCodex servers into VS Code User mcp.json without wiping inputs / other servers.
289308 */
290- function mergeVscodeUserMcpContent ( existingText , runtimeRoot ) {
309+ function mergeVscodeUserMcpContent ( existingText , runtimeRoot , options = { } ) {
291310 let doc = { }
292311 try {
293312 doc = parseJsonObject ( existingText , 'VS Code user mcp.json' )
@@ -297,7 +316,7 @@ function mergeVscodeUserMcpContent (existingText, runtimeRoot) {
297316 if ( ! doc . servers || typeof doc . servers !== 'object' || Array . isArray ( doc . servers ) ) {
298317 doc . servers = { }
299318 }
300- const managed = buildVscodeMcpServers ( runtimeRoot )
319+ const managed = buildVscodeMcpServers ( runtimeRoot , options )
301320 for ( const [ name , server ] of Object . entries ( managed ) ) {
302321 doc . servers [ name ] = server
303322 }
@@ -332,17 +351,21 @@ function codexMcpTableNames () {
332351}
333352
334353function codexTomlBlock ( target ) {
335- const servers = buildMcpServers ( target . runtimeRoot )
354+ const servers = buildMcpServers ( target . runtimeRoot , { host : target . host || 'codex' } )
355+ const agent = hostToRuntimeAgent ( target . host || 'codex' )
356+ const envLine = agent ? `env = { DEVCODEX_AGENT = ${ quoteToml ( agent ) } }` : null
336357 const lines = [
337358 '# Managed by npm install/update -g devcodex.' ,
338359 '[mcp_servers.devcodex-memory]' ,
339360 'command = "node"' ,
340361 `args = [${ servers [ 'devcodex-memory' ] . args . map ( quoteToml ) . join ( ', ' ) } ]` ,
362+ ...( envLine ? [ envLine ] : [ ] ) ,
341363 'startup_timeout_sec = 30' ,
342364 '' ,
343365 '[mcp_servers.devcodex-profile]' ,
344366 'command = "node"' ,
345367 `args = [${ servers [ 'devcodex-profile' ] . args . map ( quoteToml ) . join ( ', ' ) } ]` ,
368+ ...( envLine ? [ envLine ] : [ ] ) ,
346369 'startup_timeout_sec = 30'
347370 ]
348371 for ( const [ server , tools ] of Object . entries ( CODEX_MCP_APPROVED_TOOLS ) ) {
@@ -393,7 +416,7 @@ function addCopilotPlan(operations, target, packageRoot, fsImpl) {
393416 'json' ,
394417 `${ JSON . stringify ( hooks , null , 2 ) } \n`
395418 )
396- const mcp = { mcpServers : buildCopilotMcpServers ( target . runtimeRoot ) }
419+ const mcp = { mcpServers : buildCopilotMcpServers ( target . runtimeRoot , { host : 'copilot' } ) }
397420 addFileOperation (
398421 operations ,
399422 target . host ,
@@ -416,7 +439,7 @@ function addCopilotPlan(operations, target, packageRoot, fsImpl) {
416439 if ( seenVscode . has ( key ) ) continue
417440 seenVscode . add ( key )
418441 const existing = readText ( vscodeMcpPath , fsImpl )
419- const merged = mergeVscodeUserMcpContent ( existing , target . runtimeRoot )
442+ const merged = mergeVscodeUserMcpContent ( existing , target . runtimeRoot , { host : 'copilot' } )
420443 addFileOperation (
421444 operations ,
422445 target . host ,
@@ -464,11 +487,11 @@ function addClaudePlan(operations, target, packageRoot, fsImpl) {
464487 target . files . mcp ,
465488 mergeHostJsonContent (
466489 readText ( target . files . mcp , fsImpl ) ,
467- { mcpServers : buildMcpServers ( target . runtimeRoot ) } ,
490+ { mcpServers : buildMcpServers ( target . runtimeRoot , { host : 'claude' } ) } ,
468491 'Claude user MCP'
469492 ) ,
470493 'json' ,
471- `${ JSON . stringify ( { mcpServers : buildMcpServers ( target . runtimeRoot ) } , null , 2 ) } \n`
494+ `${ JSON . stringify ( { mcpServers : buildMcpServers ( target . runtimeRoot , { host : 'claude' } ) } , null , 2 ) } \n`
472495 )
473496}
474497
@@ -531,7 +554,7 @@ function addGeminiPlan(operations, target, packageRoot, fsImpl) {
531554 managedInstruction ( '' , source , 'gemini' )
532555 )
533556 const settings = transformedHookTemplate ( packageRoot , target , path . join ( 'gemini' , 'settings.json' ) , 'gemini' , fsImpl )
534- settings . mcpServers = buildMcpServers ( target . runtimeRoot )
557+ settings . mcpServers = buildMcpServers ( target . runtimeRoot , { host : 'gemini' } )
535558 addFileOperation (
536559 operations ,
537560 target . host ,
@@ -584,7 +607,7 @@ function addGrokPlan(operations, target, packageRoot, fsImpl) {
584607 operations ,
585608 target . host ,
586609 path . join ( target . files . plugin , '.mcp.json' ) ,
587- `${ JSON . stringify ( { mcpServers : buildMcpServers ( target . runtimeRoot ) } , null , 2 ) } \n` ,
610+ `${ JSON . stringify ( { mcpServers : buildMcpServers ( target . runtimeRoot , { host : 'grok' } ) } , null , 2 ) } \n` ,
588611 'json'
589612 )
590613
@@ -1628,6 +1651,7 @@ module.exports = {
16281651 buildCopilotMcpServers,
16291652 buildMcpServers,
16301653 buildVscodeMcpServers,
1654+ hostToRuntimeAgent,
16311655 mergeVscodeUserMcpContent,
16321656 copilotHookDocument,
16331657 digestPlan,
0 commit comments