@@ -16,8 +16,8 @@ import {
1616 callMcpTool ,
1717 createDevServer ,
1818 findAvailablePort ,
19- getAgentPort ,
2019 getDevConfig ,
20+ getDevPort ,
2121 getDevSupportedAgents ,
2222 getEndpointUrl ,
2323 invokeAgent ,
@@ -27,6 +27,7 @@ import {
2727 loadDevEnv ,
2828 loadProjectConfig ,
2929 onShutdownSignal ,
30+ requiresExactDevPort ,
3031} from '../../operations/dev' ;
3132import { OtelCollector , startOtelCollector } from '../../operations/dev/otel' ;
3233import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js' ;
@@ -265,7 +266,6 @@ export const registerDev = (program: Command) => {
265266 let invokePort = port ;
266267 let targetAgent = invokeProject ?. runtimes [ 0 ] ;
267268 if ( opts . runtime && invokeProject ) {
268- invokePort = getAgentPort ( invokeProject , opts . runtime , port , portExplicit ) ;
269269 targetAgent = invokeProject . runtimes . find ( a => a . name === opts . runtime ) ;
270270 } else if ( invokeProject && invokeProject . runtimes . length > 1 && ! opts . runtime ) {
271271 const names = invokeProject . runtimes . map ( a => a . name ) . join ( ', ' ) ;
@@ -275,13 +275,13 @@ export const registerDev = (program: Command) => {
275275 }
276276
277277 const protocol = targetAgent ?. protocol ?? 'HTTP' ;
278+ if ( targetAgent && invokeProject ) {
279+ invokePort = getDevPort ( invokeProject , targetAgent . name , protocol , port , portExplicit ) ;
280+ }
278281 recorder . set ( {
279282 agent_protocol : standardize ( AgentProtocol , protocol . toLowerCase ( ) ) ,
280283 } ) ;
281284
282- if ( protocol === 'A2A' ) invokePort = 9000 ;
283- else if ( protocol === 'MCP' ) invokePort = 8000 ;
284-
285285 if ( protocol === 'MCP' ) {
286286 await handleMcpInvoke ( invokePort , invokePrompt , opts . tool , opts . input , headers ) ;
287287 } else if ( protocol === 'A2A' ) {
@@ -405,31 +405,25 @@ export const registerDev = (program: Command) => {
405405 agent_protocol : standardize ( AgentProtocol , config . protocol . toLowerCase ( ) ) ,
406406 } ) ;
407407
408- const isA2A = config . protocol === 'A2A' ;
409- const isMcp = config . protocol === 'MCP' ;
410- const isHttp = ! isA2A && ! isMcp ;
411- const fixedPort = isA2A
412- ? 9000
413- : isMcp
414- ? 8000
415- : getAgentPort ( project , config . agentName , port , portExplicit ) ;
416- if ( isHttp && ! portExplicit && fixedPort !== port ) {
408+ const requiresExactPort = requiresExactDevPort ( config . protocol ) ;
409+ const targetPort = getDevPort ( project , config . agentName , config . protocol , port , portExplicit ) ;
410+ if ( config . protocol !== 'MCP' && ! portExplicit && targetPort !== port ) {
417411 const idx = project . runtimes . findIndex ( a => a . name === config . agentName ) ;
418412 console . log (
419- `Runtime "${ config . agentName } " is at index ${ idx } ; using port ${ fixedPort } (pass --port ${ fixedPort } to override).`
413+ `Runtime "${ config . agentName } " is at index ${ idx } ; using port ${ targetPort } (pass --port ${ targetPort } to override).`
420414 ) ;
421415 }
422- const actualPort = await findAvailablePort ( fixedPort ) ;
423- if ( ( isA2A || isMcp ) && actualPort !== fixedPort ) {
416+ const actualPort = await findAvailablePort ( targetPort ) ;
417+ if ( requiresExactPort && actualPort !== targetPort ) {
424418 throw new ValidationError (
425- `Port ${ fixedPort } is in use. ${ config . protocol } agents require port ${ fixedPort } .`
419+ `Port ${ targetPort } is in use. ${ config . protocol } agents require port ${ targetPort } .`
426420 ) ;
427421 }
428422 // An explicit -p must be honored literally; if it's taken, fail fast instead of
429423 // silently rebinding to a different port (the silent-shift behavior #1079 removes).
430- if ( isHttp && portExplicit && actualPort !== fixedPort ) {
424+ if ( ! requiresExactPort && portExplicit && actualPort !== targetPort ) {
431425 throw new ValidationError (
432- `Port ${ fixedPort } is in use. Free it or pass a different --port (no port is chosen automatically when --port is set explicitly).`
426+ `Port ${ targetPort } is in use. Free it or pass a different --port (no port is chosen automatically when --port is set explicitly).`
433427 ) ;
434428 }
435429
@@ -440,8 +434,8 @@ export const registerDev = (program: Command) => {
440434
441435 const logger = new ExecLogger ( { command : 'dev' } ) ;
442436
443- if ( actualPort !== fixedPort ) {
444- console . log ( `Port ${ fixedPort } in use, using ${ actualPort } ` ) ;
437+ if ( actualPort !== targetPort ) {
438+ console . log ( `Port ${ targetPort } in use, using ${ actualPort } ` ) ;
445439 }
446440
447441 console . log ( `Starting dev server...` ) ;
0 commit comments