@@ -1056,6 +1056,70 @@ async def test_handle_mcp_tool_invocation_runtime_error(self) -> None:
10561056 with pytest .raises (RuntimeError , match = "Agent execution failed" ):
10571057 await app ._handle_mcp_tool_invocation ("TestAgent" , context , client )
10581058
1059+ async def test_handle_mcp_tool_invocation_ignores_agent_name_in_thread_id (self ) -> None :
1060+ """Test that MCP tool invocation uses the agent_name parameter, not the name from thread_id."""
1061+ mock_agent = Mock ()
1062+ mock_agent .name = "PlantAdvisor"
1063+
1064+ app = AgentFunctionApp (agents = [mock_agent ])
1065+ client = AsyncMock ()
1066+
1067+ # Mock the entity response
1068+ mock_state = Mock ()
1069+ mock_state .entity_state = {
1070+ "schemaVersion" : "1.0.0" ,
1071+ "data" : {"conversationHistory" : []},
1072+ }
1073+ client .read_entity_state .return_value = mock_state
1074+
1075+ # Thread ID contains a different agent name (@StockAdvisor@poc123)
1076+ # but we're invoking PlantAdvisor - it should use PlantAdvisor's entity
1077+ context = json .dumps ({"arguments" : {"query" : "test query" , "threadId" : "@StockAdvisor@test123" }})
1078+
1079+ with patch .object (app , "_get_response_from_entity" ) as get_response_mock :
1080+ get_response_mock .return_value = {"status" : "success" , "response" : "Test response" }
1081+
1082+ await app ._handle_mcp_tool_invocation ("PlantAdvisor" , context , client )
1083+
1084+ # Verify signal_entity was called with PlantAdvisor's entity, not StockAdvisor's
1085+ client .signal_entity .assert_called_once ()
1086+ call_args = client .signal_entity .call_args
1087+ entity_id = call_args [0 ][0 ]
1088+
1089+ # Entity name should be dafx-PlantAdvisor, not dafx-StockAdvisor
1090+ assert entity_id .name == "dafx-PlantAdvisor"
1091+ assert entity_id .key == "test123"
1092+
1093+ async def test_handle_mcp_tool_invocation_uses_plain_thread_id_as_key (self ) -> None :
1094+ """Test that a plain thread_id (not in @name@key format) is used as-is for the key."""
1095+ mock_agent = Mock ()
1096+ mock_agent .name = "TestAgent"
1097+
1098+ app = AgentFunctionApp (agents = [mock_agent ])
1099+ client = AsyncMock ()
1100+
1101+ mock_state = Mock ()
1102+ mock_state .entity_state = {
1103+ "schemaVersion" : "1.0.0" ,
1104+ "data" : {"conversationHistory" : []},
1105+ }
1106+ client .read_entity_state .return_value = mock_state
1107+
1108+ # Plain thread_id without @name@key format
1109+ context = json .dumps ({"arguments" : {"query" : "test query" , "threadId" : "simple-thread-123" }})
1110+
1111+ with patch .object (app , "_get_response_from_entity" ) as get_response_mock :
1112+ get_response_mock .return_value = {"status" : "success" , "response" : "Test response" }
1113+
1114+ await app ._handle_mcp_tool_invocation ("TestAgent" , context , client )
1115+
1116+ client .signal_entity .assert_called_once ()
1117+ call_args = client .signal_entity .call_args
1118+ entity_id = call_args [0 ][0 ]
1119+
1120+ assert entity_id .name == "dafx-TestAgent"
1121+ assert entity_id .key == "simple-thread-123"
1122+
10591123 def test_health_check_includes_mcp_tool_enabled (self ) -> None :
10601124 """Test that health check endpoint includes mcp_tool_enabled field."""
10611125 mock_agent = Mock ()
0 commit comments